jamesnvc / lsp_server

Language Server Protocol server for SWI-Prolog
BSD 2-Clause "Simplified" License
91 stars 6 forks source link

Implement completion #3

Closed jamesnvc closed 3 years ago

jamesnvc commented 5 years ago

To work with https://github.com/ncm2/ncm2.

Carrying on from #2

rrooij commented 5 years ago

completion would not be that hard with https://www.swi-prolog.org/pldoc/doc/_SWI_/library/console_input.pl

jamesnvc commented 5 years ago

Oh nice, thank you! I will have a look at that.

On Oct 4, 2019, at 08:02, rrooij notifications@github.com wrote:

completion would not be that hard with https://www.swi-prolog.org/pldoc/doc/_SWI_/library/console_input.pl

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

jamesnvc commented 5 years ago

Hm, it looks like that might not work directly in this case...I can probably use the $atom_completions thing that it uses internally, but the issue is that the completion systems there rely on dynamically loading the program to figure out what the available atoms are.

I may eventually change this LSP server to also actually load the code its introspecting, but for now I want to keep it using the xref family of predicates to just do static analysis.

rrooij commented 5 years ago

Agree. But maybe just completing the internal library would be a good start already.

jamesnvc commented 5 years ago

Indeed. I started trying to implement completion based on a combination of the above library & xref_defined, but ran into another issue that will require some thinking about:

An LSP completion request just gives the server the name of the file and the position in the file. This means then that if the file is unsaved (as it would presumably be if you're in the process of typing), the server can't actually tell what you've just typed & want completed. This seems like kind of a wild deficiency in the API, so maybe there's a way around this. As of now, the only way I can see is to implement the didUpdate notification and maintain in the server that the current state of the file is, independent of what's actually saved, just for this purpose. That would in turn complicate xref stuff though, so I'm going to just ponder this for a little bit.

rrooij commented 5 years ago

Yes, I tried it as well and encountered the same issue. I do think that we need to implement didUpdate since we need to keep track of the modified unsaved buffer for other LSP-implementations as well.

Maintaining the current state of the file is the best way, I think.

jamesnvc commented 5 years ago

Just pushed out version 1.4.0 that tries implementing simple completion, using xref to get the loaded predicates in the current file. As discussed here, it uses the didUpdate notification to keep track of what the current text is, independent of being saved.

Thanks for helping with this; please let me know if it works for you!

rrooij commented 5 years ago

Thanks a lot, will definitely look at it!

rrooij commented 5 years ago

process

Works pretty well.

Some built-in predicates are not completed by the way. For instance, memberchk/2

rrooij commented 5 years ago

findall(X, current_predicate(X), BuiltInPredicates).

Could be used to list all available default predicates.

jamesnvc commented 5 years ago

Using current_predicate/1 unfortunately doesn't work in this case, because the LSP doesn't actually load the file you're editing -- the completions are from xref_defined/3. That's probably also why some of the built-in predicates don't show up; I think that xref doesn't know about ones that are autoloaded.

jamesnvc commented 5 years ago

Oh never mind, I see what you're saying about the default predicates! I will give that a try, thanks!