Skip to content

Commit 57aff0e

Browse files
Refactoring
1 parent 640c774 commit 57aff0e

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/SurroundingPairScopeHandler/RangeLookupTree.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import type { Range } from "@cursorless/common";
2-
import { RangeTreeNode } from "./RangeTreeNode";
32
import { RangeLookupList } from "./RangeLookupList";
43

54
/**
65
* A tree of ranges that allows for efficient lookup of ranges that contain a search item.
76
* The items must be sorted in document order.
87
*/
98
export class RangeLookupTree<T extends { range: Range }> {
10-
private children: RangeLookupList<RangeTreeNode<T>>;
9+
private children: RangeLookupList<RangeLookupTreeNode<T>>;
1110

1211
/**
1312
* @param items The items to search in. Must be sorted in document order.
@@ -25,12 +24,12 @@ export class RangeLookupTree<T extends { range: Range }> {
2524

2625
function createNodes<T extends { range: Range }>(
2726
items: T[],
28-
): RangeLookupList<RangeTreeNode<T>> {
29-
const results: RangeTreeNode<T>[] = [];
30-
const parents: RangeTreeNode<T>[] = [];
27+
): RangeLookupList<RangeLookupTreeNode<T>> {
28+
const results: RangeLookupTreeNode<T>[] = [];
29+
const parents: RangeLookupTreeNode<T>[] = [];
3130

3231
for (const item of items) {
33-
const node = new RangeTreeNode(item);
32+
const node = new RangeLookupTreeNode(item);
3433

3534
while (
3635
parents.length > 0 &&
@@ -52,3 +51,27 @@ function createNodes<T extends { range: Range }>(
5251

5352
return new RangeLookupList(results);
5453
}
54+
55+
class RangeLookupTreeNode<T extends { range: Range }> {
56+
private children: RangeLookupList<RangeLookupTreeNode<T>>;
57+
58+
constructor(private item: T) {
59+
this.children = new RangeLookupList([]);
60+
}
61+
62+
get range(): Range {
63+
return this.item.range;
64+
}
65+
66+
addChildNode(node: RangeLookupTreeNode<T>) {
67+
this.children.add(node);
68+
}
69+
70+
getSmallLestContaining(range: Range): T {
71+
const child = this.children
72+
.getContaining(range)
73+
?.getSmallLestContaining(range);
74+
75+
return child ?? this.item;
76+
}
77+
}

packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/SurroundingPairScopeHandler/RangeTreeNode.ts

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

0 commit comments

Comments
 (0)