Skip to content

Commit 0712ee8

Browse files
Include generic in java type definition (#372)
* Include generic in java type definition * Make typescript type scope type consistent with java * Add tests for typescript * Upgrade tests * Added dock string * Updated type for typescript constructor arguments * Include constructor identifier without argument list in the definition of type * Add a test Co-authored-by: Pokey Rule <pokey.rule@gmail.com>
1 parent 66f160e commit 0712ee8

30 files changed

+832
-11
lines changed

src/languages/java.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
6060
name: ["*[declarator][name]", "*[name]", "formal_parameter.identifier!"],
6161
namedFunction: ["method_declaration", "constructor_declaration"],
6262
type: trailingMatcher([
63+
"generic_type.type_arguments.type_identifier",
64+
"generic_type.type_identifier",
6365
"type_identifier",
6466
"local_variable_declaration[type]",
6567
"array_creation_expression[type]",

src/languages/typescript.ts

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ import {
99
conditionMatcher,
1010
} from "../util/nodeMatchers";
1111
import {
12+
NodeMatcher,
1213
NodeMatcherAlternative,
14+
NodeMatcherValue,
1315
ScopeType,
1416
SelectionWithEditor,
1517
} from "../typings/Types";
1618
import {
1719
getNodeInternalRange,
1820
getNodeRange,
21+
pairSelectionExtractor,
1922
selectWithLeadingDelimiter,
23+
simpleSelectionExtractor,
2024
} from "../util/nodeSelectors";
2125
import { patternFinder } from "../util/nodeFinders";
2226

@@ -69,12 +73,60 @@ const getTags = (selection: SelectionWithEditor, node: SyntaxNode) => {
6973
return startTag != null && endTag != null ? startTag.concat(endTag) : null;
7074
};
7175

72-
const findTypeNode = (node: SyntaxNode) => {
73-
const typeAnnotationNode = node.children.find((child) =>
74-
["type_annotation", "opting_type_annotation"].includes(child.type)
75-
);
76-
return typeAnnotationNode?.lastChild ?? null;
77-
};
76+
function typeMatcher(): NodeMatcher {
77+
const delimiterSelector = selectWithLeadingDelimiter(":");
78+
return function (selection: SelectionWithEditor, node: SyntaxNode) {
79+
if (
80+
node.parent?.type === "new_expression" &&
81+
node.type !== "new" &&
82+
node.type !== "arguments"
83+
) {
84+
const identifierNode = node.parent.children.find(
85+
(n) => n.type === "identifier"
86+
);
87+
const argsNode = node.parent.children.find(
88+
(n) => n.type === "type_arguments"
89+
);
90+
if (identifierNode && argsNode) {
91+
return [
92+
{
93+
node,
94+
selection: pairSelectionExtractor(
95+
selection.editor,
96+
identifierNode,
97+
argsNode
98+
),
99+
},
100+
];
101+
} else if (identifierNode) {
102+
return [
103+
{
104+
node: identifierNode,
105+
selection: simpleSelectionExtractor(
106+
selection.editor,
107+
identifierNode
108+
),
109+
},
110+
];
111+
}
112+
}
113+
114+
const typeAnnotationNode = node.children.find((child) =>
115+
["type_annotation", "opting_type_annotation"].includes(child.type)
116+
);
117+
const targetNode = typeAnnotationNode?.lastChild;
118+
119+
if (targetNode) {
120+
return [
121+
{
122+
node: targetNode,
123+
selection: delimiterSelector(selection.editor, targetNode),
124+
},
125+
];
126+
}
127+
return null;
128+
};
129+
}
78130

79131
function valueMatcher() {
80132
const pFinder = patternFinder("assignment_expression[right]", "*[value]");
@@ -160,7 +212,8 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
160212
],
161213
type: cascadingMatcher(
162214
// Typed parameters, properties, and functions
163-
matcher(findTypeNode, selectWithLeadingDelimiter(":")),
215+
typeMatcher(),
216+
// matcher(findTypeNode, selectWithLeadingDelimiter(":")),
164217
// Type alias/interface declarations
165218
patternMatcher(
166219
"export_statement?.type_alias_declaration",
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
languageId: java
2+
command:
3+
version: 1
4+
spokenForm: take type blue look
5+
action: setSelection
6+
targets:
7+
- type: primitive
8+
modifier: {type: containingScope, scopeType: type, includeSiblings: false}
9+
mark: {type: decoratedSymbol, symbolColor: blue, character: l}
10+
initialState:
11+
documentContents: |-
12+
public class MyClass {
13+
private MyClass () {
14+
Map<String, String> map = new HashMap<String, String>();
15+
List<String> list = new ArrayList<String>();
16+
}
17+
}
18+
selections:
19+
- anchor: {line: 2, character: 38}
20+
active: {line: 2, character: 61}
21+
marks:
22+
blue.l:
23+
start: {line: 3, character: 8}
24+
end: {line: 3, character: 12}
25+
finalState:
26+
documentContents: |-
27+
public class MyClass {
28+
private MyClass () {
29+
Map<String, String> map = new HashMap<String, String>();
30+
List<String> list = new ArrayList<String>();
31+
}
32+
}
33+
selections:
34+
- anchor: {line: 3, character: 8}
35+
active: {line: 3, character: 20}
36+
thatMark:
37+
- anchor: {line: 3, character: 8}
38+
active: {line: 3, character: 20}
39+
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: blue, character: l}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: type, includeSiblings: false}}]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
languageId: java
2+
command:
3+
version: 1
4+
spokenForm: take type gust
5+
action: setSelection
6+
targets:
7+
- type: primitive
8+
modifier: {type: containingScope, scopeType: type, includeSiblings: false}
9+
mark: {type: decoratedSymbol, symbolColor: default, character: g}
10+
initialState:
11+
documentContents: |-
12+
public class MyClass {
13+
private MyClass () {
14+
Map<String, String> map = new HashMap<String, String>();
15+
List<String> list = new ArrayList<String>();
16+
}
17+
}
18+
selections:
19+
- anchor: {line: 3, character: 8}
20+
active: {line: 3, character: 20}
21+
marks:
22+
default.g:
23+
start: {line: 3, character: 13}
24+
end: {line: 3, character: 19}
25+
finalState:
26+
documentContents: |-
27+
public class MyClass {
28+
private MyClass () {
29+
Map<String, String> map = new HashMap<String, String>();
30+
List<String> list = new ArrayList<String>();
31+
}
32+
}
33+
selections:
34+
- anchor: {line: 3, character: 8}
35+
active: {line: 3, character: 20}
36+
thatMark:
37+
- anchor: {line: 3, character: 8}
38+
active: {line: 3, character: 20}
39+
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: g}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: type, includeSiblings: false}}]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
languageId: java
2+
command:
3+
version: 1
4+
spokenForm: take type gust
5+
action: setSelection
6+
targets:
7+
- type: primitive
8+
modifier: {type: containingScope, scopeType: type, includeSiblings: false}
9+
mark: {type: decoratedSymbol, symbolColor: default, character: g}
10+
initialState:
11+
documentContents: |-
12+
public class MyClass {
13+
private MyClass () {
14+
Map<String, String> map = new HashMap<String, String>();
15+
List<String> list = new ArrayList<String>();
16+
}
17+
}
18+
selections:
19+
- anchor: {line: 3, character: 32}
20+
active: {line: 3, character: 49}
21+
marks:
22+
default.g:
23+
start: {line: 3, character: 42}
24+
end: {line: 3, character: 48}
25+
finalState:
26+
documentContents: |-
27+
public class MyClass {
28+
private MyClass () {
29+
Map<String, String> map = new HashMap<String, String>();
30+
List<String> list = new ArrayList<String>();
31+
}
32+
}
33+
selections:
34+
- anchor: {line: 3, character: 32}
35+
active: {line: 3, character: 49}
36+
thatMark:
37+
- anchor: {line: 3, character: 32}
38+
active: {line: 3, character: 49}
39+
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: g}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: type, includeSiblings: false}}]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
languageId: java
2+
command:
3+
version: 1
4+
spokenForm: take type harp
5+
action: setSelection
6+
targets:
7+
- type: primitive
8+
modifier: {type: containingScope, scopeType: type, includeSiblings: false}
9+
mark: {type: decoratedSymbol, symbolColor: default, character: h}
10+
initialState:
11+
documentContents: |-
12+
public class MyClass {
13+
private MyClass () {
14+
Map<String, String> map = new HashMap<String, String>();
15+
List<String> list = new ArrayList<String>();
16+
}
17+
}
18+
selections:
19+
- anchor: {line: 2, character: 8}
20+
active: {line: 2, character: 27}
21+
marks:
22+
default.h:
23+
start: {line: 2, character: 38}
24+
end: {line: 2, character: 45}
25+
finalState:
26+
documentContents: |-
27+
public class MyClass {
28+
private MyClass () {
29+
Map<String, String> map = new HashMap<String, String>();
30+
List<String> list = new ArrayList<String>();
31+
}
32+
}
33+
selections:
34+
- anchor: {line: 2, character: 38}
35+
active: {line: 2, character: 61}
36+
thatMark:
37+
- anchor: {line: 2, character: 38}
38+
active: {line: 2, character: 61}
39+
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: h}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: type, includeSiblings: false}}]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
languageId: java
2+
command:
3+
version: 1
4+
spokenForm: take type look
5+
action: setSelection
6+
targets:
7+
- type: primitive
8+
modifier: {type: containingScope, scopeType: type, includeSiblings: false}
9+
mark: {type: decoratedSymbol, symbolColor: default, character: l}
10+
initialState:
11+
documentContents: |-
12+
public class MyClass {
13+
private MyClass () {
14+
Map<String, String> map = new HashMap<String, String>();
15+
List<String> list = new ArrayList<String>();
16+
}
17+
}
18+
selections:
19+
- anchor: {line: 2, character: 27}
20+
active: {line: 2, character: 27}
21+
marks:
22+
default.l:
23+
start: {line: 3, character: 21}
24+
end: {line: 3, character: 25}
25+
finalState:
26+
documentContents: |-
27+
public class MyClass {
28+
private MyClass () {
29+
Map<String, String> map = new HashMap<String, String>();
30+
List<String> list = new ArrayList<String>();
31+
}
32+
}
33+
selections:
34+
- anchor: {line: 3, character: 8}
35+
active: {line: 3, character: 20}
36+
thatMark:
37+
- anchor: {line: 3, character: 8}
38+
active: {line: 3, character: 20}
39+
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: l}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: type, includeSiblings: false}}]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
languageId: java
2+
command:
3+
version: 1
4+
spokenForm: take type pit
5+
action: setSelection
6+
targets:
7+
- type: primitive
8+
modifier: {type: containingScope, scopeType: type, includeSiblings: false}
9+
mark: {type: decoratedSymbol, symbolColor: default, character: p}
10+
initialState:
11+
documentContents: |-
12+
public class MyClass {
13+
private MyClass () {
14+
Map<String, String> map = new HashMap<String, String>();
15+
List<String> list = new ArrayList<String>();
16+
}
17+
}
18+
selections:
19+
- anchor: {line: 3, character: 52}
20+
active: {line: 3, character: 52}
21+
marks:
22+
default.p:
23+
start: {line: 2, character: 8}
24+
end: {line: 2, character: 11}
25+
finalState:
26+
documentContents: |-
27+
public class MyClass {
28+
private MyClass () {
29+
Map<String, String> map = new HashMap<String, String>();
30+
List<String> list = new ArrayList<String>();
31+
}
32+
}
33+
selections:
34+
- anchor: {line: 2, character: 8}
35+
active: {line: 2, character: 27}
36+
thatMark:
37+
- anchor: {line: 2, character: 8}
38+
active: {line: 2, character: 27}
39+
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: p}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: type, includeSiblings: false}}]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
languageId: java
2+
command:
3+
version: 1
4+
spokenForm: take type soon
5+
action: setSelection
6+
targets:
7+
- type: primitive
8+
modifier: {type: containingScope, scopeType: type, includeSiblings: false}
9+
mark: {type: decoratedSymbol, symbolColor: default, character: s}
10+
initialState:
11+
documentContents: |-
12+
public class MyClass {
13+
private MyClass () {
14+
Map<String, String> map = new HashMap<String, String>();
15+
List<String> list = new ArrayList<String>();
16+
}
17+
}
18+
selections:
19+
- anchor: {line: 2, character: 8}
20+
active: {line: 2, character: 27}
21+
marks:
22+
default.s:
23+
start: {line: 2, character: 12}
24+
end: {line: 2, character: 18}
25+
finalState:
26+
documentContents: |-
27+
public class MyClass {
28+
private MyClass () {
29+
Map<String, String> map = new HashMap<String, String>();
30+
List<String> list = new ArrayList<String>();
31+
}
32+
}
33+
selections:
34+
- anchor: {line: 2, character: 8}
35+
active: {line: 2, character: 27}
36+
thatMark:
37+
- anchor: {line: 2, character: 8}
38+
active: {line: 2, character: 27}
39+
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: s}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: type, includeSiblings: false}}]

0 commit comments

Comments
 (0)