Prolog LSP Update - Code Formatting!

Version three of my Prolog LSP implementation has been released! The big new feature is automatic code formatting, a feature several people have expressed interest in.

You can install or update the pack with swipl pack install lsp_server and configure your editor using the instructions in the README.

As a bonus, after installing the pack, the formatter can be run stand-alone with swipl formatter $file_to_format.

4 Likes

Hello,
I just wanted to say thank you for developing and maintaining your Prolog LSP.
It is your pack that allows all of us vim users to write prolog code in our favorite environment :slight_smile:

1 Like

Ok, interesting, code formatting arrived almost at the same time for
both LSP and SWI-Thinker. Do they use the same code base, or
are these different code bases?

LSP has plenty of files by the name lsp_XXX.pl, SWI-Tinker has
only one file highlight.pl. So in SWI-TinkerI cannot do yet code
re-formatting, but in LSP its possible to do code re-formatting.

Re-formatting would beautify my code, did not yet arrive in SWI-Tinker?

Probably not :slight_smile: Code formatting in SWI-Tinker uses the formatting facilities of CodeMirror, which was copied from SWISH. This facility also deals with highlighting, but were SWISH highligting uses the CodeMirror “mode” enriched with server-side generated semantic labels, SWI-Tinker uses an approach that is very close to PceEmacs. In this scenario the editor calls Prolog, which directly highlights fragments in the editor (using CodeMirror.markText()).

Basically there was no reusable Prolog formatter (auto-indent) for SWI-Prolog. There are several independent ones, notably the one in PceEmacs (using hacky regex look-back inspired by old GNU-Emacs), sweep (using syntax analysis in GNU-Emacs), SWISH/Tinker (using CodeMirror syntax analysis). Maybe @jamesnvc can comment on the reusability of the reformatter for LSP?

You’re welcome! That’s very nice to hear, thank you!

Sure; I wrote briefly about it here. Essentially, the formatter uses prolog_read_source_term/4 to parse the source, and flattens the content into a list of tokens, with whitespace and comments made explicit (the lsp_formatter_parser module). That reified representation is processed by the predicates in lsp_formatter, which applies a series of rules to edit the whitespace to subjectively improve indentation, alignment, and other formatter preferences. I plan on adding more rules to that module as well as some configuration, so users can pick-and-choose more how things are formatted.

I hope it is somewhat reusable in other code. As I mentioned, it can also be run independently of the LSP sever as a swipl “app”; see swipl formatter -h

1 Like

What editors do you use with the prolog lsp? I was looking into using this a few days ago but my usual editor is notepad++ which doesn’t have much lsp support outside of a broken extension.

I use Emacs and I’ve tested with vim and VSCode. Unfortunately I don’t have access to a Windows machine to look at Notepad++. In any case, it seems like that editor doesn’t have a single solid LSP implementation? A cursory search just finds several “work-in-progress” clients…

Thank you!

So this auto completion you have on the bullet list. Could this
be extended to use LLM ghost texts code completion, Ă  la Copilot?

Like here:

LLM powered development
https://github.com/huggingface/llm-intellij

Theoretically it could do LLM-based completions, but with the way the LSP API works, I don’t think it could really do the “ghost text” effect. I’ve implemented LLM-based completion for a different LSP server and the experience wasn’t great, since there’s a noticeable lag waiting for completion candidates to be suggested, as the completion system with LSPs is synchronous.