|
| 1 | +import { FlashStyle, Range, TextDocument } from "@cursorless/common"; |
| 2 | +import * as path from "node:path"; |
| 3 | +import type { Tree, TreeCursor } from "web-tree-sitter"; |
| 4 | +import type { TreeSitter } from ".."; |
| 5 | +import { ide } from "../singletons/ide.singleton"; |
| 6 | +import type { Target } from "../typings/target.types"; |
| 7 | +import { flashTargets } from "../util/targetUtils"; |
| 8 | +import type { ActionReturnValue } from "./actions.types"; |
| 9 | + |
| 10 | +export default class ShowParseTree { |
| 11 | + constructor(private treeSitter: TreeSitter) { |
| 12 | + this.run = this.run.bind(this); |
| 13 | + } |
| 14 | + |
| 15 | + async run(targets: Target[]): Promise<ActionReturnValue> { |
| 16 | + await flashTargets(ide(), targets, FlashStyle.referenced); |
| 17 | + |
| 18 | + const results: string[] = ["# Cursorless parse tree"]; |
| 19 | + |
| 20 | + for (const target of targets) { |
| 21 | + const { editor, contentRange } = target; |
| 22 | + const tree = this.treeSitter.getTree(editor.document); |
| 23 | + results.push(parseTree(editor.document, tree, contentRange)); |
| 24 | + } |
| 25 | + |
| 26 | + ide().openUntitledTextDocument({ |
| 27 | + language: "markdown", |
| 28 | + content: results.join("\n\n"), |
| 29 | + }); |
| 30 | + |
| 31 | + return { thatTargets: targets }; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function parseTree( |
| 36 | + document: TextDocument, |
| 37 | + tree: Tree, |
| 38 | + contentRange: Range, |
| 39 | +): string { |
| 40 | + const resultPlayground: string[] = []; |
| 41 | + const resultQuery: string[] = []; |
| 42 | + |
| 43 | + parseCursor(resultPlayground, resultQuery, contentRange, tree.walk(), 0); |
| 44 | + |
| 45 | + return [ |
| 46 | + `## ${path.basename(document.uri.path)} [${contentRange}]\n`, |
| 47 | + `\`\`\`${document.languageId}`, |
| 48 | + document.getText(contentRange), |
| 49 | + "```", |
| 50 | + "", |
| 51 | + "```scm", |
| 52 | + ...resultQuery, |
| 53 | + "```", |
| 54 | + "", |
| 55 | + "```js", |
| 56 | + ...resultPlayground, |
| 57 | + "```", |
| 58 | + "", |
| 59 | + ].join("\n"); |
| 60 | +} |
| 61 | + |
| 62 | +function parseCursor( |
| 63 | + resultPlayground: string[], |
| 64 | + resultQuery: string[], |
| 65 | + contentRange: Range, |
| 66 | + cursor: TreeCursor, |
| 67 | + numIndents: number, |
| 68 | +): void { |
| 69 | + while (true) { |
| 70 | + const nodeRange = new Range( |
| 71 | + cursor.startPosition.row, |
| 72 | + cursor.startPosition.column, |
| 73 | + cursor.endPosition.row, |
| 74 | + cursor.endPosition.column, |
| 75 | + ); |
| 76 | + |
| 77 | + if (contentRange.intersection(nodeRange) != null) { |
| 78 | + const indentation = " ".repeat(numIndents); |
| 79 | + const fieldName = getFieldName(cursor); |
| 80 | + const prefix = indentation + fieldName; |
| 81 | + |
| 82 | + // Named node |
| 83 | + if (cursor.nodeIsNamed) { |
| 84 | + resultPlayground.push(`${prefix}${cursor.nodeType} [${nodeRange}]`); |
| 85 | + resultQuery.push(`${prefix}(${cursor.nodeType}`); |
| 86 | + |
| 87 | + // Named node with children |
| 88 | + if (cursor.gotoFirstChild()) { |
| 89 | + parseCursor( |
| 90 | + resultPlayground, |
| 91 | + resultQuery, |
| 92 | + contentRange, |
| 93 | + cursor, |
| 94 | + numIndents + 1, |
| 95 | + ); |
| 96 | + cursor.gotoParent(); |
| 97 | + resultQuery.push(`${indentation})`); |
| 98 | + } |
| 99 | + // Named node without children |
| 100 | + else { |
| 101 | + resultQuery[resultQuery.length - 1] += ")"; |
| 102 | + } |
| 103 | + } |
| 104 | + // Anonymous node |
| 105 | + else { |
| 106 | + const type = `"${cursor.nodeType}"`; |
| 107 | + resultPlayground.push(`${prefix}${type} [${nodeRange}]`); |
| 108 | + resultQuery.push(`${prefix}${type}`); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if (!cursor.gotoNextSibling()) { |
| 113 | + return; |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +function getFieldName(cursor: TreeCursor): string { |
| 119 | + const field = cursor.currentFieldName(); |
| 120 | + return field != null ? `${field}: ` : ""; |
| 121 | +} |
0 commit comments