Skip to content

Commit 4c2ec1c

Browse files
moved get node at location into editor
1 parent 9b6598b commit 4c2ec1c

File tree

15 files changed

+64
-95
lines changed

15 files changed

+64
-95
lines changed

src/core/Debug.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Location } from "@cursorless/common";
21
import { fromVscodeRange } from "@cursorless/vscode-common";
32
import {
43
Disposable,
@@ -87,14 +86,11 @@ export default class Debug {
8786
}
8887

8988
private logBranchTypes(event: TextEditorSelectionChangeEvent) {
90-
const location = new Location(
91-
ide().activeTextEditor!.document.uri,
92-
fromVscodeRange(event.selections[0]),
93-
);
94-
9589
let node: SyntaxNode;
9690
try {
97-
node = this.graph.getNodeAtLocation(location);
91+
node = ide().activeTextEditor!.getNodeAtLocation(
92+
fromVscodeRange(event.selections[0]),
93+
);
9894
} catch (error) {
9995
return;
10096
}

src/core/commandRunner/CommandRunner.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ export default class CommandRunner {
121121
hatTokenMap: readableHatMap,
122122
thatMark: this.thatMark.exists() ? this.thatMark.get() : [],
123123
sourceMark: this.sourceMark.exists() ? this.sourceMark.get() : [],
124-
getNodeAtLocation: this.graph.getNodeAtLocation,
125124
};
126125

127126
if (this.graph.testCaseRecorder.isActive()) {

src/extension.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ import makeGraph, { FactoryMap } from "./util/makeGraph";
2929
export async function activate(
3030
context: vscode.ExtensionContext,
3131
): Promise<CursorlessApi> {
32-
const { getNodeAtLocation } = await getParseTreeApi();
32+
const parseTreeApi = await getParseTreeApi();
3333
const commandServerApi = await getCommandServerApi();
3434

35-
const vscodeIDE = new VscodeIDE(context);
35+
const vscodeIDE = new VscodeIDE(context, parseTreeApi);
3636

3737
if (isTesting()) {
3838
// FIXME: At some point we'll probably want to support partial mocking
@@ -48,7 +48,6 @@ export async function activate(
4848
...graphFactories,
4949
extensionContext: () => context,
5050
commandServerApi: () => commandServerApi,
51-
getNodeAtLocation: () => getNodeAtLocation,
5251
} as FactoryMap<Graph>);
5352
graph.debug.init();
5453
graph.snippets.init();

src/ide/vscode/VscodeEditableTextEditorImpl.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
TextEditorOptions,
99
} from "@cursorless/common";
1010
import {
11+
ParseTreeApi,
1112
toVscodePositionOrRange,
1213
toVscodeRange,
1314
toVscodeSelection,
@@ -22,8 +23,8 @@ export class VscodeEditableTextEditorImpl
2223
extends VscodeTextEditorImpl
2324
implements EditableTextEditor
2425
{
25-
constructor(editor: vscode.TextEditor) {
26-
super(editor);
26+
constructor(parseTreeApi: ParseTreeApi, editor: vscode.TextEditor) {
27+
super(parseTreeApi, editor);
2728
}
2829

2930
get selections(): Selection[] {

src/ide/vscode/VscodeIDE.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { EditableTextEditor, TextEditor } from "@cursorless/common";
2-
import { toVscodeEditor } from "@cursorless/vscode-common";
2+
import { ParseTreeApi, toVscodeEditor } from "@cursorless/vscode-common";
33
import { pull } from "lodash";
44
import type * as vscode from "vscode";
55
import { ExtensionContext, window, workspace, WorkspaceFolder } from "vscode";
@@ -25,7 +25,10 @@ export default class VscodeIDE implements IDE {
2525
clipboard: VscodeClipboard;
2626
private editorMap;
2727

28-
constructor(private extensionContext: ExtensionContext) {
28+
constructor(
29+
private extensionContext: ExtensionContext,
30+
private parseTreeApi: ParseTreeApi,
31+
) {
2932
this.configuration = new VscodeConfiguration(this);
3033
this.globalState = new VscodeGlobalState(extensionContext);
3134
this.messages = new VscodeMessages();
@@ -53,7 +56,10 @@ export default class VscodeIDE implements IDE {
5356

5457
get activeEditableTextEditor(): EditableTextEditor | undefined {
5558
return window.activeTextEditor != null
56-
? new VscodeEditableTextEditorImpl(window.activeTextEditor)
59+
? new VscodeEditableTextEditorImpl(
60+
this.parseTreeApi,
61+
window.activeTextEditor,
62+
)
5763
: undefined;
5864
}
5965

@@ -62,7 +68,10 @@ export default class VscodeIDE implements IDE {
6268
}
6369

6470
public getEditableTextEditor(editor: TextEditor): EditableTextEditor {
65-
return new VscodeEditableTextEditorImpl(toVscodeEditor(editor));
71+
return new VscodeEditableTextEditorImpl(
72+
this.parseTreeApi,
73+
toVscodeEditor(editor),
74+
);
6675
}
6776

6877
public onDidChangeTextDocument(
@@ -73,7 +82,10 @@ export default class VscodeIDE implements IDE {
7382

7483
public fromVscodeEditor(editor: vscode.TextEditor): TextEditor {
7584
if (!this.editorMap.has(editor)) {
76-
this.editorMap.set(editor, new VscodeTextEditorImpl(editor));
85+
this.editorMap.set(
86+
editor,
87+
new VscodeTextEditorImpl(this.parseTreeApi, editor),
88+
);
7789
}
7890
return this.editorMap.get(editor)!;
7991
}

src/ide/vscode/VscodeTextEditorImpl.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {
2+
Position,
23
Range,
34
Selection,
45
TextDocument,
@@ -8,16 +9,22 @@ import type {
89
import {
910
fromVscodeRange,
1011
fromVscodeSelection,
12+
ParseTreeApi,
13+
toVscodePositionOrRange,
1114
} from "@cursorless/vscode-common";
1215
import { v4 as uuid } from "uuid";
1316
import * as vscode from "vscode";
17+
import { SyntaxNode } from "web-tree-sitter";
1418
import { fromVscodeDocument } from "./vscodeIdeUtil";
1519

1620
export class VscodeTextEditorImpl implements TextEditor {
1721
readonly id: string;
1822
readonly document: TextDocument;
1923

20-
constructor(protected editor: vscode.TextEditor) {
24+
constructor(
25+
protected parseTreeApi: ParseTreeApi,
26+
protected editor: vscode.TextEditor,
27+
) {
2128
this.id = uuid();
2229
this.document = fromVscodeDocument(editor.document);
2330
}
@@ -42,6 +49,15 @@ export class VscodeTextEditorImpl implements TextEditor {
4249
return this.editor === vscode.window.activeTextEditor;
4350
}
4451

52+
public getNodeAtLocation(positionOrRange: Position | Range): SyntaxNode {
53+
return this.parseTreeApi.getNodeAtLocation(
54+
new vscode.Location(
55+
this.document.uri,
56+
toVscodePositionOrRange(positionOrRange),
57+
),
58+
);
59+
}
60+
4561
public isEqual(other: TextEditor): boolean {
4662
return this.id === other.id;
4763
}

src/libs/common/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export { walkFilesSync } from "./util/walkSync";
1717
export { Listener, Notifier } from "./util/Notifier";
1818
export { TokenHatSplittingMode } from "./ide/types/Configuration";
1919
export * from "./ide/types/ide.types";
20-
export * from "./types/Location";
2120
export * from "./types/Position";
2221
export * from "./types/Range";
2322
export * from "./types/Selection";

src/libs/common/types/Location.ts

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

src/libs/common/types/TextEditor.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
TextEditorEdit,
88
TextEditorOptions,
99
} from "@cursorless/common";
10+
import type { SyntaxNode } from "web-tree-sitter";
1011

1112
export interface TextEditor {
1213
/**
@@ -47,6 +48,14 @@ export interface TextEditor {
4748
* @return `true` if the this text editor is equal to `other`.
4849
*/
4950
isEqual(other: TextEditor): boolean;
51+
52+
/**
53+
* Function to access nodes in the tree sitter.
54+
*
55+
* @param positionOrRange The position or range. Positions will be converted to an empty range.
56+
* @returns A {@link SyntaxNode node}
57+
*/
58+
getNodeAtLocation(positionOrRange: Position | Range): SyntaxNode;
5059
}
5160

5261
export interface EditableTextEditor extends TextEditor {

src/processTargets/modifiers/ItemStage/getIterationScope.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,11 @@ export function getIterationScope(
1616
context: ProcessedTargetsContext,
1717
target: Target,
1818
): { range: Range; boundary?: [Range, Range] } {
19-
let pairInfo = getSurroundingPair(
20-
context,
21-
target.editor,
22-
target.contentRange,
23-
);
19+
let pairInfo = getSurroundingPair(target.editor, target.contentRange);
2420

2521
// Iteration is necessary in case of nested strings
2622
while (pairInfo != null) {
2723
const stringPairInfo = getStringSurroundingPair(
28-
context,
2924
target.editor,
3025
pairInfo.contentRange,
3126
);
@@ -43,7 +38,7 @@ export function getIterationScope(
4338
};
4439
}
4540

46-
pairInfo = getParentSurroundingPair(context, target.editor, pairInfo);
41+
pairInfo = getParentSurroundingPair(target.editor, pairInfo);
4742
}
4843

4944
// We have not found a surrounding pair. Use the line.
@@ -53,7 +48,6 @@ export function getIterationScope(
5348
}
5449

5550
function getParentSurroundingPair(
56-
context: ProcessedTargetsContext,
5751
editor: TextEditor,
5852
pairInfo: SurroundingPairInfo,
5953
) {
@@ -64,27 +58,19 @@ function getParentSurroundingPair(
6458
}
6559
// Step out of this pair and see if we have a parent
6660
const position = editor.document.positionAt(startOffset - 1);
67-
return getSurroundingPair(context, editor, new Range(position, position));
61+
return getSurroundingPair(editor, new Range(position, position));
6862
}
6963

70-
function getSurroundingPair(
71-
context: ProcessedTargetsContext,
72-
editor: TextEditor,
73-
contentRange: Range,
74-
) {
75-
return processSurroundingPair(context, editor, contentRange, {
64+
function getSurroundingPair(editor: TextEditor, contentRange: Range) {
65+
return processSurroundingPair(editor, contentRange, {
7666
type: "surroundingPair",
7767
delimiter: "collectionBoundary",
7868
requireStrongContainment: true,
7969
});
8070
}
8171

82-
function getStringSurroundingPair(
83-
context: ProcessedTargetsContext,
84-
editor: TextEditor,
85-
contentRange: Range,
86-
) {
87-
return processSurroundingPair(context, editor, contentRange, {
72+
function getStringSurroundingPair(editor: TextEditor, contentRange: Range) {
73+
return processSurroundingPair(editor, contentRange, {
8874
type: "surroundingPair",
8975
delimiter: "string",
9076
requireStrongContainment: true,

src/processTargets/modifiers/SurroundingPairStage.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ function processedSurroundingPairTarget(
4242
target: Target,
4343
): SurroundingPairTarget[] {
4444
const pairInfo = processSurroundingPair(
45-
context,
4645
target.editor,
4746
target.contentRange,
4847
modifier.scopeType,

src/processTargets/modifiers/scopeTypeStages/BoundedNonWhitespaceStage.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export default class BoundedNonWhitespaceSequenceStage
2929
const paintTargets = paintStage.run(context, target);
3030

3131
const pairInfo = processSurroundingPair(
32-
context,
3332
target.editor,
3433
target.contentRange,
3534
{

src/processTargets/modifiers/scopeTypeStages/ContainingSyntaxScopeStage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Location, Selection } from "@cursorless/common";
1+
import { Selection } from "@cursorless/common";
22
import type { SyntaxNode } from "web-tree-sitter";
33
import { NoContainingScopeError } from "../../../errors";
44
import { getNodeMatcher } from "../../../languages/getNodeMatcher";
@@ -38,8 +38,8 @@ export default class implements ModifierStage {
3838
this.modifier.type === "everyScope",
3939
);
4040

41-
const node: SyntaxNode | null = context.getNodeAtLocation(
42-
new Location(target.editor.document.uri, target.contentRange),
41+
const node: SyntaxNode | null = target.editor.getNodeAtLocation(
42+
target.contentRange,
4343
);
4444

4545
const scopeNodes = findNearestContainingAncestorNode(node, nodeMatcher, {

src/processTargets/modifiers/surroundingPair/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Location, Range, Selection, TextEditor } from "@cursorless/common";
1+
import { Range, Selection, TextEditor } from "@cursorless/common";
22
import { SyntaxNode } from "web-tree-sitter";
33
import getTextFragmentExtractor, {
44
TextFragmentExtractor,
@@ -7,7 +7,6 @@ import {
77
ComplexSurroundingPairName,
88
SurroundingPairScopeType,
99
} from "../../../typings/targetDescriptor.types";
10-
import { ProcessedTargetsContext } from "../../../typings/Types";
1110
import { complexDelimiterMap } from "./delimiterMaps";
1211
import { SurroundingPairInfo } from "./extractSelectionFromSurroundingPairOffsets";
1312
import { findSurroundingPairParseTreeBased } from "./findSurroundingPairParseTreeBased";
@@ -28,7 +27,6 @@ import { findSurroundingPairTextBased } from "./findSurroundingPairTextBased";
2827
* `null` if none was found
2928
*/
3029
export function processSurroundingPair(
31-
context: ProcessedTargetsContext,
3230
editor: TextEditor,
3331
range: Range,
3432
scopeType: SurroundingPairScopeType,
@@ -42,7 +40,7 @@ export function processSurroundingPair(
4240
let textFragmentExtractor: TextFragmentExtractor;
4341

4442
try {
45-
node = context.getNodeAtLocation(new Location(document.uri, range));
43+
node = editor.getNodeAtLocation(range);
4644

4745
textFragmentExtractor = getTextFragmentExtractor(document.languageId);
4846
} catch (err) {

src/typings/Types.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import type {
2-
Location,
3-
Range,
4-
Selection,
5-
TextEditor,
6-
} from "@cursorless/common";
1+
import type { Range, Selection, TextEditor } from "@cursorless/common";
72
import { SyntaxNode } from "web-tree-sitter";
83
import { ActionRecord } from "../actions/actions.types";
94
import Cheatsheet from "../core/Cheatsheet";
@@ -46,7 +41,6 @@ export interface ProcessedTargetsContext {
4641
hatTokenMap: ReadOnlyHatMap;
4742
thatMark: Target[];
4843
sourceMark: Target[];
49-
getNodeAtLocation: (location: Location) => SyntaxNode;
5044
}
5145

5246
export interface SelectionWithEditor {
@@ -137,11 +131,6 @@ export interface Graph {
137131
*/
138132
readonly commandServerApi: CommandServerApi | null;
139133

140-
/**
141-
* Function to access nodes in the tree sitter.
142-
*/
143-
readonly getNodeAtLocation: (location: Location) => SyntaxNode;
144-
145134
/**
146135
* Debug logger
147136
*/

0 commit comments

Comments
 (0)