Skip to content

Commit

Permalink
Migrate Typescript "type" to next-gen; fix value removal (cursorless-…
Browse files Browse the repository at this point in the history
…dev#1925)

Note that the first commit moves `typescript.scm` to
`typescript.core.scm`, because we now have some patterns that shouldn't
be defined for TSX (cast assertions eg `<foo>bar`). I made it its own
commit because otherwise the diff is confusing. Might be easier to
review
https://github.com/cursorless-dev/cursorless/pull/1925/files/dbb4e5ea4135dad6bcff272f6bc85752a650013e..a2ced3b3ad14a27b4d47d344ddacfebba65c0915,
which excludes that commit

## Checklist

- [x] I have added
[tests](https://www.cursorless.org/docs/contributing/test-case-recorder/)
- [-] 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)
- [-] I have not broken the cheatsheet
  • Loading branch information
pokey authored Oct 3, 2023
1 parent 4a2091e commit 79b4d59
Show file tree
Hide file tree
Showing 17 changed files with 966 additions and 195 deletions.
83 changes: 1 addition & 82 deletions packages/cursorless-engine/src/languages/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { SimpleScopeTypeType } from "@cursorless/common";
import type { SyntaxNode } from "web-tree-sitter";
import {
NodeMatcher,
NodeMatcherAlternative,
SelectionWithEditor,
} from "../typings/Types";
import { NodeMatcherAlternative, SelectionWithEditor } from "../typings/Types";
import { patternFinder } from "../util/nodeFinders";
import {
argumentMatcher,
Expand All @@ -20,9 +16,6 @@ import {
extendForwardPastOptional,
getNodeInternalRange,
getNodeRange,
pairSelectionExtractor,
selectWithLeadingDelimiter,
simpleSelectionExtractor,
unwrapSelectionExtractor,
} from "../util/nodeSelectors";
import { branchMatcher } from "./branchMatcher";
Expand Down Expand Up @@ -69,61 +62,6 @@ const STATEMENT_TYPES = [
"with_statement",
];

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;
};
}

const mapTypes = ["object", "object_pattern"];
const listTypes = ["array", "array_pattern"];

Expand Down Expand Up @@ -194,25 +132,6 @@ const nodeMatchers: Partial<
"export_statement?.abstract_class_declaration", // export abstract class | abstract class
"export_statement.class", // export default class
],
type: cascadingMatcher(
// Typed parameters, properties, and functions
typeMatcher(),
// matcher(findTypeNode, selectWithLeadingDelimiter(":")),
patternMatcher(
// Type alias/interface declarations
"export_statement?.type_alias_declaration",
"export_statement?.interface_declaration",
// as type declarations
"as_expression.generic_type!",
"as_expression.predefined_type!",
// satisfies type declaration
"satisfies_expression.generic_type!",
"satisfies_expression.predefined_type!",
// <type> declaration
"type_assertion.type_arguments.generic_type!",
"type_assertion.type_arguments.predefined_type!",
),
),
argumentOrParameter: argumentMatcher("formal_parameters", "arguments"),
// XML, JSX
attribute: ["jsx_attribute"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
languageId: typescript
command:
version: 6
spokenForm: change every type
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: everyScope
scopeType: {type: type}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
const aaa: number = 0;
const bbb: number = 0;
selections:
- anchor: {line: 1, character: 22}
active: {line: 1, character: 22}
marks: {}
finalState:
documentContents: |-
const aaa: = 0;
const bbb: = 0;
selections:
- anchor: {line: 0, character: 11}
active: {line: 0, character: 11}
- anchor: {line: 1, character: 11}
active: {line: 1, character: 11}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
languageId: typescript
command:
version: 6
spokenForm: change every type
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: everyScope
scopeType: {type: type}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
function ccc() {
const aaa: number = 0;
const bbb: number = 0;
}
selections:
- anchor: {line: 2, character: 26}
active: {line: 2, character: 26}
marks: {}
finalState:
documentContents: |-
function ccc() {
const aaa: = 0;
const bbb: = 0;
}
selections:
- anchor: {line: 1, character: 15}
active: {line: 1, character: 15}
- anchor: {line: 2, character: 15}
active: {line: 2, character: 15}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
languageId: typescript
command:
version: 6
spokenForm: change every type
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: everyScope
scopeType: {type: type}
usePrePhraseSnapshot: true
initialState:
documentContents: "function ccc(aaa: string, bbb: string) {}"
selections:
- anchor: {line: 0, character: 13}
active: {line: 0, character: 13}
marks: {}
finalState:
documentContents: "function ccc(aaa: , bbb: ) {}"
selections:
- anchor: {line: 0, character: 18}
active: {line: 0, character: 18}
- anchor: {line: 0, character: 25}
active: {line: 0, character: 25}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
languageId: typescript
command:
version: 6
spokenForm: change type
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: type}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
const aaa: number = 0;
let bbb: number = 0;
var hhh: number = 0;
const ddd: number = 0, eee: number = 0;
let fff: number = 0, ggg: number = 0;
var iii: number = 0;
export const jjj: number = 0;
export let kkk: number = 0;
export var lll: number = 0;
export const mmm: number = 0, nnn: number = 0;
export let ooo: number = 0, ppp: number = 0;
let qqq: number;
var rrr: number;
let sss: number, ttt: number;
selections:
- anchor: {line: 0, character: 22}
active: {line: 0, character: 22}
- anchor: {line: 1, character: 20}
active: {line: 1, character: 20}
- anchor: {line: 2, character: 20}
active: {line: 2, character: 20}
- anchor: {line: 3, character: 39}
active: {line: 3, character: 39}
- anchor: {line: 4, character: 37}
active: {line: 4, character: 37}
- anchor: {line: 5, character: 20}
active: {line: 5, character: 20}
- anchor: {line: 6, character: 29}
active: {line: 6, character: 29}
- anchor: {line: 7, character: 27}
active: {line: 7, character: 27}
- anchor: {line: 8, character: 27}
active: {line: 8, character: 27}
- anchor: {line: 9, character: 46}
active: {line: 9, character: 46}
- anchor: {line: 10, character: 44}
active: {line: 10, character: 44}
- anchor: {line: 11, character: 16}
active: {line: 11, character: 16}
- anchor: {line: 12, character: 16}
active: {line: 12, character: 16}
- anchor: {line: 13, character: 29}
active: {line: 13, character: 29}
marks: {}
finalState:
documentContents: |-
const aaa: = 0;
let bbb: = 0;
var hhh: = 0;
const ddd: = 0, eee: = 0;
let fff: = 0, ggg: = 0;
var iii: = 0;
export const jjj: = 0;
export let kkk: = 0;
export var lll: = 0;
export const mmm: = 0, nnn: = 0;
export let ooo: = 0, ppp: = 0;
let qqq: ;
var rrr: ;
let sss: , ttt: ;
selections:
- anchor: {line: 0, character: 11}
active: {line: 0, character: 11}
- anchor: {line: 1, character: 9}
active: {line: 1, character: 9}
- anchor: {line: 2, character: 9}
active: {line: 2, character: 9}
- anchor: {line: 3, character: 11}
active: {line: 3, character: 11}
- anchor: {line: 3, character: 22}
active: {line: 3, character: 22}
- anchor: {line: 4, character: 9}
active: {line: 4, character: 9}
- anchor: {line: 4, character: 20}
active: {line: 4, character: 20}
- anchor: {line: 5, character: 9}
active: {line: 5, character: 9}
- anchor: {line: 6, character: 18}
active: {line: 6, character: 18}
- anchor: {line: 7, character: 16}
active: {line: 7, character: 16}
- anchor: {line: 8, character: 16}
active: {line: 8, character: 16}
- anchor: {line: 9, character: 18}
active: {line: 9, character: 18}
- anchor: {line: 9, character: 29}
active: {line: 9, character: 29}
- anchor: {line: 10, character: 16}
active: {line: 10, character: 16}
- anchor: {line: 10, character: 27}
active: {line: 10, character: 27}
- anchor: {line: 11, character: 9}
active: {line: 11, character: 9}
- anchor: {line: 12, character: 9}
active: {line: 12, character: 9}
- anchor: {line: 13, character: 9}
active: {line: 13, character: 9}
- anchor: {line: 13, character: 16}
active: {line: 13, character: 16}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
languageId: typescript
command:
version: 6
spokenForm: change type
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: type}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
const ddd: number = 0, eee: number = 0;
let fff: number = 0, ggg: number = 0;
export const mmm: number = 0, nnn: number = 0;
export let ooo: number = 0, ppp: number = 0;
selections:
- anchor: {line: 0, character: 38}
active: {line: 0, character: 38}
- anchor: {line: 1, character: 36}
active: {line: 1, character: 36}
- anchor: {line: 2, character: 45}
active: {line: 2, character: 45}
- anchor: {line: 3, character: 43}
active: {line: 3, character: 43}
marks: {}
finalState:
documentContents: |-
const ddd: number = 0, eee: = 0;
let fff: number = 0, ggg: = 0;
export const mmm: number = 0, nnn: = 0;
export let ooo: number = 0, ppp: = 0;
selections:
- anchor: {line: 0, character: 28}
active: {line: 0, character: 28}
- anchor: {line: 1, character: 26}
active: {line: 1, character: 26}
- anchor: {line: 2, character: 35}
active: {line: 2, character: 35}
- anchor: {line: 3, character: 33}
active: {line: 3, character: 33}
Loading

0 comments on commit 79b4d59

Please sign in to comment.