Skip to content

Commit be28878

Browse files
authored
improves test should pick testproject.nim as the main file and provide suggestions (#206)
1 parent 0991328 commit be28878

File tree

1 file changed

+121
-67
lines changed

1 file changed

+121
-67
lines changed

tests/tprojectsetup.nim

Lines changed: 121 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -102,82 +102,136 @@ proc createDidOpenParams(file: string): DidOpenTextDocumentParams =
102102
}
103103
}
104104
105+
106+
template initLangServerForTestProject() {.dirty.}=
107+
cdNewDir testProjectDir:
108+
109+
let (output, exitCode) = execNimbleYes("init")
110+
check exitCode == 0
111+
112+
let pipeServer = createPipe();
113+
let pipeClient = createPipe();
114+
115+
let
116+
server = StreamConnection.new(pipeServer)
117+
inputPipe = asyncPipeInput(pipeClient)
118+
storageDir = ensureStorageDir()
119+
120+
discard registerHandlers(server, inputPipe, storageDir);
121+
discard server.start(inputPipe);
122+
123+
let client = StreamConnection.new(pipeClient);
124+
discard client.start(asyncPipeInput(pipeServer));
125+
126+
let suggestInit = FutureStream[ProgressParams]()
127+
client.register("window/workDoneProgress/create",
128+
partial(testHandler[ProgressParams, JsonNode],
129+
(fut: suggestInit, res: newJNull())))
130+
let workspaceConfiguration = %* [{
131+
"workingDirectoryMapping": [{
132+
"directory": testProjectDir,
133+
"file": entryPoint,
134+
"projectFile": entryPoint
135+
}],
136+
"autoCheckFile": false,
137+
"autoCheckProject": false
138+
}]
139+
140+
let configInit = FutureStream[ConfigurationParams]()
141+
client.register(
142+
"workspace/configuration",
143+
partial(testHandler[ConfigurationParams, JsonNode],
144+
(fut: configInit, res: workspaceConfiguration)))
145+
146+
let diagnostics = FutureStream[PublishDiagnosticsParams]()
147+
client.registerNotification(
148+
"textDocument/publishDiagnostics",
149+
partial(testHandler[PublishDiagnosticsParams], diagnostics))
150+
151+
let progress = FutureStream[ProgressParams]()
152+
client.registerNotification(
153+
"$/progress",
154+
partial(testHandler[ProgressParams], progress))
155+
156+
let showMessage = FutureStream[ShowMessageParams]()
157+
client.registerNotification(
158+
"window/showMessage",
159+
partial(testHandler[ShowMessageParams], showMessage))
160+
161+
let initParams = InitializeParams %* {
162+
"processId": %getCurrentProcessId(),
163+
"capabilities": {
164+
"window": {
165+
"workDoneProgress": true
166+
},
167+
"workspace": {"configuration": true}
168+
}
169+
}
170+
171+
discard waitFor client.call("initialize", %initParams)
172+
client.notify("initialized", newJObject())
173+
174+
105175
suite "nimble setup":
106176
107-
test "should pick `testproject.nim` as the main file":
177+
test "should pick `testproject.nim` as the main file and provide suggestions":
108178
let testProjectDir = absolutePath "tests" / "projects" / "testproject"
109179
let entryPoint = testProjectDir / "src" / "testproject.nim"
110-
cdNewDir testProjectDir:
111-
let (output, exitCode) = execNimbleYes("init")
112-
check exitCode == 0
113-
114-
let pipeServer = createPipe();
115-
let pipeClient = createPipe();
116-
117-
let
118-
server = StreamConnection.new(pipeServer)
119-
inputPipe = asyncPipeInput(pipeClient)
120-
storageDir = ensureStorageDir()
121-
122-
discard registerHandlers(server, inputPipe, storageDir);
123-
discard server.start(inputPipe);
124-
125-
let client = StreamConnection.new(pipeClient);
126-
discard client.start(asyncPipeInput(pipeServer));
127-
128-
let suggestInit = FutureStream[ProgressParams]()
129-
client.register("window/workDoneProgress/create",
130-
partial(testHandler[ProgressParams, JsonNode],
131-
(fut: suggestInit, res: newJNull())))
132-
let workspaceConfiguration = %* [{
133-
"workingDirectoryMapping": [{
134-
"directory": testProjectDir,
135-
"file": entryPoint,
136-
"projectFile": entryPoint
137-
}],
138-
"autoCheckFile": false,
139-
"autoCheckProject": false
140-
}]
141-
142-
let configInit = FutureStream[ConfigurationParams]()
143-
client.register(
144-
"workspace/configuration",
145-
partial(testHandler[ConfigurationParams, JsonNode],
146-
(fut: configInit, res: workspaceConfiguration)))
147-
148-
let diagnostics = FutureStream[PublishDiagnosticsParams]()
149-
client.registerNotification(
150-
"textDocument/publishDiagnostics",
151-
partial(testHandler[PublishDiagnosticsParams], diagnostics))
152-
153-
let progress = FutureStream[ProgressParams]()
154-
client.registerNotification(
155-
"$/progress",
156-
partial(testHandler[ProgressParams], progress))
157-
158-
let showMessage = FutureStream[ShowMessageParams]()
159-
client.registerNotification(
160-
"window/showMessage",
161-
partial(testHandler[ShowMessageParams], showMessage))
162-
163-
let initParams = InitializeParams %* {
164-
"processId": %getCurrentProcessId(),
165-
"capabilities": {
166-
"window": {
167-
"workDoneProgress": true
168-
},
169-
"workspace": {"configuration": true}
170-
}
171-
}
172-
173-
discard waitFor client.call("initialize", %initParams)
174-
client.notify("initialized", newJObject())
180+
181+
initLangServerForTestProject()
175182
# #At this point we should know the main file is `testproject.nim` but for now just test the case were we open it
176183
client.notify("textDocument/didOpen", %createDidOpenParams(entryPoint))
177184
let (_, params) = suggestInit.read.waitFor
178-
# echo "aqui"
179185
let nimsuggestNot = notificationOutputs[^1]["value"]["title"].getStr
180186
check nimsuggestNot == &"Creating nimsuggest for {entryPoint}"
181187

188+
let completionParams = CompletionParams %* {
189+
"position": {
190+
"line": 7,
191+
"character": 0
192+
},
193+
"textDocument": {
194+
"uri": pathToUri(entryPoint)
195+
}
196+
}
197+
#We need to call it twice, so we ignore the first call.
198+
var res = client.call("textDocument/completion", %completionParams).waitFor
199+
res = client.call("textDocument/completion", %completionParams).waitFor
200+
let completionList = res.to(seq[CompletionItem]).mapIt(it.label)
201+
check completionList.len > 0
202+
203+
# test "`submodule.nim` should not be part of the nimble project file":
204+
# let testProjectDir = absolutePath "tests" / "projects" / "testproject"
205+
# let entryPoint = testProjectDir / "src" / "testproject.nim"
206+
207+
# initLangServerForTestProject()
208+
209+
# let submodule = testProjectDir / "src" / "testproject" / "submodule.nim"
210+
# client.notify("textDocument/didOpen", %createDidOpenParams(submodule))
211+
# let (_, params) = suggestInit.read.waitFor
212+
# #Entry point is still the same.
213+
# let nimsuggestNot = notificationOutputs[^1]["value"]["title"].getStr
214+
# check nimsuggestNot == &"Creating nimsuggest for {entryPoint}"
215+
# #Nimsuggest should still be able to give suggestions for the submodule
216+
217+
# let completionParams = CompletionParams %* {
218+
# "position": {
219+
# "line": 8,
220+
# "character": 2
221+
# },
222+
# "textDocument": {
223+
# "uri": pathToUri(submodule)
224+
# }
225+
# }
226+
# #We need to call it twice, so we ignore the first call.
227+
# var res = client.call("textDocument/completion", %completionParams).waitFor
228+
# res = client.call("textDocument/completion", %completionParams).waitFor
229+
# let completionList = res.to(seq[CompletionItem]).mapIt(it.label)
230+
# echo completionList
231+
# # echo actualEchoCompletionItem
232+
233+
234+
235+
182236

183237

0 commit comments

Comments
 (0)