Skip to content

Commit

Permalink
Merge pull request #564 from shufo/fix/slow-class-sort-on-windows
Browse files Browse the repository at this point in the history
  • Loading branch information
shufo authored Sep 21, 2022
2 parents 06d4b15 + 8138f96 commit 42b6dab
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
},
"dependencies": {
"ajv": "^8.11.0",
"blade-formatter": "^1.29.4",
"blade-formatter": "^1.29.6",
"find-config": "^1.0.0",
"ignore": "^5.1.8",
"tailwindcss": "^3.1.8",
Expand Down
13 changes: 8 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import ignore from "ignore";
import { Formatter } from "blade-formatter";
import { setExtensionContext } from './extensionContext';
import { telemetry, TelemetryEventNames } from './telemetry';
import { findConfigFile, readRuntimeConfig } from './runtimeConfig';
import { readRuntimeConfig } from './runtimeConfig';
import { ExtensionConstants } from "./constants";
import { messages } from "./messages";
import { formatFromCommand } from "./commands";
import { getCoreNodeModule, requireUncached } from "./util";
import { resolveTailwindConfig } from "./tailwind";
import { resolveTailwindConfig, TailwindConfig } from "./tailwind";

const { Range, Position } = vscode;
const vsctmModule = getCoreNodeModule("vscode-textmate");
Expand Down Expand Up @@ -73,16 +73,18 @@ export function activate(context: ExtensionContext) {

const runtimeConfig = readRuntimeConfig(document.uri.fsPath);

if (runtimeConfig?.sortTailwindcssClasses) {
const tailwindConfig: TailwindConfig = {};

if (runtimeConfig?.sortTailwindcssClasses || extConfig.sortTailwindcssClasses) {
const tailwindConfigPath = resolveTailwindConfig(document.uri.fsPath, runtimeConfig?.tailwindcssConfigPath ?? '');
runtimeConfig.tailwindcssConfigPath = tailwindConfigPath;
tailwindConfig.tailwindcssConfigPath = tailwindConfigPath;

try {
requireUncached(tailwindConfigPath);
} catch (error) {
// @ts-ignore
// fallback to default config
runtimeConfig.tailwindcssConfigPath = __non_webpack_require__.resolve('tailwindcss/lib/public/default-config');
tailwindConfig.tailwindcssConfigPath = __non_webpack_require__.resolve('tailwindcss/lib/public/default-config');
}
}

Expand All @@ -97,6 +99,7 @@ export function activate(context: ExtensionContext) {
sortHtmlAttributes: extConfig.sortHtmlAttributes ?? 'none',
noMultipleEmptyLines: extConfig.noMultipleEmptyLines,
...runtimeConfig, // override all settings by runtime config
...tailwindConfig,
};

const progressMessage = isLargeFile(document) ? messages.largeFileFormattingMessage : messages.formattingMessage;
Expand Down
5 changes: 5 additions & 0 deletions src/tailwind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { requireUncached } from './util';

const __config__ = 'tailwind.config.js';

export type TailwindConfig = {
tailwindcssConfigPath?: string,
tailwindcssConfig?: object,
};

/**
* Resolve tailwind config path if resolvable
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff </h1>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
<div class="xxxl:col-end-8 xxl:col-end-10 col-start-2 col-end-11 md:col-end-12 xl:col-end-10">
<h1 class="text-white">Random Stuff </h1>
</div>
6 changes: 6 additions & 0 deletions src/test/fixtures/tailwind/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"[blade]": {
"editor.defaultFormatter": "shufo.vscode-blade-formatter",
},
"bladeFormatter.format.sortTailwindcssClasses": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="col-start-2 col-end-11 md:col-end-12 xl:col-end-10 xxxl:col-end-8">
<h1 class="text-white">Random Stuff</h1>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="md:col-end-12 xl:col-end-10 col-start-2 col-end-11 xxxl:col-end-8 ">
<h1 class="text-white">Random Stuff</h1>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
content: ["*.blade.php"],
theme: {
screens: {
sm: '0',
md: '834px',
lg: '1024px',
xl: '1200px',
xxl: '1440px',
xxxl: '1900px',
},
},
};
8 changes: 7 additions & 1 deletion src/test/fixtures/test.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
"folders": [
{
"path": "project"
},
{
"path": "tailwind"
}
]
],
"settings": {
"bladeFormatter.format.sortTailwindcssClasses": false
}
}
27 changes: 26 additions & 1 deletion src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import vscode, { TextDocument } from "vscode";
import { before } from "mocha";
import { ExtensionConstants } from "../../constants";
import { formatSameAsBladeFormatter, getContent, getDoc } from "../support/util";
// const myExtension = require('../extension');
import { performance } from 'perf_hooks';

suite("Extension Test Suite", () => {
vscode.window.showInformationMessage("Start all tests.");
Expand Down Expand Up @@ -85,6 +85,18 @@ suite("Extension Test Suite", () => {
);
});

test("Should format file with runtime config / tailwindcssConfigPath (large file)", async function (this: any) {
this.timeout(20000);

const startTime = performance.now();
await formatSameAsBladeFormatter(
"withConfig/tailwindConfigPath/large_file.blade.php",
"withConfig/tailwindConfigPath/formatted.large_file.blade.php"
);
const endTime = performance.now();
assert.strictEqual((endTime - startTime) < 3000, true);
});

test("Should format file with runtime config / tailwindcssConfigPath (config does not exists error) ", async function (this: any) {
this.timeout(20000);
await formatSameAsBladeFormatter(
Expand Down Expand Up @@ -117,6 +129,19 @@ suite("Extension Test Suite", () => {
);
});

test("Should format file without runtime config / tailwind config exists", async function (this: any) {
this.timeout(20000);

const config = vscode.workspace.getConfiguration('bladeFormatter.format');
config.update('sortTailwindcssClasses', true);
await formatSameAsBladeFormatter(
"tailwindSortWithoutRuntimeConfig/index.blade.php",
"tailwindSortWithoutRuntimeConfig/formatted.index.blade.php",
{ workspace: "tailwind" }
);
config.update('sortTailwindcssClasses', false);
});

test("Should format file with runtime config / sortHtmlAttributes", async function (this: any) {
this.timeout(20000);
await formatSameAsBladeFormatter(
Expand Down
4 changes: 2 additions & 2 deletions src/test/support/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export async function formatSameAsBladeFormatter(
formattedFile: any,
options: any = {}
) {
const { actual, source } = await format("project", file);
const formatted = await getContent("project", formattedFile);
const { actual, source } = await format(options.workspace ?? "project", file);
const formatted = await getContent(options.workspace ?? "project", formattedFile);
assert.equal(actual, formatted);
}

Expand Down
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1349,10 +1349,10 @@
mem "^8.0.0"
php-parser "3.1.0"

"@shufo/tailwindcss-class-sorter@^1.0.10":
version "1.0.10"
resolved "https://registry.yarnpkg.com/@shufo/tailwindcss-class-sorter/-/tailwindcss-class-sorter-1.0.10.tgz#515e2b68d9d9e0f57671c596d1d9b3f5ddec6c33"
integrity sha512-X/GFG71d0vqlfHYbJkbAS1dCbSA01H7RqHT/8IELJE+uXsIfsEFlvEogeBmMsO1R8Rx2zA224+VWC6eFfCrpdw==
"@shufo/tailwindcss-class-sorter@^1.0.12":
version "1.0.12"
resolved "https://registry.yarnpkg.com/@shufo/tailwindcss-class-sorter/-/tailwindcss-class-sorter-1.0.12.tgz#21ed70ef157ad23ae69f8ae462812e7b4d4e9cda"
integrity sha512-dGyIdWGAPG7sMbZb/Q/RgR60DpyxZ3772mQCy1hb72ILBFYcqpOzjGt7tsR5HrV8apo4IV1j/Ako17bkbnQewA==
dependencies:
escalade "^3.1.1"
object-hash "^3.0.0"
Expand Down Expand Up @@ -2185,13 +2185,13 @@ binary@~0.3.0:
buffers "~0.1.1"
chainsaw "~0.1.0"

blade-formatter@^1.29.4:
version "1.29.4"
resolved "https://registry.yarnpkg.com/blade-formatter/-/blade-formatter-1.29.4.tgz#d9136864bf938d6dcec5eb76e8a642818fede261"
integrity sha512-sym/DMUNgIUCpuJ9/PatAxp2kDyiqe/rjdppiU280oSfHNmh26isNJJFw8O9cejOhTTZwUigvFsyglgk39RR7A==
blade-formatter@^1.29.6:
version "1.29.6"
resolved "https://registry.yarnpkg.com/blade-formatter/-/blade-formatter-1.29.6.tgz#ab11c04fe6e92d020b36a0a994fed3d9da7f9c54"
integrity sha512-Fndl52h1BRbUm+JMgS/u4gW4IIX8pFs25PqF3VV2BQ1TjeqPBbHfrcRnDC/NDVHD2VPlSIZskaIe4lFkYnSrrw==
dependencies:
"@prettier/plugin-php" "^0.19.0"
"@shufo/tailwindcss-class-sorter" "^1.0.10"
"@shufo/tailwindcss-class-sorter" "^1.0.12"
aigle "^1.14.1"
ajv "^8.9.0"
chalk "^4.1.0"
Expand Down

0 comments on commit 42b6dab

Please sign in to comment.