Configuration of extra allowed globals

This commit is contained in:
adjuvant 2023-08-28 22:31:28 +02:00 committed by XeroOl
parent 6329fb6ea3
commit e4ec4dc9c4
6 changed files with 34 additions and 14 deletions

View File

@ -60,11 +60,15 @@ fennel-ls default settings:
"checks": {
"unused-definition": true,
"unknown-module-field": true
}
},
"extra-globals": ""
}
}
```
extra-globals
: Space separated list of allowed global identifiers; in addition to a set of predefined lua globals.
Your editor can send these settings using one of these two methods:
* The client sends an `initialize` request with the structure `{initializationOptions: {"fennel-ls": {...}}, ...}`
* The client sends a `workspace/didChangeConfiguration` notfication containing the field `{settings: {"fennel-ls": {YOUR_SETTINGS}}}`

View File

@ -263,11 +263,8 @@ later by fennel-ls.language to answer requests from the client."
(local allowed-globals
(icollect [k _ (pairs _G)]
k))
;; just a couple of globals that are probably not errors
;; TODO make this configurable in a better way
(table.insert allowed-globals :vim)
(table.insert allowed-globals :love)
(each [_ v (ipairs (utils.split-spaces self.configuration.extra-globals))]
(table.insert allowed-globals v))
;; TODO clean up this code. It's awful now that there is error handling
(let [macro-file? (= (file.text:sub 1 24) ";; fennel-ls: macro-file")

View File

@ -62,7 +62,6 @@ in the \"self\" object."
;; TODO: set the warning levels of lints
;; allow all globals
;; allow some globals
;; pick from existing libraries of globals (ie love2d)
;; pick between different versions of lua (ie luajit)
;; pick a "compat always" mode that accepts anything if it could be valid in any lua
@ -97,7 +96,8 @@ in the \"self\" object."
:checks {:unused-definition (option true)
:unknown-module-field (option true)
:unnecessary-method (option true)
:bad-unpack (option true)}})
:bad-unpack (option true)}
:extra-globals (option "")})
(λ make-configuration [?c]
(make-configuration-from-template default-configuration ?c))

View File

@ -171,6 +171,10 @@ WARNING: this is only used in the test code, not in the real language server"
(table.insert result new-item)))
result))
(λ split-spaces [str]
(icollect [m (str:gmatch "[^ ]+")]
m))
{: uri->path
: path->uri
: pos->position
@ -181,4 +185,5 @@ WARNING: this is only used in the test code, not in the real language server"
: multi-sym-split
: get-ast-info
: uniq-by
: type=}
: type=
: split-spaces}

View File

@ -60,3 +60,15 @@
(is.not.nil (state.get-by-module self.server :crash-files.test1)))))
; (is.not.nil (searcher.lookup self.server :crash-files.test2))
; (is.not.nil (state.get-by-module self.server :crash-files.test2)))))
(describe "split-spaces"
(it "should split empty string"
(is.same [] (utils.split-spaces "")))
(it "should split single word"
(is.same ["foo"] (utils.split-spaces "foo")))
(it "should trim single word"
(is.same ["foo"] (utils.split-spaces " foo ")))
(it "should split multiple words"
(is.same ["foo-bar" "bar" "baz"] (utils.split-spaces "foo-bar bar baz")))
(it "should split multiple words with arbitrary white space"
(is.same ["foo-bar" "bar" "baz"] (utils.split-spaces " foo-bar bar baz "))))

View File

@ -26,11 +26,13 @@
;; (it "can infer the macro path from fennel-path"
;; (local self (doto [] (setup-server {:fennel-ls {:fennel-path "./?/?.fnl"}}))))
;; (it "can accept an allowed global"
;; (local self (doto [] (setup-server {:fennel-ls {:extra-globals "vim"}}))))
;; (it "can accept a list of allowed globals"
;; (local self (doto [] (setup-server {:fennel-ls {:extra-globals "GAMESTATE,SCREEN_CENTER_X,ETC"}}))))
(it "can set extra allowed globals"
(let [client (create-client {:settings {:fennel-ls {:extra-globals "foo-100 bar"}}})
responses (client:open-file! (.. ROOT-URI :/test.fnl) "(foo-100 bar :baz)")]
(is-matching responses
[{:method :textDocument/publishDiagnostics
:params {:diagnostics [nil]}}]
"bad")))
;; (it "can turn off strict globals"
;; (local self (doto [] (setup-server {:fennel-ls {:checks {:globals false}}}))))