Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface QueryCapture {
readonly allowMultiple: boolean;

/** The insertion delimiter to use if any */
readonly insertionDelimiter: string | undefined;
readonly insertionDelimiters: string[] | undefined;
}

/**
Expand All @@ -63,7 +63,7 @@ export interface MutableQueryCapture extends QueryCapture {
readonly document: TextDocument;
range: Range;
allowMultiple: boolean;
insertionDelimiter: string | undefined;
insertionDelimiters: string[] | undefined;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class TreeSitterQuery {
node,
document,
range: getNodeRange(node),
insertionDelimiter: undefined,
insertionDelimiters: undefined,
allowMultiple: false,
})),
}),
Expand Down Expand Up @@ -114,9 +114,9 @@ export class TreeSitterQuery {
.map(({ range }) => range)
.reduce((accumulator, range) => range.union(accumulator)),
allowMultiple: captures.some((capture) => capture.allowMultiple),
insertionDelimiter: captures.find(
(capture) => capture.insertionDelimiter != null,
)?.insertionDelimiter,
insertionDelimiters: captures.find(
(capture) => capture.insertionDelimiters != null,
)?.insertionDelimiters,
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import assert from "assert";

interface TestCase {
name: string;
captures: Omit<QueryCapture, "allowMultiple" | "insertionDelimiter">[];
captures: Omit<QueryCapture, "allowMultiple" | "insertionDelimiters">[];
isValid: boolean;
expectedErrorMessageIds: string[];
}
Expand Down Expand Up @@ -192,7 +192,7 @@ suite("checkCaptureStartEnd", () => {
testCase.captures.map((capture) => ({
...capture,
allowMultiple: false,
insertionDelimiter: undefined,
insertionDelimiters: undefined,
})),
messages,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,14 @@ class Log extends QueryPredicateOperator<Log> {
*/
class InsertionDelimiter extends QueryPredicateOperator<InsertionDelimiter> {
name = "insertion-delimiter!" as const;
schema = z.tuple([q.node, q.string]);
schema = z.union([
z.tuple([q.node, q.string]),
z.tuple([q.node, q.string, q.string]),
z.tuple([q.node, q.string, q.string, q.string]),
]);

run(nodeInfo: MutableQueryCapture, insertionDelimiter: string) {
nodeInfo.insertionDelimiter = insertionDelimiter;
run(nodeInfo: MutableQueryCapture, ...insertionDelimiters: string[]) {
nodeInfo.insertionDelimiters = insertionDelimiters;

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function fillOutCapture(capture: NameRange): MutableQueryCapture {
return {
...capture,
allowMultiple: false,
insertionDelimiter: undefined,
insertionDelimiters: undefined,
document: null as unknown as TextDocument,
node: null as unknown as SyntaxNode,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class TreeSitterScopeHandler extends BaseTreeSitterScopeHandler {
return undefined;
}

const { range: contentRange, allowMultiple, insertionDelimiter } = capture;
const { range: contentRange, allowMultiple, insertionDelimiters } = capture;

const domain =
getRelatedRange(match, scopeTypeType, "domain", true) ?? contentRange;
Expand All @@ -70,6 +70,8 @@ export class TreeSitterScopeHandler extends BaseTreeSitterScopeHandler {
true,
);

const delimiter = getInsertionDelimiter(editor, match, insertionDelimiters);

return {
editor,
domain,
Expand All @@ -84,13 +86,39 @@ export class TreeSitterScopeHandler extends BaseTreeSitterScopeHandler {
leadingDelimiterRange,
trailingDelimiterRange,
interiorRange,
delimiter: insertionDelimiter,
delimiter,
}),
],
};
}
}

function getInsertionDelimiter(
editor: TextEditor,
match: QueryMatch,
insertionDelimiters?: string[],
): string | undefined {
if (insertionDelimiters == null) {
return undefined;
}

for (const delimiter of insertionDelimiters) {
if (!delimiter.startsWith("@")) {
return delimiter;
}

const capture = findCaptureByName(match, delimiter.slice(1));
if (capture != null) {
const { range } = capture;
if (!range.isEmpty) {
return editor.document.getText(range);
}
}
}

return undefined;
}

function dropEmptyRange(range?: Range) {
return range != null && !range.isEmpty ? range : undefined;
}