From 131a90db22625f644c851a1616f8c0d6fe570e7c Mon Sep 17 00:00:00 2001 From: Nimaoth Date: Sun, 17 Mar 2024 23:32:42 +0100 Subject: [PATCH] improved pasting in vim --- config/keybindings_vim.nim | 12 +++++++++--- scripting/absytree_internal.nim | 4 ++-- scripting/absytree_internal_wasm.nim | 4 ++-- scripting/editor_text_api.nim | 6 ++++-- scripting/editor_text_api_wasm.nim | 12 +++++++++--- src/scripting_api.nim | 1 + src/text/text_editor.nim | 10 +++++----- 7 files changed, 32 insertions(+), 17 deletions(-) diff --git a/config/keybindings_vim.nim b/config/keybindings_vim.nim index 67207275..76d7a6f1 100644 --- a/config/keybindings_vim.nim +++ b/config/keybindings_vim.nim @@ -623,11 +623,17 @@ proc vimMoveToStartOfLine(editor: TextDocumentEditor, count: int = 1) = proc vimPaste(editor: TextDocumentEditor, register: string = "") {.expose("vim-paste").} = # infof"vimPaste {register}, lines: {yankedLines}" editor.addNextCheckpoint "insert" + if yankedLines: - editor.moveLast "line", Both - editor.insertText "\n", autoIndent=false + let selectionsToDelete = editor.selections + editor.selections = editor.delete(selectionsToDelete, inclusiveEnd=false) - editor.paste register + if editor.mode != "visual-line": + editor.moveLast "line", Both + editor.insertText "\n", autoIndent=false + + editor.paste register, inclusiveEnd=true + editor.setMode "normal" proc vimCloseCurrentViewOrQuit() {.expose("vim-close-current-view-or-quit").} = let openEditors = getOpenEditors().len + getHiddenEditors().len diff --git a/scripting/absytree_internal.nim b/scripting/absytree_internal.nim index 15062cd4..dc9e35f5 100644 --- a/scripting/absytree_internal.nim +++ b/scripting/absytree_internal.nim @@ -104,8 +104,8 @@ proc editor_text_printUndoHistory_void_TextDocumentEditor_int_impl*( proc editor_text_copy_void_TextDocumentEditor_string_bool_impl*( self: TextDocumentEditor; register: string = ""; inclusiveEnd: bool = false) = discard -proc editor_text_paste_void_TextDocumentEditor_string_impl*( - self: TextDocumentEditor; register: string = "") = +proc editor_text_paste_void_TextDocumentEditor_string_bool_impl*( + self: TextDocumentEditor; register: string = ""; inclusiveEnd: bool = false) = discard proc editor_text_scrollText_void_TextDocumentEditor_float32_impl*( self: TextDocumentEditor; amount: float32) = diff --git a/scripting/absytree_internal_wasm.nim b/scripting/absytree_internal_wasm.nim index 1c871f22..a5dfc61b 100644 --- a/scripting/absytree_internal_wasm.nim +++ b/scripting/absytree_internal_wasm.nim @@ -70,8 +70,8 @@ proc editor_text_printUndoHistory_void_TextDocumentEditor_int_impl( self: TextDocumentEditor; max: int = 50) {.importc.} proc editor_text_copy_void_TextDocumentEditor_string_bool_impl( self: TextDocumentEditor; register: string = ""; inclusiveEnd: bool = false) {.importc.} -proc editor_text_paste_void_TextDocumentEditor_string_impl( - self: TextDocumentEditor; register: string = "") {.importc.} +proc editor_text_paste_void_TextDocumentEditor_string_bool_impl( + self: TextDocumentEditor; register: string = ""; inclusiveEnd: bool = false) {.importc.} proc editor_text_scrollText_void_TextDocumentEditor_float32_impl( self: TextDocumentEditor; amount: float32) {.importc.} proc editor_text_scrollLines_void_TextDocumentEditor_int_impl( diff --git a/scripting/editor_text_api.nim b/scripting/editor_text_api.nim index e11b1de1..6a20ac1f 100644 --- a/scripting/editor_text_api.nim +++ b/scripting/editor_text_api.nim @@ -106,8 +106,10 @@ proc copy*(self: TextDocumentEditor; register: string = ""; inclusiveEnd: bool = false) = editor_text_copy_void_TextDocumentEditor_string_bool_impl(self, register, inclusiveEnd) -proc paste*(self: TextDocumentEditor; register: string = "") = - editor_text_paste_void_TextDocumentEditor_string_impl(self, register) +proc paste*(self: TextDocumentEditor; register: string = ""; + inclusiveEnd: bool = false) = + editor_text_paste_void_TextDocumentEditor_string_bool_impl(self, register, + inclusiveEnd) proc scrollText*(self: TextDocumentEditor; amount: float32) = editor_text_scrollText_void_TextDocumentEditor_float32_impl(self, amount) proc scrollLines*(self: TextDocumentEditor; amount: int) = diff --git a/scripting/editor_text_api_wasm.nim b/scripting/editor_text_api_wasm.nim index 6b78f42b..0b6f2009 100644 --- a/scripting/editor_text_api_wasm.nim +++ b/scripting/editor_text_api_wasm.nim @@ -695,9 +695,10 @@ proc copy*(self: TextDocumentEditor; register: string = ""; argsJsonString.cstring) -proc editor_text_paste_void_TextDocumentEditor_string_wasm(arg: cstring): cstring {. +proc editor_text_paste_void_TextDocumentEditor_string_bool_wasm(arg: cstring): cstring {. importc.} -proc paste*(self: TextDocumentEditor; register: string = "") = +proc paste*(self: TextDocumentEditor; register: string = ""; + inclusiveEnd: bool = false) = var argsJson = newJArray() argsJson.add block: when TextDocumentEditor is JsonNode: @@ -709,8 +710,13 @@ proc paste*(self: TextDocumentEditor; register: string = "") = register else: register.toJson() + argsJson.add block: + when bool is JsonNode: + inclusiveEnd + else: + inclusiveEnd.toJson() let argsJsonString = $argsJson - let res {.used.} = editor_text_paste_void_TextDocumentEditor_string_wasm( + let res {.used.} = editor_text_paste_void_TextDocumentEditor_string_bool_wasm( argsJsonString.cstring) diff --git a/src/scripting_api.nim b/src/scripting_api.nim index ef23c33d..b5b12f31 100644 --- a/src/scripting_api.nim +++ b/src/scripting_api.nim @@ -111,6 +111,7 @@ func normalized*(selection: Selection): Selection = func reverse*(selection: Selection): Selection = (selection.last, selection.first) func isEmpty*(selection: Selection): bool = selection.first == selection.last +func allEmpty*(selections: Selections): bool = selections.allIt(it.isEmpty) func contains*(selection: Selection, cursor: Cursor): bool = (cursor >= selection.first and cursor <= selection.last) func contains*(selection: Selection, other: Selection): bool = (other.first >= selection.first and other.last <= selection.last) diff --git a/src/text/text_editor.nim b/src/text/text_editor.nim index 13255d34..6cd7eb4e 100644 --- a/src/text/text_editor.nim +++ b/src/text/text_editor.nim @@ -878,16 +878,16 @@ proc copyAsync*(self: TextDocumentEditor, register: string, inclusiveEnd: bool): proc copy*(self: TextDocumentEditor, register: string = "", inclusiveEnd: bool = false) {.expose("editor.text").} = asyncCheck self.copyAsync(register, inclusiveEnd) -proc pasteAsync*(self: TextDocumentEditor, register: string): Future[void] {.async.} = +proc pasteAsync*(self: TextDocumentEditor, register: string, inclusiveEnd: bool = false): Future[void] {.async.} = let text = self.app.getRegisterTextAsync(register).await let numLines = text.count('\n') + 1 let newSelections = if numLines == self.selections.len: let lines = text.splitLines() - self.document.edit(self.selections, self.selections, lines, notify=true, record=true).mapIt(it.last.toSelection) + self.document.edit(self.selections, self.selections, lines, notify=true, record=true, inclusiveEnd=inclusiveEnd).mapIt(it.last.toSelection) else: - self.document.edit(self.selections, self.selections, [text], notify=true, record=true).mapIt(it.last.toSelection) + self.document.edit(self.selections, self.selections, [text], notify=true, record=true, inclusiveEnd=inclusiveEnd).mapIt(it.last.toSelection) # add list of selections for what was just pasted to history if newSelections.len == self.selections.len: @@ -899,8 +899,8 @@ proc pasteAsync*(self: TextDocumentEditor, register: string): Future[void] {.asy self.selections = newSelections self.scrollToCursor(Last) -proc paste*(self: TextDocumentEditor, register: string = "") {.expose("editor.text").} = - asyncCheck self.pasteAsync(register) +proc paste*(self: TextDocumentEditor, register: string = "", inclusiveEnd: bool = false) {.expose("editor.text").} = + asyncCheck self.pasteAsync(register, inclusiveEnd) proc scrollText*(self: TextDocumentEditor, amount: float32) {.expose("editor.text").} = if self.disableScrolling: