From 2c61b5401a775c9a80cf5cd7ba817f26e3c99d9d Mon Sep 17 00:00:00 2001 From: XeroOl Date: Thu, 22 Jun 2023 23:28:17 -0500 Subject: [PATCH] Various changes, and fix a searcher crash --- src/fennel-ls/handlers.fnl | 7 +++---- src/fennel-ls/language.fnl | 19 +++++++++++++------ test/diagnostic-test.fnl | 4 ++-- test/is.fnl | 2 +- test/references-test.fnl | 12 ++++++++---- test/settings-test.fnl | 2 +- test/string-processing-test.fnl | 5 ++--- 7 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/fennel-ls/handlers.fnl b/src/fennel-ls/handlers.fnl index 90b8c0f..eeeaf00 100644 --- a/src/fennel-ls/handlers.fnl +++ b/src/fennel-ls/handlers.fnl @@ -93,10 +93,10 @@ Every time the client sends a message, it gets handled by a function in the corr (if (. file.definitions symbol) (values (. file.definitions symbol) file) (language.search-main self file symbol {:stop-early? true} byte)) - (definition result-file) + ({: referenced-by &as definition} result-file) (let [result - (icollect [_ symbol (ipairs definition.referenced-by)] - ;; TODO we currently assume all references are in the same file + (icollect [_ symbol (ipairs referenced-by)] + ;; TODO I currently assume all references are in the same file (message.range-and-uri symbol result-file))] (if ?include-declaration? (table.insert result @@ -115,7 +115,6 @@ Every time the client sends a message, it gets handled by a function in the corr :range (message.ast->range symbol file)} (catch _ nil)))) - ;; All of the helper functions for textDocument/completion are here until I ;; finish refactoring them, and then they can find a home in language.fnl (λ collect-scope [scope typ callback ?target] diff --git a/src/fennel-ls/language.fnl b/src/fennel-ls/language.fnl index a606380..2480971 100644 --- a/src/fennel-ls/language.fnl +++ b/src/fennel-ls/language.fnl @@ -14,6 +14,7 @@ the data provided by compiler.fnl." (local -let- (sym :let)) (local -fn- (sym :fn)) (local -nil- (sym :nil)) +(local -setmetatable- (sym :setmetatable)) (var search nil) ;; all of the search functions are mutually recursive @@ -61,10 +62,12 @@ the data provided by compiler.fnl." (λ search-list [self file call stack opts] (match call [-require- mod] - (let [newfile (state.get-by-module self mod)] - (when newfile - (let [newitem (. newfile.ast (length newfile.ast))] - (search self newfile newitem stack (doto opts (tset :searched-through-require true)))))) + (when (= :string (type mod)) + (let [newfile (state.get-by-module self mod)] + (when newfile + (let [newitem (. newfile.ast (length newfile.ast))] + (search self newfile newitem stack (doto opts (tset :searched-through-require true))))))) + ;; A . form indexes into item 1 with the other items (where [-dot- & split] (. split 1)) (search self file (. split 1) (stack-add-split! stack split) opts) @@ -76,6 +79,10 @@ the data provided by compiler.fnl." (where [-let- _binding & body] (. body 1)) (search self file (. body (length body)) stack opts) + ;; TODO care about the setmetatable call + (where [-setmetatable- tbl _mt]) + (search self file tbl stack opts) + ;; functions evaluate to "themselves" [-fn-] (values {:definition call} file) ;; BASE CASE !! @@ -91,8 +98,8 @@ the data provided by compiler.fnl." (sym? item) (search-symbol self file item stack opts) (list? item) (search-list self file item stack opts) (= :table (type item)) (search-table self file item stack opts) - (= 0 (length stack)) {:definition item} ;; BASE CASE !! - (error (.. "I don't know what to do with " (view item)))))) + (= 0 (length stack)) {:definition item}))) ;; BASE CASE !! + ;; (error (.. "I don't know what to do with " (view item)))))) (local {:metadata METADATA :scopes {:global {:specials SPECIALS diff --git a/test/diagnostic-test.fnl b/test/diagnostic-test.fnl index f06c237..e7231d2 100644 --- a/test/diagnostic-test.fnl +++ b/test/diagnostic-test.fnl @@ -5,8 +5,8 @@ (local {: ROOT-URI : create-client} (require :test.mock-client)) -(macro find [t body ?sentinel] - (assert-compile (not ?sentinel) "you can only have one thing here, put a `(do)`") +(macro find [t body ?should-be-nil] + (assert-compile (= nil ?should-be-nil) "you can only have one thing here, put a `(do)`") (assert-compile (sequence? t) "[] square brackets please") (local result (gensym :result)) (local nil* (sym :nil)) diff --git a/test/is.fnl b/test/is.fnl index 4cf7038..0e5e25a 100644 --- a/test/is.fnl +++ b/test/is.fnl @@ -1,7 +1,7 @@ ;; this package is here to translate into lust's weird dsl (local {: view} (require :fennel)) (local {: expect} (require :test.lust)) -;; lust uses weird terminology, but equal is by __eq, same is by recursively having the same contents +;; lust uses weird terminology, but what I say is that "equal" is by __eq, "same" is by recursively having the same contents (setmetatable {:equal #(do ((. (expect $1) :to :be) $2) true) :same #(do ((. (expect $1) :to :equal) $2 $3) true) :nil #(do ((. (expect $1) :to_not :exist)) true) diff --git a/test/references-test.fnl b/test/references-test.fnl index dcc3fff..1658b82 100644 --- a/test/references-test.fnl +++ b/test/references-test.fnl @@ -8,14 +8,14 @@ (local filename (.. ROOT-URI "/imaginary-file.fnl")) -(fn check-references [body line col expected] +(fn check-references [body line col ?expected] (let [client (doto (create-client) (: :open-file! filename body)) response (client:references filename line col)] (is-matching response (where [{:jsonrpc "2.0" :id client.prev-id - : result}] - (is.same result expected))))) + :result ?result}] + (is.same ?result ?expected))))) (describe "references" (it "finds a reference from let" @@ -24,4 +24,8 @@ (it "finds a reference from let" (check-references "(let [x 10] x)" 0 6 - [{:uri filename :range (message.pos->range 0 12 0 13)}]))) + [{:uri filename :range (message.pos->range 0 12 0 13)}])) + + (it "doesn't crash here" + (check-references "(let [x nil] x.y)" 0 14 + nil))) diff --git a/test/settings-test.fnl b/test/settings-test.fnl index 0f99bf5..ddca2bc 100644 --- a/test/settings-test.fnl +++ b/test/settings-test.fnl @@ -17,7 +17,7 @@ (it "can set the macro path" (let [client (create-client {:fennel-ls {:macro-path "./?/?.fnl"}}) responses (client:open-file! (.. ROOT-URI :/test.fnl) "(import-macros {: this-is-in-modname} :modname)")] - (print ((. (require :fennel) :view) responses)))) + (assert (not (. responses 1 :params 1)) "if the import-macros fails it generates a diagnostic (for now at least)"))) ;; (it "recompiles modules if the macro files are modified)" diff --git a/test/string-processing-test.fnl b/test/string-processing-test.fnl index 6da6237..6e1a30c 100644 --- a/test/string-processing-test.fnl +++ b/test/string-processing-test.fnl @@ -12,9 +12,8 @@ ;; test for unicode utf8 utf16 nightmare ;; (it "can handle unicode" - ;; (local uri (.. ROOT-URI "test_document")) - ;; (local my-document (document.create uri "")) - ;; (document.replace my-document 0 0 0 0 "どれみふぁそらてぃど") + ;; (utils.apply-changes "" + ;; [{:range (range 0 0 0 0) :text "どれみふぁそらてぃど") ;; (document.replace my-document 0 1 0 3 "😀") ;; (document.replace my-document 0 11 0 11 "end") ;; (is-matching my-document {:text "ど😀ふぁそらてぃどend"})))