Skip to content

Commit

Permalink
adds config for langtool + port for langtool
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdsk committed Oct 14, 2021
1 parent c822898 commit 203b36d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lua/prosesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 31 additions & 8 deletions lua/prosesitter/backend/langtool.lua
Original file line number Diff line number Diff line change
@@ -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 = [=====[
Expand All @@ -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)
Expand All @@ -44,36 +60,43 @@ 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,
})

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

Expand Down
2 changes: 1 addition & 1 deletion lua/prosesitter/backend/vale.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 4 additions & 0 deletions lua/prosesitter/config/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ MinAlertLevel = suggestion
BasedOnStyles = proselint, write-good, Vale
]==]

M.langtool_cfg = [==[
maxCheckThreads=2
]==]

M.cmds = {
PsNext = "next",
PsPrev = "prev",
Expand Down
5 changes: 4 additions & 1 deletion lua/prosesitter/config/mod.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/prosesitter/on_event/check/async_cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lua/prosesitter/on_event/check/check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 203b36d

Please sign in to comment.