forked from fwcd/kotlin-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLanguageServerTestFixture.kt
164 lines (126 loc) · 6.62 KB
/
LanguageServerTestFixture.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package org.javacs.kt
import org.eclipse.lsp4j.*
import org.eclipse.lsp4j.services.LanguageClient
import org.junit.Before
import org.junit.After
import java.nio.file.Path
import java.nio.file.Paths
import java.util.concurrent.CompletableFuture
abstract class LanguageServerTestFixture(relativeWorkspaceRoot: String) : LanguageClient {
val workspaceRoot = absoluteWorkspaceRoot(relativeWorkspaceRoot)
val languageServer = createLanguageServer()
val diagnostics = mutableListOf<Diagnostic>()
fun absoluteWorkspaceRoot(relativeWorkspaceRoot: String): Path {
val testResources = testResourcesRoot()
return testResources.resolve(relativeWorkspaceRoot)
}
private fun createLanguageServer(): KotlinLanguageServer {
val languageServer = KotlinLanguageServer()
val init = InitializeParams().apply {
capabilities = ClientCapabilities().apply {
textDocument = TextDocumentClientCapabilities().apply {
completion = CompletionCapabilities().apply {
completionItem = CompletionItemCapabilities().apply {
snippetSupport = true
}
}
}
}
}
init.workspaceFolders = listOf(WorkspaceFolder().apply {
name = workspaceRoot.fileName.toString()
uri = workspaceRoot.toUri().toString()
})
languageServer.sourcePath.indexEnabled = false
languageServer.connect(this)
languageServer.initialize(init).join()
return languageServer
}
@After fun closeLanguageServer() {
languageServer.close()
}
@After fun printMemoryUsage() {
val rt = Runtime.getRuntime()
val total = rt.totalMemory().toDouble() / 1000000.0
val free = rt.freeMemory().toDouble() / 1000000.0
println("Memory after test: ${total - free} MB used / $total MB total")
}
fun renameParams(relativePath: String, line: Int, column: Int, newName: String): RenameParams =
textDocumentPosition(relativePath, line, column).run { RenameParams(textDocument, position, newName) }
fun completionParams(relativePath: String, line: Int, column: Int): CompletionParams {
val file = workspaceRoot.resolve(relativePath)
val fileId = TextDocumentIdentifier(file.toUri().toString())
val position = position(line, column)
return CompletionParams(fileId, position)
}
fun textDocumentPosition(relativePath: String, line: Int, column: Int): TextDocumentPositionParams =
textDocumentPosition(relativePath, position(line, column))
fun hoverParams(relativePath: String, line: Int, column: Int): HoverParams =
textDocumentPosition(relativePath, line, column).run { HoverParams(textDocument, position) }
fun semanticTokensParams(relativePath: String): SemanticTokensParams =
textDocumentPosition(relativePath, 0, 0).run { SemanticTokensParams(textDocument) }
fun semanticTokensRangeParams(relativePath: String, range: Range): SemanticTokensRangeParams =
textDocumentPosition(relativePath, 0, 0).run { SemanticTokensRangeParams(textDocument, range) }
fun signatureHelpParams(relativePath: String, line: Int, column: Int): SignatureHelpParams =
textDocumentPosition(relativePath, line, column).run { SignatureHelpParams(textDocument, position) }
fun definitionParams(relativePath: String, line: Int, column: Int): DefinitionParams =
textDocumentPosition(relativePath, line, column).run { DefinitionParams(textDocument, position) }
fun definitionParams(relativePath: String, position: Position): DefinitionParams =
textDocumentPosition(relativePath, position).run { DefinitionParams(textDocument, position) }
fun textDocumentPosition(relativePath: String, position: Position): TextDocumentPositionParams {
val file = workspaceRoot.resolve(relativePath)
val fileId = TextDocumentIdentifier(file.toUri().toString())
return TextDocumentPositionParams(fileId, position)
}
fun position(line: Int, column: Int) = Position(line - 1, column - 1)
fun range(startLine: Int, startColumn: Int, endLine: Int, endColumn: Int) =
Range(position(startLine, startColumn), position(endLine, endColumn))
fun uri(relativePath: String) =
workspaceRoot.resolve(relativePath).toUri()
fun referenceParams(relativePath: String, line: Int, column: Int): ReferenceParams =
ReferenceParams(
TextDocumentIdentifier(uri(relativePath).toString()),
position(line, column),
ReferenceContext(true)
)
fun open(relativePath: String) {
val file = workspaceRoot.resolve(relativePath)
val content = file.toFile().readText()
val document = TextDocumentItem(file.toUri().toString(), "Kotlin", 0, content)
languageServer.textDocumentService.didOpen(DidOpenTextDocumentParams(document))
}
private var version = 1
fun replace(relativePath: String, line: Int, char: Int, oldText: String, newText: String) {
val range = Range(position(line, char), Position(line - 1, char - 1 + oldText.length))
val edit = TextDocumentContentChangeEvent(range, oldText.length, newText)
val doc = VersionedTextDocumentIdentifier(uri(relativePath).toString(), version++)
languageServer.textDocumentService.didChange(DidChangeTextDocumentParams(doc, listOf(edit)))
}
// LanguageClient functions
override fun publishDiagnostics(diagnostics: PublishDiagnosticsParams) {
this.diagnostics.addAll(diagnostics.diagnostics)
}
override fun showMessageRequest(request: ShowMessageRequestParams?): CompletableFuture<MessageActionItem>? {
println(request.toString())
return null
}
override fun telemetryEvent(`object`: Any?) {
println(`object`.toString())
}
override fun logMessage(message: MessageParams?) = printMessage(message)
override fun showMessage(message: MessageParams?) = printMessage(message)
private fun printMessage(message: MessageParams?) {
println("[${message?.type}] ${message?.message}")
}
}
fun testResourcesRoot(): Path {
val anchorTxt = LanguageServerTestFixture::class.java.getResource("/Anchor.txt").toURI()
return Paths.get(anchorTxt).parent!!
}
open class SingleFileTestFixture(relativeWorkspaceRoot: String, val file: String) : LanguageServerTestFixture(relativeWorkspaceRoot) {
@Before fun openFile() {
open(file)
// Wait for lint, so subsequent replace(...) operations cause recovery
languageServer.textDocumentService.debounceLint.waitForPendingTask()
}
}