Skip to content

Commit

Permalink
Started working on more treesitter based cursor movement
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Aug 31, 2024
1 parent a40bdab commit 4f5e6fb
Show file tree
Hide file tree
Showing 7 changed files with 527 additions and 28 deletions.
77 changes: 77 additions & 0 deletions config/keybindings_vim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,83 @@ proc loadVimKeybindings*() {.expose("load-vim-keybindings").} =

editor.setMode("visual")

addTextCommandBlock "", "H":
if editor.selections.len == 1:
var selection = editor.setSearchQueryFromMove("word", prefix=r"\b", suffix=r"\b")
selection.last.column -= 1
editor.selection = selection
else:
let next = editor.getNextFindResult(editor.selection.last, includeAfter=false)
editor.selections = editor.selections[0..^2] & next
editor.scrollToCursor Last
editor.updateTargetColumn()

editor.setMode("visual")

addTextCommandBlock "visual", "gl":
if editor.getNextNodeWithSameType(editor.selection, includeAfter=false).getSome(selection):
editor.selections = editor.selections & selection
editor.scrollToCursor Last
editor.updateTargetColumn()

addTextCommandBlock "visual", "gh":
if editor.getNextNodeWithSameType(editor.selection, includeAfter=false).getSome(selection):
editor.selections = editor.selections[0..^2] & selection
editor.scrollToCursor Last
editor.updateTargetColumn()

addTextCommandBlock "visual", "gs":
if editor.getNextNamedSiblingNodeSelection(editor.selection, includeAfter=false).getSome(selection):
editor.selections = editor.selections & selection
editor.scrollToCursor Last
editor.updateTargetColumn()

addTextCommandBlock "visual", "gd":
if editor.getNextNamedSiblingNodeSelection(editor.selection, includeAfter=false).getSome(selection):
editor.selections = editor.selections[0..^2] & selection
editor.scrollToCursor Last
editor.updateTargetColumn()

addCommand "editor.text.visual", "S<CHAR>", "<CHAR>", proc(editor: TextDocumentEditor, c: string) =
let (left, right) = case c
of "(", ")": ("(", ")")
of "{", "}": ("{", "}")
of "[", "]": ("[", "]")
of "<", ">": ("<", ">")
of "\"": ("\"", "\"")
of "'": ("'", "'")
else:
return

var insertSelections: Selections = @[]
var insertTexts: seq[string] = @[]
for s in editor.selections:
insertSelections.add s.first.toSelection
insertSelections.add editor.doMoveCursorColumn(s.last, 1).toSelection
insertTexts.add left
insertTexts.add right

editor.addNextCheckpoint "insert"
let newSelections = editor.insertMulti(insertSelections, insertTexts)
if newSelections.len mod 2 != 0:
return

editor.selections = collect:
for i in 0..<newSelections.len div 2:
editor.includeSelectionEnd((newSelections[i * 2].first, newSelections[i * 2 + 1].last), false)

addTextCommandBlock "visual", "gi":
editor.selections = editor.selections.mapIt(block:
if it.first.line == it.last.line and abs(it.first.column - it.last.column) < 2:
it
else:
(editor.doMoveCursorColumn(it.first, 1), editor.doMoveCursorColumn(it.last, -1))
)
editor.scrollToCursor Last
editor.updateTargetColumn()

addTextCommand "visual", "gp", "select-parent-current-ts", false

addTextCommandBlock "visual", "L":
if editor.selections.len == 1:
editor.setSearchQuery(editor.getText(editor.selection, inclusiveEnd=true))
Expand Down
Binary file modified config/wasm/keybindings_plugin.wasm
Binary file not shown.
46 changes: 41 additions & 5 deletions scripting/editor_text_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ proc doMoveCursorColumn*(self: TextDocumentEditor; cursor: Cursor; offset: int;
wrap: bool = true; includeAfter: bool = true): Cursor =
editor_text_doMoveCursorColumn_Cursor_TextDocumentEditor_Cursor_int_bool_bool_impl(
self, cursor, offset, wrap, includeAfter)
proc includeSelectionEnd*(self: TextDocumentEditor; res: Selection;
includeAfter: bool = true): Selection =
editor_text_includeSelectionEnd_Selection_TextDocumentEditor_Selection_bool_impl(
self, res, 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 @@ -102,6 +106,11 @@ proc insert*(self: TextDocumentEditor; selections: seq[Selection]; text: string;
notify: bool = true; record: bool = true): seq[Selection] =
editor_text_insert_seq_Selection_TextDocumentEditor_seq_Selection_string_bool_bool_impl(
self, selections, text, notify, record)
proc insertMulti*(self: TextDocumentEditor; selections: seq[Selection];
texts: seq[string]; notify: bool = true; record: bool = true): seq[
Selection] =
editor_text_insertMulti_seq_Selection_TextDocumentEditor_seq_Selection_seq_string_bool_bool_impl(
self, selections, texts, notify, record)
proc delete*(self: TextDocumentEditor; selections: seq[Selection];
notify: bool = true; record: bool = true;
inclusiveEnd: bool = false): seq[Selection] =
Expand All @@ -128,15 +137,42 @@ proc selectLine*(self: TextDocumentEditor; line: int) =
editor_text_selectLine_void_TextDocumentEditor_int_impl(self, line)
proc selectLineCurrent*(self: TextDocumentEditor) =
editor_text_selectLineCurrent_void_TextDocumentEditor_impl(self)
proc selectParentTs*(self: TextDocumentEditor; selection: Selection) =
editor_text_selectParentTs_void_TextDocumentEditor_Selection_impl(self,
selection)
proc getParentNodeSelection*(self: TextDocumentEditor; selection: Selection;
includeAfter: bool = true): Selection =
editor_text_getParentNodeSelection_Selection_TextDocumentEditor_Selection_bool_impl(
self, selection, includeAfter)
proc getNextNamedSiblingNodeSelection*(self: TextDocumentEditor;
selection: Selection;
includeAfter: bool = true): Option[
Selection] =
editor_text_getNextNamedSiblingNodeSelection_Option_Selection_TextDocumentEditor_Selection_bool_impl(
self, selection, includeAfter)
proc getNextSiblingNodeSelection*(self: TextDocumentEditor;
selection: Selection;
includeAfter: bool = true): Option[Selection] =
editor_text_getNextSiblingNodeSelection_Option_Selection_TextDocumentEditor_Selection_bool_impl(
self, selection, includeAfter)
proc getParentNodeSelections*(self: TextDocumentEditor; selections: Selections;
includeAfter: bool = true): Selections =
editor_text_getParentNodeSelections_Selections_TextDocumentEditor_Selections_bool_impl(
self, selections, includeAfter)
proc selectParentTs*(self: TextDocumentEditor; selection: Selection;
includeAfter: bool = true) =
editor_text_selectParentTs_void_TextDocumentEditor_Selection_bool_impl(self,
selection, includeAfter)
proc printTreesitterTree*(self: TextDocumentEditor) =
editor_text_printTreesitterTree_void_TextDocumentEditor_impl(self)
proc printTreesitterTreeUnderCursor*(self: TextDocumentEditor) =
editor_text_printTreesitterTreeUnderCursor_void_TextDocumentEditor_impl(self)
proc selectParentCurrentTs*(self: TextDocumentEditor) =
editor_text_selectParentCurrentTs_void_TextDocumentEditor_impl(self)
proc selectParentCurrentTs*(self: TextDocumentEditor; includeAfter: bool = true) =
editor_text_selectParentCurrentTs_void_TextDocumentEditor_bool_impl(self,
includeAfter)
proc getNextNodeWithSameType*(self: TextDocumentEditor; selection: Selection;
offset: int = 0; includeAfter: bool = true;
wrap: bool = true; stepIn: bool = true;
stepOut: bool = true): Option[Selection] =
editor_text_getNextNodeWithSameType_Option_Selection_TextDocumentEditor_Selection_int_bool_bool_bool_bool_impl(
self, selection, offset, includeAfter, wrap, stepIn, stepOut)
proc shouldShowCompletionsAt*(self: TextDocumentEditor; cursor: Cursor): bool =
## Returns true if the completion window should automatically open at the given position
editor_text_shouldShowCompletionsAt_bool_TextDocumentEditor_Cursor_impl(self,
Expand Down
Loading

0 comments on commit 4f5e6fb

Please sign in to comment.