Skip to content

Commit

Permalink
Fixed issues with indenting
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Apr 26, 2024
1 parent 91556a9 commit 812b3c9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
14 changes: 14 additions & 0 deletions scripting/editor_text_api_wasm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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") =
Expand Down
22 changes: 18 additions & 4 deletions src/text/text_editor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 812b3c9

Please sign in to comment.