From 78da9c2edcd7dc8392e2a8297aeeae0286bf10bd Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Tue, 25 Feb 2025 01:08:07 -0800 Subject: [PATCH] Add support for :intersection as lua version. This will only accept globals which are present in every Lua version from 5.1-5.4. Using this for selflint currently fails, so let's fix that next. --- changelog.md | 1 + src/fennel-ls/docs.fnl | 7 ++++++- test/diagnostic.fnl | 24 +++++++++++++++++++----- test/utils/init.fnl | 4 +++- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/changelog.md b/changelog.md index d284c08..566d2e2 100644 --- a/changelog.md +++ b/changelog.md @@ -4,6 +4,7 @@ ### Features * Settings file: `flsproject.fnl`. Settings are now editor agnostic. +* Support `:intersection` as a Lua version; only includes globals present in every Lua. ### Bug Fixes * (set (. x y) z) wasn't being analyzed properly. diff --git a/src/fennel-ls/docs.fnl b/src/fennel-ls/docs.fnl index 322220b..812e245 100644 --- a/src/fennel-ls/docs.fnl +++ b/src/fennel-ls/docs.fnl @@ -22,9 +22,14 @@ (when (not (. lua-versions version)) (error (.. "fennel-ls doesn't know about lua version " version "\n" "The allowed versions are: " - (fennel.view (doto (icollect [key (pairs lua-versions)] key) table.sort))))) + (fennel.view (doto (icollect [key (pairs lua-versions)] key) + table.sort))))) (. lua-versions version)) +(set lua-versions.intersection + (collect [k v (pairs lua-versions.lua51)] + (if (. lua-versions.lua54 k) (values k v)))) + (local libraries {:tic-80 (require :fennel-ls.docs.generated.tic80)}) diff --git a/test/diagnostic.fnl b/test/diagnostic.fnl index 38afc8e..5c55448 100644 --- a/test/diagnostic.fnl +++ b/test/diagnostic.fnl @@ -20,8 +20,8 @@ (= e.range.end.character d.range.end.character)))) i))) -(fn check [file-contents expected unexpected] - (let [{: diagnostics} (create-client file-contents)] +(fn check [file-contents expected unexpected ?opts] + (let [{: diagnostics} (create-client file-contents nil ?opts)] (each [_ e (ipairs unexpected)] (let [i (find diagnostics e)] (faith.= nil i (.. "Lint matching " (view e) "\n" @@ -120,12 +120,12 @@ (print x +))" [{:message "expected condition and body"} {:message "tried to reference a special form without calling it"}] []) - ;; ;; recovers from missing condition (when) + ;; TODO: recovers from missing condition (when) ;; (check "(let [x (when)] ;; (print x +))" ;; [{:message #($:find ".*macros.fnl:%d+: expected body")} ;; {:message "tried to reference a special form without calling it"}] []) - ;; ;; recovers from missing body (when) + ;; TODO: recovers from missing body (when) ;; (check "(let [x (when (< (+ 9 10) 21))] ;; (print x +))" ;; [;; {:message #($:find ".*macros.fnl:%d+: expected body")} @@ -136,7 +136,21 @@ {:message "tried to reference a special form without calling it"}] []) nil) -{: test-compile-error +(fn test-lua-versions [] + (check "(local t (unpack []))" + [] [{:message "unknown identifier: unpack"}] + {:lua-version "lua51"}) + (check "(local t (unpack []))" + [{:message "unknown identifier: unpack"}] [] + {:lua-version "lua52"}) + ;; intersection should have only the things present in every version + (check "(print (unpack [_VERSION]))" + [{:message "unknown identifier: unpack"}] + [{:message "unknown identifier: _VERSION"}] + {:lua-version "intersection"})) + +{: test-lua-versions + : test-compile-error : test-parse-error : test-macro-error : test-multiple-errors} diff --git a/test/utils/init.fnl b/test/utils/init.fnl index a69f6a5..b3f35a7 100644 --- a/test/utils/init.fnl +++ b/test/utils/init.fnl @@ -41,7 +41,7 @@ (set result.text text) result)) -(fn create-client [file-contents ?opts] +(fn create-client [file-contents ?opts ?config] ;; TODO big function, split up (let [opts (or ?opts {}) (provide-root-uri file-contents) (if (= (type file-contents) :table) @@ -81,6 +81,8 @@ :jsonrpc "2.0" :method "initialize" : params}) + _ (each [k v (pairs (or ?config []))] + (tset server.configuration k v)) [{:params {: diagnostics}}] (client:open-file! uri text)] {: client : server