Skip to content

Commit 3957bed

Browse files
Started implementing set highlight for cursorless everywhere
1 parent 85fd827 commit 3957bed

File tree

8 files changed

+94
-10
lines changed

8 files changed

+94
-10
lines changed

cursorless-everywhere-talon/cursorless_everywhere_talon.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import json
2-
from typing import Any
2+
from typing import Any, Optional, Union
33

44
from talon import Context, Module, actions
55

6-
from .cursorless_everywhere_types import EditorEdit, EditorState, SelectionOffsets
6+
from .cursorless_everywhere_types import (
7+
CharacterRangeOffsets,
8+
EditorEdit,
9+
EditorState,
10+
LineRange,
11+
SelectionOffsets,
12+
)
713

814
mod = Module()
915

@@ -71,5 +77,13 @@ def private_cursorless_talonjs_run_no_wait(
7177
):
7278
"""Executes a Cursorless command, but does not wait for it to finish, nor return the response"""
7379

74-
def private_cursorless_talonjs_get_response_json() -> str:
80+
def private_cursorless_talonjs_get_response_json() -> str: # pyright: ignore [reportReturnType]
7581
"""Returns the response from the last Cursorless command"""
82+
83+
def cursorless_everywhere_set_highlight_ranges(
84+
ranges: list[Union[CharacterRangeOffsets, LineRange]], # pyright: ignore [reportGeneralTypeIssues]
85+
highlightId: Optional[str] = None,
86+
):
87+
"""Set focused element highlight ranges"""
88+
print("Setting highlight ranges", ranges, highlightId)
89+
actions.skip()

cursorless-everywhere-talon/cursorless_everywhere_types.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
from typing import TypedDict
1+
from typing import Literal, TypedDict
22

33

44
class SelectionOffsets(TypedDict):
55
anchor: int
66
active: int
77

88

9+
class CharacterRangeOffsets(TypedDict):
10+
type: Literal["character"]
11+
start: int
12+
end: int
13+
14+
15+
class LineRange(TypedDict):
16+
type: Literal["line"]
17+
start: int
18+
end: int
19+
20+
921
class EditorState(TypedDict):
1022
text: str
1123
selections: list[SelectionOffsets]

packages/cursorless-everywhere-talon-core/src/ide/TalonJsCapabilities.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { Capabilities, CommandCapabilityMap } from "@cursorless/common";
22

33
const COMMAND_CAPABILITIES: CommandCapabilityMap = {
4+
highlight: { acceptsLocation: true },
5+
46
clipboardCopy: undefined,
57
clipboardPaste: undefined,
68
toggleLineComment: undefined,
@@ -12,7 +14,6 @@ const COMMAND_CAPABILITIES: CommandCapabilityMap = {
1214
showDebugHover: undefined,
1315
extractVariable: undefined,
1416
fold: undefined,
15-
highlight: undefined,
1617
unfold: undefined,
1718
showReferences: undefined,
1819
insertLineAfter: undefined,

packages/cursorless-everywhere-talon-core/src/ide/TalonJsIDE.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { Notifier, type KeyValueStore } from "@cursorless/common";
2424
import { pull } from "lodash-es";
2525
import type { Talon } from "../types/talon.types";
2626
import type { EditorState } from "../types/types";
27+
import { setHighlightRanges } from "./setHighlightRanges";
2728
import { TalonJsCapabilities } from "./TalonJsCapabilities";
2829
import { TalonJsClipboard } from "./TalonJsClipboard";
2930
import { TalonJsConfiguration } from "./TalonJsConfiguration";
@@ -139,11 +140,11 @@ export class TalonJsIDE implements IDE {
139140
}
140141

141142
setHighlightRanges(
142-
_highlightId: string | undefined,
143-
_editor: TextEditor,
144-
_ranges: GeneralizedRange[],
143+
highlightId: string | undefined,
144+
editor: TextEditor,
145+
ranges: GeneralizedRange[],
145146
): Promise<void> {
146-
throw new Error("setHighlightRanges not implemented.");
147+
return setHighlightRanges(this.talon, editor, ranges, highlightId);
147148
}
148149

149150
onDidChangeTextDocument(
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type {
2+
GeneralizedRange,
3+
LineRange,
4+
TextEditor,
5+
} from "@cursorless/common";
6+
import type { Talon } from "../types/talon.types";
7+
import type { CharacterRangeOffsets } from "../types/types";
8+
9+
export function setHighlightRanges(
10+
talon: Talon,
11+
editor: TextEditor,
12+
ranges: GeneralizedRange[],
13+
highlightId: string | undefined,
14+
): Promise<void> {
15+
const offsetRanges = ranges.map(
16+
(range): CharacterRangeOffsets | LineRange => {
17+
if (range.type === "line") {
18+
return range;
19+
}
20+
return {
21+
type: "character",
22+
start: editor.document.offsetAt(range.start),
23+
end: editor.document.offsetAt(range.end),
24+
};
25+
},
26+
);
27+
28+
talon.actions.user.cursorless_everywhere_set_highlight_ranges(
29+
offsetRanges,
30+
highlightId,
31+
);
32+
33+
return Promise.resolve();
34+
}

packages/cursorless-everywhere-talon-core/src/types/talon.types.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import type { EditorEdit, EditorState, SelectionOffsets } from "./types";
1+
import type { LineRange } from "@cursorless/common";
2+
import type {
3+
CharacterRangeOffsets,
4+
EditorEdit,
5+
EditorState,
6+
SelectionOffsets,
7+
} from "./types";
28

39
export type TalonNamespace = "user";
410

@@ -17,6 +23,10 @@ export interface TalonActions {
1723
cursorless_everywhere_get_editor_state(): EditorState;
1824
cursorless_everywhere_set_selections(selections: SelectionOffsets[]): void;
1925
cursorless_everywhere_edit_text(edit: EditorEdit): void;
26+
cursorless_everywhere_set_highlight_ranges(
27+
ranges: (CharacterRangeOffsets | LineRange)[],
28+
highlightId: string | undefined,
29+
): void;
2030
};
2131
}
2232

packages/cursorless-everywhere-talon-core/src/types/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ export interface SelectionOffsets {
1414
active: number;
1515
}
1616

17+
export interface CharacterRangeOffsets {
18+
type: "character";
19+
start: number;
20+
end: number;
21+
}
22+
1723
export interface EditorState {
1824
text: string;
1925
languageId?: string;

packages/cursorless-everywhere-talon-e2e/src/talonMock.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ const actions: TalonActions = {
5353
}
5454
_finalEditorState.text = edit.text;
5555
},
56+
cursorless_everywhere_set_highlight_ranges(
57+
_ranges: any[],
58+
_highlightId: string | undefined,
59+
): void {
60+
// Do nothing
61+
},
5662
},
5763
};
5864

0 commit comments

Comments
 (0)