# Prolog LSP Update - Code Formatting!

**URL:** <https://swi-prolog.discourse.group/t/prolog-lsp-update-code-formatting/8911>\
**Category:** Pack\
**Created:** [March 28, 2025, 11:28pm UTC](https://swi-prolog.discourse.group/t/prolog-lsp-update-code-formatting/8911 "2025-03-28T23:28:56Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamesnvc](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jamesnvc/32/14_2.png) [@jamesnvc](https://swi-prolog.discourse.group/u/jamesnvc)\
**Post date:** [March 28, 2025, 11:28pm UTC](https://swi-prolog.discourse.group/t/prolog-lsp-update-code-formatting/8911/1 "2025-03-28T23:28:56Z")

</div>

Version three of my [Prolog LSP](https://github.com/jamesnvc/lsp_server) 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](https://github.com/jamesnvc/lsp_server).

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

---

<div class="post-metadata">

**Author:** ![kwon-young](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/kwon-young/32/4841_2.png) [@kwon-young](https://swi-prolog.discourse.group/u/kwon-young)\
**Post date:** [March 31, 2025, 7:47am UTC](https://swi-prolog.discourse.group/t/prolog-lsp-update-code-formatting/8911/2 "2025-03-31T07:47:45Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [March 31, 2025, 9:25am UTC](https://swi-prolog.discourse.group/t/prolog-lsp-update-code-formatting/8911/4 "2025-03-31T09:25:12Z")

</div>

> [@anon95304481](#):
>
> 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?

Probably not 🙂 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?

---

<div class="post-metadata">

**Author:** ![jamesnvc](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jamesnvc/32/14_2.png) [@jamesnvc](https://swi-prolog.discourse.group/u/jamesnvc)\
**Post date:** [March 31, 2025, 12:47pm UTC](https://swi-prolog.discourse.group/t/prolog-lsp-update-code-formatting/8911/5 "2025-03-31T12:47:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jamesnvc](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jamesnvc/32/14_2.png) [@jamesnvc](https://swi-prolog.discourse.group/u/jamesnvc)\
**Post date:** [March 31, 2025, 12:55pm UTC](https://swi-prolog.discourse.group/t/prolog-lsp-update-code-formatting/8911/6 "2025-03-31T12:55:31Z")

</div>

> [@jan](#):
>
> Maybe @jamesnvc can comment on the reusability of the reformatter for LSP?

Sure; I wrote briefly about it [here](https://occasionallycogent.com/prolog_code_formatter/index.html). 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`

---

<div class="post-metadata">

**Author:** ![robert314](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/robert314/32/6931_2.png) [@robert314](https://swi-prolog.discourse.group/u/robert314)\
**Post date:** [April 6, 2025, 8:33am UTC](https://swi-prolog.discourse.group/t/prolog-lsp-update-code-formatting/8911/7 "2025-04-06T08:33:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jamesnvc](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jamesnvc/32/14_2.png) [@jamesnvc](https://swi-prolog.discourse.group/u/jamesnvc)\
**Post date:** [April 6, 2025, 11:31am UTC](https://swi-prolog.discourse.group/t/prolog-lsp-update-code-formatting/8911/8 "2025-04-06T11:31:01Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![robert314](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/robert314/32/6931_2.png) [@robert314](https://swi-prolog.discourse.group/u/robert314)\
**Post date:** [April 8, 2025, 8:31am UTC](https://swi-prolog.discourse.group/t/prolog-lsp-update-code-formatting/8911/9 "2025-04-08T08:31:04Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![jamesnvc](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jamesnvc/32/14_2.png) [@jamesnvc](https://swi-prolog.discourse.group/u/jamesnvc)\
**Post date:** [April 24, 2025, 11:58pm UTC](https://swi-prolog.discourse.group/t/prolog-lsp-update-code-formatting/8911/11 "2025-04-24T23:58:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jamesnvc](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jamesnvc/32/14_2.png) [@jamesnvc](https://swi-prolog.discourse.group/u/jamesnvc)\
**Post date:** [June 17, 2025, 3:20pm UTC](https://swi-prolog.discourse.group/t/prolog-lsp-update-code-formatting/8911/14 "2025-06-17T15:20:03Z")

</div>

Yes, that would be possible if we were writing something integrated into the editor, but the problem is the way the LSP protocol works is that the server only receives messages from the editor, which then waits for the response back. One of the trade-offs of the LSP system being decoupled from any particular editor is that smoother integrations like the one you describe aren’t easily possible without changing the protocol itself.

---

<div class="post-metadata">

**Author:** ![jamesnvc](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jamesnvc/32/14_2.png) [@jamesnvc](https://swi-prolog.discourse.group/u/jamesnvc)\
**Post date:** [June 18, 2025, 4:21pm UTC](https://swi-prolog.discourse.group/t/prolog-lsp-update-code-formatting/8911/16 "2025-06-18T16:21:24Z")

</div>

> [@anon95304481](#):
>
> Well my saying is you have to change that.

Right, but what I’m saying is that with the current implementation as a separate process, running independently of the editor and only communicating over the channel defined by the protocol, the ability to have fine-grained asynchronous communication is limited.
