Still a work-in-progress – please open an issue if you have any issues or feature requests!.
Currently supports:
- diagnostics (singleton variables, syntax errors) via xref
- find references/definitions
- documentation of predicates on hover
- auto-completion of predicates
- code formatting (✨new!✨)
- symbol highlighting (✨new!✨)
- variable renaming (✨new!✨)
The code formatter can also be run stand-alone. After installing the lsp_server
pack, you can run swipl formatter <file>
at the command line.
Only tested with SWI-Prolog, as it heavily uses its introspection facilities to do its stuff.
It should work with any relatively-recent version of SWI-Prolog, but for best results (for “find references” in particular), use a version with xref_called/5
(8.1.5 or newer; past commit 303f6430de5c).
Installable as a pack with swipl pack install lsp_server
or ?- pack_install(lsp_server).
from a repl.
As of version 2.5.0, running the server over a socket is now supported by passing in the commandline arguments port <port number>
(instead of stdio
).
(lsp-register-client
(make-lsp-client
:new-connection
(lsp-stdio-connection (list "swipl"
"-g" "use_module(library(lsp_server))."
"-g" "lsp_server:main"
"-t" "halt"
"--" "stdio"))
:major-modes '(prolog-mode)
:priority 1
:multi-root t
:server-id 'prolog-ls))
(setopt eglot-server-programs (cons
(cons 'prolog-mode
(list "swipl"
"-O"
"-g" "use_module(library(lsp_server))."
"-g" "lsp_server:main"
"-t" "halt"
"--" "port" :autoport))
eglot-server-programs))
let g:LanguageClient_serverCommands = {
\ 'prolog': ['swipl',
\ '-g', 'use_module(library(lsp_server)).',
\ '-g', 'lsp_server:main',
\ '-t', 'halt',
\ '--', 'stdio']
\ }
Put the following in coc-settings.json
(which you can access by using the command :CocConfig
).
{"languageserver": {
"prolog-lsp": {
"command": "swipl",
"args": ["-g", "use_module(library(lsp_server)).",
"-g", "lsp_server:main",
"-t", "halt",
"--", "stdio"
],
"filetypes": ["prolog"]
}}
}
Put the following in $XDG_CONFIG_DIR/nvim/lsp/prolog.lua
:
return {
cmd = { 'swipl',
'-g', 'use_module(library(lsp_server))',
'-g', 'lsp_server:main',
'-t', 'halt',
'--', 'stdio' },
root_markers = { '.git', },
filetypes = { 'prolog' },
}
And add vim.lsp.enable({'prolog'})
to $XDG_CONFIG_DIR/nvim/init.lua
.
Install the neovim/nvim-lspconfig package
Put the following in $XDG_CONFIG_DIR/nvim/lua/lspconfig/prolog_lsp.lua
:
local configs = require 'lspconfig/configs'
local util = require 'lspconfig/util'
configs.prolog_lsp = {
default_config = {
cmd = {"swipl",
"-g", "use_module(library(lsp_server)).",
"-g", "lsp_server:main",
"-t", "halt",
"--", "stdio"};
filetypes = {"prolog"};
root_dir = util.root_pattern("pack.pl");
};
docs = {
description = [[
https://github.com/jamesnvc/prolog_lsp
Prolog Language Server
]];
}
}
-- vim:et ts=2 sw=2
Then add the following to init.vim
:
lua << EOF
require('lspconfig/prolog_lsp')
require('lspconfig').prolog_lsp.setup{}
EOF
Create the following file in $XDG_CONFIG_DIR/nvim/lua/plugins/lsp.lua
return {
{
"neovim/nvim-lspconfig",
opts = {
servers = {
prolog = {},
},
setup = {
prolog = function(_, opts)
local lspconfig = require("lspconfig")
local configs = require("lspconfig.configs")
local util = require("lspconfig.util")
local root_files = { ".git", "pack.pl" }
if not configs.prolog then
configs.prolog = {
default_config = {
cmd = {
"swipl",
"-g",
"use_module(library(lsp_server)).",
"-g",
"lsp_server:main",
"-t",
"halt",
"--",
"stdio",
},
filetypes = { "prolog" },
single_file_support = true,
root_dir = util.root_pattern(unpack(root_files)),
settings = {},
},
commands = {},
docs = {
description = [[
Prolog LSP server
]],
},
}
end
lspconfig.prolog.setup(opts)
end,
},
},
},
}
Choose one from the list below:
- download the latest
.vsix
file from the releases page - clone this repo and copy/symlink the
vscode/
directory to ~~/.vscode/extensions/~ - clone and build the
.vsix
file yourself by the follwing steps:cd /path/to/clone/vscode
npm install
npx vsce package
- add the resulting
.vsix
to VSCode by clicking the...
at the top right of the “Extensions” panel then selectingInstall from VSIX...
Helix already includes configuration for this Prolog LSP server, so it should mostly Just Work.
However, the default configuration gives the ‘.pl’ extension to perl, so to avoid having to manually do :set-language prolog
each time, you can add the following to $XDG_CONFIG/helix/languages.toml
to remove Perl’s association with that extension:
[[language]]
name = "perl"
file-types = ["perl"]