Skip to content

Commit

Permalink
Fixed crash in file previewer
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Nov 8, 2024
1 parent 611484d commit fea1062
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
33 changes: 15 additions & 18 deletions src/document_editor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type
platform: Platform
editors*: Table[EditorId, DocumentEditor]
pinnedEditors*: HashSet[EditorId]
pinnedDocuments*: seq[Document]
documents*: seq[Document]
editorDefaults*: seq[DocumentEditor]
onEditorRegistered*: Event[DocumentEditor]
Expand All @@ -47,6 +48,7 @@ method init*(self: DocumentEditorService): Future[Result[void, ref CatchableErro
log lvlInfo, &"DocumentEditorService.init"
self.platform = self.services.getService(PlatformService).get.platform
assert self.platform != nil
self.pinnedEditors = initHashSet[EditorId]()
return ok()

method canOpenFile*(self: DocumentFactory, path: string): bool {.base, gcsafe, raises: [].} = discard
Expand Down Expand Up @@ -215,16 +217,13 @@ proc createEditorForDocument*(self: DocumentEditorService, document: Document):

discard result.get.onMarkedDirty.subscribe () => self.platform.requestRender()

proc closeEditor*(self: DocumentEditorService, editor: DocumentEditor) =
let document = editor.getDocument()
log lvlInfo, fmt"closeEditor: '{editor.getDocument().filename}'"
proc tryCloseDocument*(self: DocumentEditorService, document: Document) =
log lvlInfo, fmt"tryCloseDocument: '{document.filename}'"

if editor.id in self.pinnedEditors:
log lvlWarn, &"Can't close editor {editor.id} for '{editor.getDocument().filename}' because it's pinned"
if document in self.pinnedDocuments:
log lvlInfo, &"Document '{document.filename}' is pinned, don't close"
return

editor.deinit()

var hasAnotherEditor = false
for id, editor in self.editors.pairs:
if editor.getDocument() == document:
Expand All @@ -236,19 +235,17 @@ proc closeEditor*(self: DocumentEditorService, editor: DocumentEditor) =
document.deinit()
self.documents.del(document)

proc tryCloseDocument*(self: DocumentEditorService, document: Document) =
log lvlInfo, fmt"tryCloseDocument: '{document.filename}'"
proc closeEditor*(self: DocumentEditorService, editor: DocumentEditor) =
let document = editor.getDocument()
log lvlInfo, fmt"closeEditor: '{editor.getDocument().filename}'"

var hasAnotherEditor = false
for id, editor in self.editors.pairs:
if editor.getDocument() == document:
hasAnotherEditor = true
break
if editor.id in self.pinnedEditors:
log lvlWarn, &"Can't close editor {editor.id} for '{editor.getDocument().filename}' because it's pinned"
return

if not hasAnotherEditor:
log lvlInfo, fmt"Document has no other editors, closing it."
document.deinit()
self.documents.del(document)
editor.deinit()

self.tryCloseDocument(document)

###########################################################################

Expand Down
7 changes: 6 additions & 1 deletion src/finder/file_previewer.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import std/[tables, json, options, strformat, strutils]
import misc/[util, custom_logger, delayed_task, custom_async, myjsonutils]
import misc/[util, custom_logger, delayed_task, custom_async, myjsonutils, array_set]
import text/[text_editor, text_document]
import scripting_api except DocumentEditor, TextDocumentEditor, AstDocumentEditor
import finder, previewer
Expand Down Expand Up @@ -40,8 +40,13 @@ proc newFilePreviewer*(vfs: VFS, services: Services,
result.tempDocument = newTextDocument(services, createLanguageServer=false)
result.tempDocument.readOnly = true

result.editors.pinnedDocuments.incl result.tempDocument

method deinit*(self: FilePreviewer) =
logScope lvlInfo, &"[deinit] Destroying file previewer"

self.editors.pinnedDocuments.excl self.tempDocument

if self.triggerLoadTask.isNotNil:
self.triggerLoadTask.deinit()
if self.tempDocument.isNotNil:
Expand Down

0 comments on commit fea1062

Please sign in to comment.