Skip to content

Commit 902c48e

Browse files
Merge pull request #30 from windicss/feat-save_sort
feat: sort on save
2 parents 86207e2 + 9fe39b0 commit 902c48e

3 files changed

Lines changed: 20 additions & 11 deletions

File tree

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@
165165
"type": "string",
166166
"default": "#AED0A4",
167167
"description": "Placeholder Color"
168+
},
169+
"windicss.runOnSave": {
170+
"type": "boolean",
171+
"default": true,
172+
"description": "A flag that controls whether or not Windi CSS classes will be sorted on save on save."
168173
}
169174
}
170175
}

src/extension.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export async function activate(ctx: ExtensionContext) {
3232
registerCompletions(ctx, CORE);
3333
registerCodeFolding(ctx);
3434
registerCommands(ctx, CORE);
35+
3536
console.log('Windi CSS Intellisense is now active!');
3637
}
3738

src/lib/commands.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { HTMLParser } from '../utils/parser';
33
import { writeFileSync } from 'fs';
44
import { dirname, join } from 'path';
55
import { StyleSheet } from 'windicss/utils/style';
6+
import { workspace } from 'vscode';
67
import type { ExtensionContext } from 'vscode';
78
import type { Core } from '../interfaces';
8-
import { sortClassNames, toggleConfig } from '../utils';
9+
import { getConfig, sortClassNames, toggleConfig } from '../utils';
910

1011
export function registerCommands(ctx: ExtensionContext, core: Core): void {
1112
ctx.subscriptions.push(
@@ -49,25 +50,27 @@ export function registerCommands(ctx: ExtensionContext, core: Core): void {
4950
commands.registerTextEditorCommand('windicss.sort', (textEditor, textEdit) => {
5051
const text = textEditor.document.getText();
5152
const parser = new HTMLParser(text);
52-
const outputHTML: string[] = [];
53-
54-
let indexStart = 0;
5553

5654
const classes = parser.parseClasses();
5755
const variants = Object.keys(core.processor?.resolveVariants() ?? {});
5856
const variantsMap = Object.assign({}, ...variants.map((value, index) => ({ [value]: index + 1 })));
5957

6058
for (const p of classes) {
61-
outputHTML.push(text.substring(indexStart, p.start));
62-
outputHTML.push(sortClassNames(p.result, variantsMap));
63-
indexStart = p.end;
59+
const sortedP = sortClassNames(p.result, variantsMap);
60+
textEdit.replace(new Range(textEditor.document.positionAt(p.start), textEditor.document.positionAt(p.end)), sortedP);
6461
}
65-
outputHTML.push(text.substring(indexStart));
66-
67-
textEdit.replace(new Range(new Position(0, 0), textEditor.document.lineAt(textEditor.document.lineCount-1).range.end), outputHTML.join(''));
6862
})
6963
);
7064

65+
// if runOnSave is enabled in settings, trigger command on file save
66+
if(getConfig('windicss.runOnSave')) {
67+
ctx.subscriptions.push(
68+
workspace.onWillSaveTextDocument((_e) => {
69+
commands.executeCommand('windicss.sort');
70+
})
71+
);
72+
}
73+
7174
ctx.subscriptions.push(
7275
commands.registerCommand('windicss.toggle-folding', () => toggleConfig('windicss.enableCodeFolding'))
7376
);
@@ -99,4 +102,4 @@ export function registerCommands(ctx: ExtensionContext, core: Core): void {
99102
commands.executeCommand('workbench.action.reloadWindow');
100103
})
101104
);
102-
}
105+
}

0 commit comments

Comments
 (0)