Skip to content

Commit

Permalink
Fixed some vim keybindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Feb 2, 2025
1 parent cc5e9d5 commit 3e00d42
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
28 changes: 17 additions & 11 deletions config/keybindings_vim.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import std/[strutils, macros, genasts, sequtils, sets, algorithm]
import plugin_runtime, keybindings_normal
import misc/[timer, util, myjsonutils, custom_unicode, id]
import misc/[timer, util, myjsonutils, custom_unicode, id, regex]
import input_api

embedSource()
Expand Down Expand Up @@ -1059,7 +1059,7 @@ proc loadVimKeybindings*() {.expose("load-vim-keybindings").} =
addTextCommandBlockDesc "", "*", "set search query to word":
editor.selection = editor.setSearchQueryFromMove("word", prefix=r"\b", suffix=r"\b").first.toSelection
addTextCommandBlockDesc "visual", "*", "set search query to selection":
editor.setSearchQuery(editor.getText(editor.selection, inclusiveEnd=true), escapeRegex=true)
discard editor.setSearchQuery(editor.getText(editor.selection, inclusiveEnd=true), escapeRegex=true)
editor.selection = editor.selection.first.toSelection
editor.setMode("normal")
addTextCommandBlockDesc "", "n", "go to next search result":
Expand Down Expand Up @@ -1438,6 +1438,21 @@ proc loadVimKeybindings*() {.expose("load-vim-keybindings").} =

editor.setMode("visual")

addTextCommandBlock "visual", "L":
if editor.selections.len == 1:
let text = editor.getText(editor.selection, inclusiveEnd=true)
let textEscaped = text.escapeRegex
let currentSearchQuery = editor.getSearchQuery()
# infof"'{text}' -> '{textEscaped}' -> '{currentSearchQuery}'"
if textEscaped != currentSearchQuery and r"\b" & textEscaped & r"\b" != currentSearchQuery:
if editor.setSearchQuery(text, escapeRegex=true):
return

let next = editor.getNextFindResult(editor.selection.last, includeAfter=false)
editor.selections = editor.selections & next
editor.scrollToCursor Last
editor.updateTargetColumn()

addTextCommandBlock "", "H":
if editor.selections.len == 1:
var selection = editor.setSearchQueryFromMove("word", prefix=r"\b", suffix=r"\b")
Expand Down Expand Up @@ -1522,15 +1537,6 @@ proc loadVimKeybindings*() {.expose("load-vim-keybindings").} =

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

addTextCommandBlock "visual", "L":
if editor.selections.len == 1:
editor.setSearchQuery(editor.getText(editor.selection, inclusiveEnd=true), escapeRegex=true)

let next = editor.getNextFindResult(editor.selection.last, includeAfter=false)
editor.selections = editor.selections & next
editor.scrollToCursor Last
editor.updateTargetColumn()

addTextCommandBlock "visual", "o":
editor.selections = editor.selections.mapIt((it.last, it.first))
editor.scrollToCursor Last
Expand Down
Binary file modified config/wasm/keybindings_plugin.wasm
Binary file not shown.
24 changes: 21 additions & 3 deletions scripting/editor_text_api_wasm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1937,20 +1937,38 @@ proc moveFirst*(self: TextDocumentEditor; move: string;
argsJsonString.cstring)


proc editor_text_setSearchQuery_void_TextDocumentEditor_string_bool_string_string_wasm(
proc editor_text_getSearchQuery_string_TextDocumentEditor_wasm(arg: cstring): cstring {.
importc.}
proc getSearchQuery*(self: TextDocumentEditor): string {.gcsafe, raises: [].} =
var argsJson = newJArray()
argsJson.add self.toJson()
let argsJsonString = $argsJson
let res {.used.} = editor_text_getSearchQuery_string_TextDocumentEditor_wasm(
argsJsonString.cstring)
try:
result = parseJson($res).jsonTo(typeof(result))
except:
raiseAssert(getCurrentExceptionMsg())


proc editor_text_setSearchQuery_bool_TextDocumentEditor_string_bool_string_string_wasm(
arg: cstring): cstring {.importc.}
proc setSearchQuery*(self: TextDocumentEditor; query: string;
escapeRegex: bool = false; prefix: string = "";
suffix: string = "") {.gcsafe, raises: [].} =
suffix: string = ""): bool {.gcsafe, raises: [].} =
var argsJson = newJArray()
argsJson.add self.toJson()
argsJson.add query.toJson()
argsJson.add escapeRegex.toJson()
argsJson.add prefix.toJson()
argsJson.add suffix.toJson()
let argsJsonString = $argsJson
let res {.used.} = editor_text_setSearchQuery_void_TextDocumentEditor_string_bool_string_string_wasm(
let res {.used.} = editor_text_setSearchQuery_bool_TextDocumentEditor_string_bool_string_string_wasm(
argsJsonString.cstring)
try:
result = parseJson($res).jsonTo(typeof(result))
except:
raiseAssert(getCurrentExceptionMsg())


proc editor_text_openSearchBar_void_TextDocumentEditor_string_bool_bool_wasm(
Expand Down
16 changes: 10 additions & 6 deletions src/text/text_editor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2643,8 +2643,11 @@ proc moveFirst*(self: TextDocumentEditor, move: string, which: SelectionCursor =
self.scrollToCursor(which)
self.updateTargetColumn(which)

proc getSearchQuery*(self: TextDocumentEditor): string {.expose("editor.text").} =
return self.searchQuery

proc setSearchQuery*(self: TextDocumentEditor, query: string, escapeRegex: bool = false,
prefix: string = "", suffix: string = "") {.expose("editor.text").} =
prefix: string = "", suffix: string = ""): bool {.expose("editor.text").} =

let query = if escapeRegex:
query.escapeRegex
Expand All @@ -2653,10 +2656,11 @@ proc setSearchQuery*(self: TextDocumentEditor, query: string, escapeRegex: bool

let finalQuery = prefix & query & suffix
if self.searchQuery == finalQuery:
return
return false

self.searchQuery = finalQuery
self.updateSearchResults()
return true

proc openSearchBar*(self: TextDocumentEditor, query: string = "", scrollToPreview: bool = true, select: bool = true) {.expose("editor.text").} =
let commandLineEditor = self.editors.commandLineEditor.TextDocumentEditor
Expand All @@ -2666,12 +2670,12 @@ proc openSearchBar*(self: TextDocumentEditor, query: string = "", scrollToPrevie
let prevSearchQuery = self.searchQuery
self.commands.openCommandLine "", proc(command: Option[string]): bool =
if command.getSome(command):
self.setSearchQuery(command)
discard self.setSearchQuery(command)
if select:
self.selection = self.getNextFindResult(self.selection.last).first.toSelection
self.scrollToCursor(self.selection.last)
else:
self.setSearchQuery(prevSearchQuery)
discard self.setSearchQuery(prevSearchQuery)
if scrollToPreview:
self.scrollToCursor(self.selection.last)

Expand All @@ -2686,7 +2690,7 @@ proc openSearchBar*(self: TextDocumentEditor, query: string = "", scrollToPrevie
var onSearchHandle = Id.new

onEditHandle[] = document.onEdit.subscribe proc(arg: tuple[document: TextDocument, edits: seq[tuple[old, new: Selection]]]) =
self.setSearchQuery(arg.document.contentString.replace(r".set-search-query \"))
discard self.setSearchQuery(arg.document.contentString.replace(r".set-search-query \"))

onActiveHandle[] = commandLineEditor.onActiveChanged.subscribe proc(editor: DocumentEditor) =
if not editor.active:
Expand All @@ -2706,7 +2710,7 @@ proc setSearchQueryFromMove*(self: TextDocumentEditor, move: string,
count: int = 0, prefix: string = "", suffix: string = ""): Selection {.expose("editor.text").} =
let selection = self.getSelectionForMove(self.selection.last, move, count)
let searchText = self.document.contentString(selection)
self.setSearchQuery(searchText, escapeRegex=true, prefix, suffix)
discard self.setSearchQuery(searchText, escapeRegex=true, prefix, suffix)
return selection

proc toggleLineComment*(self: TextDocumentEditor) {.expose("editor.text").} =
Expand Down

0 comments on commit 3e00d42

Please sign in to comment.