Skip to content

Commit 1740cd3

Browse files
Added proper find in document implementation (#2143)
Learned of the existence of vscode command `editor.actions.findWithArgs` microsoft/vscode#200453 Exists in v1.66 https://github.com/microsoft/vscode/blob/release/1.66/src/vs/editor/contrib/find/browser/findModel.ts#L58 Fixes #1878 ## Checklist - [x] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [-] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [x] I have not broken the cheatsheet --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 8983a1e commit 1740cd3

File tree

17 files changed

+114
-71
lines changed

17 files changed

+114
-71
lines changed

cursorless-talon/src/actions/actions.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
]
5050

5151
callback_actions: dict[str, Callable[[CursorlessTarget], None]] = {
52-
"findInDocument": actions.user.private_cursorless_find,
5352
"nextHomophone": cursorless_homophones_action,
5453
}
5554

cursorless-talon/src/actions/find.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

cursorless-talon/src/apps/cursorless_vscode.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
from talon import Context, actions, app
2-
3-
from ..actions.get_text import cursorless_get_text_action
4-
from ..targets.target_types import CursorlessTarget
1+
from talon import Context, actions
52

63
ctx = Context()
74

@@ -14,17 +11,6 @@
1411

1512
@ctx.action_class("user")
1613
class Actions:
17-
def private_cursorless_find(target: CursorlessTarget):
18-
"""Find text of target in editor"""
19-
texts = cursorless_get_text_action(target, ensure_single_target=True)
20-
search_text = texts[0]
21-
if len(search_text) > 200:
22-
search_text = search_text[:200]
23-
app.notify("Search text is longer than 200 characters; truncating")
24-
actions.user.private_cursorless_run_rpc_command_no_wait("actions.find")
25-
actions.sleep("50ms")
26-
actions.insert(search_text)
27-
2814
def private_cursorless_show_settings_in_ide():
2915
"""Show Cursorless-specific settings in ide"""
3016
actions.user.private_cursorless_run_rpc_command_no_wait(

cursorless-talon/src/spoken_forms.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"reference": "showReferences",
3737
"rename": "rename",
3838
"reverse": "reverseTargets",
39+
"scout": "findInDocument",
3940
"scout all": "findInWorkspace",
4041
"shuffle": "randomizeTargets",
4142
"snippet make": "generateSnippet",
@@ -45,7 +46,6 @@
4546
"unfold": "unfoldRegion"
4647
},
4748
"callback_action": {
48-
"scout": "findInDocument",
4949
"phones": "nextHomophone"
5050
},
5151
"paste_action": { "paste": "pasteFromClipboard" },

packages/common/src/ide/PassthroughIDEBase.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ export default class PassthroughIDEBase implements IDE {
135135
return this.original.workspaceFolders;
136136
}
137137

138+
public findInDocument(query: string, editor?: TextEditor): Promise<void> {
139+
return this.original.findInDocument(query, editor);
140+
}
141+
138142
public findInWorkspace(query: string): Promise<void> {
139143
return this.original.findInWorkspace(query);
140144
}

packages/common/src/ide/fake/FakeIDE.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ export default class FakeIDE implements IDE {
8585
throw Error("Not implemented");
8686
}
8787

88+
public findInDocument(_query: string, _editor: TextEditor): Promise<void> {
89+
throw Error("Not implemented");
90+
}
91+
8892
public findInWorkspace(_query: string): Promise<void> {
8993
throw Error("Not implemented");
9094
}

packages/common/src/ide/types/ide.types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ export interface IDE {
9494
listener: (event: TextDocumentChangeEvent) => void,
9595
): Disposable;
9696

97+
/**
98+
* Find occurrences of query string in the active document.
99+
* @param query The string query to search for
100+
* @param editor The editor to search in. If not provided, uses the active editor.
101+
*/
102+
findInDocument(query: string, editor?: TextEditor): Promise<void>;
103+
97104
/**
98105
* Find occurrences of query string in all files in the workspace
99106
* @param query The string query to search for

packages/common/src/types/command/ActionDescriptor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const simpleActionNames = [
1717
"editNewLineBefore",
1818
"experimental.setInstanceReference",
1919
"extractVariable",
20+
"findInDocument",
2021
"findInWorkspace",
2122
"foldRegion",
2223
"followLink",

packages/cursorless-engine/src/actions/Actions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Deselect from "./Deselect";
1111
import { EditNew } from "./EditNew";
1212
import { EditNewAfter, EditNewBefore } from "./EditNewLineAction";
1313
import ExecuteCommand from "./ExecuteCommand";
14-
import { FindInWorkspace } from "./Find";
14+
import { FindInDocument, FindInWorkspace } from "./Find";
1515
import FollowLink from "./FollowLink";
1616
import GenerateSnippet from "./GenerateSnippet";
1717
import GetTargets from "./GetTargets";
@@ -89,6 +89,7 @@ export class Actions implements ActionRecord {
8989
);
9090
executeCommand = new ExecuteCommand(this.rangeUpdater);
9191
extractVariable = new ExtractVariable(this.rangeUpdater);
92+
findInDocument = new FindInDocument(this);
9293
findInWorkspace = new FindInWorkspace(this);
9394
foldRegion = new Fold(this.rangeUpdater);
9495
followLink = new FollowLink(this);

packages/cursorless-engine/src/actions/Find.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { ide } from "../singletons/ide.singleton";
33
import { Target } from "../typings/target.types";
44
import { ensureSingleTarget } from "../util/targetUtils";
55
import { Actions } from "./Actions";
6-
import { SimpleAction, ActionReturnValue } from "./actions.types";
6+
import { ActionReturnValue, SimpleAction } from "./actions.types";
77

8-
export class FindInWorkspace implements SimpleAction {
8+
abstract class Find implements SimpleAction {
99
constructor(private actions: Actions) {
1010
this.run = this.run.bind(this);
1111
}
@@ -29,8 +29,22 @@ export class FindInWorkspace implements SimpleAction {
2929
query = text;
3030
}
3131

32-
await ide().findInWorkspace(query);
32+
await this.find(query);
3333

3434
return { thatTargets };
3535
}
36+
37+
protected abstract find(query: string): Promise<void>;
38+
}
39+
40+
export class FindInDocument extends Find {
41+
protected find(query: string): Promise<void> {
42+
return ide().findInDocument(query);
43+
}
44+
}
45+
46+
export class FindInWorkspace extends Find {
47+
protected find(query: string): Promise<void> {
48+
return ide().findInWorkspace(query);
49+
}
3650
}

0 commit comments

Comments
 (0)