diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2025-01-09 23:17:37 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-01-09 23:17:37 +0100 |
| commit | f04bb93e982060ed27e5cf6e4a8d97edeb0910b6 (patch) | |
| tree | 5dc44812f253699dc3b05be7895169ece61e6453 | |
| parent | 0a1fdd0eb9642e52f985971827beb33d54f21cf4 (diff) | |
| download | vis-lspc-f04bb93e982060ed27e5cf6e4a8d97edeb0910b6.tar.gz vis-lspc-f04bb93e982060ed27e5cf6e4a8d97edeb0910b6.zip | |
extract the upwards find code into the util module
| -rw-r--r-- | init.lua | 25 | ||||
| -rwxr-xr-x | tools/find-upwards (renamed from tools/find-root) | 2 | ||||
| -rw-r--r-- | util.lua | 17 |
3 files changed, 30 insertions, 14 deletions
@@ -1255,25 +1255,24 @@ function LanguageServer:shutdown() self:call_method('shutdown') end -local function get_root_path(ls, file_path) +--- 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 = '' - if ls.roots then - for _, glob in ipairs(ls.roots) do + local roots = ls.roots + if roots then + for _, glob in ipairs(roots) do globs = globs .. glob .. '\n' end end globs = globs .. '.git\n.hg\n' - local status, out = vis:pipe(globs, source_path .. '/tools/find-root "' .. file_path .. '"') - - if status ~= 0 then - return nil - end - - -- Skip trailing newline - return out:sub(1, #out - 1) + local root_path = util.find_upwards(globs, file_path) + return root_path and path_to_uri(root_path) end local function ls_start(ls, init_options) @@ -1307,7 +1306,7 @@ local function ls_start(ls, init_options) local params = { processId = vis_pid, clientInfo = {name = lspc.name, version = lspc.version}, - rootUri = (vis.win.file and path_to_uri(get_root_path(ls, vis.win.file.path))) or lspc.json.null, + rootUri = (vis.win.file and find_root_uri(ls, vis.win.file.path)) or lspc.json.null, capabilities = lspc.client_capabilites, } @@ -1323,13 +1322,13 @@ function LanguageServer.new(ls_conf) 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 = {}, - roots = ls_conf.roots, } setmetatable(ls, {__index = LanguageServer}) diff --git a/tools/find-root b/tools/find-upwards index d2c71ab..5f0edfe 100755 --- a/tools/find-root +++ b/tools/find-upwards @@ -12,7 +12,7 @@ while read -r glob; do cd "$BASEDIR" while [ "$PWD" != "/" ]; do - set -- $glob + set -- "$glob" if [ -e "$1" ]; then echo "$PWD" @@ -7,6 +7,9 @@ -- @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_) @@ -161,4 +164,18 @@ function util.file_line_iterator_to_n(path) 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 = vis:pipe(globs, source_path .. '/tools/find-upwards "' .. start .. '"') + + if status ~= 0 then + return nil + end + + -- Skip trailing newline + return out:sub(1, #out - 1) +end + return util |
