@@ -9,50 +9,33 @@ import fs2.io.file.Path
99import cats .parse .Parser
1010import QuickmaffsCompiler .CompileError
1111import QuickmaffsCompiler .Index
12- import langoustine .lsp .RuntimeBase .DocumentUri
13- import langoustine .lsp .RuntimeBase .uinteger
12+ import langoustine .lsp .runtime .DocumentUri
13+ import langoustine .lsp .runtime .Opt
14+ import langoustine .lsp .runtime .uinteger
1415
1516import cats .syntax .all .*
17+ import langoustine .lsp .app .LangoustineApp
1618
17- object LSP extends IOApp . Simple :
19+ object LSP extends LangoustineApp :
1820 import QuickmaffsLSP .{server , State }
1921
20- def run =
21- FS2Channel [ IO ]( 2048 , None )
22- .evalTap { channel =>
23- IO .ref( Map .empty[ DocumentUri , State ])
24- .flatMap { state =>
25- server( state).bind(channel)
26- }
22+ def server (
23+ args : List [ String ]
24+ ) : Resource [cats.effect. IO , LSPBuilder [cats.effect. IO ]] =
25+ Resource
26+ .eval( IO .ref( Map .empty[ DocumentUri , State ]))
27+ .map { state =>
28+ QuickmaffsLSP .server(state)
2729 }
28- .flatMap(channel =>
29- fs2.Stream
30- .eval(IO .never) // running the server forever
31- .concurrently(
32- fs2.io
33- .stdin[IO ](512 )
34- .through(lsp.decodePayloads)
35- .through(channel.input)
36- )
37- .concurrently(
38- channel.output
39- .through(lsp.encodePayloads)
40- .through(fs2.io.stdout[IO ])
41- )
42- )
43- .compile
44- .drain
45- .guarantee(IO .consoleForIO.errorln(" Terminating server" ))
46- end run
30+ .onFinalize(IO .consoleForIO.errorln(" Terminating server" ))
4731
4832end LSP
4933
5034object QuickmaffsLSP :
5135 import requests .*
52- import structures .*
5336 import aliases .*
5437 import enumerations .*
55- import json .*
38+ import structures .*
5639
5740 enum State :
5841 case Empty
@@ -68,7 +51,7 @@ object QuickmaffsLSP:
6851
6952 extension (s : cats.parse.Caret )
7053 def toPosition : Position =
71- Position (line = uinteger( s.line) , character = uinteger( s.col) )
54+ Position (line = s.line, character = s.col)
7255
7356 extension (s : Span )
7457 def toRange : Range = Range (s.from.toPosition, s.to.toPosition)
@@ -143,7 +126,7 @@ object QuickmaffsLSP:
143126 )
144127 )
145128 case State .RuntimeError (err) =>
146- val zero = uinteger( 0 )
129+ val zero = 0
147130 publish(
148131 Vector (
149132 Diagnostic (
@@ -210,9 +193,13 @@ object QuickmaffsLSP:
210193 foundMaybe
211194 .map(_._2)
212195 .map { vdf =>
213- Definition (Location (in.textDocument.uri, vdf.definedAt.toRange))
196+ Opt (
197+ Definition (
198+ Location (in.textDocument.uri, vdf.definedAt.toRange)
199+ )
200+ )
214201 }
215- .getOrElse(null )
202+ .getOrElse(Opt .empty )
216203 }
217204 }
218205 .handleRequest(textDocument.hover) { (in, back) =>
@@ -229,39 +216,42 @@ object QuickmaffsLSP:
229216 vdf.fullDefinition.to.offset
230217 )
231218
232- Nullable {
219+ Opt {
233220 Hover (
234221 MarkupContent (
235222 kind = MarkupKind .Markdown ,
236223 s """
237- |` $varName`
238- |---
239- |
240- |**Value**: $value
241- |
242- |**Formula**: $text
243- """ .stripMargin.trim
224+ |` $varName`
225+ |---
226+ |
227+ |**Value**: $value
228+ |
229+ |**Formula**: $text
230+ """ .stripMargin.trim
244231 )
245232 )
246233 }
247234 }
248- .getOrElse(Nullable . NULL )
235+ .getOrElse(Opt .empty )
249236
250- case _ => Nullable . NULL
237+ case _ => Opt .empty
251238 }
252239 }
253240 .handleRequest(textDocument.documentSymbol) { (in, back) =>
254241 get(in.textDocument.uri).map {
255242 case Some (State .Ok (idx, _, _)) =>
256- idx.variables.toVector.sortBy(_._1).map { case (n, df) =>
257- SymbolInformation (
258- location = Location (in.textDocument.uri, df.definedAt.toRange),
259- name = n,
260- kind = enumerations.SymbolKind .Variable
261- )
243+ Opt {
244+ idx.variables.toVector.sortBy(_._1).map { case (n, df) =>
245+ SymbolInformation (
246+ location =
247+ Location (in.textDocument.uri, df.definedAt.toRange),
248+ name = n,
249+ kind = enumerations.SymbolKind .Variable
250+ )
251+ }
262252 }
263253
264- case _ => Vector .empty
254+ case _ => Opt ( Vector .empty)
265255 }
266256 }
267257 .handleRequest(textDocument.rename) { (in, back) =>
@@ -273,7 +263,7 @@ object QuickmaffsLSP:
273263 TextEdit (range = span.toRange, newText = in.newName)
274264 }
275265
276- Nullable {
266+ Opt {
277267 WorkspaceEdit (
278268 changes = Opt (
279269 Map (
@@ -283,7 +273,7 @@ object QuickmaffsLSP:
283273 )
284274 }
285275 }
286- .getOrElse(Nullable . NULL )
276+ .getOrElse(Opt .empty )
287277 }
288278 }
289279 end server
0 commit comments