-
Notifications
You must be signed in to change notification settings - Fork 9
Adding function information to the /render page #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 62 commits
f7b084e
3151ea6
ca084e1
7a7b13f
2ae4d60
d5bbffb
a2ba7d5
7618ad8
d675eac
6e6d675
e944b01
a71484b
34e3ae6
328d86d
dcea710
69bf1a4
0d3f03e
c279feb
fac3bef
ce19dd5
4d7f20f
542004b
5dc7ce2
dc4457f
b73437c
7253af7
f69336f
26c9883
1768167
c17b84c
52c72f7
8c74c99
89a9f51
051ccc6
8adbcfd
9880873
d145111
3ab9247
009f53a
e6c8172
607b7fe
2469d6d
41ac697
fd7aa24
421878f
ffbae88
dcd0bd9
6795204
aafda36
d5a415e
546d8da
65eb0a0
c570bd3
491c11e
8ff7179
4881842
a87245f
cdaae1d
e461ea4
f372e4c
16eb3f1
5da022e
071fa8c
a409c22
cd10232
be1f075
96f7cf9
cf9a808
488d200
40cef6d
b731404
c05ddf2
734d70d
e0603a7
a586e5d
a5ca535
0aebc93
baab71d
555194f
2330f49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ import { | |
| type StatementHandlers, | ||
| } from "./generic-cfg-builder"; | ||
| import { treeSitterNoNullNodes } from "./hacks.ts"; | ||
| import { extractTaggedValueFromTreeSitterQuery } from "./query-utils.ts"; | ||
| import { type SwitchOptions, buildSwitch, collectCases } from "./switch-utils"; | ||
|
|
||
| export const goLanguageDefinition = { | ||
|
|
@@ -419,3 +420,113 @@ function processSwitchlike( | |
|
|
||
| return blockHandler.update({ entry: headNode, exit: mergeNode }); | ||
| } | ||
|
|
||
| const functionQuery = { | ||
| functionDeclaration: `(function_declaration | ||
| name :(identifier) @name)`, | ||
|
|
||
| methodDeclaration: `(method_declaration | ||
| name: (field_identifier) @name)`, | ||
|
|
||
| shortVarDeclaration: `(short_var_declaration | ||
| left: (expression_list (identifier) @name))`, | ||
|
|
||
| varSpec: `(var_spec | ||
| name: (identifier) @name)`, | ||
|
|
||
| assignmentStatement: `(assignment_statement | ||
| left: (expression_list | ||
| [ | ||
| (identifier) @name | ||
| (selector_expression) @name | ||
| ]))`, | ||
|
|
||
| name: "name", | ||
| }; | ||
|
|
||
| function findVariableBinding(func: SyntaxNode): string { | ||
| const parent = func.parent; | ||
| const anonymous = "<anonymous>"; | ||
| if (!parent) return anonymous; | ||
|
|
||
| // Walk the right-hand expression list and find the index of *this* func literal. | ||
| // I compare by node id to be safe — same node, same id. | ||
| const findFuncIndex = ( | ||
| funcNode: SyntaxNode, | ||
| right: SyntaxNode, | ||
| ): number | null => { | ||
| for (let i = 0; i < right.namedChildCount; i++) { | ||
| const child = right.namedChild(i); | ||
| if (child?.type === "func_literal" && child.id === funcNode.id) return i; | ||
| } | ||
| return null; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| }; | ||
|
|
||
| // We run the left query -> get names[], locate our func literal on the right -> get index, | ||
| // then names[index] is the binding. | ||
| // If nothing matches, return "<anonymous>". | ||
| const bindFromPair = ( | ||
| node: SyntaxNode, | ||
| leftPattern: string, | ||
| rightField: "right" | "value" = "right", | ||
| ): string => { | ||
| const left = extractTaggedValueFromTreeSitterQuery( | ||
| node, | ||
| leftPattern, | ||
| functionQuery.name, | ||
| ); | ||
| const right = node.childForFieldName(rightField); | ||
| if (!right) return anonymous; | ||
|
|
||
| const foundIndex = findFuncIndex(func, right); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: We don't care that we "found" the index, we want to know what it represents. |
||
| return foundIndex !== null ? (left[foundIndex] ?? anonymous) : anonymous; | ||
| }; | ||
|
|
||
| // := short var declaration | ||
| if (parent.parent?.type === "short_var_declaration") { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Why not a switch-case here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. on it |
||
| return bindFromPair( | ||
| parent.parent, | ||
| functionQuery.shortVarDeclaration, | ||
| "right", | ||
| ); | ||
| } | ||
|
|
||
| // = plain assignment ... | ||
| if (parent.parent?.type === "assignment_statement") { | ||
| return bindFromPair( | ||
| parent.parent, | ||
| functionQuery.assignmentStatement, | ||
| "right", | ||
| ); | ||
| } | ||
|
|
||
| // var x, y = ..., func(){}, ... | ||
| // Same idea, but Go’s var spec uses "value". | ||
| if (parent.parent?.type === "var_spec") { | ||
| return bindFromPair(parent.parent, functionQuery.varSpec, "value"); | ||
| } | ||
|
|
||
| // If we got here, we didn’t find a binding in the supported contexts. | ||
| return anonymous; | ||
| } | ||
|
|
||
| export function extractGoFunctionName(func: SyntaxNode): string | undefined { | ||
| switch (func.type) { | ||
| case "function_declaration": | ||
| return extractTaggedValueFromTreeSitterQuery( | ||
| func, | ||
| functionQuery.functionDeclaration, | ||
| functionQuery.name, | ||
| )[0]; | ||
| case "method_declaration": | ||
| return extractTaggedValueFromTreeSitterQuery( | ||
| func, | ||
| functionQuery.methodDeclaration, | ||
| functionQuery.name, | ||
| )[0]; | ||
| case "func_literal": | ||
| return findVariableBinding(func); | ||
| default: | ||
| return undefined; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer containment checks, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes