-
-
Notifications
You must be signed in to change notification settings - Fork 233
Add command to apply all canonical class suggestions at once (#2) #1508
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -14,6 +14,9 @@ import { | |
| SymbolInformation, | ||
| Position, | ||
| Range, | ||
| languages, | ||
| WorkspaceEdit, | ||
| TextEdit, | ||
| } from 'vscode' | ||
| import type { | ||
| DocumentFilter, | ||
|
|
@@ -205,6 +208,79 @@ export async function activate(context: ExtensionContext) { | |
| }), | ||
| ) | ||
|
|
||
| async function applyAllCanonicalClasses(): Promise<void> { | ||
| if (!Window.activeTextEditor) { | ||
| return | ||
| } | ||
|
|
||
| let { document } = Window.activeTextEditor | ||
| let folder = Workspace.getWorkspaceFolder(document.uri) | ||
|
|
||
| if (!folder || isExcluded(document.uri.fsPath, folder)) { | ||
| return | ||
| } | ||
|
|
||
| // Get all diagnostics for the current document | ||
| let allDiagnostics = languages.getDiagnostics(document.uri) | ||
|
|
||
| // Filter for SuggestCanonicalClasses diagnostics | ||
| // The diagnostic code will be 'suggestCanonicalClasses' based on DiagnosticKind enum | ||
| let canonicalDiagnostics = allDiagnostics.filter( | ||
| (diagnostic) => diagnostic.code === 'suggestCanonicalClasses', | ||
| ) | ||
|
|
||
| if (canonicalDiagnostics.length === 0) { | ||
| Window.showInformationMessage('No canonical class suggestions found in this document.') | ||
| return | ||
| } | ||
|
|
||
| // Create a workspace edit to apply all suggestions | ||
| let workspaceEdit = new WorkspaceEdit() | ||
| let edits: TextEdit[] = [] | ||
|
|
||
| // Sort diagnostics by position (from end to start) to avoid range invalidation | ||
| canonicalDiagnostics.sort((a, b) => { | ||
| if (a.range.start.line !== b.range.start.line) { | ||
| return b.range.start.line - a.range.start.line | ||
| } | ||
| return b.range.start.character - a.range.start.character | ||
| }) | ||
|
|
||
| for (let diagnostic of canonicalDiagnostics) { | ||
| // Access the canonical class suggestion directly from the suggestions property | ||
| // Type assertion needed since languages.getDiagnostics returns vscode.Diagnostic | ||
| let canonicalClass = (diagnostic as any).suggestions?.[0] | ||
| if (canonicalClass) { | ||
| edits.push(TextEdit.replace(diagnostic.range, canonicalClass)) | ||
| } | ||
| } | ||
|
Comment on lines
+249
to
+256
|
||
|
|
||
| workspaceEdit.set(document.uri, edits) | ||
|
|
||
| // Apply the workspace edit | ||
| let success = await Workspace.applyEdit(workspaceEdit) | ||
|
|
||
| if (success) { | ||
| let count = edits.length | ||
| Window.showInformationMessage( | ||
| `Applied ${count} canonical class suggestion${count === 1 ? '' : 's'}.`, | ||
| ) | ||
| } else { | ||
| Window.showWarningMessage('Failed to apply canonical class suggestions.') | ||
| } | ||
| } | ||
|
|
||
| context.subscriptions.push( | ||
| commands.registerCommand('tailwindCSS.applyAllCanonicalClasses', async () => { | ||
| try { | ||
| await applyAllCanonicalClasses() | ||
| } catch (error) { | ||
| let message = error instanceof Error ? error.message : 'Unknown error' | ||
| Window.showWarningMessage(`Couldn't apply canonical class suggestions: ${message}`) | ||
| } | ||
| }), | ||
| ) | ||
|
|
||
| context.subscriptions.push( | ||
| Window.onDidChangeActiveTextEditor(async () => { | ||
| await updateActiveTextEditorContext() | ||
|
|
||
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.
The sorting logic here is redundant when using
WorkspaceEdit.set(). VS Code automatically handles the application order of multiple text edits to prevent range invalidation, so sorting from end to start is unnecessary. Consider removing this sorting or adding a comment explaining why it's needed if there's a specific reason.