Skip to content

Commit b4c2158

Browse files
authored
Merge pull request #34 from CFiggers:dev
v0.0.9
2 parents e284281 + 4752833 commit b4c2158

File tree

2 files changed

+52
-41
lines changed

2 files changed

+52
-41
lines changed

src/main.janet

+49-41
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313

1414
(use judge)
1515

16-
(def version "0.0.7")
16+
(def version "0.0.9")
1717
(def commit
1818
(with [proc (os/spawn ["git" "rev-parse" "--short" "HEAD"] :xp {:out :pipe})]
19-
(let [[out] (ev/gather
20-
(ev/read (proc :out) :all)
21-
(os/proc-wait proc))]
22-
(if out (string/trimr out) ""))))
19+
(let [[out] (ev/gather
20+
(ev/read (proc :out) :all)
21+
(os/proc-wait proc))]
22+
(if out (string/trimr out) ""))))
2323

2424
(def jpm-defs (require "../libs/jpm-defs"))
2525

@@ -34,10 +34,10 @@
3434
[diagnostics env]
3535
(eval/eval-buffer content
3636
(path/relpath
37-
(os/cwd)
38-
(if (string/has-prefix? "file:" uri)
39-
(string/slice uri 5) uri)))]
40-
37+
(os/cwd)
38+
(if (string/has-prefix? "file:" uri)
39+
(string/slice uri 5) uri)))]
40+
4141
(logging/info (string/format "`eval-buffer` returned: %m" diagnostics) [:evaluation])
4242

4343
(each res diagnostics
@@ -77,9 +77,6 @@
7777
:semi :equals :question :at :lbracket
7878
:rbracket '1)))})
7979

80-
(test (peg/match uri-percent-encoding-peg "file:///c%3A/Users/pete/Desktop/code/libmpsse")
81-
@["file:///c:/Users/pete/Desktop/code/libmpsse"])
82-
8380
(defn on-document-change
8481
``
8582
Handler for the ["textDocument/didChange"](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_didChange) event.
@@ -89,7 +86,7 @@
8986
[state params]
9087
(let [content (get-in params ["contentChanges" 0 "text"])
9188
uri (first (peg/match uri-percent-encoding-peg
92-
(get-in params ["textDocument" "uri"])))]
89+
(get-in params ["textDocument" "uri"])))]
9390
(put-in state [:documents uri :content] content)
9491

9592
(if (dyn :push-diagnostics)
@@ -104,7 +101,7 @@
104101

105102
(defn on-document-diagnostic [state params]
106103
(let [uri (first (peg/match uri-percent-encoding-peg
107-
(get-in params ["textDocument" "uri"])))
104+
(get-in params ["textDocument" "uri"])))
108105
content (get-in state [:documents uri :content])
109106
[diagnostics env] (run-diagnostics uri content)
110107
message {:kind "full"
@@ -115,7 +112,7 @@
115112

116113
(defn on-document-formatting [state params]
117114
(let [uri (first (peg/match uri-percent-encoding-peg
118-
(get-in params ["textDocument" "uri"])))
115+
(get-in params ["textDocument" "uri"])))
119116
content (get-in state [:documents uri :content])
120117
new-content (freeze (fmt/format (string/slice content)))]
121118
(logging/info (string/format "old content: %m" content) [:formatting] 2)
@@ -125,7 +122,7 @@
125122
(do
126123
(logging/info "No changes" [:formatting])
127124
[:ok state :json/null])
128-
(do
125+
(do
129126
(put-in state [:documents uri] @{:content new-content})
130127
(let [message [{:range {:start {:line 0 :character 0}
131128
:end {:line 1000000 :character 1000000}}
@@ -136,7 +133,7 @@
136133
(defn on-document-open [state params]
137134
(let [content (get-in params ["textDocument" "text"])
138135
uri (first (peg/match uri-percent-encoding-peg
139-
(get-in params ["textDocument" "uri"])))
136+
(get-in params ["textDocument" "uri"])))
140137
[diagnostics env] (run-diagnostics uri content)]
141138
(put-in state [:documents uri] @{:content content
142139
:eval-env env})
@@ -171,9 +168,9 @@
171168

172169
(defn on-completion [state params]
173170
(let [uri (first (peg/match uri-percent-encoding-peg
174-
(get-in params ["textDocument" "uri"])))
171+
(get-in params ["textDocument" "uri"])))
175172
eval-env (get-in state [:documents uri :eval-env])
176-
bindings (seq [bind :in (all-bindings eval-env)]
173+
bindings (seq [bind :in (all-bindings eval-env)]
177174
(binding-to-lsp-item bind eval-env))
178175
message {:isIncomplete true
179176
:items bindings}]
@@ -185,24 +182,24 @@
185182
(def lbl (get params "label"))
186183
(def envs (seq [docu :in (state :documents)]
187184
(docu :eval-env)))
188-
185+
189186
(each env envs
190187
(when (env (symbol lbl))
191188
(set eval-env env)
192189
(break)))
193-
190+
194191
(let [message {:label lbl
195192
:documentation
196193
{:kind "markdown"
197194
:value (doc/my-doc*
198-
(symbol lbl)
199-
(or eval-env (make-env root-env)))}}]
195+
(symbol lbl)
196+
(or eval-env (make-env root-env)))}}]
200197
(logging/message message [:completion] 1)
201198
[:ok state message]))
202199

203200
(defn on-document-hover [state params]
204201
(let [uri (first (peg/match uri-percent-encoding-peg
205-
(get-in params ["textDocument" "uri"])))
202+
(get-in params ["textDocument" "uri"])))
206203
content (get-in state [:documents uri :content])
207204
eval-env (get-in state [:documents uri :eval-env])
208205
{"line" line "character" character} (get params "position")
@@ -224,17 +221,17 @@
224221
(logging/info (string "on-signature-help params: ") [:signature])
225222
(logging/info (string/format "%q" params) [:signature])
226223
(let [uri (first (peg/match uri-percent-encoding-peg
227-
(get-in params ["textDocument" "uri"])))
224+
(get-in params ["textDocument" "uri"])))
228225
content (get-in state [:documents uri :content])
229226
eval-env (get-in state [:documents uri :eval-env])
230227
{"line" line "character" character} (get params "position")
231228
{:source sexp-text :range [start end]} (lookup/sexp-at {:line line :character character} content)
232229
function-symbol (or (first (peg/match '(* "(" (any :s) (<- (to " "))) sexp-text)) "none")
233230
signature (or (doc/get-signature (symbol function-symbol) eval-env) "not found")]
234231
(case signature
235-
"not found"
232+
"not found"
236233
(do (logging/info "No signature found" [:signature]) [:ok state :json/null])
237-
(let [message {:signatures [{:label signature}]}]
234+
(let [message {:signatures [{:label signature}]}]
238235
(logging/message message [:signature])
239236
[:ok state message]))))
240237

@@ -292,7 +289,7 @@
292289
[state params]
293290
(let [message {:server-info {:name "janet-lsp"
294291
:version version
295-
:commit commit}}]
292+
:commit commit}}]
296293
(logging/message message [:info] 1)
297294
[:ok state message]))
298295

@@ -302,7 +299,7 @@
302299
``
303300
[state params]
304301
(let [request-uri (first (peg/match uri-percent-encoding-peg
305-
(get-in params ["textDocument" "uri"])))
302+
(get-in params ["textDocument" "uri"])))
306303
content (get-in state [:documents request-uri :content])
307304
eval-env (get-in state [:documents request-uri :eval-env])
308305
{"line" line "character" character} (get params "position")
@@ -318,7 +315,7 @@
318315
end is: %d
319316
-------------------------
320317
``
321-
request-uri (length content) line character define-word start end) [:definition] 2)
318+
request-uri (length content) line character define-word start end) [:definition] 2)
322319
(logging/info (string/format "symbol is: %s" (symbol define-word)) [:definition] 2)
323320
(logging/info (string/format "eval-env is: %m" eval-env) [:definition] 3)
324321
(logging/info (string/format "symbol lookup is: %m" (get eval-env (symbol define-word) nil)) [:definition] 2)
@@ -330,12 +327,12 @@
330327
message {:uri filepath
331328
:range {:start {:line (max 0 (dec line)) :character col}
332329
:end {:line (max 0 (dec line)) :character col}}}]
333-
(do
334-
(logging/message message [:definition])
335-
[:ok state message])
336-
(do
337-
(logging/info "Couldn't find definition" [:definition])
338-
[:ok state :json/null]))))
330+
(do
331+
(logging/message message [:definition])
332+
[:ok state message])
333+
(do
334+
(logging/info "Couldn't find definition" [:definition])
335+
[:ok state :json/null]))))
339336

340337
(defn on-set-trace [state params]
341338
(logging/info (string/format "on-set-trace: %m" params) [:settrace] 2)
@@ -345,6 +342,13 @@
345342
"verbose" nil)
346343
[:noresponse state])
347344

345+
(defn on-janet-tell-joke [state params]
346+
# (eprint "What's brown and sticky? A stick!")
347+
(let [message {:question "What's brown and sticky?"
348+
:answer "A stick!"}]
349+
(logging/message message [:joke])
350+
[:ok state message]))
351+
348352
(defn handle-message [message state]
349353
(let [id (get message "id")
350354
method (get message "method")
@@ -365,10 +369,11 @@
365369
# "textDocument/documentSymbol" (on-document-symbols state params) TODO: Implement this? See src/lsp/api.ts:121
366370
"textDocument/definition" (on-document-definition state params)
367371
"janet/serverInfo" (on-janet-serverinfo state params)
372+
"janet/tellJoke" (on-janet-tell-joke state params)
368373
"shutdown" (on-shutdown state params)
369374
"exit" (on-exit state params)
370375
"$/setTrace" (on-set-trace state params)
371-
(do
376+
(do
372377
(logging/info (string/format "Received unrecognized RPC: %m" method) [:handle] 1)
373378
[:noresponse state]))))
374379

@@ -395,14 +400,17 @@
395400
(logging/info (string/format "current state is: %m" state) [:priority] 3)
396401
(let [message (read-message)]
397402
(logging/info (string/format "got: %q" message) [:core] 3)
398-
(match (handle-message message state)
403+
(match (try (handle-message message state) ([err fib] [:error state err fib]))
399404
[:ok new-state & response] (do
400405
(write-response stdout (rpc/success-response (get message "id") ;response))
401406
(logging/info "successful rpc" [:core] (get message "id"))
402407
(message-loop :state new-state))
403408
[:noresponse new-state] (message-loop :state new-state)
404409

405-
[:error new-state err] (printf "unhandled error response: %m" err)
410+
[:error new-state err fib] (do
411+
(logging/err (string/format "%m" err) [:core])
412+
(debug/stacktrace fib err "")
413+
(message-loop :state new-state))
406414
[:exit] (do (file/flush stdout) (ev/sleep 0.1) (os/exit 0)))))
407415

408416
(defn find-all-module-files [path &opt search-jpm-tree explicit results]
@@ -472,8 +480,8 @@
472480
(when (or (has-value? parsed-args "--version")
473481
(has-value? parsed-args "-v"))
474482
(print "Janet LSP v" version "-" commit)
475-
(os/exit 0))
476-
483+
(os/exit 0))
484+
477485
(cmd/run
478486
(cmd/fn
479487
"A Language Server (LSP) for the Janet Programming Language."

test/test-main.janet

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
(test (main/parse-content-length "123:456:789") 456)
88
(test (main/parse-content-length "0123:456::::789") 456))
99

10+
(test (peg/match uri-percent-encoding-peg "file:///c%3A/Users/pete/Desktop/code/libmpsse")
11+
@["file:///c:/Users/pete/Desktop/code/libmpsse"])
12+
1013
(deftest "test binding-to-lsp-item"
1114
(def eval-env (table/proto-flatten (make-env root-env)))
1215

0 commit comments

Comments
 (0)