From f04bb93e982060ed27e5cf6e4a8d97edeb0910b6 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Thu, 9 Jan 2025 23:17:37 +0100 Subject: extract the upwards find code into the util module --- init.lua | 25 ++++++++++++------------- tools/find-root | 26 -------------------------- tools/find-upwards | 26 ++++++++++++++++++++++++++ util.lua | 17 +++++++++++++++++ 4 files changed, 55 insertions(+), 39 deletions(-) delete mode 100755 tools/find-root create mode 100755 tools/find-upwards diff --git a/init.lua b/init.lua index 540f487..f2d4f45 100644 --- a/init.lua +++ b/init.lua @@ -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-root deleted file mode 100755 index d2c71ab..0000000 --- a/tools/find-root +++ /dev/null @@ -1,26 +0,0 @@ -#!/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 - -echo "$BASEDIR" diff --git a/tools/find-upwards b/tools/find-upwards new file mode 100755 index 0000000..5f0edfe --- /dev/null +++ b/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 + +echo "$BASEDIR" diff --git a/util.lua b/util.lua index 3bdf227..114aad4 100644 --- a/util.lua +++ b/util.lua @@ -7,6 +7,9 @@ -- @copyright 2024 git-bruh 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 -- cgit v1.2.3