Skip to content

Commit

Permalink
Rename lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasArvidsson committed Dec 26, 2024
1 parent 1d7b1f0 commit e48da99
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Range } from "@cursorless/common";
* An iterator that allows for efficient lookup of ranges that contain a search item.
* The items must be sorted in document order.
*/
export class RangeIterator<T extends { range: Range }> {
export class RangeLookupList<T extends { range: Range }> {
private index = 0;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import type { Range } from "@cursorless/common";
import { RangeTreeNode } from "./RangeTreeNode";
import { RangeLookupList } from "./RangeLookupList";

export class RangeLookupTree<T extends { range: Range }> {
private children: RangeLookupList<RangeTreeNode<T>>;

constructor(items: T[]) {
this.children = createNodes(items);
}

getSmallLestContaining(separator: Range): T | undefined {
return this.children
.getContaining(separator)
?.getSmallLestContaining(separator);
}
}

/**
* Creates a tree of ranges from a list of ranges. This improves containing lookup time.
* @param items The ranges to create a tree from. They must be sorted in document order.
* @returns The root nodes of the tree.
*/
export function createRangeTree<T extends { range: Range }>(
function createNodes<T extends { range: Range }>(
items: T[],
): RangeTreeNode<T>[] {
): RangeLookupList<RangeTreeNode<T>> {
const results: RangeTreeNode<T>[] = [];
const parents: RangeTreeNode<T>[] = [];

Expand All @@ -33,5 +48,5 @@ export function createRangeTree<T extends { range: Range }>(
parents.push(node);
}

return results;
return new RangeLookupList(results);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
} from "@cursorless/common";
import type { LanguageDefinition } from "../../../../languages/LanguageDefinition";
import type { QueryCapture } from "../../../../languages/TreeSitterQuery/QueryCapture";
import { createRangeTree } from "./createRangeTree";
import { getDelimiterRegex } from "./getDelimiterRegex";
import { RangeIterator } from "./RangeIterator";
import { RangeLookupList } from "./RangeLookupList";
import { RangeLookupTree } from "./RangeLookupTree";
import type { DelimiterOccurrence, IndividualDelimiter } from "./types";

/**
Expand All @@ -28,14 +28,12 @@ export function getDelimiterOccurrences(
return [];
}

const disqualifyDelimiters = new RangeIterator(
const disqualifyDelimiters = new RangeLookupList(
getSortedCaptures(languageDefinition, document, "disqualifyDelimiter"),
);
const textFragments = new RangeIterator(
// We need to create a tree for text fragments since they can be nested
createRangeTree(
getSortedCaptures(languageDefinition, document, "textFragment"),
),
// We need a tree for text fragments since they can be nested
const textFragments = new RangeLookupTree(
getSortedCaptures(languageDefinition, document, "textFragment"),
);

const delimiterTextToDelimiterInfoMap = Object.fromEntries(
Expand Down Expand Up @@ -63,9 +61,8 @@ export function getDelimiterOccurrences(
const isDisqualified = delimiter != null && !delimiter.hasError();

if (!isDisqualified) {
const textFragmentRange = textFragments
.getContaining(range)
?.getSmallLestContaining(range).range;
const textFragmentRange =
textFragments.getSmallLestContaining(range)?.range;
results.push({
delimiterInfo: delimiterTextToDelimiterInfoMap[text],
textFragmentRange,
Expand Down

0 comments on commit e48da99

Please sign in to comment.