~xerool/fennel-ls

fennel-ls: Configuration of extra allowed globals v1 PROPOSED

~adjuvant: 1
 Configuration of extra allowed globals

 6 files changed, 34 insertions(+), 14 deletions(-)
#1049051 .build.yml success
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~xerool/fennel-ls/patches/44091/mbox | git am -3
Learn more about email & git

[PATCH fennel-ls] Configuration of extra allowed globals Export this patch

From: adjuvant <adjuvant@mailbox.org>

---
love is not allowed in my projects

 README.md                  |  6 +++++-
 src/fennel-ls/compiler.fnl |  7 ++-----
 src/fennel-ls/state.fnl    |  4 ++--
 src/fennel-ls/utils.fnl    |  7 ++++++-
 test/misc-test.fnl         | 12 ++++++++++++
 test/settings-test.fnl     | 12 +++++++-----
 6 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/README.md b/README.md
index 2e7cf9b..430fe97 100644
--- a/README.md
+++ b/README.md
@@ -52,11 +52,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}}}`
diff --git a/src/fennel-ls/compiler.fnl b/src/fennel-ls/compiler.fnl
index 7d0c2e1..3519b80 100644
--- a/src/fennel-ls/compiler.fnl
+++ b/src/fennel-ls/compiler.fnl
@@ -261,11 +261,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")
diff --git a/src/fennel-ls/state.fnl b/src/fennel-ls/state.fnl
index e4b158b..0d68384 100644
--- a/src/fennel-ls/state.fnl
+++ b/src/fennel-ls/state.fnl
@@ -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
@@ -95,7 +94,8 @@ in the \"self\" object."
   :macro-path (option "./?.fnl;./?/init-macros.fnl;./?/init.fnl;src/?.fnl;src/?/init-macros.fnl;src/?/init.fnl")
   :version (option "lua54")
   :checks {:unused-definition (option true)
            :unknown-module-field (option true)}})
            :unknown-module-field (option true)}
   :extra-globals (option "")})

(λ make-configuration [?c]
  (make-configuration-from-template default-configuration ?c))
diff --git a/src/fennel-ls/utils.fnl b/src/fennel-ls/utils.fnl
index 754512a..6045a41 100644
--- a/src/fennel-ls/utils.fnl
+++ b/src/fennel-ls/utils.fnl
@@ -157,6 +157,10 @@ WARNING: this is only used in the test code, not in the real language server"
(λ type= [val typ]
  (= (type val) typ))

(λ split-spaces [str]
  (icollect [m (str:gmatch "[^ ]+")]
    m))

{: uri->path
 : path->uri
 : pos->position
@@ -166,4 +170,5 @@ WARNING: this is only used in the test code, not in the real language server"
 : apply-edits
 : multi-sym-split
 : get-ast-info
 : type=}
 : type=
 : split-spaces}
diff --git a/test/misc-test.fnl b/test/misc-test.fnl
index bf8d531..5bcd35c 100644
--- a/test/misc-test.fnl
+++ b/test/misc-test.fnl
@@ -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  "))))
diff --git a/test/settings-test.fnl b/test/settings-test.fnl
index 5f84230..8d9ec50 100644
--- a/test/settings-test.fnl
+++ b/test/settings-test.fnl
@@ -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}}}))))
-- 
2.38.5
fennel-ls/patches/.build.yml: SUCCESS in 45s

[Configuration of extra allowed globals][0] from [~adjuvant][1]

[0]: https://lists.sr.ht/~xerool/fennel-ls/patches/44091
[1]: mailto:adjuvant@mailbox.org

✓ #1049051 SUCCESS fennel-ls/patches/.build.yml https://builds.sr.ht/~xerool/job/1049051