Skip to content

Add "Sort Selection" command #851

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

Merged
merged 13 commits into from
Aug 31, 2023
Prev Previous commit
Next Next commit
wip
  • Loading branch information
bradlc committed Oct 21, 2022
commit f23b3b18663aada99a7dd68f88d16baa87f3bcc9
28 changes: 16 additions & 12 deletions packages/vscode-tailwindcss/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,29 +156,33 @@ export async function activate(context: ExtensionContext) {
throw Error(`No active Tailwind project found for file ${document.uri.fsPath}`)
}
let ranges = selections.map((selection) => new Range(selection.start, selection.end))
let { error, result } = await client.sendRequest('@/tailwindCSS/sortSelection', {
uri: uri.toString(),
classLists: ranges.map((range) => document.getText(range)),
})
let result = await client.sendRequest<{ error: string } | { classLists: string[] }>(
'@/tailwindCSS/sortSelection',
{
uri: uri.toString(),
classLists: ranges.map((range) => document.getText(range)),
}
)
if (
Window.activeTextEditor.document.uri.toString() !== uri.toString() ||
JSON.stringify(Window.activeTextEditor.selections) !== selectionsJson
) {
return
}
if (error) {
if ('error' in result) {
throw Error(
{
'no-project': `No active Tailwind project found for file ${document.uri.fsPath}`,
unknown: 'An unknown error occurred.',
}[error]
}[result.error] ?? 'An unknown error occurred.'
)
} else {
let sortedClassLists = result.classLists
Window.activeTextEditor.edit((builder) => {
for (let i = 0; i < ranges.length; i++) {
builder.replace(ranges[i], sortedClassLists[i])
}
})
}
Window.activeTextEditor.edit((builder) => {
for (let i = 0; i < ranges.length; i++) {
builder.replace(ranges[i], result[i])
}
})
}

context.subscriptions.push(
Expand Down