Skip to content

Commit 3ad3c9b

Browse files
authored
implements extension/runTask (#261)
1 parent 238e968 commit 3ad3c9b

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

nimlangserver.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ proc registerRoutes(srv: RpcSocketServer, ls: LanguageServer) =
6565
srv.register("extension/capabilities", wrapRpc(partial(extensionCapabilities, ls)))
6666
srv.register("extension/suggest", wrapRpc(partial(extensionSuggest, ls)))
6767
srv.register("extension/tasks", wrapRpc(partial(tasks, ls)))
68-
68+
srv.register("extension/runTask", wrapRpc(partial(runTask, ls)))
6969
#Notifications
7070
srv.register("$/cancelRequest", wrapRpc(partial(cancelRequest, ls)))
7171
srv.register("initialized", wrapRpc(partial(initialized, ls)))

protocol/types.nim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,3 +1034,10 @@ type
10341034
NimbleTask* = object
10351035
name*: string
10361036
description*: string
1037+
1038+
RunTaskParams* = object
1039+
command*: seq[string] #command and args
1040+
1041+
RunTaskResult* = object
1042+
command*: seq[string] #command and args
1043+
output*: seq[string] #output lines

routes.nim

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import
1717
chronicles,
1818
asyncprocmonitor,
1919
json_serialization,
20-
std/[strscans, times, json, parseutils],
20+
std/[strscans, times, json, parseutils, strutils],
2121
ls,
2222
stew/[byteutils]
2323

@@ -753,21 +753,23 @@ proc exit*(
753753
result = newJNull()
754754
await p.onExit()
755755
756-
proc tasks*(
757-
ls: LanguageServer, conf: JsonNode
758-
): Future[seq[NimbleTask]] {.async, gcsafe.} =
759-
let rootPath: string = ls.initializeParams.getRootPath
760-
761-
debug "Received tasks ", rootPath = rootPath
762-
delEnv "NIMBLE_DIR"
763-
let process = await startProcess(
756+
proc startNimbleProcess(ls: LanguageServer, args: seq[string]): Future[AsyncProcessRef] {.async.} =
757+
await startProcess(
764758
"nimble",
765-
arguments = @["tasks"],
759+
arguments = args,
766760
options = {UsePath},
767-
workingDir = rootPath,
761+
workingDir = ls.initializeParams.getRootPath,
768762
stdoutHandle = AsyncProcess.Pipe,
769763
stderrHandle = AsyncProcess.Pipe,
770764
)
765+
766+
proc tasks*(
767+
ls: LanguageServer, conf: JsonNode
768+
): Future[seq[NimbleTask]] {.async.} =
769+
let rootPath: string = ls.initializeParams.getRootPath
770+
debug "Received tasks ", rootPath = rootPath
771+
delEnv "NIMBLE_DIR"
772+
let process = await ls.startNimbleProcess(@["tasks"])
771773
let res =
772774
await process.waitForExit(InfiniteDuration) #TODO handle error (i.e. no nimble file)
773775
let output = await process.stdoutStream.readLine()
@@ -778,6 +780,23 @@ proc tasks*(
778780
if name.isWord:
779781
result.add NimbleTask(name: name.strip(), description: desc.strip())
780782

783+
proc runTask*(
784+
ls: LanguageServer, params: RunTaskParams
785+
): Future[RunTaskResult] {.async.} =
786+
let process = await ls.startNimbleProcess(params.command)
787+
let res = await process.waitForExit(InfiniteDuration)
788+
result.command = params.command
789+
let prefix = "\""
790+
while not process.stdoutStream.atEof():
791+
var lines = process.stdoutStream.readLine().await.splitLines
792+
for line in lines.mitems:
793+
if line.startsWith(prefix):
794+
line = line.unescape(prefix)
795+
if line != "":
796+
result.output.add line
797+
798+
debug "Ran nimble cmd/task", command = $params.command, output = $result.output
799+
781800
#Notifications
782801
proc initialized*(ls: LanguageServer, _: JsonNode): Future[void] {.async.} =
783802
debug "Client initialized."

tests/projects/tasks/tasks.nimble

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ bin = @["tasks"]
1414
requires "nim >= 2.1.99"
1515

1616
task helloWorld, "hello world":
17-
echo "helo world"
17+
echo "hello world"
1818

1919
task anotherTask, "Another task":
2020
echo "another task"

0 commit comments

Comments
 (0)