Skip to content

Commit

Permalink
improved vim keybindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Mar 13, 2024
1 parent 2009177 commit d0067e0
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 112 deletions.
28 changes: 21 additions & 7 deletions config/keybindings_vim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ proc vimMoveTo*(editor: TextDocumentEditor, target: string, before: bool, count:
if before:
editor.moveCursorColumn(-1)

proc vimClamp*(self: TextDocumentEditor, cursor: Cursor): Cursor =
let lineLen = self.lineLength(cursor.line)
if cursor.column > 0 and cursor.column >= lineLen:
return (cursor.line, lineLen - 1)
return cursor

proc vimMotionLine*(editor: TextDocumentEditor, cursor: Cursor, count: int): Selection =
let lineLen = editor.lineLength(cursor.line)
result = ((cursor.line, 0), (cursor.line, max(0, lineLen - 1)))

proc vimMotionWord*(editor: TextDocumentEditor, cursor: Cursor, count: int): Selection =
const AlphaNumeric = {'A'..'Z', 'a'..'z', '0'..'9', '_'}

Expand Down Expand Up @@ -295,6 +305,7 @@ proc vimMotionSurroundSingleQuotesInner*(editor: TextDocumentEditor, cursor: Cur
proc vimMotionSurroundSingleQuotesOuter*(editor: TextDocumentEditor, cursor: Cursor, count: int): Selection = vimMotionSurround(editor, cursor, count, '\'', '\'', false)

# todo
addCustomTextMove "vim-line", vimMotionLine
addCustomTextMove "vim-word", vimMotionWord
addCustomTextMove "vim-WORD", vimMotionWordBig
addCustomTextMove "vim-word-inner", vimMotionWord
Expand Down Expand Up @@ -335,8 +346,8 @@ iterator iterateTextObjects(editor: TextDocumentEditor, cursor: Cursor, move: st
continue

let nextCursor = if backwards: (selection.first.line, selection.first.column - 1) else: (selection.last.line, selection.last.column + 1)
# echo &"iterate text objects {move}, {cursor} get selection for move {nextCursor} {move}"
let newSelection = editor.getSelectionForMove(nextCursor, move, 0)
# infof"iterateTextObjects({cursor}, {move}, {backwards}) nextCursor: {nextCursor}, newSelection: {newSelection}"
if newSelection == lastSelection:
break

Expand Down Expand Up @@ -435,6 +446,9 @@ proc moveSelectionEnd(editor: TextDocumentEditor, move: string, backwards: bool
editor.getLine(selection.last.line)[selection.last.column] notin Whitespace:
res = cursor
break
if backwards and selection.last.column == editor.lineLength(selection.last.line):
res = cursor
break
res.toSelection(it, which)
)

Expand Down Expand Up @@ -473,12 +487,12 @@ proc vimDeleteRight*(editor: TextDocumentEditor) =
expose "vim-delete-left", vimDeleteLeft

proc vimMoveCursorColumn(editor: TextDocumentEditor, direction: int, count: int = 1) {.expose("vim-move-cursor-column").} =
editor.moveCursorColumn(direction * max(count, 1))
editor.moveCursorColumn(direction * max(count, 1), wrap=false, includeAfter=false)
if selectLines:
editor.vimSelectLine()

proc vimMoveCursorLine(editor: TextDocumentEditor, direction: int, count: int = 1) {.expose("vim-move-cursor-line").} =
editor.moveCursorLine(direction * max(count, 1))
editor.moveCursorLine(direction * max(count, 1), includeAfter=false)
if selectLines:
editor.vimSelectLine()

Expand All @@ -497,7 +511,7 @@ proc vimMoveToEndOfLine(editor: TextDocumentEditor, count: int = 1) =
let count = max(1, count)
if count > 1:
editor.moveCursorLine(count - 1)
editor.moveLast("line")
editor.moveLast("vim-line")
editor.scrollToCursor Last

proc vimMoveCursorLineFirstChar(editor: TextDocumentEditor, direction: int, count: int = 1) =
Expand Down Expand Up @@ -926,7 +940,6 @@ proc loadVimKeybindings*() {.scriptActionWasmNims("load-vim-keybindings").} =

addTextCommand "insert", "<SPACE>", "insert-text", " "
addTextCommand "insert", "<BACKSPACE>", "delete-left"
addTextCommand "insert", "<C-h>", "delete-left"
addTextCommand "insert", "<DELETE>", "delete-right"
addTextCommandBlock "insert", "<C-w>":
editor.deleteMove("word-line", inside=false, which=SelectionCursor.First)
Expand Down Expand Up @@ -987,7 +1000,8 @@ proc loadVimKeybindings*() {.scriptActionWasmNims("load-vim-keybindings").} =
addTextCommand "", "gd", "goto-definition"
addTextCommand "", "gs", "goto-symbol"
addTextCommand "", "<C-SPACE>", "get-completions"
addTextCommand "", "<C-k>", "show-hover-for-current"
addTextCommand "", "<C-h>", "show-hover-for-current"
addTextCommand "", "<C-r>", "select-prev"
addTextCommand "", "<C-m>", "select-next"
addTextCommand "", "U", "redo"
addTextCommand "", "U", "redo"
addTextCommand "", "<C-k><C-c>", "toggle-line-comment"
27 changes: 15 additions & 12 deletions scripting/absytree_internal.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ proc editor_text_lineLength_int_TextDocumentEditor_int_impl*(
proc editor_text_screenLineCount_int_TextDocumentEditor_impl*(
self: TextDocumentEditor): int =
discard
proc editor_text_doMoveCursorColumn_Cursor_TextDocumentEditor_Cursor_int_bool_impl*(
self: TextDocumentEditor; cursor: Cursor; offset: int; wrap: bool = true): Cursor =
proc editor_text_doMoveCursorColumn_Cursor_TextDocumentEditor_Cursor_int_bool_bool_impl*(
self: TextDocumentEditor; cursor: Cursor; offset: int; wrap: bool = true;
includeAfter: bool = true): Cursor =
discard
proc editor_text_findSurroundStart_Option_Cursor_TextDocumentEditor_Cursor_int_char_char_int_impl*(
editor: TextDocumentEditor; cursor: Cursor; count: int; c0: char; c1: char;
Expand Down Expand Up @@ -128,21 +129,23 @@ proc editor_text_setAllFindResultToSelection_void_TextDocumentEditor_impl*(
proc editor_text_clearSelections_void_TextDocumentEditor_impl*(
self: TextDocumentEditor) =
discard
proc editor_text_moveCursorColumn_void_TextDocumentEditor_int_SelectionCursor_bool_impl*(
proc editor_text_moveCursorColumn_void_TextDocumentEditor_int_SelectionCursor_bool_bool_bool_impl*(
self: TextDocumentEditor; distance: int;
cursor: SelectionCursor = SelectionCursor.Config; all: bool = true) =
cursor: SelectionCursor = SelectionCursor.Config; all: bool = true;
wrap: bool = true; includeAfter: bool = true) =
discard
proc editor_text_moveCursorLine_void_TextDocumentEditor_int_SelectionCursor_bool_impl*(
proc editor_text_moveCursorLine_void_TextDocumentEditor_int_SelectionCursor_bool_bool_bool_impl*(
self: TextDocumentEditor; distance: int;
cursor: SelectionCursor = SelectionCursor.Config; all: bool = true) =
cursor: SelectionCursor = SelectionCursor.Config; all: bool = true;
wrap: bool = true; includeAfter: bool = true) =
discard
proc editor_text_moveCursorHome_void_TextDocumentEditor_SelectionCursor_bool_impl*(
self: TextDocumentEditor; cursor: SelectionCursor = SelectionCursor.Config;
all: bool = true) =
discard
proc editor_text_moveCursorEnd_void_TextDocumentEditor_SelectionCursor_bool_impl*(
proc editor_text_moveCursorEnd_void_TextDocumentEditor_SelectionCursor_bool_bool_impl*(
self: TextDocumentEditor; cursor: SelectionCursor = SelectionCursor.Config;
all: bool = true) =
all: bool = true; includeAfter: bool = true) =
discard
proc editor_text_moveCursorTo_void_TextDocumentEditor_string_SelectionCursor_bool_impl*(
self: TextDocumentEditor; str: string;
Expand All @@ -152,13 +155,13 @@ proc editor_text_moveCursorBefore_void_TextDocumentEditor_string_SelectionCursor
self: TextDocumentEditor; str: string;
cursor: SelectionCursor = SelectionCursor.Config; all: bool = true) =
discard
proc editor_text_moveCursorNextFindResult_void_TextDocumentEditor_SelectionCursor_bool_impl*(
proc editor_text_moveCursorNextFindResult_void_TextDocumentEditor_SelectionCursor_bool_bool_impl*(
self: TextDocumentEditor; cursor: SelectionCursor = SelectionCursor.Config;
all: bool = true) =
all: bool = true; wrap: bool = true) =
discard
proc editor_text_moveCursorPrevFindResult_void_TextDocumentEditor_SelectionCursor_bool_impl*(
proc editor_text_moveCursorPrevFindResult_void_TextDocumentEditor_SelectionCursor_bool_bool_impl*(
self: TextDocumentEditor; cursor: SelectionCursor = SelectionCursor.Config;
all: bool = true) =
all: bool = true; wrap: bool = true) =
discard
proc editor_text_moveCursorLineCenter_void_TextDocumentEditor_SelectionCursor_bool_impl*(
self: TextDocumentEditor; cursor: SelectionCursor = SelectionCursor.Config;
Expand Down
27 changes: 15 additions & 12 deletions scripting/absytree_internal_wasm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ proc editor_text_lineLength_int_TextDocumentEditor_int_impl(
self: TextDocumentEditor; line: int): int {.importc.}
proc editor_text_screenLineCount_int_TextDocumentEditor_impl(
self: TextDocumentEditor): int {.importc.}
proc editor_text_doMoveCursorColumn_Cursor_TextDocumentEditor_Cursor_int_bool_impl(
self: TextDocumentEditor; cursor: Cursor; offset: int; wrap: bool = true): Cursor {.importc.}
proc editor_text_doMoveCursorColumn_Cursor_TextDocumentEditor_Cursor_int_bool_bool_impl(
self: TextDocumentEditor; cursor: Cursor; offset: int; wrap: bool = true;
includeAfter: bool = true): Cursor {.importc.}
proc editor_text_findSurroundStart_Option_Cursor_TextDocumentEditor_Cursor_int_char_char_int_impl(
editor: TextDocumentEditor; cursor: Cursor; count: int; c0: char; c1: char;
depth: int = 1): Option[Cursor] {.importc.}
Expand Down Expand Up @@ -85,30 +86,32 @@ proc editor_text_setAllFindResultToSelection_void_TextDocumentEditor_impl(
self: TextDocumentEditor) {.importc.}
proc editor_text_clearSelections_void_TextDocumentEditor_impl(
self: TextDocumentEditor) {.importc.}
proc editor_text_moveCursorColumn_void_TextDocumentEditor_int_SelectionCursor_bool_impl(
proc editor_text_moveCursorColumn_void_TextDocumentEditor_int_SelectionCursor_bool_bool_bool_impl(
self: TextDocumentEditor; distance: int;
cursor: SelectionCursor = SelectionCursor.Config; all: bool = true) {.importc.}
proc editor_text_moveCursorLine_void_TextDocumentEditor_int_SelectionCursor_bool_impl(
cursor: SelectionCursor = SelectionCursor.Config; all: bool = true;
wrap: bool = true; includeAfter: bool = true) {.importc.}
proc editor_text_moveCursorLine_void_TextDocumentEditor_int_SelectionCursor_bool_bool_bool_impl(
self: TextDocumentEditor; distance: int;
cursor: SelectionCursor = SelectionCursor.Config; all: bool = true) {.importc.}
cursor: SelectionCursor = SelectionCursor.Config; all: bool = true;
wrap: bool = true; includeAfter: bool = true) {.importc.}
proc editor_text_moveCursorHome_void_TextDocumentEditor_SelectionCursor_bool_impl(
self: TextDocumentEditor; cursor: SelectionCursor = SelectionCursor.Config;
all: bool = true) {.importc.}
proc editor_text_moveCursorEnd_void_TextDocumentEditor_SelectionCursor_bool_impl(
proc editor_text_moveCursorEnd_void_TextDocumentEditor_SelectionCursor_bool_bool_impl(
self: TextDocumentEditor; cursor: SelectionCursor = SelectionCursor.Config;
all: bool = true) {.importc.}
all: bool = true; includeAfter: bool = true) {.importc.}
proc editor_text_moveCursorTo_void_TextDocumentEditor_string_SelectionCursor_bool_impl(
self: TextDocumentEditor; str: string;
cursor: SelectionCursor = SelectionCursor.Config; all: bool = true) {.importc.}
proc editor_text_moveCursorBefore_void_TextDocumentEditor_string_SelectionCursor_bool_impl(
self: TextDocumentEditor; str: string;
cursor: SelectionCursor = SelectionCursor.Config; all: bool = true) {.importc.}
proc editor_text_moveCursorNextFindResult_void_TextDocumentEditor_SelectionCursor_bool_impl(
proc editor_text_moveCursorNextFindResult_void_TextDocumentEditor_SelectionCursor_bool_bool_impl(
self: TextDocumentEditor; cursor: SelectionCursor = SelectionCursor.Config;
all: bool = true) {.importc.}
proc editor_text_moveCursorPrevFindResult_void_TextDocumentEditor_SelectionCursor_bool_impl(
all: bool = true; wrap: bool = true) {.importc.}
proc editor_text_moveCursorPrevFindResult_void_TextDocumentEditor_SelectionCursor_bool_bool_impl(
self: TextDocumentEditor; cursor: SelectionCursor = SelectionCursor.Config;
all: bool = true) {.importc.}
all: bool = true; wrap: bool = true) {.importc.}
proc editor_text_moveCursorLineCenter_void_TextDocumentEditor_SelectionCursor_bool_impl(
self: TextDocumentEditor; cursor: SelectionCursor = SelectionCursor.Config;
all: bool = true) {.importc.}
Expand Down
38 changes: 20 additions & 18 deletions scripting/editor_text_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ proc screenLineCount*(self: TextDocumentEditor): int =
## This value depends on the size of the view this editor is in and the font size
editor_text_screenLineCount_int_TextDocumentEditor_impl(self)
proc doMoveCursorColumn*(self: TextDocumentEditor; cursor: Cursor; offset: int;
wrap: bool = true): Cursor =
editor_text_doMoveCursorColumn_Cursor_TextDocumentEditor_Cursor_int_bool_impl(
self, cursor, offset, wrap)
wrap: bool = true; includeAfter: bool = true): Cursor =
editor_text_doMoveCursorColumn_Cursor_TextDocumentEditor_Cursor_int_bool_bool_impl(
self, cursor, offset, wrap, includeAfter)
proc findSurroundStart*(editor: TextDocumentEditor; cursor: Cursor; count: int;
c0: char; c1: char; depth: int = 1): Option[Cursor] =
editor_text_findSurroundStart_Option_Cursor_TextDocumentEditor_Cursor_int_char_char_int_impl(
Expand Down Expand Up @@ -124,24 +124,26 @@ proc clearSelections*(self: TextDocumentEditor) =
editor_text_clearSelections_void_TextDocumentEditor_impl(self)
proc moveCursorColumn*(self: TextDocumentEditor; distance: int;
cursor: SelectionCursor = SelectionCursor.Config;
all: bool = true) =
editor_text_moveCursorColumn_void_TextDocumentEditor_int_SelectionCursor_bool_impl(
self, distance, cursor, all)
all: bool = true; wrap: bool = true;
includeAfter: bool = true) =
editor_text_moveCursorColumn_void_TextDocumentEditor_int_SelectionCursor_bool_bool_bool_impl(
self, distance, cursor, all, wrap, includeAfter)
proc moveCursorLine*(self: TextDocumentEditor; distance: int;
cursor: SelectionCursor = SelectionCursor.Config;
all: bool = true) =
editor_text_moveCursorLine_void_TextDocumentEditor_int_SelectionCursor_bool_impl(
self, distance, cursor, all)
all: bool = true; wrap: bool = true;
includeAfter: bool = true) =
editor_text_moveCursorLine_void_TextDocumentEditor_int_SelectionCursor_bool_bool_bool_impl(
self, distance, cursor, all, wrap, includeAfter)
proc moveCursorHome*(self: TextDocumentEditor;
cursor: SelectionCursor = SelectionCursor.Config;
all: bool = true) =
editor_text_moveCursorHome_void_TextDocumentEditor_SelectionCursor_bool_impl(
self, cursor, all)
proc moveCursorEnd*(self: TextDocumentEditor;
cursor: SelectionCursor = SelectionCursor.Config;
all: bool = true) =
editor_text_moveCursorEnd_void_TextDocumentEditor_SelectionCursor_bool_impl(
self, cursor, all)
all: bool = true; includeAfter: bool = true) =
editor_text_moveCursorEnd_void_TextDocumentEditor_SelectionCursor_bool_bool_impl(
self, cursor, all, includeAfter)
proc moveCursorTo*(self: TextDocumentEditor; str: string;
cursor: SelectionCursor = SelectionCursor.Config;
all: bool = true) =
Expand All @@ -153,13 +155,13 @@ proc moveCursorBefore*(self: TextDocumentEditor; str: string;
editor_text_moveCursorBefore_void_TextDocumentEditor_string_SelectionCursor_bool_impl(
self, str, cursor, all)
proc moveCursorNextFindResult*(self: TextDocumentEditor; cursor: SelectionCursor = SelectionCursor.Config;
all: bool = true) =
editor_text_moveCursorNextFindResult_void_TextDocumentEditor_SelectionCursor_bool_impl(
self, cursor, all)
all: bool = true; wrap: bool = true) =
editor_text_moveCursorNextFindResult_void_TextDocumentEditor_SelectionCursor_bool_bool_impl(
self, cursor, all, wrap)
proc moveCursorPrevFindResult*(self: TextDocumentEditor; cursor: SelectionCursor = SelectionCursor.Config;
all: bool = true) =
editor_text_moveCursorPrevFindResult_void_TextDocumentEditor_SelectionCursor_bool_impl(
self, cursor, all)
all: bool = true; wrap: bool = true) =
editor_text_moveCursorPrevFindResult_void_TextDocumentEditor_SelectionCursor_bool_bool_impl(
self, cursor, all, wrap)
proc moveCursorLineCenter*(self: TextDocumentEditor;
cursor: SelectionCursor = SelectionCursor.Config;
all: bool = true) =
Expand Down
Loading

0 comments on commit d0067e0

Please sign in to comment.