Skip to content

Commit 1453284

Browse files
Migrate PHP statements to treesitter (#1983)
This PR begins the migration of PHP to treesitter. ## Checklist - [x] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [x] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [x] I have not broken the cheatsheet --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 479b67f commit 1453284

File tree

13 files changed

+339
-98
lines changed

13 files changed

+339
-98
lines changed

packages/cursorless-engine/src/languages/getTextFragmentExtractor.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { LegacyLanguageId } from "./LegacyLanguageId";
77
import { getNodeMatcher } from "./getNodeMatcher";
88
import { stringTextFragmentExtractor as htmlStringTextFragmentExtractor } from "./html";
99
import { stringTextFragmentExtractor as jsonStringTextFragmentExtractor } from "./json";
10-
import { stringTextFragmentExtractor as phpStringTextFragmentExtractor } from "./php";
1110
import { stringTextFragmentExtractor as rubyStringTextFragmentExtractor } from "./ruby";
1211
import { stringTextFragmentExtractor as scssStringTextFragmentExtractor } from "./scss";
1312
import { stringTextFragmentExtractor as typescriptStringTextFragmentExtractor } from "./typescript";
@@ -155,10 +154,6 @@ const textFragmentExtractors: Record<
155154
),
156155
latex: fullDocumentTextFragmentExtractor,
157156
markdown: fullDocumentTextFragmentExtractor,
158-
php: constructDefaultTextFragmentExtractor(
159-
"php",
160-
phpStringTextFragmentExtractor,
161-
),
162157
ruby: constructDefaultTextFragmentExtractor(
163158
"ruby",
164159
rubyStringTextFragmentExtractor,
Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { Selection, TextEditor } from "@cursorless/common";
22
import type { SyntaxNode } from "web-tree-sitter";
33
import { SimpleScopeTypeType } from "@cursorless/common";
4-
import {
5-
NodeMatcherAlternative,
6-
SelectionWithContext,
7-
SelectionWithEditor,
8-
} from "../typings/Types";
4+
import { NodeMatcherAlternative, SelectionWithContext } from "../typings/Types";
95
import { patternFinder } from "../util/nodeFinders";
106
import {
117
argumentMatcher,
@@ -17,40 +13,6 @@ import {
1713
} from "../util/nodeMatchers";
1814
import { getNodeRange } from "../util/nodeSelectors";
1915

20-
// Generated by the following command:
21-
// > curl https://raw.githubusercontent.com/tree-sitter/tree-sitter-php/0ce134234214427b6aeb2735e93a307881c6cd6f/src/node-types.json \
22-
// | jq '[.[] | select(.type == "_statement") | .subtypes[].type]'
23-
const STATEMENT_TYPES = [
24-
"break_statement",
25-
"class_declaration",
26-
"compound_statement",
27-
"const_declaration",
28-
"continue_statement",
29-
"declare_statement",
30-
"do_statement",
31-
"echo_statement",
32-
"empty_statement",
33-
"enum_declaration",
34-
"expression_statement",
35-
"for_statement",
36-
"foreach_statement",
37-
"function_definition",
38-
"function_static_declaration",
39-
"global_declaration",
40-
"goto_statement",
41-
"if_statement",
42-
"interface_declaration",
43-
"named_label_statement",
44-
"namespace_definition",
45-
"namespace_use_declaration",
46-
"return_statement",
47-
"switch_statement",
48-
"trait_declaration",
49-
"try_statement",
50-
"unset_statement",
51-
"while_statement",
52-
];
53-
5416
// Taken from https://www.php.net/manual/en/language.operators.assignment.php
5517
const assignmentOperators = [
5618
"=",
@@ -101,38 +63,17 @@ function castTypeExtractor(
10163
const nodeMatchers: Partial<
10264
Record<SimpleScopeTypeType, NodeMatcherAlternative>
10365
> = {
104-
statement: STATEMENT_TYPES,
105-
ifStatement: "if_statement",
106-
class: "class_declaration",
107-
className: "class_declaration[name]",
10866
name: [
10967
"assignment_expression[left]",
11068
"class_declaration[name]",
11169
"function_definition[name]",
11270
"method_declaration[name]",
11371
],
114-
comment: "comment",
115-
string: "string",
11672
type: cascadingMatcher(
11773
trailingMatcher(["~cast_expression[type]"]),
11874
matcher(patternFinder("cast_expression[type]"), castTypeExtractor),
11975
),
12076

121-
namedFunction: trailingMatcher(
122-
[
123-
"function_definition",
124-
"assignment_expression.anonymous_function_creation_expression",
125-
"assignment_expression.arrow_function",
126-
],
127-
[";"],
128-
),
129-
anonymousFunction: [
130-
"anonymous_function_creation_expression",
131-
"arrow_function",
132-
],
133-
functionCall: ["function_call_expression", "object_creation_expression"],
134-
functionName: ["function_definition[name]", "method_declaration[name]"],
135-
13677
value: leadingMatcher(
13778
[
13879
"array_element_initializer[1]",
@@ -149,14 +90,3 @@ const nodeMatchers: Partial<
14990
argumentOrParameter: argumentMatcher("arguments", "formal_parameters"),
15091
};
15192
export default createPatternMatchers(nodeMatchers);
152-
153-
export function stringTextFragmentExtractor(
154-
node: SyntaxNode,
155-
_selection: SelectionWithEditor,
156-
) {
157-
if (node.type === "string") {
158-
return getNodeRange(node);
159-
}
160-
161-
return null;
162-
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
languageId: php
2+
command:
3+
version: 6
4+
spokenForm: change funk
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: namedFunction}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: |-
15+
<?php
16+
17+
class MyClass
18+
{
19+
public function helloWorld()
20+
{
21+
22+
}
23+
}
24+
selections:
25+
- anchor: {line: 6, character: 0}
26+
active: {line: 6, character: 0}
27+
marks: {}
28+
finalState:
29+
documentContents: |-
30+
<?php
31+
32+
class MyClass
33+
{
34+
35+
}
36+
selections:
37+
- anchor: {line: 4, character: 4}
38+
active: {line: 4, character: 4}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
languageId: php
2+
command:
3+
version: 6
4+
spokenForm: change funk
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: namedFunction}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: |-
15+
<?php
16+
17+
$myFunk = function() {
18+
19+
};
20+
selections:
21+
- anchor: {line: 3, character: 0}
22+
active: {line: 3, character: 0}
23+
marks: {}
24+
finalState:
25+
documentContents: |-
26+
<?php
27+
28+
;
29+
selections:
30+
- anchor: {line: 2, character: 0}
31+
active: {line: 2, character: 0}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
languageId: php
2+
command:
3+
version: 6
4+
spokenForm: change funk
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: namedFunction}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: |
15+
<?php
16+
17+
$myFunk = fn() => 'Hello world';
18+
selections:
19+
- anchor: {line: 2, character: 24}
20+
active: {line: 2, character: 24}
21+
marks: {}
22+
finalState:
23+
documentContents: |
24+
<?php
25+
26+
;
27+
selections:
28+
- anchor: {line: 2, character: 0}
29+
active: {line: 2, character: 0}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
languageId: php
2+
command:
3+
version: 6
4+
spokenForm: change list
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: list}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: |-
15+
<?php
16+
17+
$list = [
18+
'hello',
19+
'world'
20+
];
21+
selections:
22+
- anchor: {line: 3, character: 8}
23+
active: {line: 3, character: 8}
24+
marks: {}
25+
finalState:
26+
documentContents: |-
27+
<?php
28+
29+
$list = ;
30+
selections:
31+
- anchor: {line: 2, character: 8}
32+
active: {line: 2, character: 8}
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
languageId: php
22
command:
3-
version: 1
3+
version: 6
44
spokenForm: change round
5-
action: clearAndSetSelection
6-
targets:
7-
- type: primitive
8-
modifier: {type: surroundingPair, delimiter: parentheses}
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: surroundingPair, delimiter: parentheses}
12+
usePrePhraseSnapshot: true
913
initialState:
1014
documentContents: |-
1115
<?php
1216
13-
"Hello (world)";
17+
'Hello (world)';
1418
selections:
1519
- anchor: {line: 2, character: 11}
1620
active: {line: 2, character: 11}
@@ -19,7 +23,7 @@ finalState:
1923
documentContents: |-
2024
<?php
2125
22-
"Hello ";
26+
'Hello ';
2327
selections:
2428
- anchor: {line: 2, character: 7}
2529
active: {line: 2, character: 7}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
languageId: php
2+
command:
3+
version: 6
4+
spokenForm: change round
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: surroundingPair, delimiter: parentheses}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: |-
15+
<?php
16+
17+
"Hello (world)";
18+
selections:
19+
- anchor: {line: 2, character: 11}
20+
active: {line: 2, character: 11}
21+
marks: {}
22+
finalState:
23+
documentContents: |-
24+
<?php
25+
26+
"Hello ";
27+
selections:
28+
- anchor: {line: 2, character: 7}
29+
active: {line: 2, character: 7}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
languageId: php
2+
command:
3+
version: 6
4+
spokenForm: change round
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: surroundingPair, delimiter: parentheses}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: |-
15+
<?php
16+
17+
`Hello (world)`;
18+
selections:
19+
- anchor: {line: 2, character: 11}
20+
active: {line: 2, character: 11}
21+
marks: {}
22+
finalState:
23+
documentContents: |-
24+
<?php
25+
26+
`Hello `;
27+
selections:
28+
- anchor: {line: 2, character: 7}
29+
active: {line: 2, character: 7}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
languageId: php
2+
command:
3+
version: 6
4+
spokenForm: change state
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: statement}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: |
15+
<?php
16+
17+
$myVar = 'Hello world';
18+
selections:
19+
- anchor: {line: 2, character: 3}
20+
active: {line: 2, character: 3}
21+
marks: {}
22+
finalState:
23+
documentContents: |+
24+
<?php
25+
26+
27+
selections:
28+
- anchor: {line: 2, character: 0}
29+
active: {line: 2, character: 0}

0 commit comments

Comments
 (0)