-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Show Parsed Tokens as Warnings toggle and enable client logging (#40
- Loading branch information
1 parent
7e4a2d1
commit cb318c3
Showing
4 changed files
with
195 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import * as vscode from "vscode"; | ||
import { log } from "./util"; | ||
|
||
export class Config { | ||
readonly extensionId = "fuellabs.sway-vscode-plugin"; | ||
readonly rootSection = "sway-lsp"; | ||
private readonly requiresReloadOpts = ["debug"].map( | ||
(opt) => `${this.rootSection}.${opt}` | ||
); | ||
|
||
readonly package: { | ||
version: string; | ||
} = vscode.extensions.getExtension(this.extensionId)!.packageJSON; | ||
|
||
readonly globalStorageUri: vscode.Uri; | ||
|
||
constructor(ctx: vscode.ExtensionContext) { | ||
this.globalStorageUri = ctx.globalStorageUri; | ||
vscode.workspace.onDidChangeConfiguration( | ||
this.onDidChangeConfiguration, | ||
this, | ||
ctx.subscriptions | ||
); | ||
this.refreshLogging(); | ||
} | ||
|
||
private refreshLogging() { | ||
log.setEnabled(this.traceExtension); | ||
log.info("Starting the Sway Language Client and Server"); | ||
log.info("Extension version:", this.package.version); | ||
|
||
const cfg = Object.entries(this.cfg).filter( | ||
([_, val]) => !(val instanceof Function) | ||
); | ||
log.info("Using configuration", Object.fromEntries(cfg)); | ||
} | ||
|
||
private async onDidChangeConfiguration( | ||
event: vscode.ConfigurationChangeEvent | ||
) { | ||
this.refreshLogging(); | ||
|
||
const requiresReloadOpt = this.requiresReloadOpts.find((opt) => | ||
event.affectsConfiguration(opt) | ||
); | ||
|
||
if (!requiresReloadOpt) return; | ||
|
||
const userResponse = await vscode.window.showInformationMessage( | ||
`Changing "${requiresReloadOpt}" requires a reload`, | ||
"Reload now" | ||
); | ||
|
||
if (userResponse === "Reload now") { | ||
await vscode.commands.executeCommand("workbench.action.reloadWindow"); | ||
} | ||
} | ||
|
||
// We don't do runtime config validation here for simplicity. More on stackoverflow: | ||
// https://stackoverflow.com/questions/60135780/what-is-the-best-way-to-type-check-the-configuration-for-vscode-extension | ||
|
||
private get cfg(): vscode.WorkspaceConfiguration { | ||
return vscode.workspace.getConfiguration(this.rootSection); | ||
} | ||
|
||
/** | ||
* Beware that postfix `!` operator erases both `null` and `undefined`. | ||
* This is why the following doesn't work as expected: | ||
* | ||
* ```ts | ||
* const nullableNum = vscode | ||
* .workspace | ||
* .getConfiguration | ||
* .getConfiguration("sway-lsp") | ||
* .get<number | null>(path)!; | ||
* | ||
* // What happens is that type of `nullableNum` is `number` but not `null | number`: | ||
* const fullFledgedNum: number = nullableNum; | ||
* ``` | ||
* So this getter handles this quirk by not requiring the caller to use postfix `!` | ||
*/ | ||
private get<T>(path: string): T { | ||
return this.cfg.get<T>(path)!; | ||
} | ||
|
||
get traceExtension() { | ||
return this.get<boolean>("trace.extension"); | ||
} | ||
|
||
get debug() { | ||
return { | ||
showParsedTokensAsWarnings: this.get<boolean>( | ||
"debug.showParsedTokensAsWarnings" | ||
), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as vscode from "vscode"; | ||
import { inspect } from "util"; | ||
|
||
export const log = new (class { | ||
private enabled = true; | ||
private readonly output = | ||
vscode.window.createOutputChannel("Sway LSP Client"); | ||
|
||
setEnabled(yes: boolean): void { | ||
log.enabled = yes; | ||
} | ||
|
||
// Hint: the type [T, ...T[]] means a non-empty array | ||
debug(...msg: [unknown, ...unknown[]]): void { | ||
if (!log.enabled) return; | ||
log.write("DEBUG", ...msg); | ||
} | ||
|
||
info(...msg: [unknown, ...unknown[]]): void { | ||
log.write("INFO", ...msg); | ||
} | ||
|
||
warn(...msg: [unknown, ...unknown[]]): void { | ||
debugger; | ||
log.write("WARN", ...msg); | ||
} | ||
|
||
error(...msg: [unknown, ...unknown[]]): void { | ||
debugger; | ||
log.write("ERROR", ...msg); | ||
log.output.show(true); | ||
} | ||
|
||
private write(label: string, ...messageParts: unknown[]): void { | ||
const message = messageParts.map(log.stringify).join(" "); | ||
const dateTime = new Date().toLocaleString(); | ||
log.output.appendLine(`${label} [${dateTime}]: ${message}`); | ||
} | ||
|
||
private stringify(val: unknown): string { | ||
if (typeof val === "string") return val; | ||
return inspect(val, { | ||
colors: false, | ||
depth: 6, // heuristic | ||
}); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters