Skip to content

Commit

Permalink
Add proper removal of dirty files
Browse files Browse the repository at this point in the history
  • Loading branch information
PMunch committed Nov 9, 2023
1 parent e36e39b commit 62a2fc7
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/nimlsp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ for i in 1..paramCount():
var
gotShutdown = false
initialized = false
projectFiles = initTable[string, tuple[nimsuggest: NimSuggest, openFiles: OrderedSet[string]]]()
projectFiles = initTable[string, tuple[nimsuggest: NimSuggest, openFiles: OrderedSet[string], dirtyFiles: HashSet[string]]]()
openFiles = initTable[string, tuple[projectFile: string, fingerTable: seq[seq[tuple[u16pos, offset: int]]]]]()

template whenValid(data, kind, body) =
Expand Down Expand Up @@ -534,8 +534,9 @@ proc main(ins: Stream | AsyncFile, outs: Stream | AsyncFile) {.multisync.} =

if projectFile notin projectFiles:
debugLog "Initialising project with ", projectFile, ":", nimpath
projectFiles[projectFile] = (nimsuggest: initNimsuggest(projectFile, nimpath), openFiles: initOrderedSet[string]())
projectFiles[projectFile] = (nimsuggest: initNimsuggest(projectFile, nimpath), openFiles: initOrderedSet[string](), dirtyFiles: initHashSet[string]())
projectFiles[projectFile].openFiles.incl(fileuri)
projectFiles[projectFile].dirtyFiles.incl(filestash)

for line in textDoc["textDocument"]["text"].getStr.splitLines:
openFiles[fileuri].fingerTable.add line.createUTFMapping()
Expand All @@ -550,19 +551,23 @@ proc main(ins: Stream | AsyncFile, outs: Stream | AsyncFile) {.multisync.} =
openFiles[fileuri].fingerTable.add line.createUTFMapping()
file.writeLine line
file.close()
projectFiles[openFiles[fileuri].projectFile].dirtyFiles.incl(filestash)

# Notify nimsuggest about a file modification.
discard getNimsuggest(fileuri).mod(uriToPath(fileuri), dirtyfile = filestash)
of "textDocument/didClose":
message.textDocumentNotification(DidCloseTextDocumentParams, textDoc):
let projectFile = getProjectFile(uriToPath(fileuri))
debugLog "Got document close for URI: ", fileuri, " copied to ", filestash
#removeFile(filestash)
projectFiles[projectFile].openFiles.excl(fileuri)
if projectFiles[projectFile].openFiles.len == 0:
debugLog "Trying to stop nimsuggest"
debugLog "Stopped nimsuggest with code: ",
getNimsuggest(fileuri).stopNimsuggest()
debugLog "Cleaning up dirty files"
for dirtyfile in projectFiles[openFiles[fileuri].projectFile].dirtyFiles:
removeFile(dirtyfile)

openFiles.del(fileuri)
of "textDocument/didSave":
message.textDocumentNotification(DidSaveTextDocumentParams, textDoc):
Expand All @@ -574,6 +579,7 @@ proc main(ins: Stream | AsyncFile, outs: Stream | AsyncFile) {.multisync.} =
openFiles[fileuri].fingerTable.add line.createUTFMapping()
file.writeLine line
file.close()
projectFiles[openFiles[fileuri].projectFile].dirtyFiles.incl(filestash)
debugLog "fileuri: ", fileuri, ", project file: ", openFiles[fileuri].projectFile, ", dirtyfile: ", filestash

let diagnostics = getNimsuggest(fileuri).chk(uriToPath(fileuri), dirtyfile = filestash)
Expand Down

0 comments on commit 62a2fc7

Please sign in to comment.