Skip to content

Commit

Permalink
Added new command for searching in buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Jan 17, 2025
1 parent b774902 commit fdf0e2c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
2 changes: 1 addition & 1 deletion config/keybindings_vim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ proc loadVimKeybindings*() {.expose("load-vim-keybindings").} =
editor.updateTargetColumn()

addTextCommandBlockDesc "", "/", "open search bar":
editor.openSearchQuery()
editor.openSearchBar()
if getActiveEditor().isTextEditor(editor):
editor.setMode("insert")

Expand Down
Binary file modified config/wasm/keybindings_plugin.wasm
Binary file not shown.
10 changes: 5 additions & 5 deletions scripting/editor_text_api_wasm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1913,18 +1913,18 @@ proc setSearchQuery*(self: TextDocumentEditor; query: string;
argsJsonString.cstring)


proc editor_text_openSearchQuery_void_TextDocumentEditor_string_bool_bool_wasm(
proc editor_text_openSearchBar_void_TextDocumentEditor_string_bool_bool_wasm(
arg: cstring): cstring {.importc.}
proc openSearchQuery*(self: TextDocumentEditor; query: string = "";
scrollToPreview: bool = true; select: bool = true) {.
gcsafe, raises: [].} =
proc openSearchBar*(self: TextDocumentEditor; query: string = "";
scrollToPreview: bool = true; select: bool = true) {.gcsafe,
raises: [].} =
var argsJson = newJArray()
argsJson.add self.toJson()
argsJson.add query.toJson()
argsJson.add scrollToPreview.toJson()
argsJson.add select.toJson()
let argsJsonString = $argsJson
let res {.used.} = editor_text_openSearchQuery_void_TextDocumentEditor_string_bool_bool_wasm(
let res {.used.} = editor_text_openSearchBar_void_TextDocumentEditor_string_bool_bool_wasm(
argsJsonString.cstring)


Expand Down
43 changes: 43 additions & 0 deletions src/text/text_editor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2543,6 +2543,49 @@ proc setSearchQuery*(self: TextDocumentEditor, query: string, escapeRegex: bool
self.searchQuery = finalQuery
self.updateSearchResults()

proc openSearchBar*(self: TextDocumentEditor, query: string = "", scrollToPreview: bool = true, select: bool = true) {.expose("editor.text").} =
let commandLineEditor = self.editors.commandLineEditor.TextDocumentEditor
if commandLineEditor == self:
return

let prevSearchQuery = self.searchQuery
self.commands.openCommandLine "", proc(command: Option[string]): bool =
if command.getSome(command):
self.setSearchQuery(command)
if select:
self.selection = self.getNextFindResult(self.selection.last).first.toSelection
self.scrollToCursor(self.selection.last)
else:
self.setSearchQuery(prevSearchQuery)
if scrollToPreview:
self.scrollToCursor(self.selection.last)

let document = commandLineEditor.document

commandLineEditor.disableCompletions = true
commandLineEditor.moveLast("file")
commandLineEditor.updateTargetColumn()

var onEditHandle = Id.new
var onActiveHandle = Id.new
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 \"))

onActiveHandle[] = commandLineEditor.onActiveChanged.subscribe proc(editor: DocumentEditor) =
if not editor.active:
document.onEdit.unsubscribe(onEditHandle[])
commandLineEditor.onActiveChanged.unsubscribe(onActiveHandle[])
self.onSearchResultsUpdated.unsubscribe(onSearchHandle[])

onSearchHandle[] = self.onSearchResultsUpdated.subscribe proc(_: TextDocumentEditor) =
if self.searchResults.len == 0:
self.scrollToCursor(self.selection.last)
else:
let s = self.getNextFindResult(self.selection.last)
if scrollToPreview:
self.scrollToCursor(s.last)

proc setSearchQueryFromMove*(self: TextDocumentEditor, move: string,
count: int = 0, prefix: string = "", suffix: string = ""): Selection {.expose("editor.text").} =
Expand Down

0 comments on commit fdf0e2c

Please sign in to comment.