Skip to content
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

Only show debug log in development mode #380

Merged
merged 19 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@
"default": true,
"description": "Whether to show decorations on vscode start."
},
"cursorless.debug": {
"type": "boolean",
"default": false,
"description": "Whether to show debug logs."
},
"cursorless.pendingEditDecorationTime": {
"type": "integer",
"default": 100,
Expand Down
121 changes: 121 additions & 0 deletions src/core/Debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import {
Disposable,
ExtensionMode,
Location,
TextEditorSelectionChangeEvent,
window,
workspace,
} from "vscode";
import { SyntaxNode, TreeCursor } from "web-tree-sitter";
import { Graph } from "../typings/Types";

const originalDebugLog = console.debug;
const disabledDebugLog = () => {};

console.debug = disabledDebugLog;
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved

export default class Debug {
getNodeAtLocation?: any;
disposable?: Disposable;
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved

constructor(private graph: Graph) {
this.graph.extensionContext.subscriptions.push(this);

this.evaluateSetting = this.evaluateSetting.bind(this);

switch (this.graph.extensionContext.extensionMode) {
// Development mode. Always enable.
case ExtensionMode.Development:
this.enableDebugLog();
break;
// Test mode. Always disable.
case ExtensionMode.Test:
this.disableDebugLog();
break;
// Production mode. Enable based on user setting.
case ExtensionMode.Production:
this.evaluateSetting();
workspace.onDidChangeConfiguration(this.evaluateSetting);
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
break;
}
}

init(getNodeAtLocation: any) {
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
this.getNodeAtLocation = getNodeAtLocation;
}

dispose() {
if (this.disposable) {
this.disposable.dispose();
}
}

private enableDebugLog() {
console.debug = originalDebugLog;
this.disposable = window.onDidChangeTextEditorSelection(
this.logBranchTypes()
);
}

private disableDebugLog() {
console.debug = disabledDebugLog;
this.dispose();
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
}

private evaluateSetting() {
const debugEnabled = workspace
.getConfiguration("cursorless")
.get<boolean>("debug")!;
if (debugEnabled) {
this.enableDebugLog();
} else {
this.disableDebugLog();
}
}

private logBranchTypes() {
return (event: TextEditorSelectionChangeEvent) => {
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
const location = new Location(
window.activeTextEditor!.document.uri,
event.selections[0]
);

let node: SyntaxNode;
try {
node = this.getNodeAtLocation(location);
} catch (error) {
return;
}

const ancestors: SyntaxNode[] = [node];
while (node.parent != null) {
ancestors.unshift(node.parent);
node = node.parent;
}

const cursor = node.tree.walk();
this.print(cursor, 0);

for (let i = 1; i < ancestors.length; ++i) {
cursor.gotoFirstChild();
while (cursor.currentNode().id !== ancestors[i].id) {
if (!cursor.gotoNextSibling()) {
return;
}
}
this.print(cursor, i);
}

const leafText = ancestors[ancestors.length - 1].text
.replace(/\s+/g, " ")
.substring(0, 100);
console.debug(">".repeat(ancestors.length), `"${leafText}"`);
};
}

private print(cursor: TreeCursor, depth: number) {
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
const field = cursor.currentFieldName();
const fieldText = field != null ? `${field}: ` : "";
console.debug(">".repeat(depth + 1), `${fieldText}${cursor.nodeType}`);
}
}
9 changes: 2 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as vscode from "vscode";
import graphFactories from "./util/graphFactories";
import { Graph } from "./typings/Types";
import makeGraph, { FactoryMap } from "./util/makeGraph";
import { logBranchTypes } from "./util/debug";
import { ThatMark } from "./core/ThatMark";
import { TestCaseRecorder } from "./testUtil/TestCaseRecorder";
import { getCommandServerApi, getParseTreeApi } from "./util/getExtensionApi";
Expand All @@ -18,6 +17,7 @@ export async function activate(context: vscode.ExtensionContext) {
extensionContext: () => context,
commandServerApi: () => commandServerApi,
} as FactoryMap<Graph>);
graph.debug.init(getNodeAtLocation);
graph.snippets.init();
await graph.decorations.init();
graph.hatTokenMap.init();
Expand Down Expand Up @@ -95,12 +95,7 @@ export async function activate(context: vscode.ExtensionContext) {
}
}

context.subscriptions.push(
cursorlessRecordTestCaseDisposable,
vscode.window.onDidChangeTextEditorSelection(
logBranchTypes(getNodeAtLocation)
)
);
context.subscriptions.push(cursorlessRecordTestCaseDisposable);
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved

return {
thatMark,
Expand Down
4 changes: 0 additions & 4 deletions src/test/suite/recorded.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ import {
SerializedMarks,
} from "../../testUtil/toPlainObject";
import { getCursorlessApi } from "../../util/getExtensionApi";
import { enableDebugLog } from "../../util/debug";
import { extractTargetedMarks } from "../../testUtil/extractTargetedMarks";
import asyncSafety from "./asyncSafety";
import { ReadOnlyHatMap } from "../../core/IndividualHatMap";
import { mockPrePhraseGetVersion } from "../mockPrePhraseGetVersion";
import { openNewEditor } from "../openNewEditor";
import getRecordedTestPaths from "./getRecordedTestPaths";

Expand All @@ -39,8 +37,6 @@ suite("recorded test cases", async function () {
this.timeout("100s");
this.retries(5);

enableDebugLog(false);
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved

teardown(() => {
sinon.restore();
});
Expand Down
6 changes: 6 additions & 0 deletions src/typings/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Decorations from "../core/Decorations";
import FontMeasurements from "../core/FontMeasurements";
import { CommandServerApi } from "../util/getExtensionApi";
import { ReadOnlyHatMap } from "../core/IndividualHatMap";
import Debug from "../core/Debug";

/**
* A token within a text editor, including the current display line of the token
Expand Down Expand Up @@ -445,6 +446,11 @@ export interface Graph {
* API object for interacting with the command server, if it exists
*/
readonly commandServerApi: CommandServerApi | null;

/**
* Debug logger
*/
readonly debug: Debug;
}

export type NodeMatcherValue = {
Expand Down
53 changes: 0 additions & 53 deletions src/util/debug.ts

This file was deleted.

2 changes: 2 additions & 0 deletions src/util/graphFactories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Snippets } from "../core/Snippets";
import { RangeUpdater } from "../core/updateSelections/RangeUpdater";
import Decorations from "../core/Decorations";
import FontMeasurements from "../core/FontMeasurements";
import Debug from "../core/Debug";

type ConstructorMap<T> = {
[P in keyof T]: new (t: T) => T[P];
Expand All @@ -20,6 +21,7 @@ const graphConstructors: Partial<ConstructorMap<Graph>> = {
fontMeasurements: FontMeasurements,
snippets: Snippets,
rangeUpdater: RangeUpdater,
debug: Debug,
};

const graphFactories: Partial<FactoryMap<Graph>> = Object.fromEntries(
Expand Down