vis-config
lua scripts to configure vis editor
git clone https://9o.is/git/vis-config.git
commit 4e5077815cbb080427c9f0765812278c2e9587dd Author: Jul <jul@9o.is> Date: Sat, 24 Jan 2026 05:29:59 -0500 initialize Diffstat:
| A | lib/ctags.lua | | | 122 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | lib/router.lua | | | 80 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | lib/search.lua | | | 95 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | lib/session.lua | | | 97 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | lib/status.lua | | | 73 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | lib/window-manager/init.lua | | | 43 | +++++++++++++++++++++++++++++++++++++++++++ |
| A | lib/window-manager/tmux.lua | | | 69 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | vendors/commentary.lua | | | 147 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | vendors/ctags.lua | | | 337 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | vendors/modelines.lua | | | 167 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | vendors/quickfix.lua | | | 525 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | vendors/surround.lua | | | 192 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | vendors/vis-lspc/.editorconfig | | | 16 | ++++++++++++++++ |
| A | vendors/vis-lspc/.gitlab-ci.yml | | | 20 | ++++++++++++++++++++ |
| A | vendors/vis-lspc/.lua-format | | | 27 | +++++++++++++++++++++++++++ |
| A | vendors/vis-lspc/LICENSE | | | 674 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | vendors/vis-lspc/Makefile | | | 19 | +++++++++++++++++++ |
| A | vendors/vis-lspc/README.md | | | 216 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | vendors/vis-lspc/bindings.lua | | | 75 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | vendors/vis-lspc/init.lua | | | 1836 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | vendors/vis-lspc/json.lua | | | 216 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | vendors/vis-lspc/log.lua | | | 93 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | vendors/vis-lspc/lspc.lua | | | 140 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | vendors/vis-lspc/parser.lua | | | 75 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | vendors/vis-lspc/parser_test.lua | | | 125 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | vendors/vis-lspc/supported-servers.lua | | | 121 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | vendors/vis-lspc/tools/check-format | | | 8 | ++++++++ |
| A | vendors/vis-lspc/tools/find-upwards | | | 26 | ++++++++++++++++++++++++++ |
| A | vendors/vis-lspc/util.lua | | | 307 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | vendors/vis-lspc/util_test.lua | | | 33 | +++++++++++++++++++++++++++++++++ |
| A | visrc.lua | | | 147 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 files changed, 6121 insertions(+), 0 deletions(-)
diff --git a/lib/ctags.lua b/lib/ctags.lua @@ -0,0 +1,122 @@ +local M = { + file = './tags', + flags = '-R', +} + +local tagstack = {} + +vis:option_register('ctags', 'bool', function(value, toggle) + if not vis.win then return false end + vis.win.ctags = toggle and not vis.win.ctags or value + return true +end, 'Enables ctags') + +vis:option_register('ctags-autosave', 'bool', function(value, toggle) + if not vis.win then return false end + vis.win.ctags_autosave = toggle and not vis.win.ctags_autosave or value + return true +end, 'Enables ctags autosaving') + +vis:option_register('ctags-read-flags', 'string', function(value) + if not vis.win then return false end + vis.win.ctags_readtags_flags = value + return true +end, 'Set ctags readtags flags') + +vis:command_register('tag-generate', function(_, _, win) + if not win.ctags then + vis:info('ctags not enabled') + return + end + + local fd = io.popen(string.format('ctags %s -n -f %s', M.flags, M.file)) + fd:close() +end, 'Generate ctags file') + +vis:command_register('tag', function(argv, _, win) + if not win.ctags then + vis:info('ctags not enabled') + return + end + + local tag = argv[1] + + if not tag or tag == '' then + vis:info('tag: missing argument') + return false + end + + local flags = win.ctags_readtags_flags or '' + local file = io.popen(string.format('readtags %s -t %s - %s', flags, M.file, tag)) + local line = file:read() + local success, msg, status = file:close() + + if not success then + vis:info(string.format('tag failed: [status %s] %s', status, msg)) + return false + end + + if not line or line == '' then + vis:info('tag: nothing found') + return false + end + + local prev = { + path = win.file.name, + line = win.selection.line, + col = win.selection.col, + } + + local file, lineno = line:match('^[^%s]+%s+([^%s]+)%s+(%d+)') + + if not vis:command(string.format('e %s', file)) then + vis:info(string.format('tag failed: %s cannot be opened', file)) + return false + end + + vis.win.selection:to(lineno, 0) + table.insert(tagstack, prev) + + return true +end, 'Jump to tag') + +vis:command_register('tag-back', function() + local item = table.remove(tagstack) + + if not item then + return + end + + if not vis:command(string.format('e %s', item.path)) then + vis:info(string.format('tag-back failed: %s cannot be opened', item.path)) + return + end + + vis.win.selection:to(item.line, item.col) +end, 'Go back in tag stack') + +vis.events.subscribe(vis.events.WIN_OPEN, function(win) + if not vis.win.ctags then + return + end + + win:map(vis.modes.NORMAL, '<C-t>', function() + vis:command('tag-back') + end) + + win:map(vis.modes.NORMAL, '<C-]>', function() + local tag = win.file:content(win.file:text_object_word(win.selection.pos)) + vis:command('tag ' .. tag) + end) + + win:map(vis.modes.VISUAL, '<C-]>', function() + local tag = win.file:content(win.selection.range) + vis:command('tag ' .. tag) + end) +end) + +vis.events.subscribe(vis.events.FILE_SAVE_POST, function() + if vis.win.ctags and vis.win.ctags_autosave then + vis:command('tag-generate') + end +end) diff --git a/lib/router.lua b/lib/router.lua @@ -0,0 +1,80 @@ +local wm = require('lib/window-manager') + +local stack = {} +local index = 0 +local allow = true + +local function push(item) + if item == stack[index] then + return + end + + for i = #stack, index + 1, -1 do + stack[i] = nil + end + + table.insert(stack, item) + index = #stack +end + +local function open(path) + vis:command(string.format('e %s', path)) +end + +local M = {} + +M.back = function() + if index <= 1 then + return + end + index = index - 1 + open(stack[index]) +end + +M.forward = function() + if index >= #stack then + return + end + index = index + 1 + open(stack[index]) +end + +M.navigate = function(path, cmd) + if not path then + return + end + + allow = true + open(path) + + if cmd then + vis:command(cmd) + end +end + +vis.events.subscribe(vis.events.WIN_OPEN, function(win) + if allow and win.file.name then + push(win.file.name) + allow = false + end +end) + +vis:command_register('tab', function(argv, _, win) + if not wm.enabled() then + vis:info('window manager missing') + return false + end + + win.navigate = wm.navigate + vis:command(table.concat(argv, ' ')) + win.navigate = M.navigate +end) + +vis:action_register('vis-router-back', M.back, "Go back") +vis:action_register('vis-router-forward', M.forward, "Go Forward") + +vis.events.subscribe(vis.events.WIN_OPEN, function(win) + win.navigate = M.navigate +end) + +return M diff --git a/lib/search.lua b/lib/search.lua @@ -0,0 +1,95 @@ +local session = require('lib/session') + +local M = { + find = 'ag --vimgrep -g . {path} | fzy', + dfind = 'find {path} -type d | fzy | fe', + grep = "ag --vimgrep --smart-case {pattern} {path} | fzy", + explore = 'fe', + grep_wrap = '"', +} + +local function split(str, delimiter) + local result = {} + for part in string.gmatch(str, string.format('[^%s]+', delimiter)) do + table.insert(result, part) + end + return result +end + +local function dirname(path) + local dir = path:match('(.*)/') + if dir == nil then + return '.' + else + return dir:match("^/") and dir or './'..dir + end +end + +local function run(command, onsuccess, fullscreen) + local file = io.popen(string.format('sh -c "%s"', command:gsub('"', '\\"'))) + local out = file:read() + local success, msg, status = file:close() + -- local status, out = vis:pipe(command, fullscreen) + + if success then + onsuccess(out) + end + + vis:redraw() + return success +end + +vis:command_register('find', function(argv, _, win) + local command = M.find:gsub('{path}', win.scope) + return run(command, win.navigate) +end, 'Find files') + +vis:command_register('dfind', function(_, _, win) + local command = M.find:gsub('{path}', win.scope) + return run(command, win.navigate) +end, 'Find directories') + +vis:command_register('grep', function(argv, _, win) + local pattern = #argv == 0 and content(win) or table.concat(argv, ' ') + pattern = pattern:gsub(M.grep_wrap, '\\'..M.grep_wrap) + pattern = string.format('%s%s%s', M.grep_wrap, pattern, M.grep_wrap) + + local command = M.grep:gsub('{pattern}', pattern):gsub('{path}', win.scope) + + return run(command, function(out) + out = split(out, ':') + win.navigate(out[1], string.format('normal %sG%sg|', out[2], out[3])) + end) +end, 'Grep') + +vis:command_register('explore', function(argv, force, win) + local path = './'..(win.file.name or '') + + local status, out, err = vis:pipe(path, M.explore, true) + if status ~= 0 then + return false + end + win.navigate(out) + vis:redraw() + return true; +end, 'Explore') + +vis:command_register('mru', function(argv, force, win) + local cmd = string.format("cat %s | sed 's|^'$(pwd)'/||' | fzy", session.mru_path) + return run(cmd, win.navigate) +end, 'Most recently used files') + +vis:command_register('scope', function(argv, _, win) + if win.file.name then + win.scope = dirname(win.file.name) + end + + vis:command(table.concat(argv, ' ')) + vis.win.scope = '.' +end) + +vis.events.subscribe(vis.events.WIN_OPEN, function(win) + win.scope = '.' +end) + +return M diff --git a/lib/session.lua b/lib/session.lua @@ -0,0 +1,97 @@ +local M = { + mru_max = 20, + mru_path = '/tmp/vis-mru', + pos_path = '/tmp/vis-pos', +} + +-- local function create_session_dir() + -- local cmd = 'echo "${TMPDIR-/tmp}/vis-$(id -u)/$(pwd | tr \'/\' \'~\')" | xargs -I{} sh -c \'mkdir -p "{}" && echo "{}"\'' + -- local full_cmd = "/bin/sh -c '" .. cmd:gsub("'", "'\\''") .. "'" + -- local fd = io.popen(full_cmd) + -- local dir = fd:read() + -- local success, msg, status = fd:close() + -- if not success then + -- print(msg) + -- os.exit(status) + -- end + -- return dir +-- end + +-- M.dir = create_session_dir() +-- M.mru_path = M.dir .. '/mru' +-- M.pos_path = M.dir .. '/pos' + +local function read(path) + local result = {} + local f = io.open(path) + if not f then return result end + for line in f:lines() do + table.insert(result, line) + end + f:close() + return result +end + +local function mru_write(win) + local path = win.file.path + if not path then return end + local mru = read(M.mru_path) + + if path == mru[1] then return end + + local f = io.open(M.mru_path, 'w+') + if f == nil then return end + + for i,v in ipairs(mru) do + if v == path then + table.remove(mru, i) + end + end + + table.insert(mru, 1, path) + + for _,v in pairs(mru) do + f:write(string.format('%s\n', v)) + end + + f:close() +end + +local function pos_setup(win) + local path = win.file.path + if not path then return end + local poss = read(M.pos_path) + + for _,v in pairs(poss) do + pos, pospath = v:match('^(%d+) (.+)$') + if path == pospath then + win.selection.pos = tonumber(pos) + break + end + end +end + +local function pos_write(win) + local path = win.file.path + if not path then return end + local poss = read(M.pos_path) + + local f = io.open(M.pos_path, 'w+') + if not f then return end + + for i,v in ipairs(poss) do + pos, pospath = v:match('^(%d+) (.+)$') + if not path == pospath then + f:write(string.format('%s\n', v)) + end + end + + f:write(string.format('%d %s\n', win.selection.pos, path)) + f:close() +end + +vis.events.subscribe(vis.events.WIN_OPEN, mru_write) +vis.events.subscribe(vis.events.WIN_OPEN, pos_setup) +vis.events.subscribe(vis.events.WIN_CLOSE, pos_write) + +return M diff --git a/lib/status.lua b/lib/status.lua @@ -0,0 +1,73 @@ +local wm = require('lib/window-manager') + +local mode_names = { + [vis.modes.NORMAL] = '[NORMAL]', + [vis.modes.OPERATOR_PENDING] = '[OPERATOR]', + [vis.modes.INSERT] = '[INSERT]', + [vis.modes.REPLACE] = '[REPLACE]', + [vis.modes.VISUAL] = '[VISUAL]', + [vis.modes.VISUAL_LINE] = '[VISUAL LINE]', +} + +local lastl = '' +local lastr = '' + +vis.events.subscribe(vis.events.WIN_OPEN, function(win) + if wm.enabled() then + vis:command('set statusbar off') + end +end) + +vis.events.subscribe(vis.events.WIN_CLOSE, function(win) + local wc = 0 + for _ in vis:windows() do wc = wc + 1 end + if wm.enabled() and wc == 1 then + wm.set_title('') + wm.set_status('') + end +end) + +vis.events.subscribe(vis.events.WIN_STATUS, function(win) + if win ~= vis.win then return end + + local filename = win.file.name or '[No Name]' + local modified = win.file.modified and '[+]' or '' + local recording = vis.recording and '@' or '' + local mode = mode_names[vis.mode] + local line = win.selection.line + local total_lines = #win.file.lines + local scroll_percent = 0 + local selections = #win.selections > 1 and string.format('%d-', #win.selections) or '' + local keys = tostring(vis.count or '') .. vis.input_queue:gsub(' ', '_') + + if total_lines > 0 then + scroll_percent = math.floor((line / total_lines) * 100) + end + + local left = filename + local right = string.format("%-2s %s %s %s %3s%d,%-4d %3d%%", + keys, + modified, + recording, + mode, + selections, + win.selection.line, + win.selection.col, + scroll_percent + ) + + if wm.enabled() then + if left ~= lastl then + wm.set_title('vis: ' .. left) + lastl = left + end + + if right ~= lastr then + wm.set_status(right .. ' « ') + lastr = right + end + else + win:status(left, right) + end +end) + diff --git a/lib/window-manager/init.lua b/lib/window-manager/init.lua @@ -0,0 +1,43 @@ +local tmux = require('lib/window-manager/tmux') + +local function nullfunc() + vis:info('window manager missing') + return false +end + +local nullstrategy = { + enabled = function() return false end, + init = nullfunc, + tabnew = nullfunc, + terminal = nullfunc, + navigate = nullfunc, + set_title = nullfunc, + set_status = nullfunc, +} + +local M = { + strategy = nullstrategy, +} + +M.init = function() + if os.getenv('TMUX') then + M.strategy = tmux + M.strategy.init() + end +end + +M.enabled = function(...) return M.strategy.enabled(...) end +M.tabnew = function(...) return M.strategy.tabnew(...) end +M.terminal = function(...) return M.strategy.terminal(...) end +M.navigate = function(...) return M.strategy.navigate(...) end +M.set_title = function(...) return M.strategy.set_title(...) end +M.set_status = function(...) return M.strategy.set_status(...) end + +vis.events.subscribe(vis.events.INIT, function() + M.init() +end) + +vis:command_register('tabnew', M.tabnew, 'Run vis command in a new tab page') +vis:command_register('terminal', M.terminal, 'Run shell command in a new tab page') + +return M diff --git a/lib/window-manager/tmux.lua b/lib/window-manager/tmux.lua @@ -0,0 +1,68 @@ +local fd +local pane_id + +local function write(cmd) + if io.type(fd) ~= 'file' then + vis:info('tmux client is not running') + return false + end + fd:write(cmd .. '\n') + fd:flush() + return true +end + +local tmux = { + type = 'tmux', +} + +tmux.init = function() + pane_id = os.getenv('TMUX_PANE') + + if not pane_id then + return + end + + fd = vis:communicate('tmux', + string.format("exec env -u TMUX tmux -C attach -t %s", pane_id)) + + vis.events.subscribe(vis.events.PROCESS_RESPONSE, function(name, event, code, msg) + if name ~= 'tmux' then + return + end + + if event == 'EXIT' then + vis:info('tmux client exited: ' .. code) + elseif event == 'SIGNAL' then + vis:info('tmux client received signal: ' .. code) + end + end) +end + +tmux.enabled = function() + return io.type(fd) == 'file' +end + +tmux.navigate = function(path, cmd) + cmd = cmd and "+'"..cmd.."'" or '' + return write(string.format('new-window -b vis %s %s', cmd, path)) +end + +tmux.tabnew = function(argv) + local command = table.concat(argv, ' ') + return write('new-window -b vis ' .. command) +end + +tmux.terminal = function(argv) + local command = table.concat(argv, ' '):gsub('"', '\\"') + return write(string.format('new-window -b "%s"', command)) +end + +tmux.set_title = function(title) + return write(string.format("select-pane -t %s -T '%s'", pane_id, title)) +end + +tmux.set_status = function(status) + return write(string.format("set -pt %s @pane_status '%s'", pane_id, status)) +end + +return tmux +\ No newline at end of file diff --git a/vendors/commentary.lua b/vendors/commentary.lua @@ -0,0 +1,147 @@ +-- +-- vis-commentary +-- + +local vis = _G.vis + +local comment_string = { + actionscript='//', ada='--', ansi_c='/*|*/', antlr='//', apdl='!', apl='#', + applescript='--', asp='\'', autoit=';', awk='#', b_lang='//', bash='#', + batch=':', bibtex='%', boo='#', chuck='//', cmake='#', coffeescript='#', + context='%', cpp='//', crystal='#', csharp='//', css='/*|*/', cuda='//', + dart='//', desktop='#', django='{#|#}', dmd='//', dockerfile='#', dot='//', + eiffel='--', elixir='#', erlang='%', faust='//', fennel=';;', fish='#', + forth='|\\', fortran='!', fsharp='//', gap='#', gettext='#', gherkin='#', + glsl='//', gnuplot='#', go='//', groovy='//', gtkrc='#', haskell='--', + html='<!--|-->', icon='#', idl='//', inform='!', ini='#', Io='#', + java='//', javascript='//', json='/*|*/', jsp='//', latex='%', ledger='#', + less='//', lilypond='%', lisp=';', logtalk='%', lua='--', makefile='#', + markdown='<!--|-->', matlab='#', moonscript='--', myrddin='//', + nemerle='//', nsis='#', objective_c='//', pascal='//', perl='#', php='//', + pico8='//', pike='//', pkgbuild='#', prolog='%', props='#', protobuf='//', + ps='%', pure='//', python='#', rails='#', rc='#', rebol=';', rest='.. ', + rexx='--', rhtml='<!--|-->', rstats='#', ruby='#', rust='//', sass='//', + scala='//', scheme=';', smalltalk='"|"', sml='(*)', snobol4='#', sql='#', + tcl='#', tex='%', text='', toml='#', vala='//', vb='\'', vbscript='\'', + verilog='//', vhdl='--', wsf='<!--|-->', xml='<!--|-->', yaml='#', zig='//', + nim='#', julia='#', rpmspec='#', caml='(*|*)' +} + +-- escape all magic characters with a '%' +local function esc(str) + if not str then return "" end + return (str:gsub('[[.+*?$^()%%%]-]', '%%%0')) +end + +-- escape '%' +local function pesc(str) + if not str then return "" end + return str:gsub('%%', '%%%%') +end + +local function rtrim(s) + local n = #s + while n > 0 and s:find("^%s", n) do n = n - 1 end + return s:sub(1, n) +end + +local function comment_line(lines, lnum, prefix, suffix) + if suffix ~= "" then suffix = " " .. suffix end + lines[lnum] = string.gsub(lines[lnum], + "(%s*)(.*)", + "%1" .. pesc(prefix) .. " %2" .. pesc(suffix)) +end + +local function uncomment_line(lines, lnum, prefix, suffix) + local match_str = "^(%s*)" .. esc(prefix) .. "%s?(.*)" .. esc(suffix) + local m = table.pack(lines[lnum]:match(match_str)) + lines[lnum] = m[1] .. rtrim(m[2]) +end + +local function is_comment(line, prefix) + return (line:match("^%s*(.+)"):sub(0, #prefix) == prefix) +end + +local function toggle_line_comment(lines, lnum, prefix, suffix) + if not lines or not lines[lnum] then return end + if not lines[lnum]:match("^%s*(.+)") then return end -- ignore empty lines + if is_comment(lines[lnum], prefix) then + uncomment_line(lines, lnum, prefix, suffix) + else + comment_line(lines, lnum, prefix, suffix) + end +end + +-- if one line inside the block is not a comment, comment the block. +-- only uncomment, if every single line is comment. +local function block_comment(lines, a, b, prefix, suffix) + local uncomment = true + for i=a,b do + if lines[i]:match("^%s*(.+)") and not is_comment(lines[i], prefix) then + uncomment = false + end + end + + if uncomment then + for i=a,b do + if lines[i]:match("^%s*(.+)") then + uncomment_line(lines, i, prefix, suffix) + end + end + else + for i=a,b do + if lines[i]:match("^%s*(.+)") then + comment_line(lines, i, prefix, suffix) + end + end + end +end + +vis:operator_new("gc", function(file, range, pos) + local comment = comment_string[vis.win.syntax] + local prefix, suffix = comment:match('^([^|]+)|?([^|]*)$') + if not prefix then return end + + local c = 0 + local i = 1 + local a = -1 + local b = -1 + for line in file:lines_iterator() do + local line_start = c + local line_finish = c + #line + 1 + if line_start < range.finish and line_finish > range.start then + if a == -1 then + a = i + b = i + else + b = i + end + end + c = line_finish + if c > range.finish then break end + i = i + 1 + end + block_comment(file.lines, a, b, prefix, suffix) + + return range.start +end, "Toggle comment on selected lines") + +vis:map(vis.modes.NORMAL, "gcc", function() + local win = vis.win + local lines = win.file.lines + local comment = comment_string[win.syntax] + if not comment then return end + local prefix, suffix = comment:match('^([^|]+)|?([^|]*)$') + if not prefix then return end + + for sel in win:selections_iterator() do + local lnum = sel.line + local col = sel.col + + toggle_line_comment(lines, lnum, prefix, suffix) + sel:to(lnum, col) -- restore cursor position + end + + win:draw() +end, "Toggle comment on a the current line") + diff --git a/vendors/ctags.lua b/vendors/ctags.lua @@ -0,0 +1,337 @@ +local positions = {} +local tags = { 'tags' } +local ctags = { actions = {} } + +local function abs_path(prefix, path) + if string.find(path, '^/') ~= nil then + return path, path + end + + if string.find(path, '^./') ~= nil then + path = path:sub(3) + end + + return prefix .. path, path +end + +local function is_directory(path) + local dir = io.open(path .. '/', 'r') + if dir then + dir:close() + return true + else + return false + end +end + +local function find_tags(path) + for i = #path, 1, -1 do + if path:sub(i, i) == '/' then + local prefix = path:sub(1, i) + for j = 1, #tags do + local tagfile = tags[j] + local filename + if tagfile:sub(1, 1) == '/' then + filename = tagfile + else + filename = prefix .. tagfile + end + if not is_directory(filename) then + local file = io.open(filename, 'r') + + if file ~= nil then + return file, prefix + end + end + end + end + end +end + +local function bsearch(file, word) + local buffer_size = 8096 + local format = '\n(.-)\t(.-)\t(.-);"\t' + + local from = 0 + local to = file:seek('end') + local startpos = nil + + while from <= to do + local mid = from + math.floor((to - from) / 2) + file:seek('set', mid) + + local content = file:read(buffer_size, '*line') + if content ~= nil then + local key, _, _ = string.match(content, format) + if key == nil then + break + end + + if key == word then + startpos = mid + end + + if key >= word then + to = mid - 1 + else + from = mid + 1 + end + else + to = mid - 1 + end + end + + if startpos ~= nil then + file:seek('set', startpos) + + local result = {} + while true do + local content = file:read(buffer_size, '*line') + if content == nil then + break + end + + for key, filename, excmd in string.gmatch(content, format) do + if key == word then + result[#result + 1] = { name = filename, excmd = excmd } + else + return result + end + end + end + + return result + end +end + +local function get_query() + local line = vis.win.selection.line + local pos = vis.win.selection.col + local str = vis.win.file.lines[line] + + local from, to = 0, 0 + while pos > to do + from, to = str:find('[%a_]+[%a%d_]*', to + 1) + if from == nil or from > pos then + return nil + end + end + + return string.sub(str, from, to) +end + +local function get_matches(word, path) + local file, prefix = find_tags(path) + + if file ~= nil then + local results = bsearch(file, word) + file:close() + + if results ~= nil then + local matches = {} + for i = 1, #results do + local result = results[i] + local abspath, name = abs_path(prefix, result.name) + local desc = string.format('%s%s', name, tonumber(result.excmd) and ':' .. result.excmd or '') + + matches[#matches + 1] = { desc = desc, path = abspath, excmd = result.excmd } + end + + return matches + end + end +end + +local function get_match(word, path) + local matches = get_matches(word, path) + if matches ~= nil then + for i = 1, #matches do + if matches[i].path == path then + return matches[i] + end + end + + return matches[1] + end +end + +local function escape(text) + return text:gsub('[][)(}{|+?*.]', '\\%0') + :gsub('%^', '\\^') + :gsub('^/\\%^', '/^') + :gsub('%$', '\\$') + :gsub('\\%$/$', '$/') + :gsub('\\\\%$%$/$', '\\$$') +end + +--[[ +- Can't test vis:command() as it will still return true if the edit command fails. +- Can't test File.modified as the edit command can succeed if the current file is + modified but open in another window and this behavior is useful. +- Instead just check the path again after trying the edit command. +]] +local function goto_pos(pos, force) + if pos.path ~= vis.win.file.path then + vis:command(string.format(force and 'e! "%s"' or 'e "%s"', pos.path)) + if pos.path ~= vis.win.file.path then + return false + end + end + if tonumber(pos.excmd) then + vis.win.selection:to(pos.excmd, pos.col) + else + vis.win.selection:to(1, 1) + vis:command(escape(pos.excmd)) + vis.win.selection.pos = vis.win.selection.range.start + vis.mode = vis.modes.NORMAL + end + return true +end + +local function goto_tag(path, excmd, force) + local old = { + path = vis.win.file.path, + excmd = vis.win.selection.line, + col = vis.win.selection.col, + } + + local last_search = vis.registers['/'] + if goto_pos({ path = path, excmd = excmd, col = 1 }, force) then + positions[#positions + 1] = old + vis.registers['/'] = last_search + end +end + +local function pop_pos(force) + if #positions < 1 then + return + end + if goto_pos(positions[#positions], force) then + table.remove(positions, #positions) + end +end + +local function win_path() + if vis.win.file.path == nil then + return os.getenv('PWD') .. '/' + end + return vis.win.file.path +end + +local function tag_cmd(tag, force) + local match = get_match(tag, win_path()) + if match == nil then + vis:info(string.format('Tag not found: %s', tag)) + else + goto_tag(match.path, match.excmd, force) + end +end + +local function gen_vis_menu(matches) + local width = 0 + for _, match in ipairs(matches) do + width = math.max(width, match.desc:len()) + end + -- limit max width of desc field (filename) in menu + width = math.min(width, 40) + local fmt = '%' .. #tostring(#matches) .. 'd %-' .. width .. 's %s' + + local lines = {} + for i, match in ipairs(matches) do + local desc = match.desc + if desc:len() > width then + desc = '...' .. desc:sub(desc:len() - width + 4) + end + + -- work around bug displaying tabs in vis-menu and + -- provide a clearer context + local excmd = match.excmd:gsub('%s+', ' ') + excmd = excmd:gsub('^/^', '') + excmd = excmd:gsub('$/$', '') + table.insert(lines, fmt:format(i, desc, excmd)) + end + + -- limit vis-menu height to ~1/4 the window height + -- +1 gives an empty line at bottom to signify + -- that there are no more lines to scroll through + local nlines = math.min(math.floor(vis.win.height / 4), #lines) + if nlines == #lines then + nlines = nlines + 1 + end + return 'vis-menu -l ' .. nlines .. " -p 'Choose tag:' << 'EOF'\n" .. table.concat(lines, '\n') .. '\n' .. 'EOF' +end + +local function tselect_cmd(tag, force) + local matches = get_matches(tag, win_path()) + if matches == nil then + vis:info(string.format('Tag not found: %s', tag)) + else + local status, output = vis:pipe(vis.win.file, { start = 0, finish = 0 }, gen_vis_menu(matches)) + + if status ~= 0 then + vis:info('Command failed') + return + end + + local choice = tonumber(string.match(output, '%d+')) + if choice == nil or choice < 1 or choice > #matches then + vis:info('Invalid choice') + return + end + goto_tag(matches[choice].path, matches[choice].excmd, force) + end +end + +vis:command_register('tag', function(argv, force, win, selection, range) + if #argv == 1 then + tag_cmd(argv[1], force) + end +end) + +vis:command_register('tselect', function(argv, force, win, selection, range) + if #argv == 1 then + tselect_cmd(argv[1], force) + end +end) + +vis:command_register('pop', function(argv, force, win, selection, range) + pop_pos(force) +end) + +vis:option_register('tags', 'string', function(value) + tags = {} + for str in value:gmatch('([^%s]+)') do + table.insert(tags, str) + end +end, 'Paths to search for tags (separated by spaces)') + +ctags.actions.tag = function(keys) + local query = get_query() + local force = false + if query ~= nil then + tag_cmd(query, force) + end + return 0 +end + +ctags.actions.tselect = function(keys) + local query = get_query() + local force = false + if query ~= nil then + tselect_cmd(query, force) + end + return 0 +end + +ctags.actions.pop = function(keys) + pop_pos() + return 0 +end + +vis:map(vis.modes.NORMAL, '<C-]>', ctags.actions.tag) + +vis:map(vis.modes.NORMAL, 'g<C-]>', ctags.actions.tselect) + +vis:map(vis.modes.NORMAL, '<C-t>', ctags.actions.pop) + +return ctags diff --git a/vendors/modelines.lua b/vendors/modelines.lua @@ -0,0 +1,167 @@ +-- +-- vis-modelines +-- +-- Vim's modelines are very useful for setting per-file settings in Vim. +-- For example, the filetype can't always be reliably inferred from the +-- filename, i.e. for templates with generic file extensions or script files +-- that omit the file extension altogether. +-- +-- This Vis plugin tries to read standard Vim modelines and set the following +-- (Vis) settings: +-- +-- autoindent, expandtab, numbers, tabwidth, syntax. +-- +-- Vim (by default) looks for modelines in the first 5 and last 5 lines of the +-- file. This will emulate this behaviour, but omit the setting to change this +-- threshold, as no sane person would change it (it would break everybody +-- else's Vim). +-- +-- This parser assumes you will only use *one* modeline per file, to avoid +-- having to resolve conflicts. It will use the first modeline it finds from +-- the top. +-- + +local lpeg = require("lpeg") +local P, C, Ct, R, S = lpeg.P, lpeg.C, lpeg.Ct, lpeg.R, lpeg.S + +-- vim has two styles for modelines: +-- +-- 'set' style, can be delimeted with a : at the end. Delimiter is whitespace. +-- E.g.: vim: set ft=lua sw=2 ts=2 autoindent: +-- +-- Colon style, modeline continues to the end of the line. Delimeter is +-- whitespace or ':'. +-- E.g.: vim: noai:ts=4:sw=4 ft=lua +-- +-- (All parsing based on the information in :help modeline) + +local digit = R("09") +local num = digit * digit * digit +local ver = S("><=") * num + num + P("") +local vim = P(" vim")*ver*":" + " Vim"*ver*":" + " vi:" + " ex:" + +local whitespace = S("\t ")^1 +local optwhitespace = whitespace + P("") +local prefix = (1-vim)^0 * vim * optwhitespace + +local optchars = R("az", "AZ") + R("09") + S("_\"\'") +local option = Ct(C(optchars^1) * "=" * C(optchars^1)) + C(optchars^1) + +local set = P("set") + P("se") +local setstyle = set * (whitespace * option)^0 * P(":")^-1 +local separator = (optwhitespace * P(":") * optwhitespace) + whitespace +local colonstyle = option * (separator * option)^0 + +-- matches & captures options +local modeline = Ct(prefix * (setstyle + colonstyle)) + +-- detects modelines +local modeline_detect = prefix * optwhitespace + +-- Simple Vim settings like 'autoindent' without options are mapped directly to +-- a command for Vis. If the settings is a variable (like 'ft=lua'), a mapping +-- function is used to generate the Vis command. + +local sw_set = false + +local function sw_f(value) + sw_set = true + return "set tw "..value +end + +local function ts_f(value) + if not sw_set then + return "set tw "..value + else + return nil + end +end + +local command_mapping = { + autoindent = "set ai on", + noautoindent = "set ai off", + ai = "set ai on", + noai = "set ai off", + expandtab = "set et on", + noexpandtab = "set et off", + et = "set et on", + noet = "set et off", + number = "set nu on", + nonumber = "set nu off", + nu = "set nu on", + nonu = "set nu off", + ft = function(value) return "set syntax "..value end, + filetype = function(value) return "set syntax "..value end, + + -- Vis only knows the 'tabwidth' setting instead of 'sw' and 'ts' settings. + -- We prefer the 'sw' setting for 'tabwidth', because it is what the + -- modeline wants to be inserted. How it is displayed is somewhat less + -- important. + sw = sw_f, + shiftwidth = sw_f, + ts = ts_f, + tabstop = ts_f +} + +local function parse_modeline(line) + return modeline:match(line) +end + +local function map_options(line) + local commands = {} + local opts = parse_modeline(line) + if not opts then return nil end + + for _,o in pairs(opts) do + if type(o) == "string" then -- simple options are stored as strings + local c = command_mapping[o] + if c then + table.insert(commands, c) + end + elseif type(o) == "table" then -- variables are stored as a table + local map_f = command_mapping[o[1]] + if map_f then + local c = map_f(o[2]) + if c then table.insert(commands, c) end + end + end + end + return commands +end + +local function find_modeline(lines) + if not lines then return nil end + if #lines < 10 then + for i=1,#lines do + local l = lines[i] + if modeline_detect:match(l) then + return l + end + end + else + for i=1,5 do + local l = lines[i] + if modeline_detect:match(l) then + return l + end + end + for i=0,4 do + local l = lines[#lines - i] + if modeline_detect:match(l) then + return l + end + end + end + return nil +end + +vis.events.subscribe(vis.events.START, function() + local file = vis.win.file + if not file then return end + local ml = find_modeline(file.lines) + if not ml then return end + local commands = map_options(ml) + for _,c in pairs(commands) do + vis:command(c) + end +end) diff --git a/vendors/quickfix.lua b/vendors/quickfix.lua @@ -0,0 +1,525 @@ +-- SPDX-License-Identifier: GPL-3.0-or-later +-- © 2020 Georgi Kirilov + +require("vis") +local vis = vis + +local getcwd + +if vis:module_exist"lfs" then + require"lfs" + local lfs = lfs + + getcwd = lfs.currentdir +else + getcwd = function() + return io.popen"pwd":read"*l" + end +end + +local progname = ... + +local M = { + grepformat = { + "^%s*([^:]+):(%d+):(%d+):(.*)$", -- git-grep with --column + "^%s*([^:]+):(%d+):(.-)(.*)$", + }, + errorformat = { + "^%s*([^:]+):(%d+):(%d+):(.*)$", + "^%s*([^:]+):(%d+):(.-)(.*)$", + "^(%S+) %S+ (%d+) (.-)(.*)$", -- cscope + [0] = { + ["Entering directory [`']([^']+)'"] = true, + ["Leaving directory [`']([^']+)'"] = false, + }, + }, + grepprg = "git grep --column", + makeprg = "make -k", + errorfile = "errors.err", + peek = true, + menu = false, + action = {}, +} + +local cwin +local ctitle +local lines = {valid = {}} +local no_more = "No more items" +local no_errors = "No Errors" + +local function find_nearest_after(line) + local ccur + for c, v in ipairs(lines.valid) do + if v[1] >= line then + ccur = c + break + end + end + if not ccur then + return nil, no_more + end + return {ccur, lines.valid[ccur][1]} +end + +local function find_nth_valid(count) + if count < 1 or count > #lines.valid then + return nil, no_more + end + return {count, lines.valid[count][1]} +end + +local function set_marks(win, ccur) + if not ccur then return end + local fname = win.file.name + local pwd = getcwd() + local pathname = fname:find("^/") and fname or pwd .. "/" .. fname + local i = ccur + while lines.valid[i] and lines.valid[i].path == pathname do + i = i - 1 + end + local ln = lines.valid[i + 1] + while ln and ln.path == pathname do + -- I wish there was a way to convert from ln:col to pos + -- without setting the selection + win.selection:to(ln.line, ln.column or 1) + ln.mark = win.file:mark_set(win.selection.pos) + i = i + 1 + ln = lines.valid[i] + end +end + +local function botright_reopen(filename, ccur) + -- This function closes and opens windows in a specific order, just so + -- the error window ends up at bottom position. + -- This is fragile, and does not work in all possible situations. + -- Having a window with a modified file is one example. + -- Even if the file was not modified, closing the window will lead to loss of + -- any state local to it. + -- It would be nice if vis had :botright or something to that effect. + local cursors = {} + for w in vis:windows() do + if w.file.name then + cursors[w.file.name] = w.selection.pos + end + if cwin and w ~= cwin then + w:close() + end + end + for w in vis:windows() do + if w.file.name and w.file.modified then + vis:info"No write since last change" + return false + end + end + if filename then + vis:command(string.format((cwin and "open" or "e") .. " %q", filename)) + set_marks(vis.win, ccur) + if cursors[filename] then + vis.win.selection.pos = cursors[filename] + end + else + vis:command"new" + end + return true +end + +local function counter(ccur) + return string.format("%s/%d", + ccur or "-", + #lines.valid) +end + +local function display(ccur, cline) + local ln = lines.valid[ccur] + local pwd = getcwd() + local cname = ln.path:find(pwd) and ln.path:gsub(pwd, "", 1):gsub("^/", "") or ln.path + if vis.win.file.name ~= cname then + if not botright_reopen(ln.path, ccur) then return end + end + local column = ln.column + if type(column) ~= "number" then + column = nil + -- else + -- TODO: some tools report virtual columns, others - physical. + -- local indent = vis.win.file.lines[ln.line]:match"^%s+" or "" + -- local _, tabs = indent:gsub("\t", "") + -- -- XXX: assume tools to report 8 columns-wide tabs + -- column = column - tabs * 8 + tabs + end + if cwin then + cwin.selection:to(cline, 1) + local pos = cwin.selection.pos + local clen = cwin.file.lines[cline] + lines.range = {start = pos, finish = pos + #clen - 1} + end + if ln.mark then + local newpos = vis.win.file:mark_get(ln.mark) + if newpos then + vis.win.selection.pos = newpos + end + else + -- XXX: degrade to using raw line:column; + -- so far, only triggered by consecutive hard links + -- where vis keeps the old file.name but the error list + -- switches to the new. set_marks gets confused and sets no marks. + vis.win.selection:to(ln.line, column or 1) + end + if not cwin and ln.message then + vis:info(string.format("[%s] %s", counter(ccur), ln.message:gsub("^%s", ""))) + end + lines.ccur = ccur + lines.cline = cline +end + +local function _cc(count) + if not count then return end + local location, err = find_nth_valid(count) + if not location then + vis:info(err) + return + end + return table.unpack(location) +end + +local function guard(func) + return function(...) + if #lines.valid == 0 then + vis:info(no_errors) + return + end + local ccur, cline = func(...) + if ccur and cline then + display(ccur, cline) + end + end +end + +local cc = guard(function(count) + return _cc(count or lines.ccur or 1) +end) + +local cnext = guard(function(count) + return _cc((lines.ccur or 0) + (count or 1)) +end) + +local cprev = guard(function(count) + return _cc((lines.ccur or 2) - (count or 1)) +end) + +local crewind = guard(function() + return _cc(1) +end) + +local clast = guard(function() + return _cc(#lines.valid) +end) + +local cnfile = guard(function(count) + count = count or 1 + if not lines.ccur then + lines.ccur = 1 + end + local cur_fname = lines.valid[lines.ccur].path + for i = lines.ccur + 1, #lines.valid do + local filename = lines.valid[i].path + if filename then + if filename ~= cur_fname then + count = count - 1 + end + if count == 0 then + return i, lines.valid[i][1] + end + end + end + vis:info(no_more) +end) + +local cpfile = guard(function(count) + count = count or 1 + if not lines.ccur then + lines.ccur = 1 + end + local cur_fname = lines.valid[lines.ccur].path + for i = lines.ccur - 1, 1, -1 do + local filename = lines.valid[i].path + if filename then + if filename ~= cur_fname then + count = count - 1 + end + if count == 0 then + return i, lines.valid[i][1] + end + end + end + vis:info(no_more) +end) + +local function open_error_window() + if cwin then return end + if not ctitle then + vis:info(no_errors) + return + end + local fname = vis.win.file.name + vis:command"new" + cwin = vis.win + cwin.file:insert(0, lines.buffer or "") + cwin.file.modified = false + local cline1 + if lines.cline then + cline1 = lines.cline + else + local first = find_nth_valid(1) + cline1 = first and first[2] or 1 + end + cwin.selection:to(cline1, 1) + if lines.cline then + local pos = cwin.selection.pos + local clen = cwin.file.lines[lines.cline] + lines.range = {start = pos, finish = pos + #clen - 1} + end + if #lines.valid > 0 then + if cwin.options then + cwin.options.cursorline = true + else + vis:command"set cursorline" + end + end + cwin:map(vis.modes.NORMAL, "<Enter>", function() + if #lines.valid == 0 then + vis:info(no_errors) + return + end + local location, err = find_nearest_after(vis.win.selection.line) + if not location then + vis:info(err) + return + end + display(table.unpack(location)) + if M.menu then + cwin:close() + end + end) + botright_reopen(fname, lines.ccur) + vis:feedkeys"<vis-window-prev>" +end + +local function cwindow() + if cwin then + cwin:close(true) + else + open_error_window() + end +end + +local function store_from_string(str, fmt) + if str and string.len(str) == 0 then + str = nil + end + lines = {buffer = str, valid = {}} + if not lines.buffer then return end + if not fmt then + fmt = M.errorformat + elseif type(fmt) == "string" then + fmt = {fmt} + end + local i = 0 + local dirstack = {} + local pwd = getcwd() + for ln in lines.buffer:gmatch("[^\n]*") do + i = i + 1 + for patt, push in pairs(fmt[0] or {}) do + local dir = ln:match(patt) + if dir then + if push then + table.insert(dirstack, dir) + elseif dirstack[#dirstack] == dir then + table.remove(dirstack) + end + end + end + local cwd = dirstack[#dirstack] or pwd + local filename, line, column, message + for _, f in ipairs(fmt) do + filename, line, column, message = ln:match(f) + if filename and line then + break + end + end + if filename and line then + local pathname = filename:find("^/") and filename or string.format("%s/%s", cwd, filename) + local t = {i, path = pathname, line = tonumber(line), column = tonumber(column), message = message} + table.insert(lines.valid, t) + end + end +end + +local function store_from_file(errorfile) + if errorfile then + M.errorfile = errorfile + end + local efile = io.open(errorfile or M.errorfile) + if not efile then + vis:info(string.format("Can't open errorfile %s", errorfile or M.errorfile)) + return + end + local str = efile:read"*all" + efile:close() + store_from_string(str) + return true +end + +local function store_from_window(win) + local str = win.file:content(0, win.file.size) + store_from_string(str) +end + +local function cfile(argv) + if store_from_file(argv[1]) then + local was_open + if cwin then + cwin:close(true) + was_open = true + end + ctitle = string.format(argv[1] and "%s %s" or "%s", argv[0], argv[1]) + if was_open then + open_error_window() + end + crewind() + end +end + +local function cbuffer(argv) + store_from_window(vis.win) + local fname = vis.win.file.name + ctitle = string.format(fname and "%s (%s)" or "%s", argv[0], fname) + vis.win.file.modified = false + crewind() +end + +local function _cexpr(cmd, fmt, title, is_make) + if not cmd or #cmd == 0 then vis:info"Argument required" return end + ctitle = title or cmd + local code, stdout = vis:pipe(nil, nil, cmd .. " 2>&1") + local was_open + if cwin then + cwin:close(true) + was_open = true + end + store_from_string(stdout, fmt) + lines.code = code ~= 0 and code or nil + if is_make and code == 0 then + vis:info(string.format("'%s' finished", M.makeprg)) + return + end + if was_open or M.peek or #lines.valid == 0 then + open_error_window() + end + if not M.peek and #lines.valid > 0 then + crewind() + end +end + +local function quote_spaces(argv) + for i, arg in ipairs(argv) do + if arg:find("[ \t\n]") then + argv[i] = "'" .. arg .. "'" + end + end +end + +local function cexpr(argv) + quote_spaces(argv) + _cexpr(table.concat(argv, " ")) +end + +local function grep(argv) + quote_spaces(argv) + table.insert(argv, 1, M.grepprg) + _cexpr(table.concat(argv, " "), M.grepformat) +end + +local function make(argv) + quote_spaces(argv) + table.insert(argv, 1, M.makeprg) + _cexpr(table.concat(argv, " "), M.errorformat, nil, true) +end + +local function h(msg) + return string.format("|@%s| %s", progname, msg) +end + +vis.events.subscribe(vis.events.INIT, function() + -- These commands assume an existing error list: + local ccommands = { + cn = {cnext, "Display the [arg]-th next error"}, + cp = {cprev, "Display the [arg]-th previous error"}, + cnf = {cnfile, "Display the first error in the [arg]-th next file"}, + cpf = {cpfile, "Display the last error in the [arg]-th previous file"}, + cc = {cc, "Display [arg]-th error. If [arg] is omitted, the same error is displayed again."}, + cr = {crewind, "Display the first error"}, + cla = {clast, "Display the last error"}, + } + -- These commands create a new error list: + local qcommands = { + cf = {cfile, "Read the error list from [arg]"}, + cb = {cbuffer, "Read the error list from the current file"}, + cex = {cexpr, "Create an error list using the result of [args]"}, + grep = {grep, string.format("Create an error list using the result of '%s'", M.grepprg)}, + make = {make, string.format("Create an error list using the result of '%s'", M.makeprg)}, + cw = {cwindow, "Toggle the error window"}, + } + for cmd, def in pairs(ccommands) do + local func, help = table.unpack(def) + vis:command_register(cmd, function(argv) + local count = argv[1] and tonumber(argv[1]) + func(count) + end, h(help)) + M.action[cmd] = function(arg) + -- XXX: do not convert, say, "1" to 1; a digit can be passed by vis.map but it is not a count + local count = type(arg) == "number" and arg + func(count) + end + end + for cmd, def in pairs(qcommands) do + local func, help = table.unpack(def) + vis:command_register(cmd, func, h(help)) + end + M.cexpr = _cexpr + vis:option_register("qfm", "bool", function(value, toggle) + if toggle then + M.menu = not M.menu + else + M.menu = value + end + end, h"Menu - jumping to an error with <Enter> closes the error window") + vis:option_register("qfp", "bool", function(value, toggle) + if toggle then + M.peek = not M.peek + else + M.peek = value + end + end, h"Peek - :make, :grep, and :cex do not jump to the first error") +end) + +vis.events.subscribe(vis.events.WIN_STATUS, function(win) + if win ~= cwin then return end + win:status( + string.format(" [Quickfix List]%s :%s", (win.file.modified and " [+]" or ""), ctitle), + lines.code and string.format("exit: %d « [%s] ", lines.code, counter(lines.ccur)) + or string.format("[%s] ", counter(lines.ccur)) + ) +end) + +vis.events.subscribe(vis.events.WIN_CLOSE, function(win) + if win ~= cwin then return end + cwin = nil +end) + +vis.events.subscribe(vis.events.WIN_HIGHLIGHT, function(win) + if win ~= cwin then return end + if not (lines and lines.range) then return end + win:style(win.STYLE_CURSOR, lines.range.start, lines.range.finish) +end) + +return M diff --git a/vendors/surround.lua b/vendors/surround.lua @@ -0,0 +1,192 @@ +-- SPDX-License-Identifier: GPL-3.0-or-later +-- © 2020 Georgi Kirilov + +require("vis") +local vis = vis + +local progname = ... + +local M = { + prefix = {add = {"ys", "S"}, change = {"cs", "C"}, delete = {"ds", "D"}}, +} + +local builtin_textobjects = { + ["["] = {{ "[" , "]" }, id = 7}, -- +/VIS_TEXTOBJECT_OUTER_SQUARE_BRACKET vis.h + ["{"] = {{ "{" , "}" }, id = 9}, -- +/VIS_TEXTOBJECT_OUTER_CURLY_BRACKET vis.h + ["<"] = {{ "<" , ">" }, id = 11}, -- +/VIS_TEXTOBJECT_OUTER_ANGLE_BRACKET vis.h + ["("] = {{ "(" , ")" }, id = 13}, -- +/VIS_TEXTOBJECT_OUTER_PARENTHESIS vis.h + ['"'] = {{ '"' , '"' }, id = 15}, -- +/VIS_TEXTOBJECT_OUTER_QUOTE vis.h + ["'"] = {{ "'" , "'" }, id = 17}, -- +/VIS_TEXTOBJECT_OUTER_SINGLE_QUOTE vis.h + ["`"] = {{ "`" , "`" }, id = 19}, -- +/VIS_TEXTOBJECT_OUTER_BACKTICK vis.h + {{ "" , "" }, id = 28}, -- +/VIS_TEXTOBJECT_INVALID vis.h +} + +local aliases = {} +for key, data in pairs(builtin_textobjects) do + local pair = data[1] aliases[pair[2]] = key ~= pair[2] and data or nil +end +for alias, data in pairs(aliases) do + builtin_textobjects[alias] = data +end +for alias, key in pairs({ + B = "{", + b = "(", +}) do builtin_textobjects[alias] = builtin_textobjects[key] end + +local function get_pair(key) return builtin_textobjects[key] and builtin_textobjects[key][1] end + +local function take_param(_, d) + if d and type(d[3]) == "table" then + if #d[3] == 2 then + if table.concat(d[3]):find("\xef\xbf\xbd", 1, true) then + local status, out = vis:pipe(nil, nil, "vis-menu" .. (d[4] and " -p '" .. d[4] .. ":'" or "")) + if status == 0 then + local param = out:sub(1, -2) + return {d[3][1]:gsub("\xef\xbf\xbd", param), d[3][2]:gsub("\xef\xbf\xbd", param)} + end + else + return d[3] + end + end + else + return d + end +end + +local function adjust_spacing(file, range, d) + local padding = "" + if vis.mode == vis.modes.VISUAL_LINE then + padding = d[1] ~= "\n" and "\n" or padding + elseif vis.mode ~= vis.modes.VISUAL then + local trailing = file:content(range):match("(%s*)$") + if #trailing > 0 then + range.finish = range.finish - #trailing + end + end + return padding +end + +local function add(file, range, pos) + if range.finish <= range.start then return pos end + local d = take_param(vis.win, get_pair(M.key[1], pos)) + if not d then return pos end + local padding = adjust_spacing(file, range, d) + file:insert(range.finish, d[2] .. padding) + file:insert(range.start, d[1] .. padding) + return range.start +end + +local function escape(text) + return text:gsub("[][^$)(%%.*+?-]", "%%%0") +end + +local function delimiters_in_place(file, range, pos, key, get_padding) + local start, slen, finish, flen + if vis.mode == vis.modes.VISUAL_LINE then + local block = file:content(range) + vis.count = nil + local d = get_pair(key, range.start + block:find("\n", 1, true)) + if not (d and d[1] and d[2]) then return end + local d1, d2 = escape(d[1]), escape(d[2]) + local sl = table.pack(block:match("^()[ \t]*()" .. d1 .. "[ \t]-\n()")) + if #sl == 0 then + sl = table.pack(block:match("()[ \t]*()" .. d1 .. "[ \t]-()\n")) + end + local el = table.pack(block:match("()\n[ \t]*()" .. d2 .. "()[ \t]*\n$")) + if #el == 0 then + el = table.pack(block:match("\n[ \t]*()()" .. d2 .. "[ \t]*()[^\n]-\n$")) + end + if not (#sl > 0 and #el > 0) then return end + start = range.start + sl[get_padding and 1 or 2] - 1 + slen = get_padding and sl[3] - sl[1] or #d[1] + finish = range.start + el[get_padding and 1 or 2] - 1 + flen = get_padding and el[3] - el[1] or #d[2] + else + local d = get_pair(key, pos) + if not (d and d[1] and d[2]) then return end + if file:content(range.start, #d[1]):find(d[1], 1, true) + and file:content(range.finish - #d[2], #d[2]):find(d[2], 1, true) then + start, slen, finish, flen = range.start, #d[1], range.finish - #d[2], #d[2] + end + end + return start, slen, finish, flen +end + +local function change(file, range, pos) + if range.finish <= range.start then return pos end + local start, slen, finish, flen = delimiters_in_place(file, range, pos, M.key[1]) + if not start then return pos end + local n = take_param(vis.win, get_pair(M.key[2], pos)) + if not n then return pos end + file:delete(finish, flen) + file:insert(finish, n[2]) + file:delete(start, slen) + file:insert(start, n[1]) + if pos < range.start + slen then + return (pos < range.start + #n[1] and pos < range.start + slen - 1 or slen == 1) and pos or range.start + #n[1] - 1 + elseif pos >= range.finish - flen then + return (pos < range.finish - flen + #n[2] and pos < range.finish - 1) and pos - slen + #n[1] or range.finish - slen - flen + #n[1] + #n[2] - 1 + else + return pos - slen + #n[1] + end +end + +local function delete(file, range, pos) + if range.finish <= range.start then return pos end + local start, slen, finish, flen = delimiters_in_place(file, range, pos, M.key[1], true) + if not start then return pos end + file:delete(finish, flen) + file:delete(start, slen) + return range.start +end + +local function outer(key) + return builtin_textobjects[key] and builtin_textobjects[key].id or builtin_textobjects[1].id +end + +local function va_call(id, nargs, needs_range) + return function(keys) + if #keys < nargs then return -1 end + if #keys == nargs then + M.key = {} + for key in keys:gmatch(".") do table.insert(M.key, key) end + vis:operator(id) + if needs_range then + vis:textobject(outer(M.key[1])) + end + end + return #keys + end +end + +local function h(msg) + return string.format("|@%s| %s", progname, msg) +end + +local function operator_new(prefix, handler, nargs, help) + local id = vis:operator_register(handler) + if id < 0 then + return false + end + if type(prefix) == "table" then + local needs_range = ({[change] = true, [delete] = true})[handler] + if prefix[1] then vis:map(vis.modes.NORMAL, prefix[1], va_call(id, nargs, needs_range), h(help)) end + if prefix[2] then vis:map(vis.modes.VISUAL, prefix[2], va_call(id, nargs), h(help)) end + end + return id +end + +vis.events.subscribe(vis.events.INIT, function() + M.operator = { + add = operator_new(M.prefix.add, add, 1, "Add delimiters at range boundaries"), + change = operator_new(M.prefix.change, change, 2, "Change delimiters at range boundaries"), + delete = operator_new(M.prefix.delete, delete, 1, "Delete delimiters at range boundaries"), + } + local vis_pairs = package.loaded["pairs"] or package.loaded["vis-pairs"] + if vis_pairs then + get_pair = function(key, pos) return vis_pairs.get_pair(key, vis.win, pos) end + outer = function(key) vis_pairs.key = key return vis_pairs.textobject.outer end + end +end) + +return M diff --git a/vendors/vis-lspc/.editorconfig b/vendors/vis-lspc/.editorconfig @@ -0,0 +1,16 @@ +# http://editorconfig.org + +root = true + +[*.lua] +indent_style = space +indent_size = 2 + +[.gitlab-ci.yml] +indent_style = space +indent_size = 2 + +[tools/*] +indent_style = space +indent_size = 4 +binary_next_line = true diff --git a/vendors/vis-lspc/.gitlab-ci.yml b/vendors/vis-lspc/.gitlab-ci.yml @@ -0,0 +1,20 @@ +image: muhq/lua-dev:0.2 + +stages: + - check + - test + +check-luacheck: + stage: check + script: + - make check-luacheck + +check-format: + stage: check + script: + - make check-format + +test: + stage: test + script: + - make test diff --git a/vendors/vis-lspc/.lua-format b/vendors/vis-lspc/.lua-format @@ -0,0 +1,27 @@ +column_limit: 100 +indent_width: 2 +use_tab: false +spaces_before_call: 1 +keep_simple_control_block_one_line: false +keep_simple_function_one_line: false +align_args: true +break_after_functioncall_lp: false +break_before_functioncall_rp: false +spaces_inside_functioncall_parens: false +spaces_inside_functiondef_parens: false +align_parameter: true +chop_down_parameter: false +break_after_functiondef_lp: false +break_before_functiondef_rp: false +align_table_field: true +break_after_table_lb: true +break_before_table_rb: true +chop_down_table: true +chop_down_kv_table: true +table_sep: "," +extra_sep_at_table_end: true +column_table_limit: 80 +spaces_inside_table_braces: false +break_after_operator: true +double_quote_to_single_quote: true +spaces_around_equals_in_field: true diff --git a/vendors/vis-lspc/LICENSE b/vendors/vis-lspc/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/> + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + <program> Copyright (C) <year> <name of author> + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +<https://www.gnu.org/licenses/>. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +<https://www.gnu.org/licenses/why-not-lgpl.html>. diff --git a/vendors/vis-lspc/Makefile b/vendors/vis-lspc/Makefile @@ -0,0 +1,19 @@ +.PHONY: check format check-luacheck check-format test + +LUA_FILES := $(shell find -name "*.lua" -not -path "./json.lua" -and -not -path "./tests/*") + +TEST_FILES := $(shell find -name "*_test.lua") + +check: check-luacheck check-format + +check-luacheck: + luacheck --globals=vis -- $(LUA_FILES) + +check-format: + set -e; for lf in $(LUA_FILES); do tools/check-format "$${lf}"; done + +format: + lua-format -i $(LUA_FILES) + +test: + set -e; for tf in $(TEST_FILES); do "$$tf"; done diff --git a/vendors/vis-lspc/README.md b/vendors/vis-lspc/README.md @@ -0,0 +1,216 @@ +# `vis-lspc` + +A language server protocol client for the [`vis` editor](https://github.com/martanne/vis). + +## What's working + +`vis-lspc` currently supports: +* `textDocument/completion` +* `textDocument/declaration` +* `textDocument/definition` +* `textDocument/references` +* `textDocument/typeDefinition` +* `textDocument/implementation` +* `textDocument/hover` +* `textDocument/rename` +* `textDocument/formatting` +* `Diagnostics` + +## What's not working + +Everything else. + +To my knowledge there is currently no good way to detect file changes via the Lua API. +But this is essential to support [Text Synchronization](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textSynchronization) which is required by the +LSP protocol. + +A dirty workaround we currently use is to send the whole file content in a `textDocument/didChange` +method call before calling any other method. +If someone can come up with an idea how to solve this I would appreciate contributions. + +Communicating with language-servers via other channels than stdin/stdout. + +Currently, only a handful of language servers are configured by default. +Their configuration can be found in [`supported_servers.lua`](https://gitlab.com/muhq/vis-lspc/-/blob/main/supported-servers.lua). + +## Requirements + +* `vis` must offer the `communicate` Lua API + * The API included in `vis` >= 0.9 is supported on the main branch + * For legacy support using the [first API draft patches](https://github.com/martanne/vis/pull/675) use the v0.2.x branch +* The language server you want to use. [Microsoft's list of implementations](https://microsoft.github.io/language-server-protocol/implementors/servers/) +* Optional: the JSON implementation of your choice + * must provide `encode` and `decode` methods + * `vis-lspc` tries to find a suitable JSON implementation using those candidates: + * `json` + * `cjson` + * `dkjson` + * bundled fallback (no utf8 support) + +## Installation + +1. Clone this repository into your `vis` plugins directory +2. Load the plugin in your `visrc.lua` with `require('plugins/vis-lspc')` + +Alternatively, a plugin manager like [vis-plug](https://github.com/erf/vis-plug) can be used to install `vis-lspc`. + +## Usage + +`vis-lspc` provides some default key bindings: + +### Default Bindings + + Normal mode: + <F2> - start a language server for win.syntax + <F3> - open win.file with a running language server + <C-]> | <gd> - jump to the definition of the symbol under the main cursor + <gD> - jump to declaration + <gd> - jump to definition + <gi> - jump to implementation + <gr> - show references + < D> - jump to type definition + <C-t> - go back in the jump history + < e> - show diagnostics of current line + <K> - hover over current position + Normal and Insert mode: + <C- > - get completions + + +### Available commands + + # language-server management: + lspc-start-server [syntax] - start a language server for syntax or win.syntax + lspc-stop-server [syntax] - stop the language server for syntax or win.syntax + + # file registration: + lspc-open - register the file in the current window + lspc-close - unregister the file in the current window + + # navigation commands (they all operate on the symbol under the main cursor): + lspc-completion - syntax completion + lspc-references [e | vsplit | hsplit] - select and open a reference + lspc-declaration [e | vsplit | hsplit] - select and open a declaration + lspc-definition [e | vsplit | hsplit] - open the definition + lspc-typeDeclaration [e | vsplit | hsplit] - select and open a type declaration + lspc-implementation [e | vsplit | hsplit] - I actually have no idea what this does + + lspc-back - navigate back in the goto history + + # workspace edits + lspc-rename <new name> - rename the identifier under the cursor to <new name> + lspc-format - format the file in the current window + + # development support + lspc-hover - hover over the current line + lspc-show-diagnostics - show the available diagnostics of the current line + lspc-next-diagnostic - jump to the next available diagnostic + lspc-prev-diagnostic - jump to the previous available diagnostic + +### Available configuration options + +The module table returned by `require('plugins/vis-lspc')` can be used to configure +some aspects of `vis-lspc`. + +Available options are: + +* `name = 'vis-lspc'` - the name `vis-lspc` introduces itself to a language server +* `logging = false` - enable logging only useful for debugging `vis-lspc` +* `log_file = nil` - nil, filename or function returning a filename + * If `log_file` is `nil` `vis-lspc` will create a new file in `$XDG_DATA_HOME/vis-lspc` +* `autostart = true` - try to start a language server in WIN_OPEN +* `menu_cmd = 'fzf' or 'vis-menu'` - program to prompt for user choices +* `confirm_cmd = 'vis-menu'` - program to prompt for user confirmation +* `ls_map` - a table mapping `vis` syntax names to language server configurations +* `highlight_diagnostics = 'line'` - highlight the `range` or `line`number of available diagnostics +* `diagnostic_style_id = nil` - vis style id used to highlight diagnostics, win.STYLE_LEXER_MAX is used by default +* `diagnostic_styles = { error = 'back:red', warning = 'back:yellow', information = 'back:yellow', hint = 'back:yellow', }` - styles used to highlight different diagnostics +* `workspace_edit_remember_cursor = true` - restore the primary cursor position after a workspaceEdit +* `message_level = 3` - the level of shown messages retrieved via `window/showMessage` notifications +* `show_message = 'message'` - how to present information. `'message'`: use `vis:message`; `'open'`: use a new window supporting syntax highlighting. +* `universal_root_globs = {}` - Globs to consider as workspace root for any language server (e.g. `*.git` or `*.hg`). +* `fallback_dirname_as_root = false` - If set to true a file's directory is used as workspace root if no explicit root was found. + +#### Configure your own Language Server + +If `vis-lspc` has no language server configuration for your desired language or server +you have to create a language server configuration and insert it into the `ls_map` +table. +Please have a look at #2 and share your configuration with everyone else. + +A language server configuration is a Lua table containing at least a `name` field +which is used to manage the language server and a `cmd` field which is used to +start the language server. + +**Note:** the language server must communicate with `vis-lspc` via stdio. +Your language server probably supports stdio but maybe requires a [special +command line flag](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#implementationConsiderations). + +Additional fields are: + +* `settings` - a table of arbitrary possibly nested data. It is sent in a `workspace/didChangeConfiguration` to the language server after initialization. It is also used to lookup configuration for the `workspace/configuratio` method call. +* `init_options` - table of arbitrary possibly nested data. It will be sent to the server as `initializationOptions` in the parameters of the `initialize` method call. +* `formatting_options` - table of configuration data as found in [the LSP specification](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_formatting). `tabSize` and `insertSpaces` are required. + +**Example:** The language server configuration entry in the `ls_map` for `lua-language-server` + +```lua +ls_map.lua = { + name = 'lua-language-server', + cmd = 'lua-language-server', + settings = { + Lua = {diagnostics = {globals = {'vis'}}, telemetry = {enable = false}}, + }, + formatting_options = {tabSize = 2, insertSpaces = true}, +}, +``` + +Language servers configured in `vis-lspc` can be found in `supported_servers.lua`. + +### Workspace Detection + +During server initialization an URI to the root of the workspace (a folder opened by the editor) can be passed to the server. +Workspaces are used to implement certain project wide [features](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_symbol). + +Since vis has no sense of folders and I think it is the job of each individual language server to detect the root of language idiomatic projects, workspace root detection is only activated for certain language servers by default. + +If you want to use some universal criteria to detect project roots, like always using a file's directory or considering all source-control repositories as projects you can use the configuration options `universal_root_globs` and `fallback_dirname_as_root`. + +Additionally, you can configure globs to detect a project's root for each language server using the `roots` member in its `ls_map` table entry. + +For example `roots = {'compile_commands.json', '.clangd'}` is used to detect the project root for clangd. + +### Events + +vis-lspc extends vis' event system with its own set of events: + +* `lspc.event.LS_INITIALIZED` - emitted after sending the `initialized` notification +* `lspc.event.LS_DID_OPEN` - emitted after sending the `textDocument/didOpen` notification + +All events receive the language server as first argument. + +### Extensibility + +The returned module table also includes functions you can use in your own `vis` +configuration. + +#### `lspc.lspc_open` + +Navigate between or in files, while remembering the current position in a runtime history. + +```lua +lspc_open(win, path, line, col, cmd) +``` + + - `win` - a window in which to open the file + - `path` - the path to the file to open + - `line` - the line to open. (`nil` for no position within the file). + - `col` - same as `line`, but for the column. + - `cmd` - `vis` command to open the file. (`e` or `o`, see `vis` commands) + +## License + +All code except otherwise noted is licensed under the term of GPL-3. +See the LICENSE file for more details. +Our fallback JSON implementation in `json.lua` is NOT licensed under GPL-3. +It is taken from [here](https://gist.github.com/tylerneylon/59f4bcf316be525b30ab) +and is put into public domain by [Tyler Neylon](https://github.com/tylerneylon). diff --git a/vendors/vis-lspc/bindings.lua b/vendors/vis-lspc/bindings.lua @@ -0,0 +1,75 @@ +-- Copyright (c) 2021 Florian Fischer. All rights reserved. +-- +-- This file is part of vis-lspc. +-- +-- vis-lspc is free software: you can redistribute it and/or modify it under the +-- terms of the GNU General Public License as published by the Free Software +-- Foundation, either version 3 of the License, or (at your option) any later +-- version. +-- +-- vis-lspc is distributed in the hope that it will be useful, but WITHOUT ANY +-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +-- FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License along with +-- vis-lspc found in the LICENSE file. If not, see <https://www.gnu.org/licenses/>. +-- +-- vis-lspc default bindings +vis:map(vis.modes.NORMAL, '<F2>', function() + vis:command('lspc-start-server') +end, 'lspc: start lsp server') + +vis:map(vis.modes.NORMAL, '<F3>', function() + vis:command('lspc-open') +end, 'lspc: open current file') + +vis:map(vis.modes.NORMAL, '<C-]>', function() + vis:command('lspc-definition') +end, 'lspc: jump to definition') + +vis:map(vis.modes.NORMAL, '<C-t>', function() + vis:command('lspc-back') +end, 'lspc: go back position stack') + +vis:map(vis.modes.NORMAL, '<C- >', function() + vis:command('lspc-completion') +end, 'lspc: completion') + +vis:map(vis.modes.INSERT, '<C- >', function() + vis:command('lspc-completion') + vis.mode = vis.modes.INSERT +end, 'lspc: completion') + +-- bindings inspired by nvim +-- https://github.com/neovim/nvim-lspconfig +vis:map(vis.modes.NORMAL, 'gD', function() + vis:command('lspc-declaration') +end, 'lspc: jump to declaration') + +vis:map(vis.modes.NORMAL, 'gd', function() + vis:command('lspc-definition') +end, 'lspc: jump to definition') + +vis:map(vis.modes.NORMAL, 'gi', function() + vis:command('lspc-implementation') +end, 'lspc: jump to implementation') + +vis:map(vis.modes.NORMAL, 'gr', function() + vis:command('lspc-references') +end, 'lspc: show references') + +vis:map(vis.modes.NORMAL, ' D', function() + vis:command('lspc-typeDefinition') +end, 'lspc: jump to type definition') + +vis:map(vis.modes.NORMAL, ' e', function() + vis:command('lspc-show-diagnostics') +end, 'lspc: show diagnostic of current line') + +vis:map(vis.modes.NORMAL, 'K', function() + vis:command('lspc-hover') +end, 'lspc: hover over current position') + +vis:map(vis.modes.NORMAL, '<C-K>', function() + vis:command('lspc-signature-help') +end, 'lspc: signature help') diff --git a/vendors/vis-lspc/init.lua b/vendors/vis-lspc/init.lua @@ -0,0 +1,1836 @@ +-- Copyright (c) 2021-2024 Florian Fischer. All rights reserved. +-- +-- This file is part of vis-lspc. +-- +-- vis-lspc is free software: you can redistribute it and/or modify it under the +-- terms of the GNU General Public License as published by the Free Software +-- Foundation, either version 3 of the License, or (at your option) any later +-- version. +-- +-- vis-lspc is distributed in the hope that it will be useful, but WITHOUT ANY +-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +-- FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License along with +-- vis-lspc found in the LICENSE file. If not, see <https://www.gnu.org/licenses/>. +-- +-- We require vis compiled with the communicate patch +if not vis.communicate then + vis:info('LSPC Error: language server support requires vis communicate patch') + return {} +end + +local source_str = debug.getinfo(1, 'S').source:sub(2) +local source_path = source_str:match('(.*/)') + +-- state of our language server client +local lspc = dofile(source_path .. 'lspc.lua') + +-- initialise the logging system +lspc.logger = dofile(source_path .. 'log.lua').lazyNew('lspc', lspc) + +local parser = dofile(source_path .. 'parser.lua') + +-- initialise the util module +local util = dofile(source_path .. 'util.lua').init(lspc) + +-- load a suitable json module +lspc.json = dofile(source_path .. 'json.lua') + +local jsonrpc = {} +jsonrpc.error_codes = { + -- json rpc errors + ParseError = -32700, + InvalidRequest = -32600, + MethodNotFound = -32601, + InvalidParams = -32602, + InternalError = -32603, + + ServerNotInitialized = -32002, + UnknownErrorCode = -32001, + + -- lsp errors + ContentModified = -32801, + RequestCancelled = -32800, +} + +-- get vis's pid to pass it to the language servers +local vis_pid +do + local vis_proc_file = io.open('/proc/self/stat', 'r') + if vis_proc_file then + vis_pid = vis_proc_file:read('*n') + vis_proc_file:close() + + else -- fallback if /proc/self/stat + local out = util.capture_cmd('sh -c "echo $PPID"') + vis_pid = tonumber(out) + end +end +assert(vis_pid) + +-- mapping function between vis lexer names and LSP languageIds +local function syntax_to_languageId(syntax) + -- LuaFormatter off + local map = { + ansi_c = 'c', + javascript = 'jsx', + typescript = 'tsx', + } + -- LuaFormatter on + + return map[syntax] or syntax +end + +-- map of known language servers per syntax +lspc.ls_map = dofile(source_path .. 'supported-servers.lua') + +-- return the name of the language server for this syntax +local function get_ls_name_for_syntax(syntax) + local ls_def = lspc.ls_map[syntax] + if not ls_def then + return nil, 'No language server available for ' .. syntax + end + return ls_def.name +end + +-- Document position code +-- https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocumentPositionParams + +-- We use the following position/location/file related types in vis-lspc: +-- pos - like in vis a 0-based byte offset into the file. +-- path - posix path used by vis +-- uri - file uri used by LSP + +-- lsp_position - 0-based tuple (line, character) +-- lsp_document_position - aka. LSP TextDocumentPosition, tuple of (uri, lsp_position) + +-- vis_selection - 1-based tuple (line, cul) (character) +-- Can be used with with Selection:to +-- vis_document_position - 1-based tuple of (path, line, cul) + +-- vis_range - tuple of 0-based byte offsets (finish, start) +-- lsp_range - aka. Range, tuple of two lsp_positions (start, end) + +-- There exist helper function to convert from one type into another +-- aswell as helper to retrieve the current primary selection from a vis.window + +local function path_to_uri(path) + return 'file://' .. path +end + +-- uri decode logic taken from +-- https://stackoverflow.com/questions/20405985/lua-decodeuri-luvit +local uri_decode_table = {} +for i = 0, 255 do + uri_decode_table[string.format('%02x', i)] = string.char(i) + uri_decode_table[string.format('%02X', i)] = string.char(i) +end + +local function decode_uri(s) + return (s:gsub('%%(%x%x)', uri_decode_table)) +end + +local function uri_to_path(uri) + return decode_uri(uri:gsub('file://', '')) +end + +-- get the vis_selection from current primary selection +local function get_selection(win) + return {line = win.selection.line, col = win.selection.col} +end + +-- convert lsp_position to vis_selection +local function lsp_pos_to_vis_sel(pos) + return {line = pos.line + 1, col = pos.character + 1} +end + +-- convert vis_selection to lsp_position +local function vis_sel_to_lsp_pos(pos) + return {line = pos.line - 1, character = pos.col - 1} +end + +-- convert our vis_document_position to lsp_document_position aka. TextDocumentPosition +local function vis_doc_pos_to_lsp(doc_pos) + return { + textDocument = {uri = path_to_uri(doc_pos.file)}, + position = vis_sel_to_lsp_pos({line = doc_pos.line, col = doc_pos.col}), + } +end + +-- convert lsp_document_position to vis_document_position +local function lsp_doc_pos_to_vis(doc_pos) + local pos = lsp_pos_to_vis_sel(doc_pos.position) + return { + file = uri_to_path(doc_pos.textDocument.uri), + line = pos.line, + col = pos.col, + } +end + +-- get document position of the main curser +local function vis_get_doc_pos(win) + return { + file = win.file.path, + line = win.selection.line, + col = win.selection.col, + } +end + +--- Convert a lsp_range to a vis_range +-- @param file the file in which the range lies +-- @param lsp_range the LSP range that should be converted +-- @return the according vis range +local function lsp_range_to_vis_range(file, lsp_range) + local start = lsp_pos_to_vis_sel(lsp_range.start) + local finish = lsp_pos_to_vis_sel(lsp_range['end']) + + local positions = util.vis_sorted_selections_to_pos(file, {start, finish}) + local start_pos = positions[1] + local finish_pos = positions[2] + + return {start = start_pos, finish = finish_pos} +end + +--- Check if a lsp_position lies before another. +-- @param p1 first lsp_position to compare +-- @param p2 second lsp_position to compare +-- @return true if p1 lies before p2 +local function lsp_pos_before(p1, p2) + return p1.line < p2.line or (p1.line == p2.line and p1.character < p2.character) +end + +--- Check if a lsp_range starts before another. +-- The ranges may overlap since only their start positions are compared. +-- @param r1 first lsp_range to compare +-- @param r2 second lsp_range to compare +-- @return true if r1 is starts before r2 +local function lsp_range_starts_before(r1, r2) + return lsp_pos_before(r1.start, r2.start) +end + +-- concatenate all numeric values in choices and pass it on stdin to lspc.menu_cmd +local function lspc_select(choices) + local menu_input = '' + local i = 0 + for _, c in ipairs(choices) do + i = i + 1 + menu_input = menu_input .. c .. '\n' + end + + -- select the only possible choice + if i < 2 then + return choices[1] + end + + local fullscreen = lspc.menu_cmd == 'fzf' + local status, output = util.vis_pipe(menu_input, lspc.menu_cmd, fullscreen) + + local choice = nil + if status == 0 then + -- trim newline from selection + if output:sub(-1) == '\n' then + choice = output:sub(1, -2) + else + choice = output + end + end + + vis:redraw() + return choice +end + +local function lspc_select_location(locations) + -- Collect all paths with a list of their locations so we + -- can sort the locations before calling file_line_iterator_to_n + local collected = {} + + for _, location in ipairs(locations) do + local path = uri_to_path(location.uri or location.targetUri) + local range = location.range or location.targetSelectionRange + local position = lsp_pos_to_vis_sel(range.start) + + if collected[path] == nil then + table.insert(collected, path) + collected[path] = {} + end + + table.insert(collected[path], { + ['location'] = location, + ['position'] = position, + }) + end + + local choices = {} + local cwd_components = util.capture_cmd('pwd') + -- Strip trailing newline + cwd_components = util.split_path_into_components(cwd_components:sub(1, #cwd_components - 1)) + + for _, path in ipairs(collected) do + -- Sort positions + table.sort(collected[path], function(a, b) + return a['position'].line < b['position'].line + end) + + local rel_path = util.get_relative_path(cwd_components, path) + -- Use the already open file if present to get accurate line content for references + local line_iter + if lspc.open_files[path] ~= nil then + line_iter = function(n) + if n == -1 then + return nil + end + + return lspc.open_files[path].file.lines[n] + end + else + line_iter = util.file_line_iterator_to_n(path) + end + + for _, val in ipairs(collected[path]) do + local position = val['position'] + local location = val['location'] + + local choice = rel_path .. ':' .. position.line .. ':' .. position.col .. ':' .. + line_iter(position.line) + table.insert(choices, choice) + choices[choice] = location + end + + -- close the iterator + line_iter(-1) + end + + -- select a location + local choice = lspc_select(choices) + if not choice then + return nil + end + + return choices[choice] +end + +-- get a user confirmation +-- return true if user selected yes, false otherwise +local function lspc_confirm(prompt) + local choices = 'no\nyes' + + local cmd = lspc.confirm_cmd + + if prompt then + cmd = cmd .. ' -p \'' .. prompt .. '\'' + end + + lspc:log('get confirmation using: ' .. cmd) + + local choice = nil + local status, output = util.vis_pipe(choices, cmd) + if status == 0 then + -- trim newline from selection + if output:sub(-1) == '\n' then + choice = output:sub(1, -2) + else + choice = output + end + end + + vis:redraw() + return choice == 'yes' +end + +local function vis_open_file(file, cmd) + vis:command(('%s %s'):format(cmd, file:gsub('[\\\t "\']', '\\%1'):gsub('\n', '\\n'))) +end + +-- open a doc_pos using the vis command <cmd> +local function vis_open_doc_pos(doc_pos, cmd, win) + if win and win ~= vis.win then + vis.win = win + end + assert(cmd) + if vis.win.file.path ~= doc_pos.file then + if vis.win.file.modified and cmd == 'e' then + if lspc_confirm('Save currently open file:') then + vis:command('w') + else + vis:info('Not opening new file, current file has unsaved changes') + return + end + end + vis_open_file(doc_pos.file, cmd) + if doc_pos.line then + vis.win.selection:to(doc_pos.line, doc_pos.col or 0) + end + vis:command('lspc-open') + else + vis.win.selection:to(doc_pos.line, doc_pos.col) + end +end + +-- Support jumping between document positions +-- Stack of edited document positions +local doc_pos_history = {} + +local function vis_push_doc_pos(win) + local old_doc_pos = vis_get_doc_pos(win) + table.insert(doc_pos_history, old_doc_pos) +end + +-- open a new doc_pos remembering the old if it is replaced +local function vis_open_new_doc_pos(doc_pos, cmd, win) + win = win or vis.win + if cmd == 'e' then + vis_push_doc_pos(win) + end + + vis_open_doc_pos(doc_pos, cmd, win) +end + +lspc.open_file = function(win, path, line, col, cmd) + vis_open_new_doc_pos({file = path, line = line, col = col}, cmd or 'e', win) +end + +local function vis_pop_doc_pos(win) + local last_doc_pos = table.remove(doc_pos_history) + if not last_doc_pos then + return 'Document history is empty' + end + + vis_open_doc_pos(last_doc_pos, 'e', win) +end + +-- apply a textEdit received from the language server +local function vis_apply_textEdit(win, file, textEdit) + assert(win.file == file) + + local range = lsp_range_to_vis_range(file, textEdit.range) + + file:delete(range) + file:insert(range.start, textEdit.newText) + + win.selection.anchored = false + win.selection.pos = range.start + string.len(textEdit.newText) + + win:draw() +end + +-- apply a list of textEdits received from the language server +local function vis_apply_textEdits(win, file, textEdits) + assert(win.file == file) + + local edits = {} + for _, textEdit in ipairs(textEdits) do + local range = lsp_range_to_vis_range(file, textEdit.range) + table.insert(edits, { + mark = file:mark_set(range.start), + len = range.finish - range.start, + newText = textEdit.newText, + }) + end + for _, edit in ipairs(edits) do + local pos = file:mark_get(edit.mark) + file:delete(pos, edit.len) + file:insert(pos, edit.newText) + end + win:draw() +end + +--- Close the message window +-- TODO: close the dedicated lspc message window +local function lspc_close_message_win() + if lspc.show_message == 'message' then + vis:message('') + vis:command('q') + end +end + +--- Present a message to the user +-- TODO: use a dedicated lspc message window +local function lspc_show_message(msg, hdr, syntax) + local current_win = vis.win + + if lspc.show_message == 'message' then + local to_show = (hdr or '') .. msg .. '\n' + vis:message(to_show) + vis.win.selection = vis.win.file.size - #to_show + vis:feedkeys('zt') + + elseif lspc.show_message == 'open' then + vis:command('open') + if syntax then + vis:command('set syntax ' .. syntax) + end + + vis.win.file:insert(0, msg) + vis.win.selection.pos = 0 + else + lspc:err('invalid message configuration "' .. lspc.show_message .. '".') + end + + -- reset the focus to the current window + vis.win = current_win +end + +-- apply a WorkspaceEdit received from the language server +local function vis_apply_workspaceEdit(_, _, workspaceEdit) + local file_edits = workspaceEdit.changes + assert(file_edits or workspaceEdit.documentChanges) + + -- try to convert NOT SUPPORTED TextDocumentEdit[] + -- We do not announce support for versioned DocumentChanges in our + -- client capabilities, but some LSP servers ignore our capabilities + -- sending them anyway. + if not file_edits then + file_edits = {} + for _, edit in ipairs(workspaceEdit.documentChanges) do + file_edits[edit.textDocument.uri] = edit.edits + end + end + + -- generate change summary + local summary = '--- workspace edit summary ---\n' + for uri, edits in pairs(file_edits) do + local path = uri_to_path(uri) + summary = summary .. path .. ':\n' + for i, edit in ipairs(edits) do + summary = summary .. '\t' .. i .. '.: ' .. lspc.json.encode(edit) .. '\n' + end + end + + lspc_show_message(summary) + vis:redraw() + + -- get user confirmation + local confirmation = lspc_confirm('apply changes:') + + -- close summary window + lspc_close_message_win() + + if not confirmation then + return + end + + -- apply changes to open files + for uri, edits in pairs(file_edits) do + local path = uri_to_path(uri) + + -- search all open windows for this uri + local win_with_file + for win in vis:windows() do + if win.file and win.file.path == path then + win_with_file = win + break + end + end + + -- The file is not currently opened -> open it + local opened + if not win_with_file then + vis_open_file(path, 'o') + win_with_file = vis.win + opened = true + end + + -- Remember the current primary cursor position + local old_pos = win_with_file.selection.pos + + for _, edit in ipairs(edits) do + vis_apply_textEdit(win_with_file, win_with_file.file, edit) + end + + -- Restore the remembered primary cursor position + if lspc.workspace_edit_remember_cursor then + win_with_file.selection.pos = old_pos + end + + -- save changes and close the opened window + if opened then + vis:command('wq') + end + end +end + +-- translate file line number to the relative row the line is displayed in the view of a window +-- returns an integer relative to the window if line is in view (starting at 1) +-- returns nil otherwise +local function file_lineno_to_viewport_lineno(win, file_lineno) + -- The line is not in the current viewport + if file_lineno < win.viewport.lines.start or file_lineno > win.viewport.lines.finish then + return nil + end + + -- The line is in the viewport and there is no wrapped line + if win.viewport.lines.finish - win.viewport.lines.start == win.viewport.height then + return file_lineno - win.viewport.lines.start + else -- Determine the position in the viewport considering possible prior wrapped lines + local view_lineno = 0 + for n = win.viewport.lines.start, file_lineno do + view_lineno = view_lineno + 1 + -- Wrapped lines shift our displayed line down + local line_len = #win.file.lines[n] + if not win.options.expandtab then + line_len = util.visual_chars_in_line(win, win.file.lines[n], line_len) + end + + if line_len >= win.viewport.width then + view_lineno = view_lineno + math.floor(line_len / win.viewport.width) + end + end + return view_lineno + end +end + +local function lspc_highlight_server_diagnostics(win, server_diagnostics, style) + if not style then + style = lspc.diagnostic_style_id or win.STYLE_LEXER_MAX + end + + local level_mapping = { + [1] = lspc.diagnostic_styles.error, + [2] = lspc.diagnostic_styles.warning, + [3] = lspc.diagnostic_styles.information, + [4] = lspc.diagnostic_styles.hint, + } + + for _, diagnostic in ipairs(server_diagnostics) do + local diagnostic_style = level_mapping[diagnostic.severity] or level_mapping[1] + assert(win:style_define(style, diagnostic_style)) + + if lspc.highlight_diagnostics == 'range' then + local range = diagnostic.vis_range + + -- LSP ranges use an exclusive finish + local finish = range.finish - 1 + + -- make sure to highlight only ranges which actually contain the diagnostic + if diagnostic.content == win.file:content(range) then + win:style(style, range.start, finish) + end + + elseif lspc.highlight_diagnostics == 'line' then + if not win.style_pos then + lspc:err('Vis build does not support style_pos') + return + end + + local start_line = diagnostic.range.start.line + local end_line = diagnostic.range['end'].line + for line = start_line, end_line, 1 do + local row = file_lineno_to_viewport_lineno(win, line) + if row then + -- Heuristic how many cells need to be styled + -- (at least one plus the decimal places of the line number). + for i = 0, #('' .. line) do + win:style_pos(style, i, row) + end + end + end + end + end +end + +local function lspc_highlight_diagnostics(win, diagnostics, style) + for _, server_diagnostics in pairs(diagnostics) do + lspc_highlight_server_diagnostics(win, server_diagnostics, style) + end +end + +--- LanguageServer class metatable +local LanguageServer = {} + +--- send a RPC message to the language server +-- @param req The request to send +function LanguageServer:rpc(req) + req.jsonrpc = '2.0' + + local content_part = lspc.json.encode(req) + local content_len = string.len(content_part) + + local header_part = 'Content-Length: ' .. tostring(content_len) + local msg = header_part .. '\r\n\r\n' .. content_part + lspc:log('LSPC Sending -> ' .. self.name .. ': ' .. msg) + + self.fd:write(msg) + self.fd:flush() +end + +--- Send a RPC notification to the language server +-- @param name the name of the notification +-- @param params the parameters to send +function LanguageServer:send_notification(name, params) + self:rpc({method = name, params = params}) +end + +--- Send a textDocument/didChange notification to the language server +-- @param the vis file object which changed +function LanguageServer:send_did_change(file) + lspc:log('send didChange') + local new_version = assert(lspc.open_files[file.path]).version + 1 + lspc.open_files[file.path].version = new_version + + local document = {uri = path_to_uri(file.path), version = new_version} + local changes = {{text = file:content(0, file.size)}} + local params = {textDocument = document, contentChanges = changes} + self:send_notification('textDocument/didChange', params) +end + +--- Send a rpc method call to the language server. +-- @param method name of remote procedure to call +-- @param params the parameter passed to the remote procedure +-- @param win the related vis window object +-- @param ctx a opaque context value stored with the request +function LanguageServer:call_method(method, params, win, ctx) + local id = self.id + self.id = self.id + 1 + + local req = {id = id, method = method, params = params} + self.inflight[id] = req + + self:rpc(req) + -- remember the current window to apply the effects of a + -- method call in the original window + self.inflight[id].win = win + + -- remember the user provided ctx value + -- ctx can be used to remember arbitrary data from method invocation till + -- method response handling + -- The goto-location methods remember in ctx how to open the location + self.inflight[id].ctx = ctx +end + +--- Call textDocument/<method> of the server +-- We send a didChange notification upfront to make sure the server sees our +-- current state. This is not ideal since we are sending more data than needed +-- and the server has less time to parse the new file content and do its work +-- resulting in longer stalls after method invocation. +-- @param method the name of LSP textDocument method +-- @param params the parameters of the method call +-- @param win the related vis window object +-- @param ctx a opaque context value stored with the request +function LanguageServer:call_text_document_method(method, params, win, ctx) + self:send_did_change(win.file) + self:call_method('textDocument/' .. method, params, win, ctx) +end + +local function lspc_handle_goto_method_response(req, result) + if not result or type(result) ~= 'table' or next(result) == nil then + lspc:warn(req.method .. ' found no results') + return + end + + local location + -- result actually a list of results + if type(result) == 'table' then + location = lspc_select_location(result) + if not location then + return + end + else + location = result + end + assert(location) + + -- location is a Location + local lsp_doc_pos + if location.uri then + lspc:log('Handle location: ' .. lspc.json.encode(location)) + lsp_doc_pos = { + textDocument = {uri = location.uri}, + position = { + line = location.range.start.line, + character = location.range.start.character, + }, + } + -- location is a LocationLink + elseif location.targetUri then + lspc:log('Handle locationLink: ' .. lspc.json.encode(location)) + lsp_doc_pos = { + textDocument = {uri = location.targetUri}, + position = { + line = location.targetSelectionRange.start.line, + character = location.targetSelectionRange.start.character, + }, + } + else + lspc:warn('Unknown location type: ' .. lspc.json.encode(location)) + end + + local doc_pos = lsp_doc_pos_to_vis(lsp_doc_pos) + vis_open_new_doc_pos(doc_pos, req.ctx, req.win) +end + +local function lspc_handle_completion_method_response(win, result, old_pos) + if not result or type(result) ~= 'table' or not result.items then + lspc:warn('no completion available') + return + end + + local completions = result + if result.isIncomplete ~= nil then + completions = result.items + end + + local choices = {} + for _, completion in ipairs(completions) do + table.insert(choices, completion.label) + choices[completion.label] = completion + end + + -- select a completion + local choice = lspc_select(choices) + if not choice then + return + end + + local completion = choices[choice] + + if completion.textEdit then + vis_apply_textEdit(win, win.file, completion.textEdit) + return + end + + if completion.insertText or completion.label then + -- Does our current state correspont to the state when the completion method + -- was called. + -- Otherwise we don't have a good way to apply the 'insertText' completion + if win.selection.pos ~= old_pos then + lspc:warn('can not apply textInsert because the cursor position changed') + end + + local new_word = completion.insertText or completion.label + local old_word_range = win.file:text_object_word(old_pos) + local old_word = win.file:content(old_word_range) + + lspc:log(string.format('Completion old_pos=%d, old_range={start=%d, finish=%d}, old_word=%s', + old_pos, old_word_range.start, old_word_range.finish, + old_word:gsub('\n', '\\n'))) + + -- update old_word_range and old_word and return if old_word is a prefix of the completion + local function does_completion_apply_to_pos(pos) + old_word_range = win.file:text_object_word(pos) + old_word = win.file:content(old_word_range) + + local is_prefix = new_word:sub(1, string.len(old_word)) == old_word + return is_prefix + end + + -- search for a possible completion token which we should replace with this insertText + local matches = does_completion_apply_to_pos(old_pos) + if not matches then + lspc:log('Cursor looks like its not on the completion token') + + -- try the common case the cursor is behind its completion token: fooba┃ + local next_pos_candidate = old_pos - 1 + matches = does_completion_apply_to_pos(next_pos_candidate) + if matches then + old_pos = next_pos_candidate + end + end + + local completion_start + -- we found a completion token -> replace it + if matches then + lspc:log('replace the token: ' .. old_word .. ' we found being a prefix of the completion') + win.file:delete(old_word_range) + completion_start = old_word_range.start + else + completion_start = old_pos + end + -- apply insertText + win.file:insert(completion_start, new_word) + win.selection.pos = completion_start + string.len(new_word) + win:draw() + return + end + + -- neither insertText nor textEdit where present + lspc:err('Unsupported completion') +end + +local function lspc_handle_hover_method_response(win, result, old_pos) + if not result or type(result) ~= 'table' or not result.contents then + lspc:warn('no hover available') + return + end + + local sel = util.vis_pos_to_sel(win, old_pos) + + local hover_header = + '--- hover: ' .. (win.file.path or '') .. ': ' .. sel.line .. ', ' .. sel.col .. ' ---\n' + local hover_msg = '' + -- The most common markup kind in LSP is markdown + local markup_kind = 'markdown' + + -- result is MarkedString[] + if type(result.contents) == 'table' and #result.contents > 0 then + lspc:log('hover returned list of length ' .. #result.contents) + + for i, marked_string in ipairs(result.contents) do + if i == 1 then + hover_msg = marked_string.value or marked_string + else + hover_msg = hover_msg .. '\n---\n' .. (marked_string.value or marked_string) + end + end + else -- result is either MarkedString or MarkupContent + hover_msg = result.contents.value or result.contents + if result.contents.kind and result.contents.kind == 'plaintext' then + markup_kind = 'text' + end + end + lspc_show_message(hover_msg, hover_header, markup_kind) +end + +local function lspc_handle_signature_help_method_response(win, result, call_pos) + if not result or type(result) ~= 'table' or not result.signatures or #result.signatures == 0 then + lspc:warn('no signature help available') + return + end + + local signatures = result.signatures + + local sel = util.vis_pos_to_sel(win, call_pos) + local help_header = '--- signature help: ' .. (win.file.path or '') .. ': ' .. sel.line .. ', ' .. + sel.col .. ' ---\n' + + -- local help_msg = lspc.json.encode(result) + local help_msg = '' + for _, signature in ipairs(signatures) do + local sig_msg = signature.label + if signature.documentation then + local doc = signature.documentation.value or signature.documentation + sig_msg = sig_msg .. '\n\tdocumentation: ' .. doc + end + help_msg = help_msg .. '\n' .. sig_msg + end + -- strip first new line from the message + help_msg = help_msg:sub(2) + lspc_show_message(help_msg, help_header) +end + +local function lspc_handle_rename_method_response(win, result) + -- result must always be valid because otherwise we would caught the error + -- in LanguageServer:handle_method_response + vis_apply_workspaceEdit(win, win.file, result) +end + +local function lspc_handle_formatting_method_response(win, result) + -- The result of textDocument/formatting is defined as TextEdit[] | null + if result then + vis_apply_textEdits(win, win.file, result) + end +end + +local function lspc_handle_initialize_response(ls, result) + ls.initialized = true + ls.capabilities = result.capabilities + + local params = {} + setmetatable(params, {__jsontype = 'object'}) + ls:send_notification('initialized', params) + + -- According to nvim-lspconfig sendig the lsp server settings shortly after + -- initialization is an undocumented convention. + -- See https://github.com/neovim/nvim-lspconfig/blob/ed88435764d8b00442e66d39ec3d9c360e560783/CONTRIBUTING.md + if ls.settings then + ls:send_notification('workspace/didChangeConfiguration', { + settings = ls.settings, + }) + end + + vis.events.emit(lspc.events.LS_INITIALIZED, ls) +end + +--- Dispatch method response from the server +-- @param method_response the response send from the server +-- @param req the request causing this response +function LanguageServer:handle_method_response(method_response, req) + local win = req.win + + local method = req.method + + local err = method_response.error + if err then + local err_msg = err.message + local err_code = err.code + lspc:err(err_msg .. ' (' .. err_code .. ') occurred during ' .. method) + -- Don't try to handle error responses any further + return + end + + local result = method_response.result + + -- LuaFormatter off + if method == 'textDocument/definition' or + method == 'textDocument/declaration' or + method == 'textDocument/typeDefinition' or + method == 'textDocument/implementation' or + method == 'textDocument/references' then + -- LuaFormatter on + lspc_handle_goto_method_response(req, result) + + elseif method == 'initialize' then + lspc_handle_initialize_response(self, result) + + elseif method == 'textDocument/completion' then + lspc_handle_completion_method_response(win, result, req.ctx) + + elseif method == 'textDocument/hover' then + lspc_handle_hover_method_response(win, result, req.ctx) + + elseif method == 'textDocument/signatureHelp' then + lspc_handle_signature_help_method_response(win, result, req.ctx) + + elseif method == 'textDocument/rename' then + lspc_handle_rename_method_response(win, result) + + elseif method == 'textDocument/formatting' then + lspc_handle_formatting_method_response(win, result) + + elseif method == 'shutdown' then + self:send_notification('exit') + self.fd:close() + + -- remove the ls from lspc.running + for ls_name, rls in pairs(lspc.running) do + if self == rls then + lspc.running[ls_name] = nil + break + end + end + else + lspc:warn('received unknown method ' .. method) + end + + self.inflight[method_response.id] = nil +end + +local function lspc_handle_workspace_configuration_call(ls, params, response) + local results = {} + for _, item in ipairs(params.items) do + local t = ls.settings + for k in item.section:gmatch('[^.]+') do + if not t then + break + end + t = t[k] + end + table.insert(results, t or lspc.json.null) + end + response.result = results +end + +--- Handle a method call from the server +-- @param method_call the received method call +function LanguageServer:handle_method_call(method_call) + local method = method_call.method + local response = {id = method_call.id} + if method == 'workspace/configuration' then + lspc_handle_workspace_configuration_call(self, method_call.params, response) + else + lspc:log('Unknown method call ' .. method) + response['error'] = { + code = jsonrpc.error_codes.MethodNotFound, + message = method .. ' not implemented', + } + end + self:rpc(response) +end + +-- save the diagnostics received for a file uri +local function lspc_handle_publish_diagnostics(ls, uri, diagnostics) + local file_path = uri_to_path(uri) + local file = lspc.open_files[file_path] + if file then + for _, diagnostic in ipairs(diagnostics) do + -- We convert the lsp_range to a vis_range here to do it only once. + -- It's an expensive operation that involves counting all newlines. + diagnostic.vis_range = lsp_range_to_vis_range(file.file, diagnostic.range) + + -- In some instances the range defined by the diagnostic starts + -- and ends at the same position. Highlight the exact position. + if diagnostic.vis_range.finish == diagnostic.vis_range.start then + -- We fake a one char range to retrieve its content. + -- In highlight_diagnostics we inconditionally decrement finish anyway. + diagnostic.vis_range.finish = diagnostic.vis_range.finish + 1 + end + + -- Remember the content of the diagnostic to only highlight it if the content + -- did not change + diagnostic.content = vis.win.file:content(diagnostic.vis_range) + end + + file.diagnostics[ls] = diagnostics + + lspc:log('remembered ' .. #diagnostics .. ' diagnostics for ' .. file_path) + else + lspc:log('Diagnostics for not opened file' .. file_path) + end +end + +local lsp_message_types = {'Error', 'Warning', 'Info', 'Log'} +-- show a message from the server in the UI +local function lspc_handle_show_message(show_message_params) + if show_message_params.type > lspc.message_level then + return + end + + vis:message('--- language server message ---') + local level = lsp_message_types[show_message_params.type] or 'Unknown' + vis:message(level .. ': ' .. show_message_params.message) +end + +--- Handle a notification received from the server +-- @param notification the received notification +function LanguageServer:handle_notification(notification) + local method = notification.method + if method == 'textDocument/publishDiagnostics' then + lspc_handle_publish_diagnostics(self, notification.params.uri, notification.params.diagnostics) + elseif method == 'window/showMessage' then + lspc_handle_show_message(notification.params) + end +end + +--- Dispatch between a method call and a message response +-- Those are distinquiable because for a message response we have a req +-- remembered in the inflight table +-- @param method the method message received from the server +function LanguageServer:handle_method(method) + local req = self.inflight[method.id] + if req and not method.method then + self:handle_method_response(method, req) + else + self:handle_method_call(method) + end +end + +--- Dispatch between a method call/response and a notification from the server +-- @param msg the message received from the server +function LanguageServer:handle_msg(msg) + if msg.id then + self:handle_method(msg) + else + self:handle_notification(msg) + end +end + +-- Parse the data send by the language server +-- Note the chunks received may not end with the end of a message. +-- In the worst case a data chunk contains two partial messages on at the beginning +-- and one at the end +function LanguageServer:recv_data(data) + local err = self.parser:add(data) + if err then + lspc:err(err) + return + end + + local msgs = self.parser:get_msgs() + if not msgs then + return + end + + for _, msg in ipairs(msgs) do + local resp = lspc.json.decode(msg) + self:handle_msg(resp) + end +end + +-- check if a language server is running and initialized +local function lspc_get_usable_ls(win, explicit_syntax) + local ls + local syntax = explicit_syntax or (win and win.syntax) + -- try to use the first language server managing the current file + if not syntax then + if win and win.file and lspc.open_files[win.file.path] and + next(lspc.open_files[win.file.path].language_servers) then + ls = next(lspc.open_files[win.file.path].language_servers) + + else -- there is no language server with this file open and we have no syntax to guess + return nil, 'No syntax provided and no server is running' + end + + else -- Use the syntax to guess the language server + local ls_name, err = get_ls_name_for_syntax(syntax) + if err then + return nil, err + end + + ls = lspc.running[ls_name] + if not ls then + return nil, 'No language server running for ' .. syntax + end + end + + if not ls.initialized then + return nil, 'Language server ' .. ls.name .. ' not initialized yet. Please try again' + end + + return ls +end + +local function lspc_new_file_handle(file) + return {file = file, version = 0, diagnostics = {}, language_servers = {}} +end + +--- Detect if a file is already opened by the language server +-- @param file the vis file object to check +-- @return true if the file is already opened by the language server +function LanguageServer:is_file_opened(file) + return lspc.open_files[file.path] and lspc.open_files[file.path].language_servers[self] +end + +-- close the file if associated with the language server +local function lspc_close(ls, file) + if not ls:is_file_opened(file) then + return (file.path or '[No Name]') .. ' not open in ' .. ls.name + end + ls:send_notification('textDocument/didClose', { + textDocument = {uri = path_to_uri(file.path)}, + }) + lspc.open_files[file.path].language_servers[ls] = nil + if not next(lspc.open_files[file.path].language_servers) then + lspc.open_files[file.path] = nil + end +end + +-- register a file as open with a language server and setup close and save event handlers +-- A file must be opened before any textDocument functions can be used with it. +local function lspc_open(ls, win, file) + -- already opened + if ls:is_file_opened(file) then + return file.path .. ' already open in ' .. ls.name + end + + local lspc_file_handle = lspc.open_files[file.path] or lspc_new_file_handle(file) + lspc_file_handle.language_servers[ls] = true + lspc.open_files[file.path] = lspc_file_handle + + local params = { + textDocument = { + uri = 'file://' .. file.path, + languageId = syntax_to_languageId(win.syntax), + version = 0, + text = file:content(0, file.size), + }, + } + + ls:send_notification('textDocument/didOpen', params) + + vis.events.emit(lspc.events.LS_DID_OPEN, ls, file) +end + +--- Initiate the shutdown of the language server +-- Sending the exit notification and closing the file handle are done in +-- the shutdown response handler. +function LanguageServer:shutdown() + self:call_method('shutdown') +end + +--- Find the root project URI for a specific file +-- @param ls the language server +-- @param file_path the path to the file to find the root project of +-- @return the URI of the root project or nil if none was found +local function find_root_uri(ls, file_path) + local globs = '' + + local roots = ls.roots + if roots then + for _, glob in ipairs(roots) do + globs = globs .. glob .. '\n' + end + end + + if lspc.universal_root_globs then + for _, glob in ipairs(lspc.universal_root_globs) do + globs = globs .. glob .. '\n' + end + end + + local root_path = util.find_upwards(globs, file_path) + if not root_path and lspc.fallback_dirname_as_root then + root_path = util.dirname(file_path) + end + return root_path and path_to_uri(root_path) +end + +local function ls_start(ls, init_options) + ls.fd = vis:communicate(ls.name, 'exec ' .. ls.cmd) + + -- register the response handler + vis.events.subscribe(vis.events.PROCESS_RESPONSE, function(name, event, code, msg) + if name ~= ls.name then + return + end + + if event == 'EXIT' or event == 'SIGNAL' then + if event == 'EXIT' then + vis:info('language server exited with: ' .. code) + else + vis:info('language server received signal: ' .. code) + end + + lspc.running[ls.name] = nil + return + end + + lspc:log(ls.name .. ' response(' .. event .. '): ' .. msg) + if event == 'STDERR' then + return + end + + ls:recv_data(msg) + end) + + local params = { + processId = vis_pid, + clientInfo = {name = lspc.name, version = lspc.version}, + rootUri = (vis.win.file and find_root_uri(ls, vis.win.file.path)) or lspc.json.null, + capabilities = lspc.client_capabilites, + } + + if init_options then + params.initializationOptions = init_options + end + + ls:call_method('initialize', params) +end + +function LanguageServer.new(ls_conf) + local ls = { + name = ls_conf.name, + cmd = ls_conf.cmd, + settings = ls_conf.settings, + roots = ls_conf.roots, + formatting_options = ls_conf.formatting_options, + initialized = false, + id = 0, + inflight = {}, + parser = parser.new(), + capabilities = {}, + } + setmetatable(ls, {__index = LanguageServer}) + + return ls +end + +local function lspc_start_server(syntax) + local ls_conf = lspc.ls_map[syntax] + if not ls_conf then + return nil, 'No language server available for ' .. syntax + end + + local exe = ls_conf.cmd:gmatch('%S+')() + if not os.execute('type ' .. exe .. '>/dev/null 2>/dev/null') then + -- remove the configured language server + lspc.ls_map[syntax] = nil + local msg = string.format('Language server for %s configured but %s not found', syntax, exe) + -- the warning will be visual if the language server was automatically startet + -- if the user tried to start teh server manually they will see msg as error + lspc:warn(msg) + return nil, msg + end + + if lspc.running[ls_conf.name] then + return nil, 'Already a language server running for ' .. syntax + end + + local ls = LanguageServer.new(ls_conf) + lspc.running[ls_conf.name] = ls + ls_start(ls, ls_conf.init_options) + + return ls +end + +-- generic stub implementation for all textDocument methods taking +-- a textDocumentPositionParams parameter +local function lspc_method_doc_pos(ls, method, win, argv, additional_params) + -- check if the language server has a provider for this method + if not ls.capabilities[method .. 'Provider'] then + return 'language server ' .. ls.name .. ' does not provide ' .. method + end + + if not ls:is_file_opened(win.file) then + lspc_open(ls, win, win.file) + end + + local params = vis_doc_pos_to_lsp(vis_get_doc_pos(win)) + if additional_params then + for k, v in pairs(additional_params) do + params[k] = v + end + end + + ls:call_text_document_method(method, params, win, argv) +end + +local lspc_goto_location_methods = { + declaration = function(ls, win, open_cmd) + return lspc_method_doc_pos(ls, 'declaration', win, open_cmd) + end, + definition = function(ls, win, open_cmd) + return lspc_method_doc_pos(ls, 'definition', win, open_cmd) + end, + typeDefinition = function(ls, win, open_cmd) + return lspc_method_doc_pos(ls, 'typeDefinition', win, open_cmd) + end, + implementation = function(ls, win, open_cmd) + return lspc_method_doc_pos(ls, 'implementation', win, open_cmd) + end, + references = function(ls, win, open_cmd) + return lspc_method_doc_pos(ls, 'references', win, open_cmd, + {context = {includeDeclaration = false}}) + end, +} + +local function has_diagnostics(file) + if not file or not file.diagnostics then + return false + end + + -- detect if at least one server has published diagnostics + for _, d in pairs(file.diagnostics) do + if #d then + return true + end + end + + return false +end + +local function lspc_goto_next_diagnostic(win, reverse) + if not lspc.open_files[win.file.path] then + vis:command('lspc-open') + end + + local open_file = lspc.open_files[win.file.path] + + if not has_diagnostics(open_file) then + return (win.file.path or 'window') .. ' has no available diagnostics' + end + + -- merge diagnostics + -- TODO: come up with more efficient algorithm + local diagnostics = {} + for _, server_diagnostics in pairs(open_file.diagnostics) do + for _, diagnostic in ipairs(server_diagnostics) do + table.insert(diagnostics, diagnostic) + end + end + -- sort the merged diagnostics + table.sort(diagnostics, function(d1, d2) + return lsp_range_starts_before(d1.range, d2.range) + end) + + local sel = get_selection(win) + + local previous_diagnostic + for _, diagnostic in ipairs(diagnostics) do + local start = lsp_pos_to_vis_sel(diagnostic.range.start) + local fin = lsp_pos_to_vis_sel(diagnostic.range['end']) + + -- reverse + if reverse and + (start.line > sel.line or + (start.line == sel.line and (start.col >= sel.col or sel.col <= fin.col))) then + + -- wrap around + if not previous_diagnostic then + previous_diagnostic = lsp_pos_to_vis_sel(diagnostics[#diagnostics].range.start) + end + + win.selection:to(previous_diagnostic.line, previous_diagnostic.col) + return + end + + -- forward + if start.line > sel.line or (start.line == sel.line and start.col > sel.col) then + win.selection:to(start.line, start.col) + return + end + + previous_diagnostic = start + end + + -- wrap around + if #diagnostics > 0 then + local first = lsp_pos_to_vis_sel(diagnostics[1].range.start) + win.selection:to(first.line, first.col) + end +end + +local function lspc_show_diagnostic(win, line) + if not lspc.open_files[win.file.path] then + vis:command('lspc-open') + end + local file = lspc.open_files[win.file.path] + + if not has_diagnostics(file) then + return win.file.path .. ' has no diagnostics available' + end + local diagnostics = file.diagnostics + + line = line or get_selection(win).line + lspc:log('Show diagnostics for ' .. line) + local diagnostics_to_show = {} + for ls, server_diagnostics in pairs(diagnostics) do + for _, diagnostic in ipairs(server_diagnostics) do + local start = lsp_pos_to_vis_sel(diagnostic.range.start) + if start.line == line then + diagnostic.start = start + diagnostic.server = ls.name + table.insert(diagnostics_to_show, diagnostic) + end + end + end + + local diagnostics_fmt = '%s: %d:%d %s:%s\n' + local diagnostics_msg = '' + for _, diagnostic in ipairs(diagnostics_to_show) do + diagnostics_msg = diagnostics_msg .. + string.format(diagnostics_fmt, diagnostic.server, diagnostic.start.line, + diagnostic.start.col, diagnostic.code or 'diagnostic', + diagnostic.message) + end + + if diagnostics_msg ~= '' then + lspc_show_message(diagnostics_msg) + else + lspc:warn('No diagnostics available for line: ' .. line) + end +end + +-- vis-lspc commands + +vis:command_register('lspc-back', function() + local err = vis_pop_doc_pos() + if err then + lspc:err(err) + end +end) + +for name, func in pairs(lspc_goto_location_methods) do + vis:command_register('lspc-' .. name, function(argv, _, win) + local ls, err = lspc_get_usable_ls(win, argv[1]) + if err then + lspc:err(err) + return + end + assert(ls) + + -- vis cmd how to open the new location + -- 'e' (default): in same window + -- 'vsplit': in a vertical split window + -- 'hsplit': in a horizontal split window + local open_cmd = argv[1] or 'e' + err = func(ls, win, open_cmd) + if err then + lspc:err(err) + end + end) +end + +vis:command_register('lspc-hover', function(argv, _, win) + local ls, err = lspc_get_usable_ls(win, argv[1]) + if err then + lspc:err(err) + return + end + assert(ls) + + -- remember the position where hover was called + err = lspc_method_doc_pos(ls, 'hover', win, win.selection.pos) + if err then + lspc:err(err) + end +end) + +vis:command_register('lspc-signature-help', function(argv, _, win) + local ls, err = lspc_get_usable_ls(win, argv[1]) + if err then + lspc:err(err) + return + end + assert(ls) + + -- remember the position where signatureHelp was called + err = lspc_method_doc_pos(ls, 'signatureHelp', win, win.selection.pos) + if err then + lspc:err(err) + end +end) + +vis:command_register('lspc-rename', function(argv, _, win) + local new_name = argv[1] + if not new_name then + lspc:err('lspc-rename usage: <new name> [syntax]') + return + end + + local ls, err = lspc_get_usable_ls(win, argv[2]) + if err then + lspc:err(err) + return + end + assert(ls) + + -- check if the language server has a provider for this method + if not ls.capabilities['renameProvider'] then + lspc:err('language server ' .. ls.name .. ' does not provide rename') + return + end + + if not ls:is_file_opened(win.file.path) then + lspc_open(ls, win, win.file) + end + + local params = vis_doc_pos_to_lsp(vis_get_doc_pos(win)) + params.newName = new_name + + ls:call_text_document_method('rename', params, win) +end) + +vis:command_register('lspc-format', function(argv, _, win) + local ls, err = lspc_get_usable_ls(win, argv[1]) + if err then + lspc:err(err) + return + end + assert(ls) + + -- check if the language server has a provider for this method + if not ls.capabilities['documentFormattingProvider'] then + lspc:err('language server ' .. ls.name .. ' does not provide formatting') + return + end + + if not lspc.open_files[win.file.path] then + lspc_open(ls, win, win.file) + end + + local params = { + textDocument = {uri = path_to_uri(win.file.path)}, + options = ls.formatting_options, + } + if params.options == nil then + params.options = { + tabSize = win.options.tabwidth, + insertSpaces = win.options.expandtab, + } + end + + ls:call_text_document_method('formatting', params, win) +end) + +vis:command_register('lspc-completion', function(argv, _, win) + local ls, err = lspc_get_usable_ls(win, argv[1]) + if err then + lspc:err(err) + return + end + assert(ls) + + -- remember the position where completions were requested + -- to apply insertText completions + err = lspc_method_doc_pos(ls, 'completion', win, win.selection.pos) + if err then + lspc:err(err) + end +end) + +vis:command_register('lspc-start-server', function(argv, _, win) + local syntax = argv[1] or win.syntax + if not syntax then + lspc:err('no language specified') + end + + local _, err = lspc_start_server(syntax) + if err then + lspc:err(err) + end +end) + +vis:command_register('lspc-shutdown-server', function(argv, _, win) + local ls, err = lspc_get_usable_ls(win, argv[1]) + if err then + lspc:err('no language server running: ' .. err) + return + end + assert(ls) + + ls:shutdown() +end) + +vis:command_register('lspc-close', function(argv, _, win) + local ls, err = lspc_get_usable_ls(win, argv[1]) + if err then + lspc:err(err) + return + end + assert(ls) + + lspc_close(ls, win.file) +end) + +vis:command_register('lspc-open', function(argv, _, win) + local ls, err = lspc_get_usable_ls(win, argv[1]) + if err then + lspc:err(err) + return + end + assert(ls) + + lspc_open(ls, win, win.file) +end) + +local function _lspc_next_diagnostic(win, reverse) + local err = lspc_goto_next_diagnostic(win, reverse) + if err then + lspc:err(err) + end +end + +vis:command_register('lspc-next-diagnostic', function(_, _, win) + _lspc_next_diagnostic(win, false) +end) + +vis:command_register('lspc-prev-diagnostic', function(_, _, win) + _lspc_next_diagnostic(win, true) +end) + +vis:command_register('lspc-show-diagnostics', function(argv, _, win) + local err = lspc_show_diagnostic(win, argv[1]) + if err then + lspc:err(err) + end +end) + +-- vis-lspc event hooks +vis.events.subscribe(vis.events.WIN_OPEN, function(win) + if lspc.autostart and win.syntax then + lspc_start_server(win.syntax) + end +end) + +local function highlight_event() + local win = vis.win + if not win or not win.file then + return + end + + local ls = lspc_get_usable_ls(win) + if not ls then + return + end + + local open_file = lspc.open_files[win.file.path] + if open_file and open_file.diagnostics and lspc.highlight_diagnostics then + lspc_highlight_diagnostics(win, open_file.diagnostics) + end +end + +vis.events.subscribe(vis.events.WIN_HIGHLIGHT, highlight_event) +vis.events.subscribe(vis.events.UI_DRAW, highlight_event) + +vis.events.subscribe(vis.events.FILE_OPEN, function(file) + local win = vis.win + -- the only window we can access is not our window + if not win or win.file ~= file then + return + end + + local ls = lspc_get_usable_ls(win) + if not ls then + return + end + + lspc_open(ls, win, file) +end) + +vis.events.subscribe(vis.events.FILE_SAVE_POST, function(file, path) + if not vis.win or vis.win.file ~= file then + return + end + + local file_handle = lspc.open_files[path] + if not file_handle then + return + end + + for ls in pairs(file_handle.language_servers) do + ls:send_did_change(file) + -- the server is interested in didSave notifications + if ls.capabilities.textDocumentSync and type(ls.capabilities.textDocumentSync) == 'table' and + ls.capabilities.textDocumentSync.save then + local did_save_params = {textDocument = {uri = path_to_uri(file.path)}} + ls:send_notification('textDocument/didSave', did_save_params) + end + end +end) + +vis.events.subscribe(vis.events.FILE_CLOSE, function(closed_file) + local file_handle = lspc.open_files[closed_file.path] + if not file_handle then + return + end + + for ls in pairs(file_handle.language_servers) do + lspc_close(ls, closed_file) + end +end) + +vis.events.subscribe(lspc.events.LS_INITIALIZED, function(ls) + if vis.win and vis.win.file and lspc_get_usable_ls(vis.win) == ls then + lspc_open(ls, vis.win, vis.win.file) + end +end) + +vis.events.subscribe(vis.events.QUIT, function() + for _, ls in pairs(lspc.running) do + -- attempt to gracefully shutdown the language server + ls:shutdown() + -- close the fd handle to terminate the subprocess because + -- a potential method response from the server will not be read after the + -- QUIT event + ls.fd:close() + end +end) + +vis:option_register('lspc-highlight-diagnostics', 'string', function(value) + lspc.highlight_diagnostics = value + return true +end, 'How should lspc highlight available diagnostics') + +vis:option_register('lspc-menu-cmd', 'string', function(value) + lspc.menu_cmd = value + return true +end, 'External tool vis-lspc uses to present choices in a menu') + +vis:option_register('lspc-confirm-cmd', 'string', function(value) + lspc.confirm_cmd = value + return true +end, 'External tool vis-lspc uses to ask the user for confirmation') + +vis:option_register('lspc-message-level', 'number', function(value) + lspc.message_level = value + return true +end, 'Message level to show in UI (for server messages)') + +vis:option_register('lspc-diagnostic-style-error', 'string', function(value) + lspc.diagnostic_styles.error = value +end, 'Style for diagnostic errors') + +vis:option_register('lspc-diagnostic-style-warning', 'string', function(value) + lspc.diagnostic_styles.warning = value +end, 'Style for diagnostic warnings') + +vis:option_register('lspc-diagnostic-style-information', 'string', function(value) + lspc.diagnostic_styles.information = value +end, 'Style for diagnostic information') + +vis:option_register('lspc-diagnostic-style-hint', 'string', function(value) + lspc.diagnostic_styles.hint = value +end, 'Style for diagnostic hints') + +dofile(source_path .. 'bindings.lua') +return lspc diff --git a/vendors/vis-lspc/json.lua b/vendors/vis-lspc/json.lua @@ -0,0 +1,216 @@ +--- Find or provide a suitable json module for vis-lspc. +-- +-- @module json +local json = {} + +--- Json modules to use if they are included in LUA_PATH. +local json_impls = {'json', 'cjson', 'dkjson'} + +-- find a suitable json implementation +for _, json_impl in ipairs(json_impls) do + if vis:module_exist(json_impl) then + json = require(json_impl) + if not json.encode or not json.decode then + json = nil + end + + -- found a usable json implementation + if json then + return json + end + end +end + +--[[ json.lua + +A compact pure-Lua JSON library. +The main functions are: json.encode, json.decode. + +## json.encode: + +This expects the following to be true of any tables being encoded: + * They only have string or number keys. Number keys must be represented as + strings in json; this is part of the json spec. + * They are not recursive. Such a structure cannot be specified in json. + +A Lua table is considered to be an array if and only if its set of keys is a +consecutive sequence of positive integers starting at 1. Arrays are encoded like +so: `[2, 3, false, "hi"]`. Any other type of Lua table is encoded as a json +object, encoded like so: `{"key1": 2, "key2": false}`. + +Because the Lua nil value cannot be a key, and as a table value is considerd +equivalent to a missing key, there is no way to express the json "null" value in +a Lua table. The json.null table can be used to encode the json "null" value. +{key=json.null} will be encoded to `{"key":null}`. + +An empty Lua table, {}, could be considered either a json object or array - +it's an ambiguous edge case. We choose to treat this as an object as it is the +more general type. + +To be clear, none of the above considerations is a limitation of this code. +Rather, it is what we get when we completely observe the json specification for +as arbitrary a Lua object as json is capable of expressing. + +## json.decode: + +This function parses json, with the exception that it does not pay attention to +\u-escaped unicode code points in strings. + +It is difficult for Lua to return null as a value. In order to prevent the loss +of keys with a null value in a json string, this function uses the one-off +table value json.null (which is just an empty table) to indicate null values. +This way you can check if a value is null with the conditional +`val == json.null`. + +If you have control over the data and are using Lua, I would recommend just +avoiding null values in your data to begin with. + +--]] + +-- Internal functions. + +local function kind_of(obj) + if obj == json.null then + return 'nil' + end + if type(obj) ~= 'table' then return type(obj) end + local i = 1 + for _ in pairs(obj) do + if obj[i] ~= nil then i = i + 1 else return 'table' end + end + if i == 1 then return 'table' else return 'array' end +end + +local function escape_str(s) + local in_char = {'\\', '"', '/', '\b', '\f', '\n', '\r', '\t'} + local out_char = {'\\', '"', '/', 'b', 'f', 'n', 'r', 't'} + for i, c in ipairs(in_char) do + s = s:gsub(c, '\\' .. out_char[i]) + end + return s +end + +-- Returns pos, did_find; there are two cases: +-- 1. Delimiter found: pos = pos after leading space + delim; did_find = true. +-- 2. Delimiter not found: pos = pos after leading space; did_find = false. +-- This throws an error if err_if_missing is true and the delim is not found. +local function skip_delim(str, pos, delim, err_if_missing) + pos = pos + #str:match('^%s*', pos) + if str:sub(pos, pos) ~= delim then + if err_if_missing then + error('Expected ' .. delim .. ' near position ' .. pos) + end + return pos, false + end + return pos + 1, true +end + +-- Expects the given pos to be the first character after the opening quote. +-- Returns val, pos; the returned pos is after the closing quote character. +local function parse_str_val(str, pos, val) + val = val or '' + local early_end_error = 'End of input found while parsing string.' + if pos > #str then error(early_end_error) end + local c = str:sub(pos, pos) + if c == '"' then return val, pos + 1 end + if c ~= '\\' then return parse_str_val(str, pos + 1, val .. c) end + -- We must have a \ character. + local esc_map = {b = '\b', f = '\f', n = '\n', r = '\r', t = '\t'} + local nextc = str:sub(pos + 1, pos + 1) + if not nextc then error(early_end_error) end + return parse_str_val(str, pos + 2, val .. (esc_map[nextc] or nextc)) +end + +-- Returns val, pos; the returned pos is after the number's final character. +local function parse_num_val(str, pos) + local num_str = str:match('^-?%d+%.?%d*[eE]?[+-]?%d*', pos) + local val = tonumber(num_str) + if not val then error('Error parsing number at position ' .. pos .. '.') end + return val, pos + #num_str +end + + +-- Public values and functions. + +function json.encode(obj, as_key) + local s = {} -- We'll build the string as an array of strings to be concatenated. + local kind = kind_of(obj) -- This is 'array' if it's an array or type(obj) otherwise. + if kind == 'array' then + if as_key then error('Can\'t encode array as key.') end + s[#s + 1] = '[' + for i, val in ipairs(obj) do + if i > 1 then s[#s + 1] = ', ' end + s[#s + 1] = json.encode(val) + end + s[#s + 1] = ']' + elseif kind == 'table' then + if as_key then error('Can\'t encode table as key.') end + s[#s + 1] = '{' + for k, v in pairs(obj) do + if #s > 1 then s[#s + 1] = ', ' end + s[#s + 1] = json.encode(k, true) + s[#s + 1] = ':' + s[#s + 1] = json.encode(v) + end + s[#s + 1] = '}' + elseif kind == 'string' then + return '"' .. escape_str(obj) .. '"' + elseif kind == 'number' then + if as_key then return '"' .. tostring(obj) .. '"' end + return tostring(obj) + elseif kind == 'boolean' then + return tostring(obj) + elseif kind == 'nil' then + return 'null' + else + error('Unjsonifiable type: ' .. kind .. '.') + end + return table.concat(s) +end + +json.null = {} -- This is a one-off table to represent the null value. + +function json.decode(str, pos, end_delim) + pos = pos or 1 + if pos > #str then error('Reached unexpected end of input.') end + local pos = pos + #str:match('^%s*', pos) -- Skip whitespace. + local first = str:sub(pos, pos) + if first == '{' then -- Parse an object. + local obj, key, delim_found = {}, true, true + pos = pos + 1 + while true do + key, pos = json.decode(str, pos, '}') + if key == nil then return obj, pos end + if not delim_found then error('Comma missing between object items.') end + pos = skip_delim(str, pos, ':', true) -- true -> error if missing. + obj[key], pos = json.decode(str, pos) + pos, delim_found = skip_delim(str, pos, ',') + end + elseif first == '[' then -- Parse an array. + local arr, val, delim_found = {}, true, true + pos = pos + 1 + while true do + val, pos = json.decode(str, pos, ']') + if val == nil then return arr, pos end + if not delim_found then error('Comma missing between array items.') end + arr[#arr + 1] = val + pos, delim_found = skip_delim(str, pos, ',') + end + elseif first == '"' then -- Parse a string. + return parse_str_val(str, pos + 1) + elseif first == '-' or first:match('%d') then -- Parse a number. + return parse_num_val(str, pos) + elseif first == end_delim then -- End of an object or array. + return nil, pos + 1 + else -- Parse true, false, or null. + local literals = {['true'] = true, ['false'] = false, ['null'] = json.null} + for lit_str, lit_val in pairs(literals) do + local lit_end = pos + #lit_str - 1 + if str:sub(pos, lit_end) == lit_str then return lit_val, lit_end + 1 end + end + local pos_info_str = 'position ' .. pos .. ': ' .. str:sub(pos, pos + 10) + error('Invalid json syntax starting at ' .. pos_info_str) + end +end + +return json diff --git a/vendors/vis-lspc/log.lua b/vendors/vis-lspc/log.lua @@ -0,0 +1,93 @@ +--- Simple logging module for vis-lspc. +-- @module log +-- @author Florian Fischer +-- @license GPL-3 +-- @copyright 2024 Florian Fischer +local log = {} + +--- Logger class metatable. +local Logger = {} +function Logger:log(msg) + self.log_fd:write(msg) + self.log_fd:write('\n') + self.log_fd:flush() +end +function Logger:close() + self.log_fd:close() +end + +--- Dummy logger class metatable using NOP log functions. +local DummyLogger = {} +function DummyLogger.log() +end +function DummyLogger.close() +end + +--- Logger that initialises itself on first use +local LazyInitLogger = {} +function LazyInitLogger:log(first_msg) + self.logger = log.new(self.name, self.conf.logging, self.conf.log_file) + self.log = function(lazyLogger, msg) + lazyLogger.logger:log(msg) + end + self.logger:log(first_msg) +end +function LazyInitLogger:close() + self.logger:close() +end + +--- Create a new lazily initializing logger +-- +-- The created logger will initialize itself using log.new on first use. +-- The logging and log_file fields of the conf table are passed to log.new. +-- @param name The name of the logger +-- @param conf Table containing logging and log_file members +function log.lazyNew(name, conf) + local logger = {name = name, conf = conf} + setmetatable(logger, {__index = LazyInitLogger}) + return logger +end + +--- Create a new named logger +-- @param name The name of the logger +-- @param logging Is logging enabled +-- @param log_file File path to the log file to use +function log.new(name, logging, log_file) + local logger = {name = name, log_fd = nil} + + if not logging then + setmetatable(logger, {__index = DummyLogger}) + return logger + end + + setmetatable(logger, {__index = Logger}) + + -- open the default log file in $XDG_DATA_HOME/vis-lspc + if not log_file then + local xdg_data = os.getenv('XDG_DATA_HOME') or os.getenv('HOME') .. '/.local/share' + local log_dir = xdg_data .. '/vis-lspc' + + -- ensure the direcoty exists + os.execute('mkdir -p ' .. log_dir) + + -- log file format: {timestamp}-{basename-cwd}.log + local log_file_fmt = log_dir .. '/%s-%s.log' + local timestamp = os.date('%Y-%m-%dT%H:%M:%S') + local proc = assert(io.popen('basename "${PWD}"', 'r')) + local basename_cwd = assert(proc:read('*a')):match('^%s*(.-)%s*$') + local success, _, status = proc:close() + if not success then + local err = 'getting the basename of CWD failed with exit code: ' .. status + vis:info('LSPC Error: ' .. err) + end + log_file = log_file_fmt:format(timestamp, basename_cwd) + + elseif type(log_file) == 'function' then + log_file = log_file() + end + + logger.log_fd = assert(io.open(log_file, 'w')) + return logger +end + +return log diff --git a/vendors/vis-lspc/lspc.lua b/vendors/vis-lspc/lspc.lua @@ -0,0 +1,140 @@ +--- State and methods of the language server client. +-- This module table is returned when requiring the vis-lspc plugin. +-- @module lspc +-- @author Florian Fischer +-- @license GPL-3 +-- @copyright Florian Fischer 2021-2024 +--- Initial state of the client. +-- This includes the default configuration that can be modified in +-- your visrc.lua file. +local lspc = { + -- mapping language server names to their state tables + running = {}, + open_files = {}, + name = 'vis-lspc', + version = '0.1.8', + -- write log messages to lspc.log_file + logging = false, + log_file = nil, + -- automatically start a language server when a new window is opened + autostart = true, + -- program used to let the user make choices + -- The available choices are passed to <menu_cmd> on stdin separated by '\n' + menu_cmd = 'vis-menu -l 10', + -- program used to ask the user for confirmation + confirm_cmd = 'vis-menu', + + -- should diagnostics be highlighted if available + highlight_diagnostics = 'line', + -- style id used by lspc to register the style used to highlight diagnostics + -- by default win.STYLE_LEXER_MAX is used (the last style id available for the lexer styles). See vis/ui.h. + diagnostic_style_id = nil, + -- styles used by lspc to highlight the diagnostic range + -- must be set by the user + diagnostic_styles = { + error = 'fore:red,italics,reverse', + warning = 'fore:yellow,italics,reverse', + information = 'fore:yellow,italics,reverse', + hint = 'fore:yellow,italics,reverse', + }, + + -- restore the position of the primary curser after applying a workspace edit + workspace_edit_remember_cursor = true, + + -- message level to show in the UI when receiving messages from the server + -- Error = 1, Warning = 2, Info = 3, Log = 4 + message_level = 3, + + -- How to present messages to the user. + -- 'message': use vis:message; 'open': use a new split window allowing for syntax highlighting + show_message = 'message', + + -- Globs that are considered to be workspace roots (e.g. ".git" or ".hg") + universal_root_globs = {}, + + -- Should a file's directory be used as workspace root if no explicit root was found. + fallback_dirname_as_root = false, + + -- events + events = { + LS_INITIALIZED = 'LspcEvent::LS_INITIALIZED', + LS_DID_OPEN = 'LspcEvent::LS_DID_OPEN', + }, +} + +-- check if fzf is available and use fzf instead of vis-menu per default +if os.execute('type fzf >/dev/null 2>/dev/null') then + lspc.menu_cmd = 'fzf' +end + +local supported_markup_kind = {'markdown'} + +local goto_methods_capabilities = { + linkSupport = true, + dynamicRegistration = false, +} + +--- ClientCapabilities we tell the language server when calling "initialize". +local client_capabilites = { + workspace = { + configuration = true, + didChangeConfiguration = {dynamicRegistration = false}, + }, + textDocument = { + synchronization = {dynamicRegistration = false, didSave = true}, + -- ask the server to send us only markdown completionItems + completion = { + dynamicRegistration = false, + completionItem = {documentationFormat = supported_markup_kind}, + }, + -- ask the server to send us only markdown hover results + hover = {dynamicRegistration = false, contentFormat = supported_markup_kind}, + -- ask the server to send us only markdown signatureHelp results + signatureHelp = { + dynamicRegistration = false, + signatureInformation = {documentationFormat = supported_markup_kind}, + }, + declaration = {dynamicRegistration = false, linkSupport = true}, + definition = goto_methods_capabilities, + publishDiagnostics = {relatedInformation = false}, + typeDefinition = goto_methods_capabilities, + implementation = goto_methods_capabilities, + references = {dynamicRegistration = false}, + rename = { + dynamicRegistration = false, + prepareSupport = false, + honorsChangeAnnotations = false, + }, + }, + window = {workDoneProgress = false, showDocument = {support = false}}, +} + +lspc.client_capabilites = client_capabilites + +local Lspc = {} + +--- Log a message. +-- @string: the message to log +function Lspc:log(msg) + self.logger:log(msg) +end + +--- Present a warning to the user. +-- @string: the warning message +function Lspc:warn(msg) + local warning = 'LSPC Warning: ' .. msg + self.logger:log(warning) + vis:info(warning) +end + +--- Present an error to the user. +-- @string: the error message +function Lspc:err(msg) + local warning = 'LSPC Error: ' .. msg + self.logger:log(warning) + vis:info(warning) +end + +setmetatable(lspc, {__index = Lspc}) + +return lspc diff --git a/vendors/vis-lspc/parser.lua b/vendors/vis-lspc/parser.lua @@ -0,0 +1,75 @@ +--- Stateful parser for the data send by a language server. +-- Note the chunks received may not end with the end of a message. +-- In the worst case a data chunk contains two partial messages one +-- at the beginning and one at the end. +-- @module parser +-- @author Florian Fischer +-- @license GPL-3 +-- @copyright Florian Fischer 2021-2024 +local parser = {} + +local Parser = {} + +function parser.new() + local self = {exp_len = nil, data = '', msgs = {}} + + setmetatable(self, {__index = Parser}) + return self +end + +function Parser:add(data) + self.data = self.data .. data + + -- we have not seen a complete header in data yet + if not self.exp_len then + -- search for the end of a header + -- LSP message format: 'header\r\n\r\nbody' + local header_end, content_start = self.data:find('\r\n\r\n') + + -- header is not complete yet -> save data and wait for more + if not header_end then + return + end + + -- got a complete header + local header = self.data:sub(1, header_end) + + -- extract content length from the header + self.exp_len = tonumber(header:match('%d+')) + if not self.exp_len then + return 'invalid header in data: ' .. self.data + end + + -- consume header from data + self.data = self.data:sub(content_start + 1) + end + + local data_avail = string.len(self.data) + -- we have no complete message yet -> await more data + if self.exp_len > data_avail then + return + end + + local complete_msg = self.data:sub(1, self.exp_len) + table.insert(self.msgs, complete_msg) + + -- consume complete_msg from data + self.data = self.data:sub(self.exp_len + 1) + + local leftover = data_avail - self.exp_len + + -- reset exp_len to search for a new header + self.exp_len = nil + + if leftover > 0 then + return self:add('') + end +end + +function Parser:get_msgs() + local msgs = self.msgs + self.msgs = {} + return msgs +end + +return parser diff --git a/vendors/vis-lspc/parser_test.lua b/vendors/vis-lspc/parser_test.lua @@ -0,0 +1,125 @@ +#!/usr/bin/env lua5.4 + +local parser = require('parser') + +local function build_msg(body) + return 'Content-Length: ' .. tostring(string.len(body)) .. '\r\n\r\n' .. body +end + +local lunatest = require('lunatest') + +function test_complete_msg() -- luacheck: ignore 111 + local msg = build_msg('foo') + local p = parser.new() + local err = p:add(msg) + lunatest.assert_nil(err) + + local msgs = p:get_msgs() + lunatest.assert_table(msgs) + lunatest.assert_len(1, msgs) + lunatest.assert_equal(msgs[1], 'foo') +end + +function test_two_complete_msgs() -- luacheck: ignore 111 + local p = parser.new() + local data = build_msg('foo') + local err = p:add(data) + lunatest.assert_nil(err) + + local msgs = p:get_msgs() + lunatest.assert_table(msgs) + lunatest.assert_len(1, msgs) + lunatest.assert_equal(msgs[1], 'foo') + + data = build_msg('bar') + err = p:add(data) + lunatest.assert_nil(err) + msgs = p:get_msgs() + lunatest.assert_table(msgs) + lunatest.assert_len(1, msgs) + lunatest.assert_equal(msgs[1], 'bar') +end + +function test_two_complete_msgs_at_once() -- luacheck: ignore 111 + local data = build_msg('foo') .. build_msg('bar') + local p = parser.new() + local err = p:add(data) + lunatest.assert_nil(err) + + local msgs = p:get_msgs() + lunatest.assert_table(msgs) + lunatest.assert_len(2, msgs) + lunatest.assert_equal(msgs[1], 'foo') + lunatest.assert_equal(msgs[2], 'bar') +end + +function test_split_msg() -- luacheck: ignore 111 + local msg = build_msg('foo') + local part1 = msg:sub(1, -3) + local part2 = msg:sub(-2) + local p = parser.new() + local err = p:add(part1) + lunatest.assert_nil(err) + + err = p:add(part2) + lunatest.assert_nil(err) + + local msgs = p:get_msgs() + lunatest.assert_table(msgs) + lunatest.assert_len(1, msgs) + lunatest.assert_equal(msgs[1], 'foo') +end + +function test_complete_and_split_msg() -- luacheck: ignore 111 + local msg = build_msg('foo') .. build_msg('bar') + local part1 = msg:sub(1, -3) + local part2 = msg:sub(-2) + local p = parser.new() + local err = p:add(part1) + lunatest.assert_nil(err) + + err = p:add(part2) + lunatest.assert_nil(err) + + local msgs = p:get_msgs() + lunatest.assert_table(msgs) + lunatest.assert_len(2, msgs) + lunatest.assert_equal(msgs[1], 'foo') + lunatest.assert_equal(msgs[2], 'bar') +end + +function test_split_hdr() -- luacheck: ignore 111 + local msg = build_msg('foo') + local part1 = msg:sub(1, 3) + local part2 = msg:sub(4) + local p = parser.new() + local err = p:add(part1) + lunatest.assert_nil(err) + + err = p:add(part2) + lunatest.assert_nil(err) + + local msgs = p:get_msgs() + lunatest.assert_table(msgs) + lunatest.assert_len(1, msgs) + lunatest.assert_equal(msgs[1], 'foo') +end + +function test_split_hdr_body_sep() -- luacheck: ignore 111 + local msg = build_msg('foo') + local part1 = msg:sub(1, 19) + local part2 = msg:sub(20) + local p = parser.new() + local err = p:add(part1) + lunatest.assert_nil(err) + + err = p:add(part2) + lunatest.assert_nil(err) + + local msgs = p:get_msgs() + lunatest.assert_table(msgs) + lunatest.assert_len(1, msgs) + lunatest.assert_equal(msgs[1], 'foo') +end + +lunatest.run() diff --git a/vendors/vis-lspc/supported-servers.lua b/vendors/vis-lspc/supported-servers.lua @@ -0,0 +1,121 @@ +-- Copyright (c) 2022 Florian Fischer. All rights reserved. +-- +-- This file is part of vis-lspc. +-- +-- vis-lspc is free software: you can redistribute it and/or modify it under the +-- terms of the GNU General Public License as published by the Free Software +-- Foundation, either version 3 of the License, or (at your option) any later +-- version. +-- +-- vis-lspc is distributed in the hope that it will be useful, but WITHOUT ANY +-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +-- FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License along with +-- vis-lspc found in the LICENSE file. If not, see <https://www.gnu.org/licenses/>. +-- +-- List of supported and preconfigured language server implementations +local source_str = debug.getinfo(1, 'S').source:sub(2) +local source_path = source_str:match('(.*/)') + +local lspc = dofile(source_path .. 'lspc.lua') + +local clangd = { + name = 'clangd', + cmd = 'clangd', + roots = {'compile_commands.json', '.clangd'}, +} +local typescript = { + name = 'typescript', + cmd = 'typescript-language-server --stdio', + roots = {'package.json', 'tsconfig.json', 'jsconfig.json'}, +} + +return { + c = clangd, + cpp = clangd, + ansi_c = clangd, + -- pylsp (python-lsp-server) language server configuration + -- https://github.com/python-lsp/python-lsp-server + python = { + name = 'python-lsp-server', + cmd = 'pylsp', + roots = {'requirements.txt', 'setup.py'}, + }, + -- lua (lua-language-server) language server configuration + -- https://github.com/sumneko/lua-language-server + lua = { + name = 'lua-language-server', + cmd = 'lua-language-server', + settings = { + Lua = {diagnostics = {globals = {'vis'}}, telemetry = {enable = false}}, + }, + }, + -- typescript (typescript-language-server) language server configuration + -- https://github.com/typescript-language-server/typescript-language-server + javascript = typescript, + typescript = typescript, + -- dart language server configuration + -- https://github.com/dart-lang/sdk/blob/master/pkg/analysis_server/tool/lsp_spec/README.md + dart = { + name = 'dart', + cmd = 'dart language-server --client-id vis-lspc --client-version ' .. lspc.version, + roots = {'pubspec.yaml'}, + }, + -- haskell (haskell-language-server) + -- https://github.com/haskell/haskell-language-server + haskell = { + name = 'haskell', + cmd = 'haskell-language-server-wrapper --lsp', + roots = {'hie.yaml', 'cabal.project', 'Setup.hs', 'stack.yaml', '*.cabal'}, + }, + + -- ocaml (ocaml-language-server) + -- https://github.com/ocaml/ocaml-lsp + caml = { + name = 'ocaml', + cmd = 'ocamllsp', + roots = { + 'dune-workspace', + 'dune-project', + 'Makefile', + 'opam', + '*.opam', + 'esy.json', + 'dune', + }, + }, + + -- go (gopls) + -- https://github.com/golang/tools/tree/master/gopls + go = {name = 'go', cmd = 'gopls', roots = {'Gopkg.toml', 'go.mod'}}, + + -- bash (bash-language-server) + -- https://github.com/bash-lsp/bash-language-server + bash = {name = 'bash-language-server', cmd = 'bash-language-server start'}, + + -- html (html-language-server) + -- https://github.com/hrsh7th/vscode-langservers-extracted + html = { + name = 'html-language-server', + cmd = 'vscode-html-language-server --stdio', + }, + + -- css (css-language-server) + -- https://github.com/hrsh7th/vscode-langservers-extracted + css = { + name = 'css-language-server', + cmd = 'vscode-css-language-server --stdio', + }, + + -- json (json-language-server) + -- https://github.com/hrsh7th/vscode-langservers-extracted + json = { + name = 'json-language-server', + cmd = 'vscode-json-language-server --stdio', + }, + + -- rust (rust-analyzer) + -- https://github.com/rust-lang/rust-analyzer + rust = {name = 'rust', cmd = 'rust-analyzer', roots = {'Cargo.toml'}}, +} diff --git a/vendors/vis-lspc/tools/check-format b/vendors/vis-lspc/tools/check-format @@ -0,0 +1,8 @@ +#!/bin/sh + +LUA_FILE=$1 +lua-format "${LUA_FILE}" > "${LUA_FILE}.fmt" +diff "${LUA_FILE}" "${LUA_FILE}.fmt" +RET=$? +rm "${LUA_FILE}.fmt" +exit ${RET} diff --git a/vendors/vis-lspc/tools/find-upwards b/vendors/vis-lspc/tools/find-upwards @@ -0,0 +1,26 @@ +#!/bin/sh + +set -eu + +if [ ! "${1:-}" ]; then + exit 1 +fi + +BASEDIR="$(dirname "$1")" + +while read -r glob; do + cd "$BASEDIR" + + while [ "$PWD" != "/" ]; do + set -- "$glob" + + if [ -e "$1" ]; then + echo "$PWD" + exit 0 + fi + + cd .. + done +done + +exit 1 diff --git a/vendors/vis-lspc/util.lua b/vendors/vis-lspc/util.lua @@ -0,0 +1,307 @@ +--- Module containing simple utility functions for vis-lspc/ +-- @module util +-- @author Florian Fischer +-- @author git-bruh <prathamIN@proton.me> +-- @license GPL-3 +-- @copyright 2024 Florian Fischer +-- @copyright 2024 git-bruh <prathamIN@proton.me> +local util = {} + +local source_str = debug.getinfo(1, 'S').source:sub(2) +local source_path = source_str:match('(.*/)') + +local lspc + +function util.init(lspc_) + lspc = lspc_ + return util +end + +--- Execute a command and capture its output. +-- @param cmd the command to execute +-- @return the output of the command written to stdout +function util.capture_cmd(cmd) + local p = assert(io.popen(cmd, 'r')) + local s = assert(p:read('*a')) + local success, _, status = p:close() + if not success then + local err = cmd .. ' failed with exit code: ' .. status + lspc:err(err) + end + return s +end + +local vis_supports_pipe_buf = pcall(vis.pipe, vis, 'foo', 'true', false) + +--- Wrapper for the two vis:pipe variants. +-- If vis does not support vis:pipe(input, cmd), prefix the command +-- with a printf call piping the result to the original command. +-- @param input The input to pipe to the command +-- @param cmd The external command to pipe the input to +function util.vis_pipe(input, cmd, fullscreen) + if vis_supports_pipe_buf then + return vis:pipe(input, cmd, fullscreen or false) + end + + local escaped_input = input:gsub('\'', '\'"\'"\'') + cmd = 'printf %s \'' .. escaped_input .. '\' | ' .. cmd + return vis:pipe(vis.win.file, {start = 0, finish = 0}, cmd) +end + +--- Split a path into its components +-- @param path the path to split into components +-- @return a table containing the path components +function util.split_path_into_components(path) + local components = {} + + if #path == 1 then + return nil + end + + -- Skip the initial '/' + local start_idx = 2 + + while true do + local slash = path:find('/', start_idx + 1) + + if slash == nil then + table.insert(components, path:sub(start_idx, #path)) + return components + else + table.insert(components, path:sub(start_idx, slash - 1)) + start_idx = slash + 1 + end + end +end + +--- Get a path relative to the current working directory +-- @param cwd_components Table of the path components of the CWD +-- @param absolute_path_or_components absolute path or table of its path components +-- @return the relative path +function util.get_relative_path(cwd_components, absolute_path_or_components) + local absolute_components + if type(absolute_path_or_components) == 'string' then + absolute_components = util.split_path_into_components(absolute_path_or_components) + else + absolute_components = absolute_path_or_components + end + + for idx = 1, #cwd_components do + local cwd = cwd_components[idx] + local absolute = absolute_components[idx] + + if cwd ~= absolute then + local dir = '' + + -- Atleast the first component must match for us to convert + -- it to a relative path + if idx ~= 1 then + for _ = idx, #cwd_components do + dir = dir .. '..' .. '/' + end + + -- Skip trailing '/' + dir = dir:sub(1, #dir - 1) + end + + for i = idx, #absolute_components do + dir = dir .. '/' .. absolute_components[i] + end + + return dir + end + end + + -- cwd shorter than absolute path + local dir = '' + + for i = #cwd_components + 1, #absolute_components do + dir = dir .. '/' .. absolute_components[i] + end + + -- Skip leading '/' + return dir:sub(2) +end + +--- Strip the last component from a pathname +-- @param the pathname +-- @return the pathname up to the last '/' +function util.dirname(name) + if name == '.' or name == '..' or name == '/' then + return name + end + + -- strip a trailing path separator + if name:sub(#name, #name) == '/' then + name = name:sub(1, #name - 1) + end + + local dirname = name:match('(.*)[/]') + -- There was no path separator in name. + if not dirname then + return '.' + end + + -- The name started with the root dir. + if dirname == '' then + return '/' + end + + return dirname +end + +--- Create an iterator yielding the nth line of a file +-- +-- @param path The path to the file +function util.file_line_iterator_to_n(path) + local file = assert(io.open(path, 'r')) + local lines = file:lines() + local last_line = nil + local last_n = 1 + + return function(n) + if n == -1 then + file:close() + return nil + end + + if n < last_n then + -- We might have multiple references on the same line, so we can + -- get called again with the previous line number + if (n + 1) == last_n then + return last_line + end + + return nil + end + + for line in lines do + if n == last_n then + last_n = last_n + 1 + last_line = line + + return line + end + + last_n = last_n + 1 + end + + -- Iterator exhausted + return nil + end +end + +--- Find file based on globs in the parent file system tree +-- @param globs a new line separated string of file globs +-- @param start the starting path +function util.find_upwards(globs, start) + local status, out = util.vis_pipe(globs, '\'' .. source_path:gsub('\'', '\'\\\'\'') .. + '/tools/find-upwards\' "' .. start .. '"') + + if status ~= 0 or out == nil then + return nil + end + + -- Skip trailing newline + return out:sub(1, #out - 1) +end + +-- get the vis_selection from current primary selection +local function get_selection(win) + return {line = win.selection.line, col = win.selection.col} +end + +--- Calculate the 0-based byte offsets from multiple sorted selections +-- @param file the file to calculate the positions in +-- @param sorted_selections a table of sorted vis_selections +-- @return a table of positions +function util.vis_sorted_selections_to_pos(file, sorted_selections) + local positions = {} + if file.pos_by_linecol then + for _, sel in ipairs(sorted_selections) do + table.insert(positions, file:pos_by_linecol(sel.line, sel.col)) + end + return positions + end + + local line_count = 0 + local pos = 0 + local sel_i = 1 + local sel = sorted_selections[sel_i] + for line in file:lines_iterator() do + line_count = line_count + 1 + while line_count == sel.line do + table.insert(positions, pos + (sel.col - 1)) + sel_i = sel_i + 1 + -- no more selections to convert + if sel_i > #sorted_selections then + break + end + sel = sorted_selections[sel_i] + end + + pos = pos + #line + 1 + end + return positions +end + +local function vis_pos_before(p1, p2) + return p1.line < p2.line or (p1.line == p2.line and p1.col < p2.col) +end + +--- Calculate the 0-based byte offsets from multiple selections +-- @param file the file to calculate the positions in +-- @param selections a table of selections +-- @return a table of positions +function util.vis_selections_to_pos(file, selections) + table.sort(selections, vis_pos_before) + return util.vis_sorted_selections_to_pos(file, selections) +end + +--- Get the line and column from a 0-based byte offset +-- ATTENTION: the fallback version of this function modifies the primary +-- selection so it is not safe to call it for example during WIN_HIGHLIGHT events +-- @param pos the 0-based byte offset into the file +-- @return the 1-based line number +-- @return the 1-based column +function util.vis_pos_to_sel(win, pos) + if win.file.linecol_by_pos then + local lineno, col = win.file:linecol_by_pos(pos) + return {line = lineno, col = col} + end + + local old_selection = get_selection(win) + -- move primary selection + win.selection.pos = pos + local sel = get_selection(win) + -- restore old primary selection + win.selection:to(old_selection.line, old_selection.col) + return sel +end + +--- Count the visual characters in a line +-- This is useful to detect wrapped lines including tabs which may add a +-- unspecified amount of white space to a line depending on their position and +-- the used tabwidth. +-- @param win the window containing the line +-- @param line the line to count +-- @param nchars the number of characters in the line +-- @return the number of visual characters +util.visual_chars_in_line = function(win, line, nchars) + -- fast string iteration inspired by: + -- https://stackoverflow.com/a/49222705 + local l = {string.byte(line, 1, nchars)} + local line_len = 0 + for i = 1, nchars do + local c = l[i] -- Note: produces char codes instead of chars. + if c == 9 then -- '\t' + local chars_to_tab_stop = win.options.tabwidth - (line_len % win.options.tabwidth) + line_len = line_len + chars_to_tab_stop + else + line_len = line_len + 1 + end + end + return line_len +end + +return util diff --git a/vendors/vis-lspc/util_test.lua b/vendors/vis-lspc/util_test.lua @@ -0,0 +1,33 @@ +#!/usr/bin/env lua5.4 + +-- mock vis global +vis = {} -- luacheck: ignore 111 +local util = require('util') + +local lunatest = require('lunatest') + +function test_dirname() -- luacheck: ignore 111 + lunatest.assert_equal('/usr', util.dirname('/usr/lib')) + lunatest.assert_equal('/', util.dirname('/usr/')) + lunatest.assert_equal('.', util.dirname('usr')) + lunatest.assert_equal('.', util.dirname('.')) + lunatest.assert_equal('..', util.dirname('..')) + lunatest.assert_equal('/', util.dirname('/')) +end + +function test_visual_chars_in_line() -- luacheck: ignore 111 + local win = {options = {tabwidth = 4}} -- win mock + local s = '\tfo' -- visual chars == 6 + lunatest.assert_equal(util.visual_chars_in_line(win, s, #s), 6) + + s = 'f\tfo' -- visual chars == 6 + lunatest.assert_equal(util.visual_chars_in_line(win, s, #s), 6) + + s = 'fo\tfo' -- visual chars == 6 + lunatest.assert_equal(util.visual_chars_in_line(win, s, #s), 6) + + s = 'foo\tfo' -- visual chars == 6 + lunatest.assert_equal(util.visual_chars_in_line(win, s, #s), 6) +end + +lunatest.run() diff --git a/visrc.lua b/visrc.lua @@ -0,0 +1,147 @@ +require('vis') +require('vendors/commentary') +require('vendors/surround') +require('vendors/modelines') +require('lib/window-manager') +require('lib/status') +require('lib/ctags') +require('lib/session') +require('lib/router') +require('lib/search') + +local m = vis.modes + +vis.events.subscribe(vis.events.INIT, function() + vis:command('set autoindent on') + vis:command('set ignorecase on') + vis:command('set layout v') + + -- navigation + map({m.NORMAL, m.INSERT}, '<M-Up>', '<vis-window-prev>') + map({m.NORMAL, m.INSERT}, '<M-Down>', '<vis-window-next>') + map({m.NORMAL, m.INSERT}, '<M-Left>', '<vis-router-back>') + map({m.NORMAL, m.INSERT}, '<M-Right>', '<vis-router-forward>') + map({m.NORMAL, m.INSERT}, '<M-q>', cmd('q'), 'Quit') + map({m.NORMAL, m.INSERT}, '<F1>', cmd("tabnew +help-fullscreen"), 'Help fullscreen') + + -- navigation via search + map({m.NORMAL, m.VISUAL}, '-', cmd('find'), 'File search') + map({m.NORMAL, m.VISUAL}, '_', cmd('mru'), 'Most recently used files') + map({m.NORMAL, m.VISUAL}, ' f', cmd('find'), '[F]ile search') + map({m.NORMAL, m.VISUAL}, ' d', cmd('dfind'), '[D]irectory search') + map({m.NORMAL, m.VISUAL}, ' g', cmd('grep'), '[G]rep search') + map({m.NORMAL, m.VISUAL}, ' F', cmd('scope find'), '[F]ile scoped search') + map({m.NORMAL, m.VISUAL}, ' G', cmd('scope grep'), '[G]rep scoped search') + map({m.NORMAL, m.VISUAL}, ' D', cmd('scope dfind'), '[D]irectory scoped search') + map({m.NORMAL, m.VISUAL}, ' tf', cmd('tab find'), '[T]ab [F]ile search') + map({m.NORMAL, m.VISUAL}, ' tg', cmd('tab grep'), '[T]ab [G]rep search') + map({m.NORMAL, m.VISUAL}, ' td', cmd('tab dfind'), '[T]ab [D]irectory search') + map({m.NORMAL, m.VISUAL}, ' tF', cmd('tab scope find'), '[T]ab [F]ile scoped search') + map({m.NORMAL, m.VISUAL}, ' tG', cmd('tab scope grep'), '[T]ab [G]rep scoped search') + map({m.NORMAL, m.VISUAL}, ' tD', cmd('tab scope dfind'), '[T]ab [D]irectory scoped search') + + -- kill the habit + -- map({m.NORMAL, m.INSERT}, '<M-t>', cmd("tab 'fe %s | xargs -r vis'"), 'New window') + -- map({m.NORMAL, m.VISUAL}, '-', cmd('explore'), 'Explore') + -- map({m.NORMAL, m.VISUAL}, ' te', cmd('explore -t'), '[T]ab [E]xplore') + + -- misc + map(m.NORMAL, 'go', ins('o<Escape>')) + map(m.NORMAL, 'gO', ins('O<Escape>')) + + -- system clipboard by default + map(m.NORMAL, 'y', '<vis-register>+<vis-operator-yank>') + map(m.VISUAL, 'y', '<vis-register>+<vis-operator-yank>') + map(m.VISUAL_LINE, 'y', '<vis-register>+<vis-operator-yank>') + map(m.NORMAL, 'd', '<vis-register>+<vis-operator-delete>') + map(m.VISUAL, 'd', '<vis-register>+<vis-operator-delete>') + map(m.VISUAL_LINE, 'd', '<vis-register>+<vis-operator-delete>') + map(m.NORMAL, 'p', '<vis-register>+<vis-put-after>') + map(m.VISUAL, 'p', '<vis-register>+<vis-put-after>') + map(m.VISUAL_LINE, 'p', '<vis-register>+<vis-put-after>') + map(m.NORMAL, 'P', '<vis-register>+<vis-put-before>') + map(m.VISUAL, 'P', '<vis-register>+<vis-put-before>') + map(m.VISUAL_LINE, 'P', '<vis-register>+<vis-put-before>') + + -- center + map(m.NORMAL, 'n', '<vis-motion-search-repeat-forward>zz') + map(m.NORMAL, 'N', '<vis-motion-search-repeat-backward>zz') + + map({m.NORMAL, m.VISUAL}, 'K', function() + local sec = vis.count or 1 + local name = content(vis.win) + vis.count = nil + vis:command(string.format('terminal man %d %s', sec, name)) + end, 'Man') +end) + +vis.events.subscribe(vis.events.WIN_OPEN, function() + vis:command('set tabwidth 4') + -- vis:command('set ctags on') + -- vis:command('set ctags-autosave on') +end, 1) + +function map(modes, ...) + if type(modes) ~= 'table' then + vis:map(modes, ...) + return + end + for _, mode in pairs(modes) do + vis:map(mode, ...) + end +end + +function ins(x) + return function() + local pos = vis.win.selection.pos + vis:feedkeys(x) + vis.win.selection.pos = pos + return 1 + end +end + +function cmd(x) + return function() + local fn = vis.win.file.name + if fn then + x = string.format(x, fn) + end + vis:command(x) + end +end + +function content(win) + local sel = win.selection + local range = sel.anchored and sel.range or win.file:text_object_word(sel.pos) + return win.file:content(range) +end + +-- useful for startup (vis +help-fullscreen) +vis:command_register('help-fullscreen', function(argv) + vis:command('help') + vis:feedkeys('<vis-window-prev>') + vis:command('q') +end) + +vis:command_register('normal', function(argv) + vis.mode = vis.modes.NORMAL + vis:feedkeys(table.concat(argv)) +end) + +-- move this to an autocomplete module +-- vis.events.subscribe(vis.events.INIT, function() + -- map(vis.modes.INSERT, '<C-x><C-o>', function() + -- local win = vis.win + -- local sel = win.selection + -- local pos = sel.pos > 1 and sel.pos - 1 or sel.pos + -- local range = win.file:text_object_word(pos) + -- local pattern = win.file:content(range) + -- pattern = pattern:gsub('%s', '') == '' and '.' or string.format("'%s'", pattern) + + -- local success, out = popen(string.format("ag --vimgrep -g %s | fzy", pattern)) + -- if success then + -- vis:insert(out:sub(range.finish - range.start + 1)) + -- end + -- vis:redraw() + -- end, 'Complete file name') +-- end)