-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 user facing command Blade: Format Document
✅ Closes: #60
- Loading branch information
Showing
5 changed files
with
162 additions
and
1 deletion.
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
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,40 @@ | ||
import vscode, { commands, TextEditor, TextEditorEdit, window, WorkspaceConfiguration } from "vscode"; | ||
import { readRuntimeConfig } from './runtimeConfig'; | ||
import { getCoreNodeModule } from "./util"; | ||
import { Formatter } from "blade-formatter"; | ||
import { Logger } from "./logger"; | ||
|
||
const vsctmModule = getCoreNodeModule("vscode-textmate"); | ||
const onigurumaModule = getCoreNodeModule("vscode-oniguruma"); | ||
const { Range, Position } = vscode; | ||
|
||
export const formatFromCommand = | ||
async function (editor: TextEditor, edit: TextEditorEdit) { | ||
try { | ||
const extConfig = vscode.workspace.getConfiguration( | ||
"bladeFormatter.format" | ||
); | ||
const runtimeConfig = readRuntimeConfig(editor.document.uri.fsPath); | ||
const options = { | ||
vsctm: vsctmModule, | ||
oniguruma: onigurumaModule, | ||
indentSize: extConfig.indentSize, | ||
wrapLineLength: extConfig.wrapLineLength, | ||
wrapAttributes: extConfig.wrapAttributes, | ||
useTabs: extConfig.useTabs, | ||
sortTailwindcssClasses: extConfig.sortTailwindcssClasses, | ||
...runtimeConfig, | ||
}; | ||
const originalText = editor.document.getText(); | ||
const lastLine = editor.document.lineAt(editor.document.lineCount - 1); | ||
const range = new Range(new Position(0, 0), lastLine.range.end); | ||
const formatted = await new Formatter(options).formatContent(originalText); | ||
|
||
await editor.edit((editBuilder) => { | ||
editBuilder.replace(range, formatted); | ||
}); | ||
} catch (e) { | ||
const logger = new Logger(); | ||
logger.logError("Error formatting document", e); | ||
} | ||
} |
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
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,108 @@ | ||
import { window } from "vscode"; | ||
|
||
type LogLevel = "DEBUG" | "INFO" | "WARN" | "ERROR" | "NONE"; | ||
|
||
export class Logger { | ||
private outputChannel = window.createOutputChannel("BladeFormatter"); | ||
|
||
private logLevel: LogLevel = "INFO"; | ||
|
||
public setOutputLevel(logLevel: LogLevel) { | ||
this.logLevel = logLevel; | ||
} | ||
|
||
/** | ||
* Append messages to the output channel and format it with a title | ||
* | ||
* @param message The message to append to the output channel | ||
*/ | ||
public logDebug(message: string, data?: unknown): void { | ||
if ( | ||
this.logLevel === "NONE" || | ||
this.logLevel === "INFO" || | ||
this.logLevel === "WARN" || | ||
this.logLevel === "ERROR" | ||
) { | ||
return; | ||
} | ||
this.logMessage(message, "DEBUG"); | ||
if (data) { | ||
this.logObject(data); | ||
} | ||
} | ||
|
||
/** | ||
* Append messages to the output channel and format it with a title | ||
* | ||
* @param message The message to append to the output channel | ||
*/ | ||
public logInfo(message: string, data?: unknown): void { | ||
if ( | ||
this.logLevel === "NONE" || | ||
this.logLevel === "WARN" || | ||
this.logLevel === "ERROR" | ||
) { | ||
return; | ||
} | ||
this.logMessage(message, "INFO"); | ||
if (data) { | ||
this.logObject(data); | ||
} | ||
} | ||
|
||
/** | ||
* Append messages to the output channel and format it with a title | ||
* | ||
* @param message The message to append to the output channel | ||
*/ | ||
public logWarning(message: string, data?: unknown): void { | ||
if (this.logLevel === "NONE" || this.logLevel === "ERROR") { | ||
return; | ||
} | ||
this.logMessage(message, "WARN"); | ||
if (data) { | ||
this.logObject(data); | ||
} | ||
} | ||
|
||
public logError(message: string, error?: unknown) { | ||
if (this.logLevel === "NONE") { | ||
return; | ||
} | ||
this.logMessage(message, "ERROR"); | ||
if (typeof error === "string") { | ||
// Errors as a string usually only happen with | ||
// plugins that don't return the expected error. | ||
this.outputChannel.appendLine(error); | ||
} else if (error instanceof Error) { | ||
if (error?.message) { | ||
this.logMessage(error.message, "ERROR"); | ||
} | ||
if (error?.stack) { | ||
this.outputChannel.appendLine(error.stack); | ||
} | ||
} else if (error) { | ||
this.logObject(error); | ||
} | ||
} | ||
|
||
public show() { | ||
this.outputChannel.show(); | ||
} | ||
|
||
private logObject(data: unknown): void { | ||
const message = JSON.stringify(data, null, 2); // dont use prettier to keep it simple | ||
|
||
this.outputChannel.appendLine(message); | ||
} | ||
|
||
/** | ||
* Append messages to the output channel and format it with a title | ||
* | ||
* @param message The message to append to the output channel | ||
*/ | ||
private logMessage(message: string, logLevel: LogLevel): void { | ||
const title = new Date().toLocaleTimeString(); | ||
this.outputChannel.appendLine(`["${logLevel}" - ${title}] ${message}`); | ||
} | ||
} |