Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | 1x 6x 6x 1x 1x 5x 1x 6x 5x 5x 5x 5x 5x 4x 1x 1x 5x 5x 5x 5x 4x 1x 1x 1x 1x | import * as vscode from 'vscode';
import { getPrefixesWithErrorCode } from '@src/utilities/vscode/diagnostic';
export const cleanDocument = {
id: 'mentor.command.cleanDocument',
handler: async (documentUri?: vscode.Uri) => {
const targetUri = documentUri ?? vscode.window.activeTextEditor?.document.uri;
if (!targetUri) {
vscode.window.showErrorMessage('No document selected.');
return;
}
const document = vscode.workspace.textDocuments.find(doc => doc.uri.toString() === targetUri.toString())
?? await vscode.workspace.openTextDocument(targetUri);
const activeEditor = vscode.window.activeTextEditor?.document.uri.toString() === targetUri.toString()
? vscode.window.activeTextEditor
: undefined;
// Step 1: Remove unused prefixes (if any).
await removeUnusedPrefixes(document);
// Step 2: Sort the prefixes.
await vscode.commands.executeCommand('mentor.command.sortPrefixes', document.uri);
// Step 3: Format document (if a formatter exists).
await formatDocument(targetUri, activeEditor);
}
};
async function removeUnusedPrefixes(document: vscode.TextDocument): Promise<boolean> {
const documentDiagnostics = vscode.languages.getDiagnostics(document.uri);
const unusedPrefixes = getPrefixesWithErrorCode(document, documentDiagnostics, 'UnusedNamespacePrefixHint');
if (unusedPrefixes.length === 0) {
return false;
}
// Reuse existing command so behavior stays consistent.
await vscode.commands.executeCommand('mentor.command.deletePrefixes', document.uri, unusedPrefixes);
return true;
}
async function formatDocument(documentUri: vscode.Uri, editor?: vscode.TextEditor): Promise<boolean> {
const tabSize = typeof editor?.options.tabSize === 'number' ? editor.options.tabSize : 2;
const insertSpaces = typeof editor?.options.insertSpaces === 'boolean' ? editor.options.insertSpaces : true;
const edits = await vscode.commands.executeCommand<vscode.TextEdit[]>(
'vscode.executeFormatDocumentProvider',
documentUri,
{ tabSize, insertSpaces }
);
if (!edits || edits.length === 0) {
return false;
}
const edit = new vscode.WorkspaceEdit();
edit.set(documentUri, edits);
await vscode.workspace.applyEdit(edit);
return true;
} |