From a122d9d3612d47dd85693a788e40916d48e839c5 Mon Sep 17 00:00:00 2001 From: XeroOl Date: Fri, 17 May 2024 20:45:16 -0500 Subject: [PATCH] Make the root folder optional --- src/fennel-ls/compiler.fnl | 10 ++++++---- src/fennel-ls/searcher.fnl | 28 +++++++++++++++------------- test/utils/client.fnl | 25 +++++++++++++------------ test/utils/init.fnl | 8 ++++---- 4 files changed, 38 insertions(+), 33 deletions(-) diff --git a/src/fennel-ls/compiler.fnl b/src/fennel-ls/compiler.fnl index b3d21d6..fd6d4a9 100644 --- a/src/fennel-ls/compiler.fnl +++ b/src/fennel-ls/compiler.fnl @@ -55,7 +55,7 @@ identifiers are declared / referenced in which places." {:start position :end position})) -(λ compile [{:configuration {: macro-path} : root-uri &as self} file] +(λ compile [{:configuration {: macro-path} :root-uri ?root-uri &as self} file] "Compile the file, and record all the useful information from the compiler into the file object" ;; The useful information being recorded: (let [definitions-by-scope (doto {} (setmetatable has-tables-mt)) @@ -364,14 +364,16 @@ identifiers are declared / referenced in which places." ;; This is bad; we mutate fennel.macro-path (let [old-macro-path fennel.macro-path] - (set fennel.macro-path - (searcher.add-workspaces-to-path macro-path [root-uri])) + (when ?root-uri + (set fennel.macro-path + (searcher.add-workspaces-to-path macro-path [?root-uri]))) ;; compile (each [_i form (ipairs (if macro-file? (ast->macro-ast ast) ast))] (filter-errors :compiler (xpcall #(fennel.compile form opts) fennel.traceback))) - (set fennel.macro-path old-macro-path)) + (when ?root-uri + (set fennel.macro-path old-macro-path))) (each [_ cmd (ipairs defer)] (cmd)) diff --git a/src/fennel-ls/searcher.fnl b/src/fennel-ls/searcher.fnl index 6db669c..6e5c1b1 100644 --- a/src/fennel-ls/searcher.fnl +++ b/src/fennel-ls/searcher.fnl @@ -42,19 +42,21 @@ I suspect this file may be gone after a bit of refactoring." (case (io.open (utils.uri->path uri)) f (do (f:close) true)))) -(λ lookup [{:configuration {: fennel-path} : root-uri &as self} mod] - (let [mod (mod:gsub "%." sep) - root-path (utils.uri->path root-uri)] - (accumulate [uri nil - segment (fennel-path:gmatch "[^;]+") - &until uri] - (let [segment (segment:gsub "%?" mod) - segment (if (absolute? segment) - segment - (join root-path segment)) - segment (utils.path->uri segment)] - (if (file-exists? self segment) - segment))))) +(λ lookup [{:configuration {: fennel-path} :root-uri ?root-uri &as self} mod] + "Use the fennel path to find a file on disk" + (when ?root-uri + (let [mod (mod:gsub "%." sep) + root-path (utils.uri->path ?root-uri)] + (accumulate [uri nil + segment (fennel-path:gmatch "[^;]+") + &until uri] + (let [segment (segment:gsub "%?" mod) + segment (if (absolute? segment) + segment + (join root-path segment)) + segment (utils.path->uri segment)] + (if (file-exists? self segment) + segment)))))) {: lookup : add-workspaces-to-path} diff --git a/test/utils/client.fnl b/test/utils/client.fnl index 6b3c858..28f094f 100644 --- a/test/utils/client.fnl +++ b/test/utils/client.fnl @@ -11,24 +11,25 @@ (.. "file://" ROOT-PATH)) (local default-encoding :utf-8) -(local default-params - {:capabilities {:general {:positionEncodings [default-encoding]}} - :clientInfo {:name "Neovim" :version "0.7.2"} - :initializationOptions {} - :processId 16245 - :rootPath ROOT-PATH - :rootUri ROOT-URI - :trace "off" - :workspaceFolders [{:name ROOT-PATH - :uri ROOT-URI}]}) (local mt {}) -(fn create-client [?opts] +(fn create-client [?opts ?provide-root-uri] (let [self (doto {:server [] :prev-id 1} (setmetatable mt)) + params (or (?. ?opts :params) + {:capabilities {:general {:positionEncodings [default-encoding]}} + :clientInfo {:name "Neovim" :version "0.7.2"} + :initializationOptions {} + :processId 16245 + :rootPath (if ?provide-root-uri ROOT-PATH) + :rootUri (if ?provide-root-uri ROOT-URI) + :trace "off" + :workspaceFolders (if ?provide-root-uri + [{:name ROOT-PATH + :uri ROOT-URI}])}) initialize {:id 1 :jsonrpc "2.0" :method "initialize" - :params (or (?. ?opts :params) default-params)} + : params} result (dispatch.handle* self.server initialize)] (case (?. ?opts :settings) settings diff --git a/test/utils/init.fnl b/test/utils/init.fnl index 48411b8..e9ad63a 100644 --- a/test/utils/init.fnl +++ b/test/utils/init.fnl @@ -34,10 +34,10 @@ result)) (fn create-client-with-files [file-contents ?client-options] - (let [file-contents (if (= (type file-contents) :string) - {:main.fnl file-contents} - file-contents) - self (create-client ?client-options) + (let [(?give-a-root-uri file-contents) (if (= (type file-contents) :string) + (values nil {:main.fnl file-contents}) + (values true file-contents)) + self (create-client ?client-options ?give-a-root-uri) locations []] (each [name marked (pairs file-contents)] (if (not= name :main.fnl)