Skip to content
This repository has been archived by the owner on Aug 12, 2023. It is now read-only.

feat: add swipl #1600

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions doc/BUILTINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2097,6 +2097,24 @@ local sources = { null_ls.builtins.diagnostics.stylint }
- Command: `stylint`
- Args: `{ "$FILENAME" }`

### [swipl](https://www.swi-prolog.org/)

SWI-Prolog is a versatile implementation of the Prolog language.

#### Usage

```lua
local sources = { null_ls.builtins.diagnostics.swipl }
```

#### Defaults

- Filetypes: `{ "prolog" }`
- Method: `diagnostics`
- Command: `swipl`
- Args: `{ "-q", "-t", "halt(1)", "-s", "$FILENAME" }`


### [swiftlint](https://github.com/realm/SwiftLint)

A tool to enforce Swift style and conventions.
Expand Down
4 changes: 2 additions & 2 deletions doc/MAIN.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ handle common conditional checks.
- `utils.root_matches`: accepts a Lua string matcher pattern. Returns `true` if
the root matches the specified pattern.

- `utils.root_has_file_matches`: accepts a Lua string matcher pattern. Returns `true` if
a file matches the specified pattern.
- `utils.root_has_file_matches`: accepts a Lua string matcher pattern. Returns
`true` if a file matches the specified pattern.

On registration, null-ls will store conditional sources in state and check
`condition` at the first opportunity (typically upon entering a named buffer).
Expand Down
82 changes: 82 additions & 0 deletions lua/null-ls/builtins/diagnostics/swipl.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
local h = require("null-ls.helpers")
local methods = require("null-ls.methods")
local DIAGNOSTICS_ON_SAVE = methods.internal.DIAGNOSTICS_ON_SAVE

return h.make_builtin({
method = DIAGNOSTICS_ON_SAVE,
filetypes = { "prolog" },
generator_opts = {
command = "swipl",
args = { "-q", "-t", "halt(1)", "-s", "$FILENAME" },
format = "raw",
check_exit_code = function(code)
return code <= 1
end,
on_output = function(params, done)
local diagnostics = {}
local lines = vim.fn.split(params.err, "\r\n\\|\r\\|\n")
Copy link
Owner

@jose-elias-alvarez jose-elias-alvarez Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small improvement: we could consider using vim.split here instead, which would simplify your check for emptiness a few lines down.

Edit: also, if you used the helper I mentioned, this would be handled automatically.

local err = false
local warning = false
local row = 0
local col = 0
local msg = ""
local i = 1
if lines[1]:match("v:null") then
done(diagnostics)
return
end
while i <= #lines do
local line = lines[i]
if err then
local _, _, m = line:find("^ERROR: (.*)")
if m then
msg = msg .. m
i = i + 1
else
err = false
table.insert(diagnostics, {
row = row,
col = col,
source = "swipl",
message = msg,
severity = 1,
})
end
elseif warning then
local _, _, m = line:find("^Warning: (.*)")
if m then
msg = msg .. m
i = i + 1
else
warning = false
table.insert(diagnostics, {
row = row,
col = col,
source = "swipl",
message = msg,
severity = 2,
})
end
else
local _
i = i + 1
_, _, row, msg = line:find("^Warning:.-:(%d+):%s*(.*)")
warning = msg ~= nil
_, _, row, msg = line:find("^ERROR:.-:(%d+):%s*(.*)")
err = msg ~= nil
end
end
if err or warning then
table.insert(diagnostics, {
row = row,
col = col,
source = "swipl",
message = msg,
severity = err and 1 or 2,
})
end
done(diagnostics)
end,
},
factory = h.generator_factory,
})