From 40c522ae06738b5e81eb982553f99f3dde7ac480 Mon Sep 17 00:00:00 2001 From: TANIGUHCI Masaya Date: Fri, 23 Jun 2023 22:37:36 +0900 Subject: [PATCH 1/3] feat: add swipl --- doc/BUILTINS.md | 18 +++++ lua/null-ls/builtins/diagnostics/swipl.lua | 89 ++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 lua/null-ls/builtins/diagnostics/swipl.lua diff --git a/doc/BUILTINS.md b/doc/BUILTINS.md index 6dac294ae..c6e80fffa 100644 --- a/doc/BUILTINS.md +++ b/doc/BUILTINS.md @@ -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. diff --git a/lua/null-ls/builtins/diagnostics/swipl.lua b/lua/null-ls/builtins/diagnostics/swipl.lua new file mode 100644 index 000000000..b54dd49e6 --- /dev/null +++ b/lua/null-ls/builtins/diagnostics/swipl.lua @@ -0,0 +1,89 @@ +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") + 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*(.*)") + if msg then + warning = true + goto continue + end + _, _, row, msg = line:find("^ERROR:.-:(%d+):%s*(.*)") + if msg then + err = true + goto continue + end + end + ::continue:: + 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, +}) From cf780f6bd62d90f539fdc853bf0c3b564faaeead Mon Sep 17 00:00:00 2001 From: TANIGUHCI Masaya Date: Fri, 23 Jun 2023 22:43:38 +0900 Subject: [PATCH 2/3] Format scripts --- doc/MAIN.md | 4 +- lua/null-ls/builtins/diagnostics/swipl.lua | 166 ++++++++++----------- 2 files changed, 85 insertions(+), 85 deletions(-) diff --git a/doc/MAIN.md b/doc/MAIN.md index 1343152a7..2df4c4d13 100644 --- a/doc/MAIN.md +++ b/doc/MAIN.md @@ -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). diff --git a/lua/null-ls/builtins/diagnostics/swipl.lua b/lua/null-ls/builtins/diagnostics/swipl.lua index b54dd49e6..25ec3d430 100644 --- a/lua/null-ls/builtins/diagnostics/swipl.lua +++ b/lua/null-ls/builtins/diagnostics/swipl.lua @@ -3,87 +3,87 @@ 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") - 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*(.*)") - if msg then - warning = true - goto continue - end - _, _, row, msg = line:find("^ERROR:.-:(%d+):%s*(.*)") - if msg then - err = true - goto continue - end - end - ::continue:: - 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, + 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") + 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*(.*)") + if msg then + warning = true + goto continue + end + _, _, row, msg = line:find("^ERROR:.-:(%d+):%s*(.*)") + if msg then + err = true + goto continue + end + end + ::continue:: + 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, }) From 21d40929d4668944dc6c885b33459a46021f5d0c Mon Sep 17 00:00:00 2001 From: TANIGUHCI Masaya Date: Fri, 23 Jun 2023 22:51:38 +0900 Subject: [PATCH 3/3] Remove goto --- lua/null-ls/builtins/diagnostics/swipl.lua | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lua/null-ls/builtins/diagnostics/swipl.lua b/lua/null-ls/builtins/diagnostics/swipl.lua index 25ec3d430..32ed99e35 100644 --- a/lua/null-ls/builtins/diagnostics/swipl.lua +++ b/lua/null-ls/builtins/diagnostics/swipl.lua @@ -61,17 +61,10 @@ return h.make_builtin({ local _ i = i + 1 _, _, row, msg = line:find("^Warning:.-:(%d+):%s*(.*)") - if msg then - warning = true - goto continue - end + warning = msg ~= nil _, _, row, msg = line:find("^ERROR:.-:(%d+):%s*(.*)") - if msg then - err = true - goto continue - end + err = msg ~= nil end - ::continue:: end if err or warning then table.insert(diagnostics, {