-
-
Notifications
You must be signed in to change notification settings - Fork 79
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
Changes from 18 commits
8302929
f84cd1a
08616c3
17cb4af
d1f335e
769c7bf
be01fd2
9f3685c
4caadf4
81dbe6e
1ab83e2
9bf3f16
eb5dc7b
98e66eb
8a41f5e
8fa4b21
5d1033a
989682e
be38fe3
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,128 @@ | ||
import { | ||
Disposable, | ||
ExtensionMode, | ||
Location, | ||
TextEditorSelectionChangeEvent, | ||
window, | ||
workspace, | ||
} from "vscode"; | ||
import { SyntaxNode, TreeCursor } from "web-tree-sitter"; | ||
import { Graph } from "../typings/Types"; | ||
|
||
export default class Debug { | ||
private disposableConfiguration?: Disposable; | ||
private disposableSelection?: Disposable; | ||
active: boolean; | ||
|
||
constructor(private graph: Graph) { | ||
this.graph.extensionContext.subscriptions.push(this); | ||
|
||
this.evaluateSetting = this.evaluateSetting.bind(this); | ||
this.logBranchTypes = this.logBranchTypes.bind(this); | ||
this.active = true; | ||
|
||
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(); | ||
this.disposableConfiguration = workspace.onDidChangeConfiguration( | ||
this.evaluateSetting | ||
); | ||
break; | ||
} | ||
} | ||
|
||
init() {} | ||
|
||
log(...args: any[]) { | ||
if (this.active) { | ||
console.debug(...args); | ||
} | ||
} | ||
|
||
dispose() { | ||
if (this.disposableConfiguration) { | ||
this.disposableConfiguration.dispose(); | ||
} | ||
if (this.disposableSelection) { | ||
this.disposableSelection.dispose(); | ||
} | ||
} | ||
|
||
private enableDebugLog() { | ||
this.active = true; | ||
this.disposableSelection = window.onDidChangeTextEditorSelection( | ||
this.logBranchTypes | ||
); | ||
} | ||
|
||
private disableDebugLog() { | ||
this.active = false; | ||
if (this.disposableSelection) { | ||
this.disposableSelection.dispose(); | ||
} | ||
} | ||
|
||
private evaluateSetting() { | ||
const debugEnabled = workspace | ||
.getConfiguration("cursorless") | ||
.get<boolean>("debug")!; | ||
if (debugEnabled) { | ||
this.enableDebugLog(); | ||
} else { | ||
this.disableDebugLog(); | ||
} | ||
} | ||
|
||
private logBranchTypes(event: TextEditorSelectionChangeEvent) { | ||
const location = new Location( | ||
window.activeTextEditor!.document.uri, | ||
event.selections[0] | ||
); | ||
|
||
let node: SyntaxNode; | ||
try { | ||
node = this.graph.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.printCursorLocationInfo(cursor, 0); | ||
|
||
for (let i = 1; i < ancestors.length; ++i) { | ||
cursor.gotoFirstChild(); | ||
while (cursor.currentNode().id !== ancestors[i].id) { | ||
if (!cursor.gotoNextSibling()) { | ||
return; | ||
} | ||
} | ||
this.printCursorLocationInfo(cursor, i); | ||
} | ||
|
||
const leafText = ancestors[ancestors.length - 1].text | ||
.replace(/\s+/g, " ") | ||
.substring(0, 100); | ||
console.debug(">".repeat(ancestors.length), `"${leafText}"`); | ||
} | ||
|
||
private printCursorLocationInfo(cursor: TreeCursor, depth: number) { | ||
const field = cursor.currentFieldName(); | ||
const fieldText = field != null ? `${field}: ` : ""; | ||
console.debug(">".repeat(depth + 1), `${fieldText}${cursor.nodeType}`); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,6 @@ import { | |
import { ThatMark } from "../ThatMark"; | ||
import { TestCaseRecorder } from "../../testUtil/TestCaseRecorder"; | ||
import { canonicalizeAndValidateCommand } from "../../util/canonicalizeAndValidateCommand"; | ||
import { SyntaxNode } from "web-tree-sitter"; | ||
import { CommandArgument } from "./types"; | ||
import { isString } from "../../util/type"; | ||
|
||
|
@@ -22,7 +21,6 @@ export default class CommandRunner { | |
private graph: Graph, | ||
private thatMark: ThatMark, | ||
private sourceMark: ThatMark, | ||
private getNodeAtLocation: (location: vscode.Location) => SyntaxNode, | ||
private testCaseRecorder: TestCaseRecorder | ||
) { | ||
graph.extensionContext.subscriptions.push(this); | ||
|
@@ -40,8 +38,10 @@ export default class CommandRunner { | |
|
||
async runCommand(commandArgument: CommandArgument) { | ||
try { | ||
console.debug(`commandArgument:`); | ||
console.debug(JSON.stringify(commandArgument, null, 3)); | ||
if (this.graph.debug.active) { | ||
this.graph.debug.log(`commandArgument:`); | ||
this.graph.debug.log(JSON.stringify(commandArgument, null, 3)); | ||
} | ||
|
||
const { | ||
spokenForm, | ||
|
@@ -76,7 +76,7 @@ export default class CommandRunner { | |
hatTokenMap: readableHatMap, | ||
thatMark: this.thatMark.exists() ? this.thatMark.get() : [], | ||
sourceMark: this.sourceMark.exists() ? this.sourceMark.get() : [], | ||
getNodeAtLocation: this.getNodeAtLocation, | ||
getNodeAtLocation: this.graph.getNodeAtLocation, | ||
}; | ||
|
||
const selections = processTargets(processedTargetsContext, targets); | ||
|
@@ -110,8 +110,8 @@ export default class CommandRunner { | |
this.testCaseRecorder.commandErrorHook(); | ||
const err = e as Error; | ||
vscode.window.showErrorMessage(err.message); | ||
console.debug(err.message); | ||
console.debug(err.stack); | ||
this.graph.debug.log(err.message); | ||
this.graph.debug.log(err.stack); | ||
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'm tempted to make these just 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 was also thinking that. |
||
throw err; | ||
} | ||
} | ||
|
This file was deleted.
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.
do we need to set this to null? or is it idempotent?
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.
You shouldn't be able to end up in disable twice without having enabled in between. I tried toggling it back and forward without any problem.
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.
no but the issue is that if the extension gets deactivated it will get called twice. probably not a big deal, but just being fastidious
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.
eg if you enable log, then disable it, then deactivate extension