From 812b3c9b57b2128be7978f58280a979ea9b5720a Mon Sep 17 00:00:00 2001 From: Nimaoth Date: Fri, 26 Apr 2024 21:48:26 +0200 Subject: [PATCH] Fixed issues with indenting --- scripting/editor_text_api_wasm.nim | 14 ++++++++++++++ src/text/text_editor.nim | 22 ++++++++++++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/scripting/editor_text_api_wasm.nim b/scripting/editor_text_api_wasm.nim index ae68ccc6..615fd6d8 100644 --- a/scripting/editor_text_api_wasm.nim +++ b/scripting/editor_text_api_wasm.nim @@ -633,6 +633,20 @@ proc unindent*(self: TextDocumentEditor) = argsJsonString.cstring) +proc editor_text_insertIndent_void_TextDocumentEditor_wasm(arg: cstring): cstring {. + importc.} +proc insertIndent*(self: TextDocumentEditor) = + var argsJson = newJArray() + argsJson.add block: + when TextDocumentEditor is JsonNode: + self + else: + self.toJson() + let argsJsonString = $argsJson + let res {.used.} = editor_text_insertIndent_void_TextDocumentEditor_wasm( + argsJsonString.cstring) + + proc editor_text_undo_void_TextDocumentEditor_string_wasm(arg: cstring): cstring {. importc.} proc undo*(self: TextDocumentEditor; checkpoint: string = "word") = diff --git a/src/text/text_editor.nim b/src/text/text_editor.nim index 2d3765f7..bd16aac4 100644 --- a/src/text/text_editor.nim +++ b/src/text/text_editor.nim @@ -931,11 +931,15 @@ proc unindent*(self: TextDocumentEditor) {.expose("editor.text").} = var indentSelections: Selections = @[] for l in linesToIndent: - let firstNonWhitespace = self.document.lines[l].firstNonWhitespace - indentSelections.add ((l, 0), (l, min(self.document.indentStyle.indentColumns, firstNonWhitespace))) + case self.document.indentStyle.kind + of Spaces: + let firstNonWhitespace = self.document.lines[l].firstNonWhitespace + indentSelections.add ((l, 0), (l, min(self.document.indentStyle.indentColumns, firstNonWhitespace))) + of Tabs: + indentSelections.add ((l, 0), (l, 1)) var selections = self.selections - discard self.document.delete(indentSelections.normalized, self.selections, inclusiveEnd=self.useInclusiveSelections) + discard self.document.delete(indentSelections.normalized, self.selections) for s in selections.mitems: if s.first.line in linesToIndent: @@ -944,6 +948,16 @@ proc unindent*(self: TextDocumentEditor) {.expose("editor.text").} = s.last.column = max(0, s.last.column - self.document.indentStyle.indentColumns) self.selections = selections +proc insertIndent*(self: TextDocumentEditor) {.expose("editor.text").} = + var insertTexts = newSeq[string]() + + # todo: for spaces, calculate alignment + let indent = self.document.indentStyle.getString() + for selection in self.selections: + insertTexts.add indent + + self.selections = self.document.edit(self.selections, self.selections, insertTexts).mapIt(it.last.toSelection) + proc undo*(self: TextDocumentEditor, checkpoint: string = "word") {.expose("editor.text").} = if self.document.undo(self.selections, true, checkpoint).getSome(selections): self.selections = selections @@ -2103,7 +2117,7 @@ proc applySelectedCompletion*(self: TextDocumentEditor) {.expose("editor.text"). self.addNextCheckpoint("insert") let newSelections = self.document.edit(editSelections, self.selections, insertTexts) - self.selection = newSelections[newSelections.high] + self.selection = newSelections[newSelections.high].last.toSelection self.currentSnippetData = snippetData self.selectNextTabStop()