From 96eeb0844d81d4467f5181f0fc7609a539308344 Mon Sep 17 00:00:00 2001 From: "James N. V. Cash" Date: Thu, 6 Feb 2025 15:53:52 -0500 Subject: [PATCH 1/3] Record changes to the new(?) dynamic predicate doc_text/2 doesn't seem to be used anywhere else; without this, various other predicates use lsp_state:full_text/2 and lsp_state:full_text_next/2 (both? I am somewhat unclear what the difference is) and so don't see unsaved updates to files --- libraries/lsp_server_metta/prolog/lsp_prolog_changes.pl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libraries/lsp_server_metta/prolog/lsp_prolog_changes.pl b/libraries/lsp_server_metta/prolog/lsp_prolog_changes.pl index c762531cac2..a9302890b15 100644 --- a/libraries/lsp_server_metta/prolog/lsp_prolog_changes.pl +++ b/libraries/lsp_server_metta/prolog/lsp_prolog_changes.pl @@ -10,8 +10,10 @@ */ :- use_module(library(readutil), [read_file_to_codes/3]). +:- use_module(lsp_metta_workspace, [xref_maybe/2]). :- dynamic doc_text/2. +:- dynamic lsp_state:full_text_next/2. %! handle_doc_changes(+File:atom, +Changes:list) is det. % @@ -30,6 +32,10 @@ doc_text_fallback(Path, OrigCodes), replace_codes(OrigCodes, StartLine, StartChar, ReplaceLen, ChangeCodes, NewText), + transaction(( retractall(lsp_state:full_text_next(Path, _)), + assertz(lsp_state:full_text_next(Path, NewText)) + )), + % TODO put the retractall/assertz in a transaction? retractall(doc_text(Path, _)), assertz(doc_text(Path, NewText)). handle_doc_change(Path, Change) :- From 4876942a13566388b5366d79265941d288bda6d4 Mon Sep 17 00:00:00 2001 From: "James N. V. Cash" Date: Thu, 6 Feb 2025 15:55:39 -0500 Subject: [PATCH 2/3] Implement completion using metta_atom_xref/3 Previously this was trying to use the prolog xref to look up metta definitions which obviously isn't going to do anything terrible helpful. --- .../prolog/lsp_metta_completion.pl | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/libraries/lsp_server_metta/prolog/lsp_metta_completion.pl b/libraries/lsp_server_metta/prolog/lsp_metta_completion.pl index 98e1bae979f..17f0efdc681 100644 --- a/libraries/lsp_server_metta/prolog/lsp_metta_completion.pl +++ b/libraries/lsp_server_metta/prolog/lsp_metta_completion.pl @@ -23,12 +23,10 @@ %:- use_module(lsp_metta_utils, [linechar_offset/3]). :- use_module(lsp_metta_changes, [doc_text_fallback_d4/2]). -% James added -:- use_module(library(prolog_xref), [xref_defined/3, xref_source/2]). :- include(lsp_metta_include). -:- use_module(lsp_metta_workspace, [source_file_text/2]). +:- use_module(lsp_metta_workspace, [source_file_text/2, xref_metta_source/1]). :- discontiguous(handle_completions/3). @@ -52,6 +50,7 @@ % prefix_at(File, Position, Prefix) :- source_file_text(File, DocCodes), + xref_metta_source(File), setup_call_cleanup( open_string(DocCodes, Stream), ( linechar_offset(Stream, Position, _), @@ -63,18 +62,23 @@ % completions_at(File, Position, Completions) :- prefix_at(File, Position, Prefix), - xref_source(File, [silent(true)]), + findall(Defn, + ( metta_atom_xref(Atom, File, _Loc), + once(( Atom = [':>', Defn|_] + ; Atom = [':', [Defn|_]|_] + ; Atom = [':', Defn|_] + ; Atom = ['=', [Defn|_]|_] + ; Atom = ['=', Defn|_] + )) + ), + Defns), findall( Result, - ( xref_defined(File, Goal, _), - functor(Goal, Name, Arity), + ( member(Name, Defns), atom_concat(Prefix, _, Name), - args_str(Arity, Args), - format(string(Func), "~w(~w)$0", [Name, Args]), - format(string(Label), "~w/~w", [Name, Arity]), - Result = _{label: Label, - insertText: Func, - insertTextFormat: 2}), + Result = _{label: Name, + insertText: Name, + insertTextFormat: 1}), Completions ). % From d796af6bc6d79f4c59bbdeeb174772c318544deb Mon Sep 17 00:00:00 2001 From: "James N. V. Cash" Date: Fri, 7 Feb 2025 11:58:44 -0500 Subject: [PATCH 3/3] Coalesce findalls, also use corelib & stdlib for completions --- .../prolog/lsp_metta_completion.pl | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/libraries/lsp_server_metta/prolog/lsp_metta_completion.pl b/libraries/lsp_server_metta/prolog/lsp_metta_completion.pl index 17f0efdc681..0aebed9ccf0 100644 --- a/libraries/lsp_server_metta/prolog/lsp_metta_completion.pl +++ b/libraries/lsp_server_metta/prolog/lsp_metta_completion.pl @@ -20,6 +20,7 @@ :- use_module(library(apply), [maplist/3]). :- use_module(library(lists), [numlist/3]). :- use_module(library(yall)). +:- use_module(library(filesex)). %:- use_module(lsp_metta_utils, [linechar_offset/3]). :- use_module(lsp_metta_changes, [doc_text_fallback_d4/2]). @@ -62,25 +63,27 @@ % completions_at(File, Position, Completions) :- prefix_at(File, Position, Prefix), - findall(Defn, - ( metta_atom_xref(Atom, File, _Loc), - once(( Atom = [':>', Defn|_] - ; Atom = [':', [Defn|_]|_] - ; Atom = [':', Defn|_] - ; Atom = ['=', [Defn|_]|_] - ; Atom = ['=', Defn|_] - )) - ), - Defns), - findall( - Result, - ( member(Name, Defns), - atom_concat(Prefix, _, Name), - Result = _{label: Name, - insertText: Name, - insertTextFormat: 1}), - Completions - ). + findall(Result, + % Use definitions from the current file, corelib, and stdlib + % TODO: also look at imported definitions? + ( ( metta_atom_xref(Atom, File, _Loc) + ; ( metta_atom_xref(Atom, Path, _), + directory_file_path(_, F, Path), + memberchk(F, ['corelib.metta', 'stdlib_mettalog.metta']) ) + ), + (( Atom = [':>', Defn|_] + ; Atom = [':', [Defn|_]|_] + ; Atom = [':', Defn|_] + ; Atom = ['=', [Defn|_]|_] + ; Atom = ['=', Defn|_] + )), + atom(Defn), + atom_concat(Prefix, _, Defn), + Result = _{label: Defn, + insertText: Defn, + insertTextFormat: 1}), + Completions + ). % args_str(Arity, Str) :- numlist(1, Arity, Args),