From 203b36d1a6d6cf3c1d9ea3492505999ea9a276ae Mon Sep 17 00:00:00 2001 From: dskleingeld Date: Fri, 15 Oct 2021 01:02:22 +0200 Subject: [PATCH] adds config for langtool + port for langtool --- lua/prosesitter.lua | 2 +- lua/prosesitter/backend/langtool.lua | 39 ++++++++++++++++---- lua/prosesitter/backend/vale.lua | 2 +- lua/prosesitter/config/defaults.lua | 4 ++ lua/prosesitter/config/mod.lua | 5 ++- lua/prosesitter/on_event/check/async_cmd.lua | 2 +- lua/prosesitter/on_event/check/check.lua | 3 +- 7 files changed, 44 insertions(+), 13 deletions(-) diff --git a/lua/prosesitter.lua b/lua/prosesitter.lua index 4fa4729..99a433d 100644 --- a/lua/prosesitter.lua +++ b/lua/prosesitter.lua @@ -70,7 +70,7 @@ function M:setup(user_cfg) end if cfg.langtool_bin ~= nil then - langtool.start_server(on_event, cfg.langtool_bin) + langtool.start_server(on_event, cfg) end if cfg.default_cmds then diff --git a/lua/prosesitter/backend/langtool.lua b/lua/prosesitter/backend/langtool.lua index 304b3a5..eb60b99 100644 --- a/lua/prosesitter/backend/langtool.lua +++ b/lua/prosesitter/backend/langtool.lua @@ -1,8 +1,11 @@ local util = require("prosesitter/util") local log = require("prosesitter/log") local shared = require("prosesitter/shared") +local defaults = require("prosesitter/config/defaults") local M = {} +M.url = "set in start_server function" + function M.setup_binairy() vim.fn.mkdir(util.plugin_path, "p") local install_script = [=====[ @@ -20,11 +23,24 @@ function M.setup_binairy() mv languagetool/*/* languagetool ]=====] - local ok_msg = "[prosesitter] installed vale with default styles" - local err_msg= "[prosesitter] could not setup vale styles" + local ok_msg = "[prosesitter] installed language tool" + local err_msg = "[prosesitter] could not setup language tool" util:shell_in_new_window(install_script, ok_msg, err_msg) end +function M.setup_cfg() + local exists = 1 + if vim.fn.filereadable(util.plugin_path .. "/langtool.cfg") ~= exists then + local file = io.open(util.plugin_path .. "/langtool.cfg", "w") + if file == nil then + print("fatal error: could not open/create fresh LanguageTool config") + end + + file:write(defaults.langtool_cfg) + file:flush() + file:close() + end +end local function mark_rdy_if_responding(on_event) local on_exit = function(text) @@ -44,28 +60,33 @@ local function mark_rdy_if_responding(on_event) local async = require("prosesitter/on_event/check/async_cmd") local do_check = function() if not M.langtool_running then - local curl_args = { "--no-progress-meter", "--data", "@-", "http://localhost:8081/v2/check" } + local curl_args = { "--no-progress-meter", "--data", "@-", M.url } async.dispatch_with_stdin("language=en-US&text=hi", "curl", curl_args, on_exit) end end for timeout = 0, 15, 1 do - vim.defer_fn(do_check, timeout*1000) + vim.defer_fn(do_check, timeout * 1000) end end -function M.start_server(on_event, path) +-- using depedency injection here (on_event) to break +-- dependency loop +function M.start_server(on_event, cfg) local on_exit = function() M.langtool_running = false end + M.url = "http://localhost:" .. cfg.langtool_port .. "/v2/check" local res = vim.fn.jobstart({ "java", "-cp", - path, + cfg.langtool_bin, "org.languagetool.server.HTTPServer", + "--config", + cfg.langtool_cfg, "--port", - "8081", + cfg.langtool_port, }, { on_exit = on_exit, }) @@ -73,7 +94,9 @@ function M.start_server(on_event, path) if res > 0 then mark_rdy_if_responding(on_event) else - error("could not start language server using path: " .. path) + error("could not start language server using path: " .. cfg.langtool_bin) + log.error("could not start language server using path: " .. cfg.langtool_bin) + end end diff --git a/lua/prosesitter/backend/vale.lua b/lua/prosesitter/backend/vale.lua index 3080f37..a956062 100644 --- a/lua/prosesitter/backend/vale.lua +++ b/lua/prosesitter/backend/vale.lua @@ -47,7 +47,7 @@ function M.setup_binairy_and_styles() util:shell_in_new_window(install_script, ok_msg, err_msg) end -function M.setup_default_cfg() +function M.setup_cfg() local exists = 1 if vim.fn.filereadable(util.plugin_path .. "/vale_cfg.ini") ~= exists then local file = io.open(util.plugin_path .. "/vale_cfg.ini", "w") diff --git a/lua/prosesitter/config/defaults.lua b/lua/prosesitter/config/defaults.lua index 081103b..10c8818 100644 --- a/lua/prosesitter/config/defaults.lua +++ b/lua/prosesitter/config/defaults.lua @@ -68,6 +68,10 @@ MinAlertLevel = suggestion BasedOnStyles = proselint, write-good, Vale ]==] +M.langtool_cfg = [==[ +maxCheckThreads=2 +]==] + M.cmds = { PsNext = "next", PsPrev = "prev", diff --git a/lua/prosesitter/config/mod.lua b/lua/prosesitter/config/mod.lua index 4aeea2e..c017d4e 100644 --- a/lua/prosesitter/config/mod.lua +++ b/lua/prosesitter/config/mod.lua @@ -24,6 +24,8 @@ local Cfg = { vale_cfg = util.plugin_path .. "/vale_cfg.ini", vale_bin = false, langtool_bin = false, + langtool_port = 34287, -- just a random port thats probably free + langtool_cfg = util.plugin_path .. "/langtool.cfg", default_cmds = true, auto_enable = true, disabled_ext = {}, -- empty so nothing disabled @@ -67,7 +69,7 @@ function M:setup(user_cfg) local do_setup = vim.fn.input("vale is not installed, install vale? y/n: ") if do_setup == "y" then vale.setup_binairy_and_styles() - vale.setup_default_cfg() + vale.setup_cfg() else print("please setup vale manually and adjust your config") return nil @@ -80,6 +82,7 @@ function M:setup(user_cfg) local do_setup = vim.fn.input("Language tool not installed, install language tool? y/n: ") if do_setup == "y" then langtool.setup_binairy() + langtool.setup_cfg() else print("please set up language tool manually and adjust your config") return nil diff --git a/lua/prosesitter/on_event/check/async_cmd.lua b/lua/prosesitter/on_event/check/async_cmd.lua index d032fd6..1091cc6 100644 --- a/lua/prosesitter/on_event/check/async_cmd.lua +++ b/lua/prosesitter/on_event/check/async_cmd.lua @@ -8,7 +8,7 @@ local handle local function on_stderr(err, data) assert(not err, err) if data then - print("stderr chunk", data) + log.error(data) end end diff --git a/lua/prosesitter/on_event/check/check.lua b/lua/prosesitter/on_event/check/check.lua index 8c34950..5c3c226 100644 --- a/lua/prosesitter/on_event/check/check.lua +++ b/lua/prosesitter/on_event/check/check.lua @@ -27,7 +27,8 @@ local function do_check() local results = langtool.add_spans(json) marks.mark_results(results, req.areas, "langtool", langtool.to_meta) end - local curl_args = { "--no-progress-meter", "--data", "@-", "http://localhost:8081/v2/check" } + + local curl_args = { "--no-progress-meter", "--data", "@-", langtool.url} async.dispatch_with_stdin(langtool_query(req.text), "curl", curl_args, post_langtool) end