Skip to content

Commit ee6dbf5

Browse files
committed
Initial Perl support and some tests
1 parent c15b818 commit ee6dbf5

24 files changed

+669
-0
lines changed

packages/cursorless-engine/languages/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const supportedLanguageIds = [
1414
"jsonc",
1515
"latex",
1616
"markdown",
17+
"perl",
1718
"php",
1819
"python",
1920
"ruby",

packages/cursorless-engine/languages/getNodeMatcher.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import java from "./java";
1818
import { patternMatchers as json } from "./json";
1919
import latex from "./latex";
2020
import markdown from "./markdown";
21+
import { patternMatchers as perl } from "./perl";
2122
import php from "./php";
2223
import python from "./python";
2324
import { patternMatchers as ruby } from "./ruby";
@@ -68,6 +69,7 @@ const languageMatchers: Record<
6869
jsonc: json,
6970
latex,
7071
markdown,
72+
perl,
7173
php,
7274
python,
7375
ruby,

packages/cursorless-engine/languages/getTextFragmentExtractor.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { SupportedLanguageId } from "./constants";
77
import { getNodeMatcher } from "./getNodeMatcher";
88
import { stringTextFragmentExtractor as htmlStringTextFragmentExtractor } from "./html";
99
import { stringTextFragmentExtractor as jsonStringTextFragmentExtractor } from "./json";
10+
import { stringTextFragmentExtractor as perlStringTextFragmentExtractor } from "./perl";
1011
import { stringTextFragmentExtractor as phpStringTextFragmentExtractor } from "./php";
1112
import { stringTextFragmentExtractor as rubyStringTextFragmentExtractor } from "./ruby";
1213
import { stringTextFragmentExtractor as scssStringTextFragmentExtractor } from "./scss";
@@ -162,6 +163,10 @@ const textFragmentExtractors: Record<
162163
),
163164
latex: fullDocumentTextFragmentExtractor,
164165
markdown: fullDocumentTextFragmentExtractor,
166+
perl: constructDefaultTextFragmentExtractor(
167+
"perl",
168+
perlStringTextFragmentExtractor,
169+
),
165170
php: constructDefaultTextFragmentExtractor(
166171
"php",
167172
phpStringTextFragmentExtractor,
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import {
2+
ancestorChainNodeMatcher,
3+
cascadingMatcher,
4+
createPatternMatchers,
5+
matcher,
6+
patternMatcher,
7+
} from "../util/nodeMatchers";
8+
import { NodeMatcherAlternative, SelectionWithEditor } from "../typings/Types";
9+
import { SimpleScopeTypeType } from "@cursorless/common";
10+
import { SyntaxNode } from "web-tree-sitter";
11+
import { getNodeRange, unwrapSelectionExtractor } from "../util/nodeSelectors";
12+
import { patternFinder } from "../util/nodeFinders";
13+
14+
// Generated by the following command:
15+
// curl https://raw.githubusercontent.com/ganezdragon/tree-sitter-perl/ee1001210af5f32ba14d2ced834636548e1b6485/src/node-types.json \
16+
// | jq '[.[] | select(.type|match("_statement")) | .type ]'
17+
const STATEMENT_TYPES = [
18+
"ellipsis_statement",
19+
"for_simple_statement",
20+
"for_statement_1",
21+
"for_statement_2",
22+
"foreach_simple_statement",
23+
"foreach_statement",
24+
"if_simple_statement",
25+
"if_statement",
26+
"loop_control_statement",
27+
"loop_control_statement",
28+
"named_block_statement",
29+
"package_statement",
30+
"pod_statement",
31+
"require_statement",
32+
"single_line_statement",
33+
"unless_simple_statement",
34+
"unless_statement",
35+
"until_simple_statement",
36+
"until_statement",
37+
"use_constant_statement",
38+
"use_no_feature_statement",
39+
"use_no_if_statement",
40+
"use_no_statement",
41+
"use_no_subs_statement",
42+
"use_parent_statement",
43+
"when_simple_statement",
44+
"while_simple_statement",
45+
"while_statement",
46+
];
47+
48+
const EXPRESSION_TYPES = [
49+
"array",
50+
"assignment",
51+
"begin",
52+
// MANY MORE TODO ...
53+
];
54+
55+
const EXPRESSION_STATEMENT_PARENT_TYPES = [
56+
"begin",
57+
"block",
58+
"do",
59+
"do_block",
60+
"else",
61+
"lambda",
62+
"method",
63+
"then",
64+
];
65+
66+
const assignmentOperators = [
67+
"=",
68+
"+=",
69+
"-=",
70+
"*=",
71+
"/=",
72+
"||=",
73+
"//=",
74+
"|=",
75+
"&&=",
76+
"&=",
77+
"%=",
78+
">>=",
79+
"<<=",
80+
"^=",
81+
];
82+
83+
const mapKeyValueSeparators = [",", "=>"];
84+
85+
const nodeMatchers: Partial<
86+
Record<SimpleScopeTypeType, NodeMatcherAlternative>
87+
> = {
88+
map: "hash",
89+
list: "array",
90+
condition: matcher(
91+
patternFinder("while_statement[condition]"),
92+
unwrapSelectionExtractor,
93+
),
94+
string: [
95+
"string_single_quoted",
96+
"string_double_quoted",
97+
"string_q_quoted",
98+
"string_qq_quoted",
99+
],
100+
ifStatement: "if_statement",
101+
functionCall: [
102+
"call_expression",
103+
"call_expression_with_just_name",
104+
"method_invocation",
105+
],
106+
comment: "comments",
107+
namedFunction: ["function_definition"],
108+
anonymousFunction: "anonymous_function",
109+
regularExpression: ["regex_pattern", "regex_pattern_qr"],
110+
collectionKey: "*[key]", // TODO: child of "value: hash?"
111+
collectionItem: "*[value]", // TODO: child of "value: hash?"
112+
argumentOrParameter: [
113+
"empty_parenthesized_argument",
114+
"parenthesized_argument",
115+
"argument",
116+
],
117+
className: "package_name",
118+
};
119+
120+
export const patternMatchers = createPatternMatchers(nodeMatchers);
121+
122+
export function stringTextFragmentExtractor(
123+
node: SyntaxNode,
124+
_selection: SelectionWithEditor,
125+
) {
126+
// heredoc_content does not seem to supported by tree-sitter-perl,
127+
// leaving it anyway since it won't hurt
128+
if (node.type === "string_content" || node.type === "heredoc_content") {
129+
return getNodeRange(node);
130+
}
131+
132+
return null;
133+
}
134+
135+
// EOF
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
languageId: perl
2+
command:
3+
version: 4
4+
spokenForm: change arg
5+
action: {name: clearAndSetSelection}
6+
targets:
7+
- type: primitive
8+
modifiers:
9+
- type: containingScope
10+
scopeType: {type: argumentOrParameter}
11+
usePrePhraseSnapshot: true
12+
initialState:
13+
documentContents: some_funky_func( "and", "three", "args" )
14+
selections:
15+
- anchor: {line: 0, character: 36}
16+
active: {line: 0, character: 36}
17+
marks: {}
18+
finalState:
19+
documentContents: some_funky_func( )
20+
selections:
21+
- anchor: {line: 0, character: 17}
22+
active: {line: 0, character: 17}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
languageId: perl
2+
command:
3+
version: 4
4+
spokenForm: change call
5+
action: {name: clearAndSetSelection}
6+
targets:
7+
- type: primitive
8+
modifiers:
9+
- type: containingScope
10+
scopeType: {type: functionCall}
11+
usePrePhraseSnapshot: true
12+
initialState:
13+
documentContents: |-
14+
my $var = 1;
15+
$var = func();
16+
$var = 2;
17+
selections:
18+
- anchor: {line: 1, character: 12}
19+
active: {line: 1, character: 12}
20+
marks: {}
21+
finalState:
22+
documentContents: |-
23+
my $var = 1;
24+
$var = ;
25+
$var = 2;
26+
selections:
27+
- anchor: {line: 1, character: 7}
28+
active: {line: 1, character: 7}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
languageId: perl
2+
command:
3+
version: 4
4+
spokenForm: change call
5+
action: {name: clearAndSetSelection}
6+
targets:
7+
- type: primitive
8+
modifiers:
9+
- type: containingScope
10+
scopeType: {type: functionCall}
11+
usePrePhraseSnapshot: true
12+
initialState:
13+
documentContents: |-
14+
my $var = 1;
15+
$var = func_with_params( $var );
16+
$var = 2;
17+
selections:
18+
- anchor: {line: 1, character: 27}
19+
active: {line: 1, character: 27}
20+
marks: {}
21+
finalState:
22+
documentContents: |-
23+
my $var = 1;
24+
$var = ;
25+
$var = 2;
26+
selections:
27+
- anchor: {line: 1, character: 7}
28+
active: {line: 1, character: 7}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
languageId: perl
2+
command:
3+
version: 4
4+
spokenForm: change call
5+
action: {name: clearAndSetSelection}
6+
targets:
7+
- type: primitive
8+
modifiers:
9+
- type: containingScope
10+
scopeType: {type: functionCall}
11+
usePrePhraseSnapshot: true
12+
initialState:
13+
documentContents: |-
14+
my $var = 1;
15+
$var = $object->method( );
16+
$var = 2;
17+
selections:
18+
- anchor: {line: 1, character: 24}
19+
active: {line: 1, character: 24}
20+
marks: {}
21+
finalState:
22+
documentContents: |-
23+
my $var = 1;
24+
$var = ;
25+
$var = 2;
26+
selections:
27+
- anchor: {line: 1, character: 7}
28+
active: {line: 1, character: 7}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
languageId: perl
2+
command:
3+
version: 4
4+
spokenForm: change call
5+
action: {name: clearAndSetSelection}
6+
targets:
7+
- type: primitive
8+
modifiers:
9+
- type: containingScope
10+
scopeType: {type: functionCall}
11+
usePrePhraseSnapshot: true
12+
initialState:
13+
documentContents: |-
14+
my $var = 1;
15+
$var = Other::Package::func();
16+
$var = 2;
17+
selections:
18+
- anchor: {line: 1, character: 24}
19+
active: {line: 1, character: 24}
20+
marks: {}
21+
finalState:
22+
documentContents: |-
23+
my $var = 1;
24+
$var = ;
25+
$var = 2;
26+
selections:
27+
- anchor: {line: 1, character: 7}
28+
active: {line: 1, character: 7}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
languageId: perl
2+
command:
3+
version: 4
4+
spokenForm: change class name
5+
action: {name: clearAndSetSelection}
6+
targets:
7+
- type: primitive
8+
modifiers:
9+
- type: containingScope
10+
scopeType: {type: className}
11+
usePrePhraseSnapshot: true
12+
initialState:
13+
documentContents: $var = Some::Package::func()
14+
selections:
15+
- anchor: {line: 0, character: 17}
16+
active: {line: 0, character: 17}
17+
marks: {}
18+
finalState:
19+
documentContents: $var = ::func()
20+
selections:
21+
- anchor: {line: 0, character: 7}
22+
active: {line: 0, character: 7}

0 commit comments

Comments
 (0)