Skip to content

Commit

Permalink
improved pasting in vim
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Mar 17, 2024
1 parent 5facd3e commit 131a90d
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 17 deletions.
12 changes: 9 additions & 3 deletions config/keybindings_vim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions scripting/absytree_internal.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down
4 changes: 2 additions & 2 deletions scripting/absytree_internal_wasm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 4 additions & 2 deletions scripting/editor_text_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down
12 changes: 9 additions & 3 deletions scripting/editor_text_api_wasm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)


Expand Down
1 change: 1 addition & 0 deletions src/scripting_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions src/text/text_editor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 131a90d

Please sign in to comment.