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 4 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 @@ -109,6 +109,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
11 changes: 4 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ 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 { initDebug, debugSetNodeAtLocation } from "./util/debug";
import { ThatMark } from "./core/ThatMark";
import { TestCaseRecorder } from "./testUtil/TestCaseRecorder";
import { getCommandServerApi, getParseTreeApi } from "./util/getExtensionApi";
import isTesting from "./testUtil/isTesting";
import CommandRunner from "./core/commandRunner/CommandRunner";

export async function activate(context: vscode.ExtensionContext) {
initDebug(context);
const { getNodeAtLocation } = await getParseTreeApi();
const commandServerApi = await getCommandServerApi();
debugSetNodeAtLocation(getNodeAtLocation);

const graph = makeGraph({
...graphFactories,
Expand Down Expand Up @@ -95,12 +97,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
84 changes: 68 additions & 16 deletions src/util/debug.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,73 @@
import * as vscode from "vscode";
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
import {
Disposable,
ExtensionContext,
ExtensionMode,
Location,
TextEditorSelectionChangeEvent,
window,
workspace,
} from "vscode";
import { SyntaxNode, TreeCursor } from "web-tree-sitter";

export function logBranchTypes(getNodeAtLocation: any) {
return (event: vscode.TextEditorSelectionChangeEvent) => {
const location = new vscode.Location(
vscode.window.activeTextEditor!.document.uri,
const originalDebugLog = console.debug;
let _getNodeAtLocation: any;
let disposable: Disposable;

export function initDebug(context: ExtensionContext) {
switch (context.extensionMode) {
// Development mode. Always enable.
case ExtensionMode.Development:
enableDebugLog();
break;
// Test mode. Always disable.
case ExtensionMode.Test:
disableDebugLog();
break;
// Production mode. Enable based on user setting.
case ExtensionMode.Production:
evaluateSetting();
workspace.onDidChangeConfiguration(evaluateSetting);
break;
}
}

export function debugSetNodeAtLocation(getNodeAtLocation: any) {
_getNodeAtLocation = getNodeAtLocation;
}

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

function disableDebugLog() {
console.debug = () => {};
if (disposable) {
disposable.dispose();
}
}

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

function logBranchTypes() {
return (event: TextEditorSelectionChangeEvent) => {
const location = new Location(
window.activeTextEditor!.document.uri,
event.selections[0]
);

let node: SyntaxNode;
try {
node = getNodeAtLocation(location);
node = _getNodeAtLocation(location);
} catch (error) {
return;
}
Expand All @@ -21,12 +78,6 @@ export function logBranchTypes(getNodeAtLocation: any) {
node = node.parent;
}

const print = (cursor: TreeCursor, depth: number) => {
const field = cursor.currentFieldName();
const fieldText = field != null ? `${field}: ` : "";
console.debug(">".repeat(depth + 1), `${fieldText}${cursor.nodeType}`);
};

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

Expand All @@ -47,7 +98,8 @@ export function logBranchTypes(getNodeAtLocation: any) {
};
}

const originalDebugLog = console.debug;
export function enableDebugLog(enable: boolean) {
console.debug = enable ? originalDebugLog : () => {};
}
const print = (cursor: TreeCursor, depth: number) => {
const field = cursor.currentFieldName();
const fieldText = field != null ? `${field}: ` : "";
console.debug(">".repeat(depth + 1), `${fieldText}${cursor.nodeType}`);
};