From 082882009721efe4d4bab0565dc70f7d45ac602b Mon Sep 17 00:00:00 2001 From: XeroOl Date: Sat, 26 Jul 2025 19:10:41 -0500 Subject: [PATCH] completions no longer suggest invalid fields --- src/fennel-ls/compiler.fnl | 13 +++---------- src/fennel-ls/completion.fnl | 25 +++++++++++++------------ src/fennel-ls/lint.fnl | 9 ++------- src/fennel-ls/utils.fnl | 7 ++++++- test/lint.fnl | 4 +++- 5 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/fennel-ls/compiler.fnl b/src/fennel-ls/compiler.fnl index 12aca0f..52f2db6 100644 --- a/src/fennel-ls/compiler.fnl +++ b/src/fennel-ls/compiler.fnl @@ -346,15 +346,8 @@ identifiers are declared / referenced in which places." &into allowed-globals] extra-global) - (fn parse-ast [parser] - (icollect [ok ast parser &until (not ok)] ast)) - - (fn macro-file? [file] - (or (file.uri:match "%.fnlm$") - (= (file.text:sub 1 24) ";; fennel-ls: macro-file"))) - - ;; TODO clean up this code. It's awful now that there is error handling - (let [macro-file? (macro-file? file) + (let [macro-file? (or (file.uri:match "%.fnlm$") + (= (file.text:sub 1 24) ";; fennel-ls: macro-file")) plugin {:name "fennel-ls" :versions ["1.4.1" "1.4.2" "1.5.0" "1.5.1" "1.5.3" "1.5.4"] @@ -395,7 +388,7 @@ identifiers are declared / referenced in which places." (fn _p1 [p2 p3] (filter-errors :parser (xpcall #(p p2 p3) fennel.traceback)))) - ast (parse-ast parser)] + ast (icollect [ok ast parser &until (not ok)] ast)] (λ traverse [ast] "runs on every ast tree that was parsed" diff --git a/src/fennel-ls/completion.fnl b/src/fennel-ls/completion.fnl index fa37610..0cf1ff3 100644 --- a/src/fennel-ls/completion.fnl +++ b/src/fennel-ls/completion.fnl @@ -71,18 +71,19 @@ is set to true and we report that we support completionItem/resolve." (set (. seen definition) true) (add-completion! name definition) (each [field def ?string-method (navigate.iter-fields server definition)] - (if (or (= :self (tostring (?. def :metadata :fnl/arglist 1))) - ?string-method - (and (fennel.list? def.definition) - (or (fennel.sym? (. def.definition 1) "fn") - (fennel.sym? (. def.definition 1) "λ")) - (or (and (fennel.table? (. def.definition 2)) - (fennel.sym? (. def.definition 2 1) "self")) - (and (fennel.sym? (. def.definition 2)) - (fennel.table? (. def.definition 3)) - (fennel.sym? (?. def.definition 3 1) "self"))))) - (add-completion-recursively! (.. name ":" field) def) - (add-completion-recursively! (.. name "." field) def))) + (when (utils.valid-sym-field? field) + (if (or (= :self (tostring (?. def :metadata :fnl/arglist 1))) + ?string-method + (and (fennel.list? def.definition) + (or (fennel.sym? (. def.definition 1) "fn") + (fennel.sym? (. def.definition 1) "λ")) + (or (and (fennel.table? (. def.definition 2)) + (fennel.sym? (. def.definition 2 1) "self")) + (and (fennel.sym? (. def.definition 2)) + (fennel.table? (. def.definition 3)) + (fennel.sym? (?. def.definition 3 1) "self"))))) + (add-completion-recursively! (.. name ":" field) def) + (add-completion-recursively! (.. name "." field) def)))) (set (. seen definition) false))) (fn expression-completions [] diff --git a/src/fennel-ls/lint.fnl b/src/fennel-ls/lint.fnl index d9690c2..2f7644a 100644 --- a/src/fennel-ls/lint.fnl +++ b/src/fennel-ls/lint.fnl @@ -34,11 +34,6 @@ You can read more about how to add lints in docs/linting.md" (table.insert (assert (. lints t) (.. "unknown lint type " t)) lint)) (table.insert (assert (. lints lint.type) (.. "unknown lint type " lint.type)) lint))))) -(fn could-be-rewritten-as-sym? [str] - (and (= :string (type str)) (not (str:find "^%d")) - (not (str:find "[^!$%*+/0-9<=>?A-Z\\^_a-z|\128-\255-]")))) - - (add-lint :unused-definition {:what-it-does "Marks bindings that aren't read. Completely overwriting a value doesn't count @@ -192,7 +187,7 @@ You can read more about how to add lints in docs/linting.md" method (. ast 3)] (if (and (sym? (. ast 1) ":") (sym? object) - (could-be-rewritten-as-sym? method)) + (utils.valid-sym-field? method)) {:range (message.ast->range server file ast) :message (.. "unnecessary : call: use (" (tostring object) ":" method ")") :severity message.severity.WARN})))}) @@ -218,7 +213,7 @@ You can read more about how to add lints in docs/linting.md" (let [all-rewritable? (faccumulate [syms true i 3 (- (length ast) 1) &until (not syms)] - (could-be-rewritten-as-sym? (. ast i)))] + (utils.valid-sym-field? (. ast i)))] (if (and (sym? (. ast 1) "tset") (sym? (. ast 2)) all-rewritable?) diff --git a/src/fennel-ls/utils.fnl b/src/fennel-ls/utils.fnl index 104ea26..55776ea 100644 --- a/src/fennel-ls/utils.fnl +++ b/src/fennel-ls/utils.fnl @@ -215,6 +215,10 @@ WARNING: this is only used in the test code, not in the real language server" suffix)] (.. clean-path clean-suffix)))) +(fn valid-sym-field? [str] + (and (= :string (type str)) + (not (str:find "[^!#$%&*+/0-9<=>?A-Z\\^_a-z|\128-\255-]")))) + (fn find [t x ?k] (match (next t ?k) (k x) k (k y_) (find t x k))) @@ -235,4 +239,5 @@ WARNING: this is only used in the test code, not in the real language server" : path-join : path-sep : endswith - : find} + : find + : valid-sym-field?} diff --git a/test/lint.fnl b/test/lint.fnl index 717b5b2..bb9a85b 100644 --- a/test/lint.fnl +++ b/test/lint.fnl @@ -191,7 +191,9 @@ ;; Lint only triggers on keys that can be written as a sym (check "(local tbl {}) (tset tbl \"hello-world\" 249)" [{:code :unnecessary-tset}]) - (assert-ok "(local tbl {}) (tset tbl \"01234567\" 249)") + ;; symbols like tbl.01234567 *are* valid >:) + (check "(local tbl {}) (tset tbl \"01234567\" 249)" + [{:code :unnecessary-tset}]) (assert-ok "(local tbl {}) (tset tbl \"hello world\" 1)") (assert-ok "(local tbl {}) (tset tbl \"0123.4567\" 1)") nil)