Skip to content

Commit

Permalink
Implement go-to-definition for qualified types (#235)
Browse files Browse the repository at this point in the history
* Support all assignment patterns in outline / go-to-symbol

* Refactor 'findNameInPattern()' in terms of 'findInPattern()'

* Implement go-to-definition for qualified types
  • Loading branch information
rvanasa authored Aug 16, 2023
1 parent 9e82262 commit d24a92b
Showing 1 changed file with 5 additions and 38 deletions.
43 changes: 5 additions & 38 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 @@ -109,41 +109,8 @@ export function locationFromDefinition(definition: Definition) {

// 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 Expand Up @@ -232,9 +199,9 @@ function getTypeSearchPath(node: Node): Search[] {
},
]) ||
matchNode(node, 'DotH', (qual: Node, name: string) => [
...getTypeSearchPath(qual),
...getQualifierSearchPath(qual),
{
type: 'type',
type: 'variable',
name,
},
]) ||
Expand Down

0 comments on commit d24a92b

Please sign in to comment.