-
-
Notifications
You must be signed in to change notification settings - Fork 94
Refactor transformations and add name transformation #56
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 9 commits
12ba0df
79ed944
0a96484
89e0c85
cd7c36a
b1061eb
c77d9f0
9c3b2f1
3a79d5d
8365b2c
91f4ade
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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import * as vscode from "vscode"; | ||
| import { SyntaxNode } from "web-tree-sitter"; | ||
|
|
||
| export function logBranchTypes(getNodeAtLocation: any) { | ||
|
Member
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. I generally try to avoid Also, I'd probably opt for a class here rather than returning a function, but let's leave for now as it's just for debugging |
||
| return (event: vscode.TextEditorSelectionChangeEvent) => { | ||
| const location = new vscode.Location( | ||
| vscode.window.activeTextEditor!.document.uri, | ||
| event.selections[0] | ||
| ); | ||
|
|
||
| const ancestors: SyntaxNode[] = []; | ||
| let node: SyntaxNode = getNodeAtLocation(location); | ||
| while (node.parent) { | ||
| ancestors.unshift(node.parent); | ||
| node = node.parent; | ||
| } | ||
|
Comment on lines
+11
to
+16
Member
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. Much cleaner 😊. I still prefer
Collaborator
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. Dang, I forgot about that! I will probably continue to forget about it unless an eslint rule is instituted 😅
Member
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. haha yeah good point. I think this would do the trick? |
||
|
|
||
| ancestors.forEach((node, i) => console.debug(">".repeat(i + 1), node.type)); | ||
| const leafText = ancestors[ancestors.length - 1].text | ||
| .replace(/\s+/g, " ") | ||
| .substring(0, 100); | ||
| console.debug(">".repeat(ancestors.length), `"${leafText}"`); | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,41 @@ | ||
| import { SyntaxNode } from "web-tree-sitter"; | ||
| import { TextEditor } from "vscode"; | ||
| import { getNameNode, getKeyNode, getValueNode } from "../treeSitterUtils"; | ||
| import { | ||
| delimitedMatcher, | ||
| hasType, | ||
| simpleSelectionExtractor, | ||
| makeRange, | ||
| childNodeMatcher, | ||
| getNodeWithLeadingDelimiter, | ||
| } from "../nodeMatchers"; | ||
| import { getKeyNode, getValueNode } from "../treeSitterUtils"; | ||
| delimitedSelector, | ||
| selectWithLeadingDelimiter, | ||
| } from "../nodeSelectors"; | ||
| import { composedMatcher, matcher, typeMatcher } from "../nodeMatchers"; | ||
| import { findNode, typedNodeFinder } from "../nodeFinders"; | ||
|
|
||
| export function getPojoMatchers( | ||
| dictionaryTypes: string[], | ||
| listTypes: string[], | ||
| listElementMatcher: (node: SyntaxNode) => boolean | ||
| ) { | ||
| return { | ||
| dictionary: hasType(...dictionaryTypes), | ||
| pair: delimitedMatcher( | ||
| (node) => node.type === "pair", | ||
| (node) => node.type === "," || node.type === "}" || node.type === "{", | ||
| ", " | ||
| dictionary: typeMatcher(...dictionaryTypes), | ||
| pair: matcher( | ||
| findNode((node) => node.type === "pair"), | ||
| delimitedSelector( | ||
| (node) => node.type === "," || node.type === "}" || node.type === "{", | ||
| ", " | ||
| ) | ||
| ), | ||
| pairKey(editor: TextEditor, node: SyntaxNode) { | ||
| if (node.type !== "pair") { | ||
| return null; | ||
| } | ||
|
|
||
| return simpleSelectionExtractor(getKeyNode(node)!); | ||
| }, | ||
| value: childNodeMatcher(getValueNode, getNodeWithLeadingDelimiter), | ||
| list: hasType(...listTypes), | ||
| listElement: delimitedMatcher( | ||
| (node) => | ||
| listTypes.includes(node.parent?.type ?? "") && listElementMatcher(node), | ||
| (node) => node.type === "," || node.type === "[" || node.type === "]", | ||
| ", " | ||
| pairKey: composedMatcher([typedNodeFinder("pair"), getKeyNode]), | ||
| value: matcher(getValueNode, selectWithLeadingDelimiter), | ||
| name: matcher(getNameNode), | ||
| list: typeMatcher(...listTypes), | ||
| listElement: matcher( | ||
| findNode( | ||
| (node) => | ||
| listTypes.includes(node.parent?.type ?? "") && | ||
| listElementMatcher(node) | ||
| ), | ||
| delimitedSelector( | ||
| (node) => node.type === "," || node.type === "[" || node.type === "]", | ||
| ", " | ||
| ) | ||
| ), | ||
| string: hasType("string"), | ||
| string: typeMatcher("string"), | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,27 @@ | ||
| import { SyntaxNode } from "web-tree-sitter"; | ||
| import { getPojoMatchers } from "./getPojoMatchers"; | ||
| import { | ||
| childNodeMatcher, | ||
| delimitedMatcher, | ||
| getNodeWithLeadingDelimiter, | ||
| hasType, | ||
| possiblyWrappedNode, | ||
| cascadingMatcher, | ||
| composedMatcher, | ||
| matcher, | ||
| notSupported, | ||
| typeMatcher, | ||
| } from "../nodeMatchers"; | ||
| import { NodeMatcher, ScopeType } from "../Types"; | ||
| import { getDefinitionNode } from "../treeSitterUtils"; | ||
| import { NodeFinder, NodeMatcher, ScopeType } from "../Types"; | ||
| import { | ||
| getDefinitionNode, | ||
| getLeftNode, | ||
| getNameNode, | ||
| } from "../treeSitterUtils"; | ||
| import { | ||
| findNode, | ||
| typedNodeFinder, | ||
| findPossiblyWrappedNode, | ||
| } from "../nodeFinders"; | ||
| import { | ||
| delimitedSelector, | ||
| selectWithLeadingDelimiter, | ||
| } from "../nodeSelectors"; | ||
|
|
||
| // TODO figure out how to properly use super types | ||
| // Generated by the following command: | ||
|
|
@@ -113,10 +126,10 @@ const ARGUMENT_TYPES = [ | |
| "keyword_argument", | ||
| ]; | ||
|
|
||
| function possiblyDecoratedDefinition(...typeNames: string[]): NodeMatcher { | ||
| return possiblyWrappedNode( | ||
| (node) => node.type === "decorated_definition", | ||
| (node) => typeNames.includes(node.type), | ||
| function possiblyDecoratedDefinition(...typeNames: string[]): NodeFinder { | ||
| return findPossiblyWrappedNode( | ||
| typedNodeFinder("decorated_definition"), | ||
| typedNodeFinder(...typeNames), | ||
| (node) => [getDefinitionNode(node)] | ||
| ); | ||
| } | ||
|
|
@@ -130,23 +143,39 @@ const nodeMatchers: Record<ScopeType, NodeMatcher> = { | |
| ["list", "list_comprehension"], | ||
| (node) => LIST_ELEMENT_TYPES.includes(node.type) | ||
| ), | ||
| ifStatement: hasType("if_statement"), | ||
| class: possiblyDecoratedDefinition("class_definition"), | ||
| statement: hasType(...STATEMENT_TYPES), | ||
| arrowFunction: hasType("lambda"), | ||
| functionCall: hasType("call"), | ||
| argumentOrParameter: delimitedMatcher( | ||
| (node) => | ||
| (node.parent?.type === "argument_list" && | ||
| ARGUMENT_TYPES.includes(node.type)) || | ||
| (PARAMETER_LIST_TYPES.includes(node.parent?.type ?? "") && | ||
| PARAMETER_TYPES.includes(node.type)), | ||
| (node) => node.type === "," || node.type === "(" || node.type === ")", | ||
| ", " | ||
| ifStatement: typeMatcher("if_statement"), | ||
| class: matcher(possiblyDecoratedDefinition("class_definition")), | ||
| statement: typeMatcher(...STATEMENT_TYPES), | ||
| name: cascadingMatcher( | ||
| matcher(getNameNode), | ||
| matcher((node) => (node.type === "assignment" ? getLeftNode(node) : null)) | ||
| ), | ||
| functionName: composedMatcher([ | ||
| possiblyDecoratedDefinition("function_definition"), | ||
| getNameNode, | ||
| ]), | ||
| className: composedMatcher([ | ||
| possiblyDecoratedDefinition("class_definition"), | ||
| getNameNode, | ||
| ]), | ||
|
Comment on lines
+153
to
+160
Member
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. well these were too easy 😂. did you try it on a decorated definition? I'd think it would not be able to find the name on the decoration node |
||
| arrowFunction: typeMatcher("lambda"), | ||
| functionCall: typeMatcher("call"), | ||
| argumentOrParameter: matcher( | ||
| findNode( | ||
| (node) => | ||
| (node.parent?.type === "argument_list" && | ||
| ARGUMENT_TYPES.includes(node.type)) || | ||
| (PARAMETER_LIST_TYPES.includes(node.parent?.type ?? "") && | ||
| PARAMETER_TYPES.includes(node.type)) | ||
| ), | ||
| delimitedSelector( | ||
| (node) => node.type === "," || node.type === "(" || node.type === ")", | ||
| ", " | ||
| ) | ||
| ), | ||
| namedFunction: possiblyDecoratedDefinition("function_definition"), | ||
| comment: hasType("comment"), | ||
| type: childNodeMatcher(getTypeNode, getNodeWithLeadingDelimiter), | ||
| namedFunction: matcher(possiblyDecoratedDefinition("function_definition")), | ||
| comment: typeMatcher("comment"), | ||
| type: matcher(getTypeNode, selectWithLeadingDelimiter), | ||
| }; | ||
|
|
||
| export default nodeMatchers; | ||
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.
I might move the
NodeMatcherabove down to right above this one; it got separated in the rebaseI also might add a docstring for this type and the next one as they're important abstractions