Skip to content

Commit 3d12d2f

Browse files
committed
more lsp4j-free
1 parent 8066526 commit 3d12d2f

File tree

5 files changed

+48
-45
lines changed

5 files changed

+48
-45
lines changed

modules/language-support/src/test/scala/playground/language/DiagnosticProviderTests.scala

+6-7
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ object DiagnosticProviderTests extends SimpleIOSuite {
9595
)
9696

9797
pureTest("empty file - no diagnostics") {
98-
assertNoDiff(provider.getDiagnostics("test.smithyql", ""), Nil)
98+
assertNoDiff(provider.getDiagnostics(""), Nil)
9999
}
100100

101101
pureTest("file doesn't parse at all") {
102102
val input = "horrendous <parsing mistake>"
103103
assertNoDiff(
104-
provider.getDiagnostics("test.smithyql", input),
104+
provider.getDiagnostics(input),
105105
List(
106106
CompilationError(
107107
err = CompilationErrorDetails.ParseError(expectationString = "{"),
@@ -117,7 +117,7 @@ object DiagnosticProviderTests extends SimpleIOSuite {
117117
val input = "AnyOp {}"
118118

119119
assertNoDiff(
120-
provider.getDiagnostics("test.smithyql", input),
120+
provider.getDiagnostics(input),
121121
List(
122122
CompilationError(
123123
err = CompilationErrorDetails.AmbiguousService(
@@ -138,8 +138,7 @@ object DiagnosticProviderTests extends SimpleIOSuite {
138138

139139
assertNoDiff(
140140
provider.getDiagnostics(
141-
"test.smithyql",
142-
input,
141+
input
143142
),
144143
List(
145144
CompilationError.error(
@@ -165,7 +164,7 @@ object DiagnosticProviderTests extends SimpleIOSuite {
165164
|noop#NoRunnerService.Noop {}""".stripMargin
166165

167166
assertNoDiff(
168-
provider.getDiagnostics("test.smithyql", input),
167+
provider.getDiagnostics(input),
169168
List(
170169
CompilationError.info(
171170
err = CompilationErrorDetails.UnsupportedProtocols(
@@ -184,7 +183,7 @@ object DiagnosticProviderTests extends SimpleIOSuite {
184183
"""playground.std#Random.NextUUID {}
185184
|playground.std#Clock.CurrentTimestamp {}""".stripMargin
186185

187-
assertNoDiff(provider.getDiagnostics("test.smithyql", input), Nil)
186+
assertNoDiff(provider.getDiagnostics(input), Nil)
188187
}
189188

190189
}

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

+12-7
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ import playground.language
2424
import playground.language.CodeLensProvider
2525
import playground.language.CommandProvider
2626
import playground.language.CommandResultReporter
27+
import playground.language.CompletionItem
2728
import playground.language.CompletionProvider
2829
import playground.language.DiagnosticProvider
2930
import playground.language.DocumentSymbolProvider
3031
import playground.language.Feedback
3132
import playground.language.FormattingProvider
3233
import playground.language.TextDocumentProvider
34+
import playground.language.TextEdit
3335
import playground.language.Uri
3436
import playground.lsp.buildinfo.BuildInfo
3537
import playground.smithyql.Position
@@ -64,12 +66,12 @@ trait LanguageServer[F[_]] {
6466

6567
def formatting(
6668
documentUri: Uri
67-
): F[List[lsp4j.TextEdit]]
69+
): F[List[LSPTextEdit]]
6870

6971
def completion(
7072
documentUri: Uri,
7173
position: LSPPosition,
72-
): F[Either[List[lsp4j.CompletionItem], lsp4j.CompletionList]]
74+
): F[List[LSPCompletionItem]]
7375

7476
def diagnostic(
7577
documentUri: Uri
@@ -203,14 +205,15 @@ object LanguageServer {
203205

204206
def formatting(
205207
documentUri: Uri
206-
): F[List[lsp4j.TextEdit]] = TextDocumentProvider[F].get(documentUri).flatMap { doc =>
207-
formattingProvider(documentUri).map(_.map(converters.toLSP.textEdit(_, LocationMap(doc))))
208+
): F[List[LSPTextEdit]] = TextDocumentProvider[F].get(documentUri).flatMap { doc =>
209+
val map = LocationMap(doc)
210+
formattingProvider(documentUri).map(_.map(LSPTextEdit(_, map)))
208211
}
209212

210213
def completion(
211214
documentUri: Uri,
212215
position: LSPPosition,
213-
): F[Either[List[lsp4j.CompletionItem], lsp4j.CompletionList]] = TextDocumentManager[F]
216+
): F[List[LSPCompletionItem]] = TextDocumentManager[F]
214217
.get(documentUri)
215218
.map { documentText =>
216219
val map = LocationMap(documentText)
@@ -220,9 +223,8 @@ object LanguageServer {
220223
documentText,
221224
position.unwrap(map),
222225
)
223-
.map(converters.toLSP.completionItem(map, _))
226+
.map(LSPCompletionItem(_, map))
224227
}
225-
.map(Left(_))
226228

227229
def diagnostic(
228230
documentUri: Uri
@@ -355,6 +357,9 @@ case class InitializeResult(
355357
serverInfo: ServerInfo,
356358
)
357359

360+
case class LSPCompletionItem(item: CompletionItem, map: LocationMap)
361+
case class LSPTextEdit(textEdit: TextEdit, map: LocationMap)
362+
358363
case class LSPPosition(line: Int, character: Int) {
359364

360365
def unwrap(map: LocationMap): Position = Position(map.toOffset(line, character).getOrElse(-1))

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ final class PlaygroundLanguageServerAdapter[F[_]: Sync](
134134
.formatting(
135135
documentUri = converters.fromLSP.uri(params.getTextDocument)
136136
)
137-
.map(_.asJava)
137+
.map { edits =>
138+
edits.map(converters.toLSP.textEdit).asJava
139+
}
138140
)
139141

140142
@JsonRequest("textDocument/completion")
@@ -150,10 +152,9 @@ final class PlaygroundLanguageServerAdapter[F[_]: Sync](
150152
position = converters.fromLSP.position(params.getPosition),
151153
)
152154
.map {
153-
_.leftMap(_.asJava).fold(
154-
messages.Either.forLeft(_),
155-
messages.Either.forRight(_),
156-
)
155+
_.map(converters.toLSP.completionItem)
156+
.asJava
157+
.pipe(messages.Either.forLeft(_))
157158
}
158159
)
159160

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

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

46+
@deprecated("should be private")
4647
def documentSymbol(
4748
map: LocationMap,
4849
sym: DocumentSymbol,
@@ -64,7 +65,12 @@ object converters {
6465
case SymbolKind.Package => lsp4j.SymbolKind.Package
6566
}
6667

67-
def completionItem(
68+
def completionItem(item: LSPCompletionItem): lsp4j.CompletionItem = completionItem(
69+
item.map,
70+
item.item,
71+
)
72+
73+
private def completionItem(
6874
map: LocationMap,
6975
item: CompletionItem,
7076
): lsp4j.CompletionItem = {
@@ -128,7 +134,9 @@ object converters {
128134
.tap(_.setSortText(item.sortText.orNull))
129135
}
130136

131-
def textEdit(
137+
def textEdit(edit: LSPTextEdit): lsp4j.TextEdit = textEdit(edit.textEdit, edit.map)
138+
139+
private def textEdit(
132140
edit: TextEdit,
133141
map: LocationMap,
134142
): lsp4j.TextEdit =
@@ -143,6 +151,7 @@ object converters {
143151
new lsp4j.TextEdit(r, what)
144152
}
145153

154+
@deprecated("should be private")
146155
def diagnostic(
147156
map: LocationMap,
148157
diag: CompilationError,
@@ -167,6 +176,7 @@ object converters {
167176
)
168177
)
169178

179+
@deprecated("should be private")
170180
def codeLens(
171181
map: LocationMap,
172182
lens: CodeLens,
@@ -180,14 +190,12 @@ object converters {
180190
)
181191
)
182192

183-
@deprecated
184-
def range(
193+
private def range(
185194
map: LocationMap,
186195
coreRange: SourceRange,
187196
): lsp4j.Range = new lsp4j.Range(position(map, coreRange.start), position(map, coreRange.end))
188197

189-
@deprecated
190-
def position(
198+
private def position(
191199
map: LocationMap,
192200
pos: Position,
193201
): lsp4j.Position = {
@@ -208,14 +216,6 @@ object converters {
208216
def uri(wf: WorkspaceFolder)
209217
: playground.language.Uri = playground.language.Uri.fromUriString(wf.getUri())
210218

211-
@deprecated
212-
def position(
213-
map: LocationMap,
214-
pos: lsp4j.Position,
215-
): Position = Position(
216-
map.toOffset(pos.getLine(), pos.getCharacter()).getOrElse(-1)
217-
)
218-
219219
def position(pos: lsp4j.Position): LSPPosition = LSPPosition(pos.getLine(), pos.getCharacter())
220220

221221
}

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

+10-12
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,16 @@ object LanguageServerIntegrationTestSharedServer
4040
documentUri = Uri.fromPath(f.workspaceDir.toPath / "empty.smithyql"),
4141
position = LSPPosition(0, 0),
4242
)
43-
.map {
44-
case Left(e) =>
45-
assert.same(
46-
e.map(_.getLabel()),
47-
List(
48-
"NextUUID",
49-
"GetWeather",
50-
"Noop",
51-
"CurrentTimestamp",
52-
),
53-
)
54-
case Right(v) => failure(s"Right was found, but Left was expected: $v")
43+
.map { e =>
44+
assert.same(
45+
e.map(_.item.label),
46+
List(
47+
"NextUUID",
48+
"GetWeather",
49+
"Noop",
50+
"CurrentTimestamp",
51+
),
52+
)
5553
}
5654
}
5755

0 commit comments

Comments
 (0)