Skip to content

Commit

Permalink
Adds formatting support when nph is in path
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgomez committed Sep 19, 2024
1 parent 78bc646 commit 4978c3c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions nimlangserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ proc registerRoutes(srv: RpcSocketServer, ls: LanguageServer) =
srv.register("textDocument/rename", ls.addRpcToCancellable(wrapRpc(partial(rename, ls))))
srv.register("textDocument/inlayHint", ls.addRpcToCancellable(wrapRpc(partial(inlayHint, ls))))
srv.register("textDocument/signatureHelp", ls.addRpcToCancellable(wrapRpc(partial(signatureHelp, ls))))
srv.register("textDocument/formatting", ls.addRpcToCancellable(wrapRpc(partial(formatting, ls))))
srv.register("workspace/executeCommand", wrapRpc(partial(executeCommand, ls)))
srv.register("workspace/symbol", ls.addRpcToCancellable(wrapRpc(partial(workspaceSymbol, ls))))
srv.register(
Expand Down
40 changes: 38 additions & 2 deletions routes.nim
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import macros, strformat, chronos,
import macros, strformat, chronos, chronos/asyncproc,
json_rpc/server, os, sugar, sequtils,
suggestapi, protocol/enums, protocol/types, with, tables, strutils,
./utils, chronicles,
asyncprocmonitor, json_serialization,
std/[strscans, times, json, parseutils], ls

proc getNphPath(): Option[string] =
let path = findExe "nph"
if path == "": none(string)
else: some path

#routes
proc initialize*(p: tuple[ls: LanguageServer, onExit: OnExitCallback], params: InitializeParams):
Future[InitializeResult] {.async.} =
Expand Down Expand Up @@ -71,7 +76,8 @@ proc initialize*(p: tuple[ls: LanguageServer, onExit: OnExitCallback], params: I
resolveProvider: some(false)
)),
documentSymbolProvider: some(true),
codeActionProvider: some(true)
codeActionProvider: some(true),
documentFormattingProvider: some(getNphPath().isSome)
)
)
# Support rename by default, but check if we can also support prepare
Expand Down Expand Up @@ -606,6 +612,36 @@ proc signatureHelp*(ls: LanguageServer, params: SignatureHelpParams, id: int):
else:
return none[SignatureHelp]()


proc format*(nphPath, filePath: string): Future[Option[TextEdit]] {.async.} =
debug "nph starts", nphPath = nphPath, filePath = filePath
let process = await startProcess(nphPath, arguments = @[filePath], options = { UsePath })
let res = await process.waitForExit(InfiniteDuration)
if res != 0:
error "There was an error trying to format the document. "
#TODO show notification to the user containing the error.
return none(TextEdit)
debug "nph completes ", res = res
let formattedText = readFile(filePath)
let fullRange = Range(
start: Position(line: 0, character: 0),
`end`: Position(line: int.high, character: int.high)
)
some TextEdit(range: fullRange, newText: formattedText)

proc formatting*(ls: LanguageServer, params: DocumentFormattingParams, id: int):
Future[seq[TextEdit]] {.async.} =
with (params.textDocument):
asyncSpawn ls.addProjectFileToPendingRequest(id.uint, uri)
debug "Received Formatting request ", filePath = ls.uriStorageLocation(uri), params = params[]
let filePath = ls.uriStorageLocation(uri)
if not fileExists(filePath):
warn "File doenst exist ", filePath = filePath, uri = uri
return @[]
let formatTextEdit = await format(getNphPath().get(), filePath)
if formatTextEdit.isSome:
return @[formatTextEdit.get]

proc workspaceSymbol*(ls: LanguageServer, params: WorkspaceSymbolParams, id: int):
Future[seq[SymbolInformation]] {.async.} =
if ls.lastNimsuggest != nil:
Expand Down

0 comments on commit 4978c3c

Please sign in to comment.