Skip to content

Commit 4b4bacb

Browse files
committed
No more LSP in LanguageServer!
1 parent 3d12d2f commit 4b4bacb

File tree

6 files changed

+56
-53
lines changed

6 files changed

+56
-53
lines changed

modules/language-support/src/main/scala/playground/language/CodeLensProvider.scala

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ case class CodeLens(
7171
command: Command,
7272
)
7373

74+
// note: could be made a bit more type safe sometime
7475
case class Command(
7576
title: String,
7677
command: String,

modules/lsp/src/main/scala/playground/lsp/LanguageServer.scala

+16-19
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import cats.tagless.FunctorK
1212
import cats.tagless.implicits.*
1313
import cats.~>
1414
import io.circe.Json
15-
import org.eclipse.lsp4j
1615
import playground.CompilationError
1716
import playground.CompilationFailed
1817
import playground.FileCompiler
@@ -21,12 +20,14 @@ import playground.OperationCompiler
2120
import playground.PreludeCompiler
2221
import playground.ServiceIndex
2322
import playground.language
23+
import playground.language.CodeLens
2424
import playground.language.CodeLensProvider
2525
import playground.language.CommandProvider
2626
import playground.language.CommandResultReporter
2727
import playground.language.CompletionItem
2828
import playground.language.CompletionProvider
2929
import playground.language.DiagnosticProvider
30+
import playground.language.DocumentSymbol
3031
import playground.language.DocumentSymbolProvider
3132
import playground.language.Feedback
3233
import playground.language.FormattingProvider
@@ -39,9 +40,7 @@ import playground.smithyql.SourceRange
3940
import playground.types.*
4041
import smithy4s.dynamic.DynamicSchemaIndex
4142

42-
import scala.jdk.CollectionConverters.*
43-
44-
// todo: independentize this from lsp4j and move to kernel
43+
// todo: move to kernel
4544
trait LanguageServer[F[_]] {
4645

4746
def initialize[A](workspaceFolders: List[Uri]): F[InitializeResult]
@@ -75,15 +74,15 @@ trait LanguageServer[F[_]] {
7574

7675
def diagnostic(
7776
documentUri: Uri
78-
): F[lsp4j.DocumentDiagnosticReport]
77+
): F[List[LSPDiagnostic]]
7978

8079
def codeLens(
8180
documentUri: Uri
82-
): F[List[lsp4j.CodeLens]]
81+
): F[List[LSPCodeLens]]
8382

8483
def documentSymbol(
8584
documentUri: Uri
86-
): F[List[lsp4j.DocumentSymbol]]
85+
): F[List[LSPDocumentSymbol]]
8786

8887
def didChangeWatchedFiles: F[Unit]
8988

@@ -228,7 +227,7 @@ object LanguageServer {
228227

229228
def diagnostic(
230229
documentUri: Uri
231-
): F[lsp4j.DocumentDiagnosticReport] = TextDocumentManager[F]
230+
): F[List[LSPDiagnostic]] = TextDocumentManager[F]
232231
.get(documentUri)
233232
.map { documentText =>
234233
val diags = diagnosticProvider.getDiagnostics(
@@ -237,18 +236,13 @@ object LanguageServer {
237236

238237
val map = LocationMap(documentText)
239238

240-
new lsp4j.DocumentDiagnosticReport(
241-
new lsp4j.RelatedFullDocumentDiagnosticReport(
242-
diags
243-
.map(converters.toLSP.diagnostic(map, _))
244-
.asJava
245-
)
246-
)
239+
diags
240+
.map(LSPDiagnostic(_, map))
247241
}
248242

249243
def codeLens(
250244
documentUri: Uri
251-
): F[List[lsp4j.CodeLens]] = TextDocumentManager[F]
245+
): F[List[LSPCodeLens]] = TextDocumentManager[F]
252246
.get(documentUri)
253247
.map { documentText =>
254248
val map = LocationMap(documentText)
@@ -258,17 +252,17 @@ object LanguageServer {
258252
documentUri = documentUri,
259253
documentText = documentText,
260254
)
261-
.map(converters.toLSP.codeLens(map, _))
255+
.map(LSPCodeLens(_, map))
262256
}
263257

264258
def documentSymbol(
265259
documentUri: Uri
266-
): F[List[lsp4j.DocumentSymbol]] = TextDocumentManager[F]
260+
): F[List[LSPDocumentSymbol]] = TextDocumentManager[F]
267261
.get(documentUri)
268262
.map { text =>
269263
val map = LocationMap(text)
270264

271-
DocumentSymbolProvider.make(text).map(converters.toLSP.documentSymbol(map, _))
265+
DocumentSymbolProvider.make(text).map(LSPDocumentSymbol(_, map))
272266
}
273267

274268
def didChangeWatchedFiles: F[Unit] = ServerLoader[F]
@@ -357,6 +351,9 @@ case class InitializeResult(
357351
serverInfo: ServerInfo,
358352
)
359353

354+
case class LSPDiagnostic(diagnostic: CompilationError, map: LocationMap)
355+
case class LSPCodeLens(lens: CodeLens, map: LocationMap)
356+
case class LSPDocumentSymbol(sym: DocumentSymbol, map: LocationMap)
360357
case class LSPCompletionItem(item: CompletionItem, map: LocationMap)
361358
case class LSPTextEdit(textEdit: TextEdit, map: LocationMap)
362359

modules/lsp/src/main/scala/playground/lsp/PlaygroundLanguageServerAdapter.scala

+13-5
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,17 @@ final class PlaygroundLanguageServerAdapter[F[_]: Sync](
162162
def diagnostic(
163163
params: lsp4j.DocumentDiagnosticParams
164164
): CompletableFuture[lsp4j.DocumentDiagnosticReport] = d.unsafeToCompletableFuture(
165-
impl.diagnostic(
166-
documentUri = converters.fromLSP.uri(params.getTextDocument)
167-
)
165+
impl
166+
.diagnostic(
167+
documentUri = converters.fromLSP.uri(params.getTextDocument)
168+
)
169+
.map { diags =>
170+
new lsp4j.DocumentDiagnosticReport(
171+
new lsp4j.RelatedFullDocumentDiagnosticReport(
172+
diags.map(converters.toLSP.diagnostic).asJava
173+
)
174+
)
175+
}
168176
)
169177

170178
@JsonRequest("textDocument/codeLens")
@@ -175,7 +183,7 @@ final class PlaygroundLanguageServerAdapter[F[_]: Sync](
175183
.codeLens(
176184
documentUri = converters.fromLSP.uri(params.getTextDocument)
177185
)
178-
.map(_.asJava)
186+
.map(_.map(converters.toLSP.codeLens).asJava)
179187
)
180188

181189
@JsonRequest("workspace/executeCommand")
@@ -213,7 +221,7 @@ final class PlaygroundLanguageServerAdapter[F[_]: Sync](
213221
.unsafeToCompletableFuture(
214222
impl
215223
.documentSymbol(converters.fromLSP.uri(params.getTextDocument()))
216-
.map(_.map(messages.Either.forRight(_)).asJava)
224+
.map(_.map(converters.toLSP.documentSymbol).map(messages.Either.forRight(_)).asJava)
217225
)
218226

219227
@JsonRequest("smithyql/runQuery")

modules/lsp/src/main/scala/playground/lsp/converters.scala

+12-6
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ object converters {
4343
case MessageType.Info => lsp4j.MessageType.Info
4444
}
4545

46-
@deprecated("should be private")
47-
def documentSymbol(
46+
def documentSymbol(sym: LSPDocumentSymbol): lsp4j.DocumentSymbol = documentSymbol(
47+
sym.map,
48+
sym.sym,
49+
)
50+
51+
private def documentSymbol(
4852
map: LocationMap,
4953
sym: DocumentSymbol,
5054
): lsp4j.DocumentSymbol =
@@ -151,8 +155,9 @@ object converters {
151155
new lsp4j.TextEdit(r, what)
152156
}
153157

154-
@deprecated("should be private")
155-
def diagnostic(
158+
def diagnostic(diag: LSPDiagnostic): lsp4j.Diagnostic = diagnostic(diag.map, diag.diagnostic)
159+
160+
private def diagnostic(
156161
map: LocationMap,
157162
diag: CompilationError,
158163
): lsp4j.Diagnostic = new lsp4j.Diagnostic()
@@ -176,8 +181,9 @@ object converters {
176181
)
177182
)
178183

179-
@deprecated("should be private")
180-
def codeLens(
184+
def codeLens(lens: LSPCodeLens): lsp4j.CodeLens = codeLens(lens.map, lens.lens)
185+
186+
private def codeLens(
181187
map: LocationMap,
182188
lens: CodeLens,
183189
): lsp4j.CodeLens = new lsp4j.CodeLens(range(map, lens.range))

modules/lsp/src/test/scala/playground/lsp/LanguageServerIntegrationTestSharedServer.scala

+13-17
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@ package playground.lsp
33
import cats.effect.IO
44
import cats.effect.Resource
55
import com.comcast.ip4s.*
6-
import org.eclipse.lsp4j.CodeLensParams
7-
import org.eclipse.lsp4j.Command
8-
import org.eclipse.lsp4j.CompletionParams
9-
import org.eclipse.lsp4j.DiagnosticSeverity
10-
import org.eclipse.lsp4j.DocumentDiagnosticParams
11-
import org.eclipse.lsp4j.DocumentSymbolParams
12-
import org.eclipse.lsp4j.Position
13-
import org.eclipse.lsp4j.TextDocumentIdentifier
6+
import org.eclipse.lsp4j
147
import org.http4s.HttpRoutes
158
import org.http4s.ember.server.EmberServerBuilder
169
import org.http4s.implicits.*
1710
import org.http4s.server.Server
11+
import playground.CompilationErrorDetails.ParseError
12+
import playground.DiagnosticSeverity
13+
import playground.language.Command
1814
import playground.language.Uri
1915
import playground.lsp.buildinfo.BuildInfo
2016
import playground.lsp.harness.LanguageServerIntegrationTests
2117
import playground.lsp.harness.TestClient
18+
import playground.smithyql.parser.ParsingFailure
2219
import weaver.*
2320

2421
import scala.concurrent.duration.*
@@ -59,12 +56,11 @@ object LanguageServerIntegrationTestSharedServer
5956
documentUri = Uri.fromPath(f.workspaceDir.toPath / "broken.smithyql")
6057
)
6158
.map { report =>
62-
val diagnosticItems =
63-
report.getRelatedFullDocumentDiagnosticReport().getItems().asScala.toList
59+
val diagnosticItems = report
6460

6561
assert(diagnosticItems.size == 1) &&
66-
assert.same(diagnosticItems.head.getSeverity(), DiagnosticSeverity.Error) &&
67-
assert(diagnosticItems.head.getMessage.contains("Parsing failure"))
62+
assert.same(diagnosticItems.head.diagnostic.severity, DiagnosticSeverity.Error) &&
63+
assert(diagnosticItems.head.diagnostic.err.isInstanceOf[ParseError])
6864
}
6965
}
7066

@@ -74,7 +70,7 @@ object LanguageServerIntegrationTestSharedServer
7470
Uri.fromPath(f.workspaceDir.toPath / "demo.smithyql")
7571
)
7672
.map { symbols =>
77-
assert.eql(symbols.map(_.getName()), List("playground.std#Random", "NextUUID"))
73+
assert.eql(symbols.map(_.sym.name), List("playground.std#Random", "NextUUID"))
7874
}
7975
}
8076

@@ -85,14 +81,14 @@ object LanguageServerIntegrationTestSharedServer
8581
)
8682
.map { lenses =>
8783
assert.same(
88-
lenses.map(_.getCommand()),
84+
lenses.map(_.lens.command),
8985
List(
90-
new Command(
86+
Command(
9187
"Run SmithyQL file",
9288
"smithyql.runQuery",
9389
List(
94-
Uri.fromPath(f.workspaceDir.toPath / "demo.smithyql").value: Object
95-
).asJava,
90+
Uri.fromPath(f.workspaceDir.toPath / "demo.smithyql").value
91+
),
9692
)
9793
),
9894
)

modules/lsp/src/test/scala/playground/lsp/LanguageServerReloadIntegrationTests.scala

+1-6
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,7 @@ object LanguageServerReloadIntegrationTests
136136
documentUri = f.workspaceDir / "input.smithyql"
137137
)
138138
.map { diags =>
139-
val items = diags
140-
.getRelatedFullDocumentDiagnosticReport()
141-
.getItems()
142-
.asScala
143-
.toList
144-
.map(_.getMessage())
139+
val items = diags.map(_.diagnostic.err)
145140

146141
assert(errorLogs.isEmpty) &&
147142
assert(items.isEmpty)

0 commit comments

Comments
 (0)