Skip to content

Commit bc8f9f4

Browse files
authored
Merge pull request #19 from CFiggers/dev
v0.0.4
2 parents 80ef23e + 1d44e68 commit bc8f9f4

8 files changed

+111
-82
lines changed

libs/fmt.janet

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
"A list of forms that are control forms and should be indented two spaces."
108108
(invert ["fn" "match" "with" "with-dyns" "def" "def-" "var" "var-" "defn" "defn-"
109109
"varfn" "defmacro" "defmacro-" "defer" "edefer" "loop" "seq" "tabseq" "generate" "coro"
110-
"for" "each" "eachp" "eachk" "case" "cond" "defglobal" "varglobal"
110+
"for" "each" "eachp" "eachk" "case" "cond" "do" "defglobal" "varglobal"
111111
"if" "when" "when-let" "when-with" "while" "with-syms" "with-vars"
112112
"if-let" "if-not" "if-with" "let" "short-fn" "try" "unless" "default" "forever" "upscope"
113113
"repeat" "eachy" "forv" "compwhen" "compif" "ev/spawn" "ev/do-thread" "ev/with-deadline"
@@ -232,4 +232,4 @@
232232
[file]
233233
(def source (slurp file))
234234
(def out (format source))
235-
(spit file out))
235+
(spit file out))

project.janet

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
(declare-project
22
:name "janet-lsp"
33
:description "A Language Server (LSP) for the Janet Programming Language"
4-
:version "0.0.3"
4+
:version "0.0.4"
55
:dependencies ["https://github.com/janet-lang/spork.git"
66
"https://github.com/ianthehenry/judge.git"])
77

8-
(def cflags
9-
(case (os/which)
10-
:windows []
11-
["-s"]))
8+
# (def cflags
9+
# (case (os/which)
10+
# :windows []
11+
# ["-s"]))
1212

13-
(declare-executable
14-
:name "janet-lsp"
15-
:entry "src/main.janet"
16-
:cflags cflags
17-
:install true)
13+
# (declare-executable
14+
# :name "janet-lsp"
15+
# :entry "src/main.janet"
16+
# :cflags cflags
17+
# :install true)
1818

1919
(declare-archive
20-
:name "janet-lsp"
21-
:entry "/src/main")
20+
:name "janet-lsp"
21+
:entry "/src/main")
22+
23+
(declare-binscript
24+
:main "src/janet-lsp"
25+
:hardcode-syspath true
26+
:is-janet true)

src/eval.janet

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
(when ret
6262
(buffer/push-string buf str)
6363
(buffer/push-string buf "\n")))
64+
65+
(setdyn :eval-env (make-env root-env))
6466

6567
(def eval-fiber
6668
(fiber/new

src/janet-lsp

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(import janet-lsp)
2+
3+
(defn main [& args]
4+
(janet-lsp/main ;args))

src/lookup.janet

+29-26
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
(test (peg/match word-peg "") nil)
4545

4646
(defn word-at [location source]
47-
# (logging/log (string/format "word-at received location: %m" location))
48-
# (logging/log (string/format "word-at received source: %m" source))
4947
(let [{:character character-pos :line line-pos} location
5048
line ((string/split "\n" source) line-pos)
5149
parsed (or (sort-by last (or (peg/match word-peg line) @[[0 "" 0]])))
@@ -126,35 +124,40 @@
126124
(let [a 1 b 2]
127125
(first-where |(< (first $) 0) [[-2 :a] [-1 :b] [0 :c]])))
128126
``)
129-
(map |(string/slice sample ($ 0) ($ 1))
130-
@[@[0 14]
131-
@[16 263]
132-
@[50 262]
133-
@[78 261]
134-
@[114 126]
135-
@[134 249]
136-
@[143 156]
137-
@[169 248]
138-
@[175 192]
139-
@[183 191]
140-
@[207 225]
141-
@[216 224]
142-
@[240 247]
143-
@[282 390]
144-
@[304 311]
145-
@[314 389]
146-
@[333 388]
147-
@[347 362]
148-
@[350 359]]))
127+
(test (map |[$ (string/slice sample ($ 0) ($ 1))] @[@[0 14] @[16 263] @[50 262] @[78 261] @[114 126] @[134 249] @[143 156] @[169 248] @[175 192] @[183 191] @[207 225] @[216 224] @[240 247] @[282 390] @[304 311] @[314 389] @[333 388] @[347 362] @[350 359]])
128+
@[[@[0 14] "(import spork)"]
129+
[@[16 263] "(defmacro first-where [pred ds]\n (with-syms [$pred $ds]\n ~(let [,$pred ,pred ,$ds ,ds]\n (var ret \"\")\n (for i 0 (length ,$ds)\n (when (,$pred (,$ds i))\n (set ret (,$ds i))\n (break)))\n ret)))"]
130+
[@[50 262] "(with-syms [$pred $ds]\n ~(let [,$pred ,pred ,$ds ,ds]\n (var ret \"\")\n (for i 0 (length ,$ds)\n (when (,$pred (,$ds i))\n (set ret (,$ds i))\n (break)))\n ret))"]
131+
[@[78 261] "(let [,$pred ,pred ,$ds ,ds]\n (var ret \"\")\n (for i 0 (length ,$ds)\n (when (,$pred (,$ds i))\n (set ret (,$ds i))\n (break)))\n ret)"]
132+
[@[114 126] "(var ret \"\")"]
133+
[@[134 249] "(for i 0 (length ,$ds)\n (when (,$pred (,$ds i))\n (set ret (,$ds i))\n (break)))"]
134+
[@[143 156] "(length ,$ds)"]
135+
[@[169 248] "(when (,$pred (,$ds i))\n (set ret (,$ds i))\n (break))"]
136+
[@[175 192] "(,$pred (,$ds i))"]
137+
[@[183 191] "(,$ds i)"]
138+
[@[207 225] "(set ret (,$ds i))"]
139+
[@[216 224] "(,$ds i)"]
140+
[@[240 247] "(break)"]
141+
[@[282 390] "(defn main [& args]\n (+ 1 1)\n (let [a 1 b 2]\n (first-where |(< (first $) 0) [[-2 :a] [-1 :b] [0 :c]])))"]
142+
[@[304 311] "(+ 1 1)"]
143+
[@[314 389] "(let [a 1 b 2]\n (first-where |(< (first $) 0) [[-2 :a] [-1 :b] [0 :c]]))"]
144+
[@[333 388] "(first-where |(< (first $) 0) [[-2 :a] [-1 :b] [0 :c]])"]
145+
[@[347 362] "(< (first $) 0)"]
146+
[@[350 359] "(first $)"]]))
149147

150148
(defn sexp-at [location source]
151149
(let [{:character character-pos :line line-pos} location
152150
idx (+ character-pos (sum (map (comp inc length) (array/slice (string/split "\n" source) 0 line-pos))))
153-
s-exps (peg/match sexp-peg source)
154-
sexp-range (last (filter |(<= ($ 0) idx ($ 1)) s-exps))]
155-
{:source (string/slice source ;sexp-range) :range sexp-range}))
151+
s-exps (or (peg/match sexp-peg source) @[])]
152+
(if-let [sexp-range (last (filter |(< ($ 0) idx ($ 1)) s-exps))]
153+
{:source (string/slice source ;sexp-range) :range sexp-range}
154+
{:source "" :range @[line-pos character-pos]})))
156155

157-
(test (sexp-at {:character 15 :line 2} "(def a-startup-symbol [])\n\n(import spork/argparse)") {:range @[27 50] :source "(import spork/argparse)"})
156+
(test (sexp-at {:line 2 :character 3} "(def a-startup-symbol [])\n\nsymbol\n\n(import spork/argparse)")
157+
{:range @[2 3] :source ""})
158+
159+
(test (sexp-at {:character 15 :line 2} "(def a-startup-symbol [])\n\n(import spork/argparse)")
160+
{:range @[27 50] :source "(import spork/argparse)"})
158161

159162
(deftest "sexp-at"
160163
(def sample

src/main.janet

+40-30
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@
2323
(defn parse-content-length [input]
2424
(scan-number (string/trim ((string/split ":" input) 1))))
2525

26+
(defn run-diagnostics [uri content]
27+
(let [items @[]
28+
eval-result (eval/eval-buffer content (path/basename uri))]
29+
30+
(each res eval-result
31+
(match res
32+
{:location [line col] :message message}
33+
(array/push items
34+
{:range
35+
{:start {:line (max 0 (dec line)) :character col}
36+
:end {:line (max 0 (dec line)) :character col}}
37+
:message message})))
38+
39+
items))
40+
2641
(defn on-document-change
2742
``
2843
Handler for the ["textDocument/didChange"](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_didChange) event.
@@ -32,31 +47,24 @@
3247
[state params]
3348
(let [content (get-in params ["contentChanges" 0 "text"])
3449
uri (get-in params ["textDocument" "uri"])]
35-
3650
(put-in state [:documents uri] @{:content content})
37-
38-
(pp (eval/eval-buffer content (path/basename uri)))
39-
40-
[:noresponse state]))
41-
51+
52+
(if (dyn :push-diagnostics)
53+
(let [d (run-diagnostics uri content)]
54+
(if (empty? d)
55+
[:noresponse state]
56+
[:ok state {:method "textDocument/publishDiagnostics"
57+
:params {:uri uri
58+
:diagnostics d}} :notify true]))
59+
[:noresponse state])))
4260

4361
(defn on-document-diagnostic [state params]
4462
(let [uri (get-in params ["textDocument" "uri"])
4563
content (get-in state [:documents uri :content])
46-
items @[]
47-
eval-result (eval/eval-buffer content (path/basename uri))]
48-
49-
(each res eval-result
50-
(match res
51-
{:location [line col] :message message}
52-
(array/push items
53-
{:range
54-
{:start {:line (max 0 (dec line)) :character col}
55-
:end {:line (max 0 (dec line)) :character col}}
56-
:message message})))
64+
diagnostics (run-diagnostics uri content)]
5765

5866
[:ok state {:kind "full"
59-
:items items}]))
67+
:items diagnostics}]))
6068

6169
(defn on-document-formatting [state params]
6270
(let [uri (get-in params ["textDocument" "uri"])
@@ -69,7 +77,7 @@
6977
[:ok state :json/null]
7078
(do (put-in state [:documents uri] {:content new-content})
7179
[:ok state [{:range {:start {:line 0 :character 0}
72-
:end {:line 1000000 :character 1000000}}
80+
:end {:line 1000000 :character 1000000}}
7381
:newText new-content}]]))))
7482

7583
(defn on-document-open [state params]
@@ -141,6 +149,12 @@
141149
that this server provides so the client knows what it can request.
142150
``
143151
[state params]
152+
(logging/log (string/format "on-initialize called with these params: %m" params))
153+
154+
(if-let [diagnostic? (get-in params ["capabilities" "textDocument" "diagnostic"])]
155+
(setdyn :push-diagnostics false)
156+
(setdyn :push-diagnostics true))
157+
144158
[:ok state {:capabilities {:completionProvider {:resolveProvider true}
145159
:textDocumentSync {:openClose true
146160
:change 1 # send the Full document https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocumentSyncKind
@@ -182,7 +196,7 @@
182196
(let [id (get message "id")
183197
method (get message "method")
184198
params (get message "params")]
185-
(comment logging/log (string/format "handle-message received method request: %m" method))
199+
(logging/log (string/format "handle-message received method request: %m" method))
186200
(case method
187201
"initialize" (on-initialize state params)
188202
"initialized" [:noresponse state]
@@ -225,19 +239,18 @@
225239
(let [input (file/read stdin :line)
226240
content-length (+ (parse-content-length input) (read-offset))
227241
input (file/read stdin content-length)]
228-
# (print "spork/json and jayson are identical: " (deep= (json/decode input) (jayson/decode input)))
229242
(json/decode input)))
230243

231244
(defn message-loop [&named state]
232245
(let [message (read-message)]
233246
(match (handle-message message state)
234-
[:ok new-state response] (do
247+
[:ok new-state & response] (do
235248
(logging/log "successful rpc")
236-
(write-response stdout (rpc/success-response (get message "id") response))
249+
(write-response stdout (rpc/success-response (get message "id") ;response))
237250
(message-loop :state new-state))
238251
[:noresponse new-state] (message-loop :state new-state)
239252

240-
[:error new-state error] (pp "unhandled error response")
253+
[:error new-state err] (printf "unhandled error response: %m" err)
241254
[:exit] (do (file/flush stdout) (ev/sleep 2) nil))))
242255

243256
(defn find-all-module-files [path &opt search-jpm-tree explicit results]
@@ -271,16 +284,13 @@
271284
(map |(string "./" $))))
272285

273286
(defn start-language-server []
274-
# (setdyn :debug true)
275287
(print "Starting LSP")
276288
(logging/log "Starting LSP")
277289
(when (dyn :debug) (spit "janetlsp.log.txt" ""))
278290

291+
(merge-module root-env jpm-defs nil true)
279292
(setdyn :eval-env (make-env root-env))
280293

281-
# (merge-module (dyn :eval-env) (((curenv) 'module/paths) :value))
282-
(merge-module (dyn :eval-env) jpm-defs)
283-
284294
(each path (find-unique-paths (find-all-module-files (os/cwd) (not ((dyn :opts) :dont-search-jpm-tree))))
285295
(cond
286296
(string/has-suffix? ".janet" path) (array/push (((dyn :eval-env) 'module/paths) :value) [path :source])
@@ -324,7 +334,7 @@
324334
--stdio (flag) "Use STDIO."
325335
[--debug -d] (flag) "Print debug messages."
326336
[--console -c] (flag) "Start a debug console instead of starting the Language Server."
327-
[--debug-port -p] (optional :int++) "What port to start the debug console on. Defaults to 8037."]
337+
[--debug-port -p] (optional :int++) "What port to start or connect to the debug console on. Defaults to 8037."]
328338

329339
(default stdio true)
330340
(default debug-port 8037)
@@ -336,7 +346,7 @@
336346
:debug-port debug-port})
337347

338348
(setdyn :opts opts)
339-
(setdyn :debug debug)
349+
(when debug (setdyn :debug true))
340350
(setdyn :out stderr)
341351

342352
(if console

src/rpc.janet

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# (import spork/json)
22
(import ../libs/jayson)
33

4-
(defn success-response [id result]
5-
(jayson/encode {:jsonrpc "2.0"
6-
:id id
7-
:result result}))
4+
(defn success-response [id result &keys opts]
5+
(def rpc
6+
(if (opts :notify)
7+
(merge {:jsonrpc "2.0"}
8+
result)
9+
{:jsonrpc "2.0"
10+
:id id
11+
:result result}))
12+
(jayson/encode rpc))

test/test-main.janet

+8-8
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@
7878

7979
(deftest "test find-unique-paths"
8080
(test (find-unique-paths (find-all-module-files (os/cwd)))
81-
@["./src/:all:.janet"
82-
"./libs/:all:.janet"
83-
"./test/:all:.janet"
84-
"./build/:all:.jimage"]))
81+
@["./src/:all:.janet"
82+
"./libs/:all:.janet"
83+
"./test/:all:.janet"
84+
"./build/:all:.jimage"]))
8585

8686
(deftest "test find-unique-paths"
8787
(test (find-unique-paths (find-all-module-files (os/cwd) true))
88-
@["./src/:all:.janet"
89-
"./libs/:all:.janet"
90-
"./test/:all:.janet"
91-
"./build/:all:.jimage"]))
88+
@["./src/:all:.janet"
89+
"./libs/:all:.janet"
90+
"./test/:all:.janet"
91+
"./build/:all:.jimage"]))

0 commit comments

Comments
 (0)