Skip to content

Conversation

@damien-schneider
Copy link

Added a command to batch apply all the suggested class changes.

* Initial plan

* Add applyAllCanonicalClasses command implementation

Co-authored-by: damien-schneider <74979845+damien-schneider@users.noreply.github.com>

* Format code with prettier

Co-authored-by: damien-schneider <74979845+damien-schneider@users.noreply.github.com>

* Address code review feedback: use edits.length and improve error handling

Co-authored-by: damien-schneider <74979845+damien-schneider@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: damien-schneider <74979845+damien-schneider@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings December 2, 2025 18:06
Copilot finished reviewing on behalf of damien-schneider December 2, 2025 18:09
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new VS Code command tailwindCSS.applyAllCanonicalClasses that allows users to batch apply all canonical class suggestions in the current document at once, streamlining the process of converting non-canonical Tailwind classes to their canonical forms.

  • Implements a new command that filters diagnostics with code 'suggestCanonicalClasses' and applies all suggested replacements
  • Includes proper error handling and user feedback via information/warning messages
  • Registers the command with appropriate enablement conditions

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
packages/vscode-tailwindcss/src/extension.ts Adds the applyAllCanonicalClasses function that retrieves canonical class diagnostics, creates text edits from their suggestions, and applies them via WorkspaceEdit; registers the command with try-catch error handling
packages/vscode-tailwindcss/package.json Registers the new tailwindCSS.applyAllCanonicalClasses command with title and enablement condition for when editor has focus and document is in file or remote scheme

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +241 to +248
// 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
})

Copy link

Copilot AI Dec 2, 2025

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.

Suggested change
// 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
})

Copilot uses AI. Check for mistakes.
Comment on lines +249 to +256
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))
}
}
Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When diagnostics don't have a suggestions property (or it's empty), those diagnostics will be silently skipped, potentially causing a mismatch between the number of diagnostics found and the number of suggestions applied. Consider tracking skipped diagnostics and warning the user if some suggestions couldn't be applied:

let skipped = 0
for (let diagnostic of canonicalDiagnostics) {
  let canonicalClass = (diagnostic as any).suggestions?.[0]
  if (canonicalClass) {
    edits.push(TextEdit.replace(diagnostic.range, canonicalClass))
  } else {
    skipped++
  }
}

if (skipped > 0) {
  Window.showWarningMessage(
    `Applied ${edits.length} suggestion${edits.length === 1 ? '' : 's'}, but ${skipped} could not be processed.`
  )
}

Copilot uses AI. Check for mistakes.
@thecrypticace
Copy link
Contributor

This should probably be implemented as a "fix all" code action and not a custom command.

See: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_codeAction

Look for SourceFixAll

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants