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

Include generic in java type definition #372

Merged
merged 9 commits into from
Dec 10, 2021
2 changes: 2 additions & 0 deletions src/languages/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
name: ["*[declarator][name]", "*[name]", "formal_parameter.identifier!"],
namedFunction: ["method_declaration", "constructor_declaration"],
type: trailingMatcher([
"generic_type.type_arguments.type_identifier",
"generic_type.type_identifier",
"type_identifier",
"local_variable_declaration[type]",
"array_creation_expression[type]",
Expand Down
67 changes: 60 additions & 7 deletions src/languages/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import {
conditionMatcher,
} from "../util/nodeMatchers";
import {
NodeMatcher,
NodeMatcherAlternative,
NodeMatcherValue,
ScopeType,
SelectionWithEditor,
} from "../typings/Types";
import {
getNodeInternalRange,
getNodeRange,
pairSelectionExtractor,
selectWithLeadingDelimiter,
simpleSelectionExtractor,
} from "../util/nodeSelectors";
import { patternFinder } from "../util/nodeFinders";

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

const findTypeNode = (node: SyntaxNode) => {
const typeAnnotationNode = node.children.find((child) =>
["type_annotation", "opting_type_annotation"].includes(child.type)
);
return typeAnnotationNode?.lastChild ?? null;
};
function typeMatcher(): NodeMatcher {
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
const delimiterSelector = selectWithLeadingDelimiter(":");
return function (selection: SelectionWithEditor, node: SyntaxNode) {
if (
node.parent?.type === "new_expression" &&
node.type !== "new" &&
node.type !== "arguments"
) {
const identifierNode = node.parent.children.find(
(n) => n.type === "identifier"
);
const argsNode = node.parent.children.find(
(n) => n.type === "type_arguments"
);
if (identifierNode && argsNode) {
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
return [
{
node,
selection: pairSelectionExtractor(
selection.editor,
identifierNode,
argsNode
),
},
];
} else if (identifierNode) {
return [
{
node: identifierNode,
selection: simpleSelectionExtractor(
selection.editor,
identifierNode
),
},
];
}
}

const typeAnnotationNode = node.children.find((child) =>
["type_annotation", "opting_type_annotation"].includes(child.type)
);
const targetNode = typeAnnotationNode?.lastChild;

if (targetNode) {
return [
{
node: targetNode,
selection: delimiterSelector(selection.editor, targetNode),
},
];
}
return null;
};
}

function valueMatcher() {
const pFinder = patternFinder("assignment_expression[right]", "*[value]");
Expand Down Expand Up @@ -160,7 +212,8 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
],
type: cascadingMatcher(
// Typed parameters, properties, and functions
matcher(findTypeNode, selectWithLeadingDelimiter(":")),
typeMatcher(),
// matcher(findTypeNode, selectWithLeadingDelimiter(":")),
// Type alias/interface declarations
patternMatcher(
"export_statement?.type_alias_declaration",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
languageId: java
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not worth rewriting all these tests, but I thought we had aligned on "clear" instead of "take" to make these tests easier to read. Makes it a bit painful having to manually count offsets to figure out what the test is doing

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're completely correct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fwiw I'm planning a rework of the tests to make modifier tests clearer / more compact, and then the "clear" / "take" thing won't matter. Not sure if I've already filed an issue but I will at some point

Copy link
Member Author

@AndreasArvidsson AndreasArvidsson Dec 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that sounds really useful

command:
version: 1
spokenForm: take type blue look
action: setSelection
targets:
- type: primitive
modifier: {type: containingScope, scopeType: type, includeSiblings: false}
mark: {type: decoratedSymbol, symbolColor: blue, character: l}
initialState:
documentContents: |-
public class MyClass {
private MyClass () {
Map<String, String> map = new HashMap<String, String>();
List<String> list = new ArrayList<String>();
}
}
selections:
- anchor: {line: 2, character: 38}
active: {line: 2, character: 61}
marks:
blue.l:
start: {line: 3, character: 8}
end: {line: 3, character: 12}
finalState:
documentContents: |-
public class MyClass {
private MyClass () {
Map<String, String> map = new HashMap<String, String>();
List<String> list = new ArrayList<String>();
}
}
selections:
- anchor: {line: 3, character: 8}
active: {line: 3, character: 20}
thatMark:
- anchor: {line: 3, character: 8}
active: {line: 3, character: 20}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: blue, character: l}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: type, includeSiblings: false}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
languageId: java
command:
version: 1
spokenForm: take type gust
action: setSelection
targets:
- type: primitive
modifier: {type: containingScope, scopeType: type, includeSiblings: false}
mark: {type: decoratedSymbol, symbolColor: default, character: g}
initialState:
documentContents: |-
public class MyClass {
private MyClass () {
Map<String, String> map = new HashMap<String, String>();
List<String> list = new ArrayList<String>();
}
}
selections:
- anchor: {line: 3, character: 8}
active: {line: 3, character: 20}
marks:
default.g:
start: {line: 3, character: 13}
end: {line: 3, character: 19}
finalState:
documentContents: |-
public class MyClass {
private MyClass () {
Map<String, String> map = new HashMap<String, String>();
List<String> list = new ArrayList<String>();
}
}
selections:
- anchor: {line: 3, character: 8}
active: {line: 3, character: 20}
thatMark:
- anchor: {line: 3, character: 8}
active: {line: 3, character: 20}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: g}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: type, includeSiblings: false}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
languageId: java
command:
version: 1
spokenForm: take type gust
action: setSelection
targets:
- type: primitive
modifier: {type: containingScope, scopeType: type, includeSiblings: false}
mark: {type: decoratedSymbol, symbolColor: default, character: g}
initialState:
documentContents: |-
public class MyClass {
private MyClass () {
Map<String, String> map = new HashMap<String, String>();
List<String> list = new ArrayList<String>();
}
}
selections:
- anchor: {line: 3, character: 32}
active: {line: 3, character: 49}
marks:
default.g:
start: {line: 3, character: 42}
end: {line: 3, character: 48}
finalState:
documentContents: |-
public class MyClass {
private MyClass () {
Map<String, String> map = new HashMap<String, String>();
List<String> list = new ArrayList<String>();
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
}
}
selections:
- anchor: {line: 3, character: 32}
active: {line: 3, character: 49}
thatMark:
- anchor: {line: 3, character: 32}
active: {line: 3, character: 49}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: g}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: type, includeSiblings: false}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
languageId: java
command:
version: 1
spokenForm: take type harp
action: setSelection
targets:
- type: primitive
modifier: {type: containingScope, scopeType: type, includeSiblings: false}
mark: {type: decoratedSymbol, symbolColor: default, character: h}
initialState:
documentContents: |-
public class MyClass {
private MyClass () {
Map<String, String> map = new HashMap<String, String>();
List<String> list = new ArrayList<String>();
}
}
selections:
- anchor: {line: 2, character: 8}
active: {line: 2, character: 27}
marks:
default.h:
start: {line: 2, character: 38}
end: {line: 2, character: 45}
finalState:
documentContents: |-
public class MyClass {
private MyClass () {
Map<String, String> map = new HashMap<String, String>();
List<String> list = new ArrayList<String>();
}
}
selections:
- anchor: {line: 2, character: 38}
active: {line: 2, character: 61}
thatMark:
- anchor: {line: 2, character: 38}
active: {line: 2, character: 61}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: h}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: type, includeSiblings: false}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
languageId: java
command:
version: 1
spokenForm: take type look
action: setSelection
targets:
- type: primitive
modifier: {type: containingScope, scopeType: type, includeSiblings: false}
mark: {type: decoratedSymbol, symbolColor: default, character: l}
initialState:
documentContents: |-
public class MyClass {
private MyClass () {
Map<String, String> map = new HashMap<String, String>();
List<String> list = new ArrayList<String>();
}
}
selections:
- anchor: {line: 2, character: 27}
active: {line: 2, character: 27}
marks:
default.l:
start: {line: 3, character: 21}
end: {line: 3, character: 25}
finalState:
documentContents: |-
public class MyClass {
private MyClass () {
Map<String, String> map = new HashMap<String, String>();
List<String> list = new ArrayList<String>();
}
}
selections:
- anchor: {line: 3, character: 8}
active: {line: 3, character: 20}
thatMark:
- anchor: {line: 3, character: 8}
active: {line: 3, character: 20}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: l}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: type, includeSiblings: false}}]
39 changes: 39 additions & 0 deletions src/test/suite/fixtures/recorded/languages/java/takeTypePit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
languageId: java
command:
version: 1
spokenForm: take type pit
action: setSelection
targets:
- type: primitive
modifier: {type: containingScope, scopeType: type, includeSiblings: false}
mark: {type: decoratedSymbol, symbolColor: default, character: p}
initialState:
documentContents: |-
public class MyClass {
private MyClass () {
Map<String, String> map = new HashMap<String, String>();
List<String> list = new ArrayList<String>();
}
}
selections:
- anchor: {line: 3, character: 52}
active: {line: 3, character: 52}
marks:
default.p:
start: {line: 2, character: 8}
end: {line: 2, character: 11}
finalState:
documentContents: |-
public class MyClass {
private MyClass () {
Map<String, String> map = new HashMap<String, String>();
List<String> list = new ArrayList<String>();
}
}
selections:
- anchor: {line: 2, character: 8}
active: {line: 2, character: 27}
thatMark:
- anchor: {line: 2, character: 8}
active: {line: 2, character: 27}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: p}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: type, includeSiblings: false}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
languageId: java
command:
version: 1
spokenForm: take type soon
action: setSelection
targets:
- type: primitive
modifier: {type: containingScope, scopeType: type, includeSiblings: false}
mark: {type: decoratedSymbol, symbolColor: default, character: s}
initialState:
documentContents: |-
public class MyClass {
private MyClass () {
Map<String, String> map = new HashMap<String, String>();
List<String> list = new ArrayList<String>();
}
}
selections:
- anchor: {line: 2, character: 8}
active: {line: 2, character: 27}
marks:
default.s:
start: {line: 2, character: 12}
end: {line: 2, character: 18}
finalState:
documentContents: |-
public class MyClass {
private MyClass () {
Map<String, String> map = new HashMap<String, String>();
List<String> list = new ArrayList<String>();
}
}
selections:
- anchor: {line: 2, character: 8}
active: {line: 2, character: 27}
thatMark:
- anchor: {line: 2, character: 8}
active: {line: 2, character: 27}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: s}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: type, includeSiblings: false}}]
Loading