language-server: first pass at JSON parsers

This commit is contained in:
Liam Fitzgerald 2020-01-03 17:19:16 +11:00
parent f4ed556717
commit ed3fc3f54c
5 changed files with 348 additions and 2 deletions

View File

@ -67,11 +67,15 @@
?+ mark (on-poke:def mark vase)
%handle-http-request
(handle-http-request:lsp !<([eyre-id=@ta inbound-request:eyre] vase))
%json
(handle-json-rpc:lsp !<(json vase))
==
[cards this]
::
++ on-watch
|= =path
?: ?=([%primary ~] path)
`this
?. ?=([%http-response @ ~] path)
(on-watch:def path)
`this
@ -97,8 +101,8 @@
=, dejs:format
|^
%: ot
uri+so
:- %data
method+so
:- %params
%- of
:~ sync+sync
completion+position
@ -137,6 +141,14 @@
^- (list card)
(give-simple-payload:app eyre-id (json-response:gen (json-to-octs jon)))
::
++ handle-json-rpc
|= jon=json
^- (quip card _state)
~& "Received JSON req"
:_ state
[%give %fact `/primary %json !>([%s 'test-json'])]~
::
:: +handle-http-request: received on a new connection established
::
++ handle-http-request

View File

@ -0,0 +1,208 @@
/- lsp=language-server
|%
:: ++ request-by-type
:: |* type=mold
:: $>(type requester:lsp)
::
++ pos-jon
^- json
:- %o
%: malt
['character' %n '3']
['line' %n '5']
~
==
::
++ range-jon
^- json
:- %o
%: malt
['start' pos-jon]
['end' pos-jon]
~
==
::
++ change-jon
^- json
:- %o
%: malt
['text' `json`[%s `@t`'text']]
['rangeLength' [%n '3']]
['range' range-jon]
~
==
::
++ changes-jon
^- json
:- %a
^- (list json)
[change-jon ~]
::
++ text-document-jon
^- json
:- %o
%: malt
['uri' %s 'file://']
['version' %s '1']
~
==
::
++ did-change-jon
^- json
:- %o
%: malt
['contentChanges' changes-jon]
['textDocument' text-document-jon]
~
==
::
++ did-c-event-jon
^- json
:- %o
%: malt
['id' `json`[%s '1']]
['method' `json`[%s `@t`'textDocument/didChange']]
['params' did-change-jon]
~
==
::
++ did-open-jon
^- json
:- %o
%: malt
['textDocument' text-document-item-jon]
~
==
::
++ did-open-event-jon
^- json
:- %o
^- (map cord json)
%: malt
['id' `json`[%s '3']]
['method' `json`[%s 'textDocument/didOpen']]
['params' did-open-jon]
~
==
++ text-document-item-jon
^- json
:- %o
^- (map cord json)
%: malt
['text' %s 'text']
['uri' %s 'file://uri']
['version' %s '1']
~
==
::
++ downcase
|= a=@
(add a 32)
++ parse-method
|= =tape
:: TODO: gross
^- cord
%- crip
%- zing
%+ join "--"
^- (list ^tape)
%+ turn
^- (list (list ^tape))
%+ scan
tape
%+ more
fas
;~ plug
(star low)
(star ;~(plug (cook downcase hig) (star low)))
==
|= words=(list ^tape)
^- ^tape
(zing (join "-" words))
:: ::
++ parse-request
=, dejs-soft:format
|= jon=json
?> ?=([%o *] jon)
=/ method=cord
=+ `json`(~(got by p.jon) 'method')
?> ?=([%s *] -) (parse-method (trip p:-))
=/ id=(unit cord)
%- (lift |=(j=json ?>(?=([%s *] j) p.j)))
`(unit json)`(~(get by p.jon) 'id')
=/ params=json
(~(got by p.jon) 'params')
:: ^- request:lsp
^- request-message:lsp
:- id
?+ method !!
%text-document--did-change
(parse-text-document--did-change params)
%text-document--did-open
(parse-text-document--did-open params)
==
::
++ parse-text-document-item
|= jon=json
^- text-document-item:lsp
%. jon
=, dejs:format
%: ot
uri+so
version+so
text+so
~
==
::
++ parse-text-document--did-change
|= jon=json
^- text-document--did-change:request:lsp ::%text-document--did-change)
:- %text-document--did-change
=, dejs:format
%. jon
%: ot
'textDocument'^parse-text-document-id
'contentChanges'^parse-text-document-changes
~
:: 'contentChanges'
==
++ parse-text-document--did-open
|= jon=json
^- text-document--did-open:request:lsp
=, dejs:format
?> ?=([%o *] jon)
:- %text-document--did-open
(parse-text-document-item (~(got by p.jon) 'textDocument'))
::
++ parse-text-document-id
=, dejs:format
%: ot
uri+so
version+so
~
==
++ parse-text-document-changes
=, dejs:format
%- ar
%: ou
range+(uf ~ (pe ~ range))
'rangeLength'^(uf ~ (pe ~ ni))
text+(un so)
~
==
++ range
=, dejs:format
%: ot
start+position
end+position
~
==
::
++ position
=, dejs:format
%: ot
line+ni
character+ni
~
==
--

View File

@ -0,0 +1,10 @@
/- *language-server
|_ req=request
++ grab
|%
++ noun request
++ json
--
--

View File

@ -0,0 +1,56 @@
|%
::
+$ versioned-doc-id
[uri=@t version=@t]
::
:: ++ request
:: |%
:: +$ text-document--did-change
:: --
:: $%
:: text-document--did-change:request
:: ==
+$ response-kind
[method=cord result=mold]
::
+$ request-message
[id=(unit cord) all:request]
::
++ response
|* kind=response-kind
[id=(unit term) kind]
::
:: ++ notification
:: |* kind=response-kind
:: kind
+$ position
[row=@ud col=@ud]
::
+$ text-document-item
[uri=@t version=@t text=@t]
::
++ request
|%
+$ all
$%
text-document--did-change
text-document--did-open
==
+$ text-document--did-change
[%text-document--did-change versioned-doc-id changes=(list change)]
+$ text-document--did-open
[%text-document--did-open text-document-item]
--
::
+$ change
$: range=(unit range)
range-length=(unit @ud)
text=@t
==
::
+$ range
$: start=position
end=position
==
::
--

View File

@ -0,0 +1,60 @@
:: tests for lsp JSON parsing
/- lsp-types=language-server
/+ *test, *language-server-json
|%
::
++ pos-jon
^- json
:- %o
%: malt
['character' %n '3']
['line' %n '5']
~
==
::
++ range-jon
^- json
:- %o
%: malt
['start' pos-jon]
['end' pos-jon]
~
==
::
++ change-jon
^- json
:- %o
%: malt
['text' `json`[%s `@t`'text']]
['rangeLength' [%n '3']]
['range' range-jon]
~
==
::
++ changes-jon
^- json
:- %a
^- (list json)
[change-jon ~]
::
++ text-document-jon
^- json
:- %o
%: malt
['uri' %s 'file://']
['version' %s '1']
~
==
::
++ test-parse-did-change
%+ expect-eq
!> [%text-document--did-change ['file://' '1'] [[~ [[5 3] [5 3]]] `3 'text']~]
!> %- parse-text-document--did-change
^- json
:- %o
%: malt
['contentChanges' changes-jon]
['textDocument' text-document-jon]
~
==
--