Skip to content

Commit

Permalink
Supported references
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuxiang committed Mar 23, 2022
1 parent 406b7f2 commit d4777a4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { onCompletion } from "./completion";
import { onDefinition } from "./definition";
import { onFormatting } from "./formatting";
import { onHover } from "./hover";
import { onReferences } from "./references";
import { onRename } from "./rename";
import { Solidity } from "./solidity";

Expand All @@ -36,6 +37,7 @@ export function createServer(
connection.onHover(onHover);
connection.onCompletion(onCompletion);
connection.onRenameRequest(onRename);
connection.onReferences(onReferences);

connection.onDidChangeConfiguration(({ settings }) => {
options = settings.solidity;
Expand Down
12 changes: 11 additions & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import {
EventDefinition,
Expression,
FunctionDefinition,
Identifier,
IdentifierPath,
ImportDirective,
MemberAccess,
ModifierDefinition,
SourceUnit,
Statement,
StructDefinition,
TypeName,
UserDefinedTypeName,
UserDefinedValueTypeDefinition,
VariableDeclaration,
} from "solidity-ast";
Expand Down Expand Up @@ -50,10 +53,13 @@ export type Definition =

export type DefinitionNode = Definition & ASTNodeData;
export type ImportNode = ImportDirective & ASTNodeData;
export type IdentifierNode = (Identifier | MemberAccess | UserDefinedTypeName) &
ASTNodeData;

export function parse(
node: ASTNode,
root: ASTNode,
identifiers: ASTNode[],
definitions: ASTNode[],
scopes: Map<number, DefinitionNode[]>,
nodes: ASTNode[],
Expand Down Expand Up @@ -83,6 +89,10 @@ export function parse(
} else {
scopes.set(scopeId, [node]);
}
case "Identifier":
case "MemberAccess":
case "UserDefinedTypeName":
identifiers.push(node);
break;
}
switch (node.nodeType) {
Expand Down Expand Up @@ -175,7 +185,7 @@ export function parse(
for (const child of children) {
if (!child) continue;
child.parent = node;
parse(child, root, definitions, scopes, nodes, nodeMap);
parse(child, root, identifiers, definitions, scopes, nodes, nodeMap);
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/references.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Location, Range, ReferenceParams } from "vscode-languageserver";
import { documents, solidityMap } from ".";

export function onReferences({
textDocument: { uri },
position,
}: ReferenceParams): Location[] {
const document = documents.get(uri);
if (!document) return [];

const solidity = solidityMap.get(uri);
if (!solidity) return [];

const node = solidity.getCurrentNodes(position)[0];
let nodeId = node.id;
const ref = Reflect.get(node, "referencedDeclaration");
if (ref) {
nodeId = ref;
}
const items = solidity.identifiers.filter(
(i) => i.referencedDeclaration == nodeId
);

return items.map((i) => {
return Location.create(
uri,
Range.create(
document.positionAt(i.srcStart!),
document.positionAt(i.srcEnd!)
)
);
});
}
10 changes: 9 additions & 1 deletion src/solidity.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { SourceUnit } from "solidity-ast";
import { Position, TextDocument } from "vscode-languageserver-textdocument";
import { ASTNode, DefinitionNode, ImportNode, parse } from "./parse";
import {
ASTNode,
DefinitionNode,
IdentifierNode,
ImportNode,
parse,
} from "./parse";

export class Solidity {
document: TextDocument;
identifiers: IdentifierNode[] = [];
definitions: DefinitionNode[] = [];
nodes = new Map<string, ASTNode[]>();
scopes = new Map<number, DefinitionNode[]>();
Expand All @@ -21,6 +28,7 @@ export class Solidity {
parse(
root,
root,
this.identifiers,
this.definitions,
this.scopes,
this.nodes.get(uri)!,
Expand Down

0 comments on commit d4777a4

Please sign in to comment.