Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate text fragment to scm #2336

Merged
merged 4 commits into from
May 17, 2024
Merged
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
23 changes: 23 additions & 0 deletions data/fixtures/recorded/languages/ruby/changeRound3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
languageId: ruby
command:
version: 7
spokenForm: change round
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: surroundingPair, delimiter: parentheses}
usePrePhraseSnapshot: true
initialState:
documentContents: "# (aaa)"
selections:
- anchor: {line: 0, character: 3}
active: {line: 0, character: 3}
marks: {}
finalState:
documentContents: "# "
selections:
- anchor: {line: 0, character: 2}
active: {line: 0, character: 2}
3 changes: 0 additions & 3 deletions packages/cursorless-engine/src/languages/csharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
leadingMatcher,
matcher,
trailingMatcher,
typeMatcher,
conditionMatcher,
patternMatcher,
} from "../util/nodeMatchers";
Expand Down Expand Up @@ -134,7 +133,6 @@ const getMapMatchers = {
(node: SyntaxNode) => node.childForFieldName("initializer"),
]),
),
string: typeMatcher("string_literal"),
};

const nodeMatchers: Partial<
Expand Down Expand Up @@ -169,7 +167,6 @@ const nodeMatchers: Partial<
),
namedFunction: NAMED_FUNCTION_TYPES,
functionName: NAMED_FUNCTION_TYPES.map((t) => t + "[name]"),
comment: "comment",
regularExpression: "regex",
type: trailingMatcher(["*[type]"]),
name: [
Expand Down
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
5 changes: 4 additions & 1 deletion queries/c.scm
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
(if_statement) @ifStatement

(string_literal) @string @textFragment
(
(string_literal) @string @textFragment
(#child-range! @textFragment 0 -1 true true)
)
(comment) @comment @textFragment
5 changes: 5 additions & 0 deletions queries/csharp.scm
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
(if_statement) @ifStatement
(class_declaration) @class
(
(string_literal) @string @textFragment
(#child-range! @textFragment 0 -1 true true)
)
(comment) @comment @textFragment
11 changes: 7 additions & 4 deletions queries/go.scm
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
(var_declaration)
] @statement

[
(interpreted_string_literal)
(raw_string_literal)
] @string @textFragment
(
[
(interpreted_string_literal)
(raw_string_literal)
] @string @textFragment
(#child-range! @textFragment 0 -1 true true)
)

(comment) @comment @textFragment

Expand Down
9 changes: 8 additions & 1 deletion queries/ruby.scm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(comment) @comment
(comment) @comment @textFragment
(hash) @map
(regex) @regularExpression
(call) @functionCall
Expand Down Expand Up @@ -29,3 +29,10 @@
name: (_) @className
) @_.domain
) @_.iteration

(string) @string

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

(
[
(raw_string_literal)
(string_literal)
] @string @textFragment
(#child-range! @textFragment 0 -1 true true)
)

[
(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
Loading