Skip to content

Commit

Permalink
Refactor 'findNameInPattern()' in terms of 'findInPattern()'
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanasa committed Aug 15, 2023
1 parent 0c9d4c3 commit 3ce64fe
Showing 1 changed file with 3 additions and 37 deletions.
40 changes: 3 additions & 37 deletions src/server/navigation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AST, Node, Span } from 'motoko/lib/ast';
import { Location, Position, Range } from 'vscode-languageserver';
import { Context, getContext } from './context';
import { findNodes, matchNode, asNode } from './syntax';
import { findNodes, matchNode, asNode, findInPattern } from './syntax';
import { getAbsoluteUri } from './utils';

interface Reference {
Expand Down Expand Up @@ -107,43 +107,9 @@ export function locationFromDefinition(definition: Definition) {
return location;
}

// TODO: refactor to use `findInPattern()`
function findNameInPattern(search: Search, pat: Node): Node | undefined {
const matchAny = (...args: Node[]) => {
for (const field of args) {
const result = findNameInPattern(search, field);
if (result) {
return result;
}
}
return;
};
const match = (arg: Node) => findNameInPattern(search, arg);
return (
matchNode(pat, 'VarP', (name: string) =>
name === search.name ? pat : undefined,
) ||
matchNode(pat, 'ObjP', (...args: Node[]) => {
for (const field of args) {
const aliasNode = field.args![0] as Node;
const alias = matchNode(
aliasNode,
'VarP',
(alias) => alias,
field.name,
);
if (alias === search.name) {
return aliasNode || field;
}
}
return;
}) ||
matchNode(pat, 'TupP', matchAny) ||
matchNode(pat, 'AltP', matchAny) ||
matchNode(pat, 'AnnotP', match) ||
matchNode(pat, 'ParP', match) ||
matchNode(pat, 'OptP', match) ||
matchNode(pat, 'TagP', (_tag, arg: Node) => match(arg))
return findInPattern(pat, (name, node) =>
name === search.name ? node : undefined,
);
}

Expand Down

0 comments on commit 3ce64fe

Please sign in to comment.