Skip to content

Commit

Permalink
Include generic in java type definition (#372)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
AndreasArvidsson and pokey authored Dec 10, 2021
1 parent 66f160e commit 0712ee8
Show file tree
Hide file tree
Showing 30 changed files with 832 additions and 11 deletions.
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 {
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) {
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
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}}]
39 changes: 39 additions & 0 deletions src/test/suite/fixtures/recorded/languages/java/takeTypeGust.yml
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}}]
39 changes: 39 additions & 0 deletions src/test/suite/fixtures/recorded/languages/java/takeTypeGust2.yml
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>();
}
}
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}}]
39 changes: 39 additions & 0 deletions src/test/suite/fixtures/recorded/languages/java/takeTypeHarp.yml
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}}]
39 changes: 39 additions & 0 deletions src/test/suite/fixtures/recorded/languages/java/takeTypeLook.yml
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}}]
39 changes: 39 additions & 0 deletions src/test/suite/fixtures/recorded/languages/java/takeTypeSoon.yml
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

0 comments on commit 0712ee8

Please sign in to comment.