Skip to content

Commit

Permalink
Migrate PHP statements to treesitter (#1983)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
nathanheffley and pre-commit-ci-lite[bot] authored Nov 2, 2023
1 parent 479b67f commit 1453284
Show file tree
Hide file tree
Showing 13 changed files with 339 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { LegacyLanguageId } from "./LegacyLanguageId";
import { getNodeMatcher } from "./getNodeMatcher";
import { stringTextFragmentExtractor as htmlStringTextFragmentExtractor } from "./html";
import { stringTextFragmentExtractor as jsonStringTextFragmentExtractor } from "./json";
import { stringTextFragmentExtractor as phpStringTextFragmentExtractor } from "./php";
import { stringTextFragmentExtractor as rubyStringTextFragmentExtractor } from "./ruby";
import { stringTextFragmentExtractor as scssStringTextFragmentExtractor } from "./scss";
import { stringTextFragmentExtractor as typescriptStringTextFragmentExtractor } from "./typescript";
Expand Down Expand Up @@ -155,10 +154,6 @@ const textFragmentExtractors: Record<
),
latex: fullDocumentTextFragmentExtractor,
markdown: fullDocumentTextFragmentExtractor,
php: constructDefaultTextFragmentExtractor(
"php",
phpStringTextFragmentExtractor,
),
ruby: constructDefaultTextFragmentExtractor(
"ruby",
rubyStringTextFragmentExtractor,
Expand Down
72 changes: 1 addition & 71 deletions packages/cursorless-engine/src/languages/php.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { Selection, TextEditor } from "@cursorless/common";
import type { SyntaxNode } from "web-tree-sitter";
import { SimpleScopeTypeType } from "@cursorless/common";
import {
NodeMatcherAlternative,
SelectionWithContext,
SelectionWithEditor,
} from "../typings/Types";
import { NodeMatcherAlternative, SelectionWithContext } from "../typings/Types";
import { patternFinder } from "../util/nodeFinders";
import {
argumentMatcher,
Expand All @@ -17,40 +13,6 @@ import {
} from "../util/nodeMatchers";
import { getNodeRange } from "../util/nodeSelectors";

// Generated by the following command:
// > curl https://raw.githubusercontent.com/tree-sitter/tree-sitter-php/0ce134234214427b6aeb2735e93a307881c6cd6f/src/node-types.json \
// | jq '[.[] | select(.type == "_statement") | .subtypes[].type]'
const STATEMENT_TYPES = [
"break_statement",
"class_declaration",
"compound_statement",
"const_declaration",
"continue_statement",
"declare_statement",
"do_statement",
"echo_statement",
"empty_statement",
"enum_declaration",
"expression_statement",
"for_statement",
"foreach_statement",
"function_definition",
"function_static_declaration",
"global_declaration",
"goto_statement",
"if_statement",
"interface_declaration",
"named_label_statement",
"namespace_definition",
"namespace_use_declaration",
"return_statement",
"switch_statement",
"trait_declaration",
"try_statement",
"unset_statement",
"while_statement",
];

// Taken from https://www.php.net/manual/en/language.operators.assignment.php
const assignmentOperators = [
"=",
Expand Down Expand Up @@ -101,38 +63,17 @@ function castTypeExtractor(
const nodeMatchers: Partial<
Record<SimpleScopeTypeType, NodeMatcherAlternative>
> = {
statement: STATEMENT_TYPES,
ifStatement: "if_statement",
class: "class_declaration",
className: "class_declaration[name]",
name: [
"assignment_expression[left]",
"class_declaration[name]",
"function_definition[name]",
"method_declaration[name]",
],
comment: "comment",
string: "string",
type: cascadingMatcher(
trailingMatcher(["~cast_expression[type]"]),
matcher(patternFinder("cast_expression[type]"), castTypeExtractor),
),

namedFunction: trailingMatcher(
[
"function_definition",
"assignment_expression.anonymous_function_creation_expression",
"assignment_expression.arrow_function",
],
[";"],
),
anonymousFunction: [
"anonymous_function_creation_expression",
"arrow_function",
],
functionCall: ["function_call_expression", "object_creation_expression"],
functionName: ["function_definition[name]", "method_declaration[name]"],

value: leadingMatcher(
[
"array_element_initializer[1]",
Expand All @@ -149,14 +90,3 @@ const nodeMatchers: Partial<
argumentOrParameter: argumentMatcher("arguments", "formal_parameters"),
};
export default createPatternMatchers(nodeMatchers);

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

return null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
languageId: php
command:
version: 6
spokenForm: change funk
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: namedFunction}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
<?php
class MyClass
{
public function helloWorld()
{
}
}
selections:
- anchor: {line: 6, character: 0}
active: {line: 6, character: 0}
marks: {}
finalState:
documentContents: |-
<?php
class MyClass
{
}
selections:
- anchor: {line: 4, character: 4}
active: {line: 4, character: 4}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
languageId: php
command:
version: 6
spokenForm: change funk
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: namedFunction}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
<?php
$myFunk = function() {
};
selections:
- anchor: {line: 3, character: 0}
active: {line: 3, character: 0}
marks: {}
finalState:
documentContents: |-
<?php
;
selections:
- anchor: {line: 2, character: 0}
active: {line: 2, character: 0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
languageId: php
command:
version: 6
spokenForm: change funk
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: namedFunction}
usePrePhraseSnapshot: true
initialState:
documentContents: |
<?php
$myFunk = fn() => 'Hello world';
selections:
- anchor: {line: 2, character: 24}
active: {line: 2, character: 24}
marks: {}
finalState:
documentContents: |
<?php
;
selections:
- anchor: {line: 2, character: 0}
active: {line: 2, character: 0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
languageId: php
command:
version: 6
spokenForm: change list
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: list}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
<?php
$list = [
'hello',
'world'
];
selections:
- anchor: {line: 3, character: 8}
active: {line: 3, character: 8}
marks: {}
finalState:
documentContents: |-
<?php
$list = ;
selections:
- anchor: {line: 2, character: 8}
active: {line: 2, character: 8}
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
languageId: php
command:
version: 1
version: 6
spokenForm: change round
action: clearAndSetSelection
targets:
- type: primitive
modifier: {type: surroundingPair, delimiter: parentheses}
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: surroundingPair, delimiter: parentheses}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
<?php
"Hello (world)";
'Hello (world)';
selections:
- anchor: {line: 2, character: 11}
active: {line: 2, character: 11}
Expand All @@ -19,7 +23,7 @@ finalState:
documentContents: |-
<?php
"Hello ";
'Hello ';
selections:
- anchor: {line: 2, character: 7}
active: {line: 2, character: 7}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
languageId: php
command:
version: 6
spokenForm: change round
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: surroundingPair, delimiter: parentheses}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
<?php
"Hello (world)";
selections:
- anchor: {line: 2, character: 11}
active: {line: 2, character: 11}
marks: {}
finalState:
documentContents: |-
<?php
"Hello ";
selections:
- anchor: {line: 2, character: 7}
active: {line: 2, character: 7}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
languageId: php
command:
version: 6
spokenForm: change round
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: surroundingPair, delimiter: parentheses}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
<?php
`Hello (world)`;
selections:
- anchor: {line: 2, character: 11}
active: {line: 2, character: 11}
marks: {}
finalState:
documentContents: |-
<?php
`Hello `;
selections:
- anchor: {line: 2, character: 7}
active: {line: 2, character: 7}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
languageId: php
command:
version: 6
spokenForm: change state
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: statement}
usePrePhraseSnapshot: true
initialState:
documentContents: |
<?php
$myVar = 'Hello world';
selections:
- anchor: {line: 2, character: 3}
active: {line: 2, character: 3}
marks: {}
finalState:
documentContents: |+
<?php
selections:
- anchor: {line: 2, character: 0}
active: {line: 2, character: 0}
Loading

0 comments on commit 1453284

Please sign in to comment.