Skip to content

Commit b1ecc92

Browse files
Change debug log to represent tree sitter query syntax (#1554)
from ``` > source_file > source_file >> declarations >> declarations >>> settings_declaration >>> settings_declaration >>>> left: settings() >>>> left: settings() >>>> "settings()" >>>> "settings()" ``` to ``` (source_file (declarations (settings_declaration left: "settings()" @cursor ) ) ) ``` `console.debug` is the culprit in why we got to duplicated lines. Switching to `console.log` removes the duplicated lines. I think this is an improvement until we do #1115 I'm not totally sure if I like the `@cursor`. ## Checklist - [ ] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [ ] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [ ] I have not broken the cheatsheet
1 parent 440b6b2 commit b1ecc92

File tree

1 file changed

+42
-18
lines changed
  • packages/cursorless-engine/src/core

1 file changed

+42
-18
lines changed

packages/cursorless-engine/src/core/Debug.ts

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class Debug {
3838

3939
log(...args: any[]) {
4040
if (this.active) {
41-
console.debug(...args);
41+
console.log(...args);
4242
}
4343
}
4444

@@ -93,27 +93,51 @@ export class Debug {
9393
}
9494

9595
const cursor = node.tree.walk();
96-
this.printCursorLocationInfo(cursor, 0);
97-
98-
for (let i = 1; i < ancestors.length; ++i) {
99-
cursor.gotoFirstChild();
100-
while (cursor.currentNode().id !== ancestors[i].id) {
101-
if (!cursor.gotoNextSibling()) {
102-
return;
103-
}
96+
this.printCursorLocationInfo(ancestors, cursor, 0);
97+
}
98+
99+
private printCursorLocationInfo(
100+
nodes: SyntaxNode[],
101+
cursor: TreeCursor,
102+
index: number,
103+
) {
104+
const field = cursor.currentFieldName();
105+
const fieldText = field != null ? `${field}: ` : "";
106+
const indent = " ".repeat(index);
107+
const nodeIsLast = index === nodes.length - 1;
108+
const { nodeIsNamed } = cursor;
109+
let text = `${indent}${fieldText}`;
110+
111+
if (nodeIsNamed) {
112+
text += `(${cursor.nodeType}`;
113+
if (nodeIsLast) {
114+
text += ")";
104115
}
105-
this.printCursorLocationInfo(cursor, i);
116+
} else {
117+
text += `"${cursor.nodeType}"`;
118+
}
119+
120+
console.log(text);
121+
122+
if (
123+
!nodeIsLast &&
124+
this.cursorGoToChildWithId(cursor, nodes[index + 1].id)
125+
) {
126+
this.printCursorLocationInfo(nodes, cursor, index + 1);
106127
}
107128

108-
const leafText = ancestors[ancestors.length - 1].text
109-
.replace(/\s+/g, " ")
110-
.substring(0, 100);
111-
console.debug(">".repeat(ancestors.length), `"${leafText}"`);
129+
if (nodeIsNamed && !nodeIsLast) {
130+
console.log(`${indent})`);
131+
}
112132
}
113133

114-
private printCursorLocationInfo(cursor: TreeCursor, depth: number) {
115-
const field = cursor.currentFieldName();
116-
const fieldText = field != null ? `${field}: ` : "";
117-
console.debug(">".repeat(depth + 1), `${fieldText}${cursor.nodeType}`);
134+
private cursorGoToChildWithId(cursor: TreeCursor, id: number): boolean {
135+
cursor.gotoFirstChild();
136+
while (cursor.currentNode().id !== id) {
137+
if (!cursor.gotoNextSibling()) {
138+
return false;
139+
}
140+
}
141+
return true;
118142
}
119143
}

0 commit comments

Comments
 (0)