Skip to content

Commit b09fc8d

Browse files
authored
Merge pull request #32 from CFiggers:uri-decoding
- Bugfix - Decode URIs before saving to or lookup up from `state`
2 parents 568f434 + 9cddd68 commit b09fc8d

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

src/main.janet

+43-8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,33 @@
5353
(logging/info (string/format "`run-diagnostics` is returning this eval-context: %m" env) [:evaluation] 1)
5454
[items env]))
5555

56+
(def uri-percent-encoding-peg
57+
~{:bang (/ (<- "%21") "!")
58+
:hash (/ (<- "%23") "#")
59+
:dollar (/ (<- "%24") "$")
60+
:amp (/ (<- "%26") "&")
61+
:tick (/ (<- "%27") "'")
62+
:lparen (/ (<- "%28") "(")
63+
:rparen (/ (<- "%29") ")")
64+
:star (/ (<- (* "%2" (+ "A" "a"))) "*")
65+
:plus (/ (<- (* "%2" (+ "B" "b"))) "+")
66+
:comma (/ (<- (* "%2" (+ "C" "c"))) ",")
67+
:slash (/ (<- (* "%2" (+ "F" "f"))) "/")
68+
:colon (/ (<- (* "%3" (+ "A" "a"))) ":")
69+
:semi (/ (<- (* "%3" (+ "B" "b"))) ";")
70+
:equals (/ (<- (* "%3" (+ "D" "d"))) "=")
71+
:question (/ (<- (* "%3" (+ "F" "f"))) "?")
72+
:at (/ (<- "%40") "@")
73+
:lbracket (/ (<- (* "%5" (+ "B" "b"))) "[")
74+
:rbracket (/ (<- (* "%5" (+ "D" "d"))) "]")
75+
:main (% (some (+ :bang :hash :dollar :amp :tick :lparen
76+
:rparen :star :plus :comma :slash :colon
77+
:semi :equals :question :at :lbracket
78+
:rbracket '1)))})
79+
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+
5683
(defn on-document-change
5784
``
5885
Handler for the ["textDocument/didChange"](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_didChange) event.
@@ -61,7 +88,8 @@
6188
``
6289
[state params]
6390
(let [content (get-in params ["contentChanges" 0 "text"])
64-
uri (get-in params ["textDocument" "uri"])]
91+
uri (first (peg/match uri-percent-encoding-peg
92+
(get-in params ["textDocument" "uri"])))]
6593
(put-in state [:documents uri :content] content)
6694

6795
(if (dyn :push-diagnostics)
@@ -75,7 +103,8 @@
75103
[:noresponse state])))
76104

77105
(defn on-document-diagnostic [state params]
78-
(let [uri (get-in params ["textDocument" "uri"])
106+
(let [uri (first (peg/match uri-percent-encoding-peg
107+
(get-in params ["textDocument" "uri"])))
79108
content (get-in state [:documents uri :content])
80109
[diagnostics env] (run-diagnostics uri content)
81110
message {:kind "full"
@@ -85,7 +114,8 @@
85114
[:ok state message]))
86115

87116
(defn on-document-formatting [state params]
88-
(let [uri (get-in params ["textDocument" "uri"])
117+
(let [uri (first (peg/match uri-percent-encoding-peg
118+
(get-in params ["textDocument" "uri"])))
89119
content (get-in state [:documents uri :content])
90120
new-content (freeze (fmt/format (string/slice content)))]
91121
(logging/info (string/format "old content: %m" content) [:formatting] 2)
@@ -105,7 +135,8 @@
105135

106136
(defn on-document-open [state params]
107137
(let [content (get-in params ["textDocument" "text"])
108-
uri (get-in params ["textDocument" "uri"])
138+
uri (first (peg/match uri-percent-encoding-peg
139+
(get-in params ["textDocument" "uri"])))
109140
[diagnostics env] (run-diagnostics uri content)]
110141
(put-in state [:documents uri] @{:content content
111142
:eval-env env})
@@ -139,7 +170,8 @@
139170
:fiber 6 :nil 6)})))
140171

141172
(defn on-completion [state params]
142-
(let [uri (get-in params ["textDocument" "uri"])
173+
(let [uri (first (peg/match uri-percent-encoding-peg
174+
(get-in params ["textDocument" "uri"])))
143175
eval-env (get-in state [:documents uri :eval-env])
144176
bindings (seq [bind :in (all-bindings eval-env)]
145177
(binding-to-lsp-item bind eval-env))
@@ -169,7 +201,8 @@
169201
[:ok state message]))
170202

171203
(defn on-document-hover [state params]
172-
(let [uri (get-in params ["textDocument" "uri"])
204+
(let [uri (first (peg/match uri-percent-encoding-peg
205+
(get-in params ["textDocument" "uri"])))
173206
content (get-in state [:documents uri :content])
174207
eval-env (get-in state [:documents uri :eval-env])
175208
{"line" line "character" character} (get params "position")
@@ -190,7 +223,8 @@
190223
(logging/info (string/format "%q" state) [:signature])
191224
(logging/info (string "on-signature-help params: ") [:signature])
192225
(logging/info (string/format "%q" params) [:signature])
193-
(let [uri (get-in params ["textDocument" "uri"])
226+
(let [uri (first (peg/match uri-percent-encoding-peg
227+
(get-in params ["textDocument" "uri"])))
194228
content (get-in state [:documents uri :content])
195229
eval-env (get-in state [:documents uri :eval-env])
196230
{"line" line "character" character} (get params "position")
@@ -267,7 +301,8 @@
267301
Called by the LSP client to request the location of a symbol's definition.
268302
``
269303
[state params]
270-
(let [request-uri (get-in params ["textDocument" "uri"])
304+
(let [request-uri (first (peg/match uri-percent-encoding-peg
305+
(get-in params ["textDocument" "uri"])))
271306
content (get-in state [:documents request-uri :content])
272307
eval-env (get-in state [:documents request-uri :eval-env])
273308
{"line" line "character" character} (get params "position")

0 commit comments

Comments
 (0)