Skip to content

Commit

Permalink
fix #36
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregoor committed Nov 18, 2020
1 parent c7aea09 commit bb3e88c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 67 deletions.
17 changes: 8 additions & 9 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,8 @@ export const findActions = (code: Code, cursor: Range) => ({
: [],
});

const modifieresPressed = (action: BaseAction, event: KeyboardEvent) =>
modifierKeys.every((key) =>
action.on && key in action.on ? action.on[key] == event[key] : true
);
const modifieresPressed = (on: BaseAction["on"], event: KeyboardEvent) =>
modifierKeys.every((key) => (on && key in on ? on[key] == event[key] : true));

export function findAction(code: Code, cursor: Range, event: KeyboardEvent) {
for (const action of getBaseActions(code, cursor)) {
Expand All @@ -277,7 +275,7 @@ export function findAction(code: Code, cursor: Range, event: KeyboardEvent) {
("code" in action.on
? action.on.code === event.code
: action.on.key === event.key) &&
modifieresPressed(action, event)
modifieresPressed(action.on, event)
) {
return () => action.do(code, cursor, event.shiftKey);
}
Expand All @@ -291,10 +289,11 @@ export function findAction(code: Code, cursor: Range, event: KeyboardEvent) {
for (const action of actions) {
if (
action.on &&
("code" in action.on
? action.on.code === event.code
: action.on.key === event.key) &&
modifieresPressed(action, event)
(Array.isArray(action.on) ? action.on : [action.on]).some(
(on) =>
("code" in on ? on.code === event.code : on.key === event.key) &&
modifieresPressed(on, event)
)
) {
return () => action.do();
}
Expand Down
12 changes: 10 additions & 2 deletions src/nodes/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,11 @@ export const expressions: NodeDefs = {
},
},
StringLiteral: {
hasSlot: (node, start) => start > node.start!,
hasSlot: (node, start) => true,
},
TemplateLiteral: { hasSlot: () => true },
TemplateElement: { hasSlot: () => true },
BooleanLiteral: { hasSlot: selectNode },
NullLiteral: { hasSlot: selectNode },

Identifier: { hasSlot: () => true },

Expand All @@ -240,6 +241,13 @@ export const expressions: NodeDefs = {
},
actions: changeOperationActions,
},
LogicalExpression: {
hasSlot(node, start, { source }) {
const range = selectOperator(node, source);
return range.includes(start) ? range : false;
},
actions: changeOperationActions,
},

ArrayExpression: {
hasSlot(node, start) {
Expand Down
51 changes: 0 additions & 51 deletions src/nodes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,57 +20,6 @@ import {
} from "./utils";

const nodeDefs: NodeDefs = {
BooleanLiteral: { hasSlot: selectNode },
NullLiteral: { hasSlot: selectNode },
StringLiteral: { hasSlot: (node, start) => node.start !== start },

BinaryExpression: {
hasSlot(node, start, { source }) {
const operator = selectOperator(node, source);
return operator.includes(start) ? operator : false;
},
},
LogicalExpression: {
hasSlot(node, start, { source }) {
const operator = selectOperator(node, source);
return operator.includes(start) ? operator : false;
},
},

VariableDeclaration: {
hasSlot(node, start) {
const kindRange = selectKind(node);
return kindRange.includes(start) ? kindRange : false;
},
actions: ({ node, code, cursor }) =>
selectKind(node).equals(cursor)
? (["const", "let", "var"] as const)
.filter((kind) => node.kind != kind)
.map((kind) => ({
info: { type: "CHANGE_DECLARATION_KIND", kind },
on: { code: "Key" + kind[0].toUpperCase() },
do: () => ({
code: code.replaceSource(
new Range(node.start!, node.start! + node.kind.length),
kind
),
nextCursor: ({ ast }, { start }) =>
selectKind(getNode(ast, start) as typeof node),
}),
}))
: null,
},
VariableDeclarator: {
actions: ({ node, path, code }) =>
node.init && {
on: { code: "Space" },
do: () => ({
code: code.replaceSource(new Range(node.end!), "= null"),
nextCursor: ({ ast }) => selectNodeFromPath(ast, [...path, "init"]),
}),
},
},

...expressions,
...statements,
};
Expand Down
31 changes: 27 additions & 4 deletions src/nodes/statements.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import t from "@babel/types";

import { getNodeFromPath } from "../ast-utils";
import { selectNode, selectNodeFromPath } from "../cursor/utils";
import { getNode, getNodeFromPath } from "../ast-utils";
import { selectKind, selectNode, selectNodeFromPath } from "../cursor/utils";
import { Range } from "../utils";
import { NodeDefs } from "./utils";

Expand All @@ -28,13 +28,36 @@ export const statements: NodeDefs = {
})),
},

VariableDeclaration: {
hasSlot(node, start) {
const kindRange = selectKind(node);
return kindRange.includes(start) ? kindRange : false;
},
actions: ({ node, code, cursor }) =>
selectKind(node).equals(cursor)
? (["const", "let", "var"] as const)
.filter((kind) => node.kind != kind)
.map((kind) => ({
info: { type: "CHANGE_DECLARATION_KIND", kind },
on: { code: "Key" + kind[0].toUpperCase() },
do: () => ({
code: code.replaceSource(
new Range(node.start!, node.start! + node.kind.length),
kind
),
nextCursor: ({ ast }, { start }) =>
selectKind(getNode(ast, start) as typeof node),
}),
}))
: null,
},
VariableDeclarator: {
actions: ({ node, path, code, cursor }) =>
!node.init &&
cursor.start == node.id.end! && {
on: { key: "=" },
on: [{ code: "Space" }, { key: "=" }],
do: () => ({
code: code.replaceSource(cursor, "= null"),
code: code.replaceSource(new Range(node.end!), "= null"),
nextCursor: ({ ast }) => selectNodeFromPath(ast, [...path, "init"]),
}),
},
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Change, KeyConfig, Range } from "../utils";

export type NodeAction = {
info?: any;
on?: KeyConfig;
on?: KeyConfig | KeyConfig[];
do: () => Change<ValidCode>;
};
export type NodeActions = false | null | NodeAction | NodeActions[];
Expand Down

0 comments on commit bb3e88c

Please sign in to comment.