Skip to content

Commit 06cf306

Browse files
committed
Implements willSaveWaitUntil and autoformat there so file is not marked as dirty
1 parent bf21855 commit 06cf306

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

nimlangserver.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ proc registerRoutes(srv: RpcSocketServer, ls: LanguageServer) =
7979
"workspace/didChangeConfiguration", wrapRpc(partial(didChangeConfiguration, ls))
8080
)
8181
srv.register("textDocument/didChange", wrapRpc(partial(didChange, ls)))
82+
srv.register("textDocument/willSaveWaitUntil", wrapRpc(partial(willSaveWaitUntil, ls)))
8283
srv.register("$/setTrace", wrapRpc(partial(setTrace, ls)))
8384

8485
proc showHelp() =

routes.nim

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ proc initialize*(
7171
openClose: some(true),
7272
change: some(TextDocumentSyncKind.Full.int),
7373
willSave: some(false),
74-
willSaveWaitUntil: some(false),
74+
willSaveWaitUntil: some(true),
7575
save: some(SaveOptions(includeText: some(true))),
7676
)
7777
),
@@ -924,33 +924,36 @@ proc didChange*(
924924

925925
ls.scheduleFileCheck(uri)
926926

927-
proc autoFormat(ls: LanguageServer, config: NlsConfig, uri: string) {.async.} =
928-
let nphPath = getNphPath()
929-
let shouldAutoFormat =
930-
nphPath.isSome and ls.serverCapabilities.documentFormattingProvider.get(false) and
931-
ls.clientCapabilities.workspace.get(ClientCapabilities_workspace()).applyEdit.get(
932-
false
933-
) and config.formatOnSave.get(false)
934-
if shouldAutoFormat:
927+
proc willSaveWaitUntil*(
928+
ls: LanguageServer,
929+
params: WillSaveTextDocumentParams
930+
): Future[seq[TextEdit]] {.async.} =
931+
debug "Received willSaveWaitUntil request"
932+
933+
let
934+
uri = params.textDocument.uri
935+
config = await ls.getWorkspaceConfiguration()
936+
nphPath = getNphPath()
937+
938+
let shouldFormat =
939+
nphPath.isSome and
940+
ls.serverCapabilities.documentFormattingProvider.get(false) and
941+
config.formatOnSave.get(false)
942+
943+
if shouldFormat:
944+
debug "Formatting document before save", uri = uri
935945
let formatTextEdit = await ls.format(nphPath.get(), uri)
936946
if formatTextEdit.isSome:
937-
let workspaceEdit = WorkspaceEdit(
938-
documentChanges: some @[
939-
TextDocumentEdit(
940-
textDocument: VersionedTextDocumentIdentifier(uri: uri),
941-
edits: some @[formatTextEdit.get()],
942-
)
943-
]
944-
)
945-
discard await ls.applyEdit(ApplyWorkspaceEditParams(edit: workspaceEdit))
947+
return @[formatTextEdit.get]
948+
949+
return @[]
946950

947951
proc didSave*(
948952
ls: LanguageServer, params: DidSaveTextDocumentParams
949953
): Future[void] {.async, gcsafe.} =
950954
let
951955
uri = params.textDocument.uri
952956
config = await ls.getWorkspaceConfiguration()
953-
asyncSpawn ls.autoFormat(config, uri)
954957
let nimsuggest = await ls.tryGetNimsuggest(uri)
955958

956959
if nimsuggest.isNone:

0 commit comments

Comments
 (0)