Skip to content

Commit

Permalink
Migrate text fragment to scm
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasArvidsson committed May 16, 2024
1 parent dbb5fb8 commit a783895
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 163 deletions.
2 changes: 1 addition & 1 deletion packages/cursorless-engine/src/languages/getNodeMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function getNodeMatcher(
const matcher = matchers[scopeTypeType];

if (matcher == null) {
return notSupported;
return notSupported(scopeTypeType);
}

if (includeSiblings) {
Expand Down
117 changes: 0 additions & 117 deletions packages/cursorless-engine/src/languages/getTextFragmentExtractor.ts

This file was deleted.

21 changes: 4 additions & 17 deletions packages/cursorless-engine/src/languages/ruby.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { SimpleScopeTypeType } from "@cursorless/common";
import type { SyntaxNode } from "web-tree-sitter";
import { NodeMatcherAlternative } from "../typings/Types";
import { patternFinder } from "../util/nodeFinders";
import {
ancestorChainNodeMatcher,
argumentMatcher,
Expand All @@ -9,11 +13,6 @@ import {
patternMatcher,
trailingMatcher,
} from "../util/nodeMatchers";
import { NodeMatcherAlternative, SelectionWithEditor } from "../typings/Types";
import { SimpleScopeTypeType } from "@cursorless/common";
import type { SyntaxNode } from "web-tree-sitter";
import { getNodeRange } from "../util/nodeSelectors";
import { patternFinder } from "../util/nodeFinders";

// Generated by the following command:
// curl https://raw.githubusercontent.com/tree-sitter/tree-sitter-ruby/1ebfdb288842dae5a9233e2509a135949023dd82/src/node-types.json \
Expand Down Expand Up @@ -161,7 +160,6 @@ const nodeMatchers: Partial<
1,
),
),
string: "string",
functionName: ["method[name]", "singleton_method[name]"],
anonymousFunction: cascadingMatcher(
patternMatcher("lambda", "do_block"),
Expand Down Expand Up @@ -194,14 +192,3 @@ const nodeMatchers: Partial<
collectionItem: argumentMatcher(...mapTypes, ...listTypes),
};
export const patternMatchers = createPatternMatchers(nodeMatchers);

export function stringTextFragmentExtractor(
node: SyntaxNode,
_selection: SelectionWithEditor,
) {
if (node.type === "string_content" || node.type === "heredoc_content") {
return getNodeRange(node);
}

return null;
}
2 changes: 0 additions & 2 deletions packages/cursorless-engine/src/languages/rust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ const nodeMatchers: Partial<
],
1,
),
string: ["raw_string_literal", "string_literal"],
condition: cascadingMatcher(
patternMatcher("while_expression[condition]", "if_expression[condition]"),
matcher(
Expand All @@ -159,7 +158,6 @@ const nodeMatchers: Partial<
),
functionCall: ["call_expression", "macro_invocation", "struct_expression"],
functionCallee: "call_expression[function]",
comment: ["line_comment", "block_comment"],
list: ["array_expression", "tuple_expression"],
collectionItem: argumentMatcher(
"array_expression",
Expand Down
3 changes: 0 additions & 3 deletions packages/cursorless-engine/src/languages/scala.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ const nodeMatchers: Partial<
"trait_definition[name]",
],

string: ["interpolated_string_expression", "string"],
comment: "comment",

// list.size(), does not count foo.size (field_expression), or foo size (postfix_expression)
functionCall: "call_expression",
namedFunction: "function_definition",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import {
ComplexSurroundingPairName,
Selection,
SurroundingPairScopeType,
} from "@cursorless/common";
import type { SyntaxNode } from "web-tree-sitter";
import { LanguageDefinitions } from "../../../languages/LanguageDefinitions";
import getTextFragmentExtractor from "../../../languages/getTextFragmentExtractor";
import { Target } from "../../../typings/target.types";
import { SurroundingPairTarget } from "../../targets";
import { getContainingScopeTarget } from "../getContainingScopeTarget";
Expand Down Expand Up @@ -117,21 +115,7 @@ function processSurroundingPairCore(
return containingScope?.[0].contentRange;
}

// Then try to use the legacy text fragment extractor if it exists
const textFragmentExtractor = getTextFragmentExtractor(document.languageId);

if (textFragmentExtractor == null) {
// If the text fragment extractor doesn't exist, or if it explicitly is
// set to `null`, then we just use text-based algorithm on entire document
return document.range;
}

const selectionWithEditor = {
editor,
selection: new Selection(range.start, range.end),
};

return textFragmentExtractor(node, selectionWithEditor);
return document.range;
})();

if (textFragmentRange != null) {
Expand Down
11 changes: 5 additions & 6 deletions packages/cursorless-engine/src/util/nodeMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,11 @@ export function cascadingMatcher(...matchers: NodeMatcher[]): NodeMatcher {
};
}

export const notSupported: NodeMatcher = (
_selection: SelectionWithEditor,
_node: SyntaxNode,
) => {
throw new Error("Node type not supported");
};
export function notSupported(scopeTypeType: SimpleScopeTypeType): NodeMatcher {
return (_selection: SelectionWithEditor, _node: SyntaxNode) => {
throw new Error(`Node type '${scopeTypeType}' not supported`);
};
}

export function createPatternMatchers(
nodeMatchers: Partial<Record<SimpleScopeTypeType, NodeMatcherAlternative>>,
Expand Down
7 changes: 7 additions & 0 deletions queries/ruby.scm
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@
name: (_) @className
) @_.domain
) @_.iteration

(string) @string

[
(string_content)
(heredoc_content)
] @textFragment
10 changes: 10 additions & 0 deletions queries/rust.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,13 @@
(if_expression)
(if_let_expression)
] @ifStatement

[
(raw_string_literal)
(string_literal)
] @string @textFragment

[
(line_comment)
(block_comment)
] @comment @textFragment
7 changes: 7 additions & 0 deletions queries/scala.scm
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
(if_expression) @ifStatement

[
(string)
(interpolated_string_expression)
] @string @textFragment

(comment) @comment @textFragment

0 comments on commit a783895

Please sign in to comment.