Skip to content

Commit 0b09519

Browse files
authored
Small refactor (#190)
* Remove undef type for absent fields Nothing in common/json-util.rkt is prefered now. It supports automatic json encode/decode. * Rewrite error codes with enum type, move it to common/interfaces.rkt * Use racket/base lang instead of racket in non test files * Add test for error path * Allow client-wait-response forward error response resolve #168
1 parent 0d24184 commit 0b09519

14 files changed

Lines changed: 93 additions & 68 deletions

File tree

common/interfaces.rkt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
(json-type-out WorkspaceEdit)
2121
(json-type-out CodeAction)
2222
(json-type-out DiagnosticSeverity)
23+
(json-type-out ErrorCode)
2324
(json-type-out Diagnostic)
2425
(json-type-out Location)
2526
(json-type-out DocumentHighlight)
@@ -75,6 +76,21 @@
7576
[Information 3]
7677
[Hint 4])
7778

79+
(define-json-enum ErrorCode
80+
;; Defined by JSON RPC
81+
[ParseError -32700]
82+
[InvalidRequest -32600]
83+
[MethodNotFound -32601]
84+
[InvalidParams -32602]
85+
[InternalError -32603]
86+
[ServerErrorStart -32099]
87+
[ServerErrorEnd -32000]
88+
[ServerNotInitialized -32002]
89+
[UnknownErrorCode -32001]
90+
91+
;; Defined by LSP protocol
92+
[RequestCancelled -32800])
93+
7894
(define-json-struct Diagnostic
7995
[range Range]
8096
[severity DiagnosticSeverity]

common/path-util.rkt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
#lang racket
1+
#lang racket/base
22

33
(provide path->uri
44
uri->path)
55

6-
(require net/url)
6+
(require net/url
7+
racket/string)
78

89
(define path->uri (compose url->string path->url))
910

common/version.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#lang racket
1+
#lang racket/base
22

33
(provide version>=9.0?)
44

doclib/autocomplete/module.rkt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@
1111
; particular means that you must release the source code for the
1212
; modified software. See http://www.gnu.org/copyleft/lesser.html
1313
; for more information.
14-
#lang racket
15-
(require setup/link)
14+
#lang racket/base
15+
16+
(require setup/link
17+
racket/set
18+
racket/match
19+
racket/string
20+
racket/list
21+
racket/path)
1622

1723
(define (collections)
1824
(define h1

doclib/internal-types.rkt

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,6 @@
7070
[right exact-nonnegative-integer?])
7171
#:transparent)
7272

73-
(define undef-object (gensym 'undef))
74-
75-
(define (undef? x)
76-
(eq? x undef-object))
77-
78-
(define (undef/c pred?)
79-
(λ (x)
80-
(or/c (undef? x) (pred? x))))
81-
8273
(define (interval-map-of value/c)
8374
(define value-flat/c (coerce-flat-contract 'interval-map-of value/c))
8475
(define value? (flat-contract-predicate value-flat/c))
@@ -89,7 +80,3 @@
8980
(for/and ([(k value) (in-dict m)])
9081
(value? value))))))
9182

92-
(provide undef?
93-
undef/c
94-
undef-object)
95-

lsp/error-codes.rkt

Lines changed: 0 additions & 18 deletions
This file was deleted.

lsp/methods.rkt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
racket/match
77
racket/class
88
racket/async-channel
9-
"error-codes.rkt"
109
"../common/interfaces.rkt"
1110
"../common/json-util.rkt"
1211
"responses.rkt"
@@ -125,7 +124,7 @@
125124
(define id-ref (hash-ref msg 'id void))
126125
(define id (if ((or/c number? string?) id-ref) id-ref (json-null)))
127126
(define err "The JSON sent is not a valid request object.")
128-
(send-response (error-response id INVALID-REQUEST err))]))
127+
(send-response (error-response id ErrorCode-InvalidRequest err))]))
129128

130129
;; Handle a request. This procedure should always return a jsexpr
131130
;; which is a suitable response object.
@@ -172,7 +171,7 @@
172171
[_
173172
(eprintf "invalid request for method ~v\n" method)
174173
(define err (format "The method ~v was not found" method))
175-
(error-response id METHOD-NOT-FOUND err)])))
174+
(error-response id ErrorCode-MethodNotFound err)])))
176175

177176
;; Handle a notification. Because notifications do not require
178177
;; a response, this procedure always returns void.
@@ -206,7 +205,7 @@
206205
(define ((report-request-error id method) exn)
207206
(eprintf "Caught exn in request ~v\n~a\n" method (exn->string exn))
208207
(define err (format "internal error in method ~v" method))
209-
(error-response id INTERNAL-ERROR err))
208+
(error-response id ErrorCode-InternalError err))
210209

211210
;;
212211
;; Requests
@@ -279,7 +278,7 @@
279278
(set! already-initialized? #t)
280279
resp]
281280
[_
282-
(error-response id INVALID-PARAMS "initialize failed")]))
281+
(error-response id ErrorCode-InvalidParams "initialize failed")]))
283282

284283
(define (shutdown id)
285284
(set! already-shutdown? #t)

lsp/responses.rkt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#lang racket/base
22
(require json
3-
racket/contract/base)
3+
racket/contract/base
4+
"../common/interfaces.rkt"
5+
"../common/json-util.rkt")
46

57
(define not-given (gensym 'not-given))
68

@@ -12,7 +14,7 @@
1214

1315
;; Constructor for a response object representing failure.
1416
(define (error-response id code message [data not-given])
15-
(define err (hasheq 'code code
17+
(define err (hasheq 'code (->jsexpr code)
1618
'message message))
1719
(define err* (if (eq? data not-given)
1820
err
@@ -26,7 +28,7 @@
2628
[success-response
2729
((or/c number? string?) jsexpr? . -> . jsexpr?)]
2830
[error-response
29-
(->* ((or/c number? string? (json-null)) number? string?)
31+
(->* ((or/c number? string? (json-null)) (or/c number? ErrorCode?) string?)
3032
(any/c)
3133
jsexpr?)]))
3234

lsp/text-document.rkt

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
racket/list
55
racket/bool
66
racket/contract
7-
"error-codes.rkt"
87
"../common/interfaces.rkt"
98
"../common/json-util.rkt"
109
"responses.rkt"
@@ -80,7 +79,7 @@
8079
(doc-hover doc pos))))
8180
(success/enc id result)]
8281
[_
83-
(error-response id INVALID-PARAMS "textDocument/hover failed")]))
82+
(error-response id ErrorCode-InvalidParams "textDocument/hover failed")]))
8483

8584
;; Code Action request
8685
(define (code-action id params)
@@ -100,9 +99,9 @@
10099
(doc-code-action doc range))))
101100
(success/enc id actions)]
102101
[(hash-table ['textDocument (DocIdentifier-js #:uri uri)])
103-
(error-response id INVALID-PARAMS
102+
(error-response id ErrorCode-InvalidParams
104103
(format "textDocument/codeAction failed uri is not a path ~a" uri))]
105-
[_ (error-response id INVALID-PARAMS "textDocument/codeAction failed")]))
104+
[_ (error-response id ErrorCode-InvalidParams "textDocument/codeAction failed")]))
106105

107106
;; Signature Help request
108107
(define (signatureHelp id params)
@@ -116,7 +115,7 @@
116115
(doc-signature-help doc pos))))
117116
(success/enc id result)]
118117
[_
119-
(error-response id INVALID-PARAMS "textDocument/signatureHelp failed")]))
118+
(error-response id ErrorCode-InvalidParams "textDocument/signatureHelp failed")]))
120119

121120
;; Completion Request
122121
(define (completion id params)
@@ -129,7 +128,7 @@
129128
(λ (doc) (doc-completion doc pos))))
130129
(success/enc id result)]
131130
[_
132-
(error-response id INVALID-PARAMS "textDocument/completion failed")]))
131+
(error-response id ErrorCode-InvalidParams "textDocument/completion failed")]))
133132

134133
;; Definition request
135134

@@ -143,7 +142,7 @@
143142
(λ (doc) (doc-definition doc uri pos))))
144143
(success/enc id result)]
145144
[_
146-
(error-response id INVALID-PARAMS "textDocument/definition failed")]))
145+
(error-response id ErrorCode-InvalidParams "textDocument/definition failed")]))
147146

148147
;; Reference request
149148
(define (references id params)
@@ -157,7 +156,7 @@
157156
(λ (doc) (doc-references doc uri pos include-decl?))))
158157
(success/enc id result)]
159158
[_
160-
(error-response id INVALID-PARAMS "textDocument/references failed")]))
159+
(error-response id ErrorCode-InvalidParams "textDocument/references failed")]))
161160

162161
;; Document Highlight request
163162
(define (document-highlight id params)
@@ -170,7 +169,7 @@
170169
(λ (doc) (doc-highlights doc pos))))
171170
(success/enc id result)]
172171
[_
173-
(error-response id INVALID-PARAMS "textDocument/documentHighlight failed")]))
172+
(error-response id ErrorCode-InvalidParams "textDocument/documentHighlight failed")]))
174173

175174
;; Rename request
176175
(define (_rename id params)
@@ -184,7 +183,7 @@
184183
(λ (doc) (doc-rename doc uri pos new-name))))
185184
(success/enc id result)]
186185
[_
187-
(error-response id INVALID-PARAMS "textDocument/rename failed")]))
186+
(error-response id ErrorCode-InvalidParams "textDocument/rename failed")]))
188187

189188
;; Prepare rename
190189
(define (prepareRename id params)
@@ -197,7 +196,7 @@
197196
(λ (doc) (doc-prepare-rename doc pos))))
198197
(success/enc id result)]
199198
[_
200-
(error-response id INVALID-PARAMS "textDocument/prepareRename failed")]))
199+
(error-response id ErrorCode-InvalidParams "textDocument/prepareRename failed")]))
201200

202201
;; Document Symbol request
203202
(define (document-symbol id params)
@@ -209,15 +208,15 @@
209208
(λ (doc) (doc-symbols doc uri))))
210209
(success/enc id results)]
211210
[_
212-
(error-response id INVALID-PARAMS "textDocument/documentSymbol failed")]))
211+
(error-response id ErrorCode-InvalidParams "textDocument/documentSymbol failed")]))
213212

214213
;; Inlay Hint
215214
(define (inlay-hint id params)
216215
(match params
217216
[(hash-table ['textDocument (DocIdentifier-js #:uri uri)]
218217
['range (^Range _ _)])
219218
(success/enc id '())]
220-
[_ (error-response id INVALID-PARAMS "textDocument/inlayHint failed")]))
219+
[_ (error-response id ErrorCode-InvalidParams "textDocument/inlayHint failed")]))
221220

222221
;; Full document formatting request
223222
(define (formatting! id params)
@@ -236,7 +235,7 @@
236235
(Range start end)
237236
#:formatting-options opts))))]
238237
[_
239-
(error-response id INVALID-PARAMS "textDocument/formatting failed")]))
238+
(error-response id ErrorCode-InvalidParams "textDocument/formatting failed")]))
240239

241240
;; Range Formatting request
242241
(define (range-formatting! id params)
@@ -251,7 +250,7 @@
251250
id
252251
(doc-format-edits doc range #:formatting-options opts))))]
253252
[_
254-
(error-response id INVALID-PARAMS "textDocument/rangeFormatting failed")]))
253+
(error-response id ErrorCode-InvalidParams "textDocument/rangeFormatting failed")]))
255254

256255
;; On-type formatting request
257256
(define (on-type-formatting! id params)
@@ -287,7 +286,7 @@
287286
#:on-type? #t
288287
#:formatting-options opts))))]
289288
[_
290-
(error-response id INVALID-PARAMS "textDocument/onTypeFormatting failed")]))
289+
(error-response id ErrorCode-InvalidParams "textDocument/onTypeFormatting failed")]))
291290

292291
(define (full-semantic-tokens id params)
293292
(match params
@@ -299,15 +298,15 @@
299298
(Range (doc-abs-pos->pos doc 0)
300299
(doc-abs-pos->pos doc (doc-end-abs-pos doc))))))
301300
(semantic-tokens uri id safe-doc full-range)]
302-
[_ (error-response id INVALID-PARAMS "textDocument/semanticTokens/full failed")]))
301+
[_ (error-response id ErrorCode-InvalidParams "textDocument/semanticTokens/full failed")]))
303302

304303
(define (range-semantic-tokens id params)
305304
(match params
306305
[(hash-table ['textDocument (DocIdentifier-js #:uri uri)]
307306
['range (as-Range range)])
308307
(define safe-doc (lsp-get-doc uri))
309308
(semantic-tokens uri id safe-doc range)]
310-
[_ (error-response id INVALID-PARAMS "textDocument/semanticTokens/range failed")]))
309+
[_ (error-response id ErrorCode-InvalidParams "textDocument/semanticTokens/range failed")]))
311310

312311
(define (semantic-tokens uri id safe-doc range)
313312
(define tokens

lsp/workspace.rkt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
#lang racket
1+
#lang racket/base
22
(provide didRenameFiles
33
didChangeWorkspaceFolders
44
didChangeWatchedFiles
55
didChangeConfiguration
66
update-configuration)
77
(require compiler/module-suffix
8-
json)
8+
json
9+
racket/match)
910
(require "../common/json-util.rkt"
1011
"../common/path-util.rkt"
1112
"../common/interfaces.rkt"

0 commit comments

Comments
 (0)