Skip to content

Commit

Permalink
feat: move unstable no_sort feature to ext config
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenisx committed Oct 18, 2021
1 parent 474bdae commit 2d7eb18
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 45 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ This Extension supports the following properties as of now:
- Eg: `cssvar.themes: ["dark"]`. This will help the extension
distinguish between similarly named variables.
- `cssvar.excludeThemedVariables`: `boolean`
- `cssvar.unstable`: `string[]`
- All unstable features are by default disabled.
- Supports specific string constants, pointing to Unstable features:
- `"no_sort"` - Disables VSCode's in-built sorting feature for this extension
- `cssvar.disableSort`: `boolean`
- Disables VSCode's default sorting functionality for this extension.

*`cssvar.files` should contain relative/absolute path from
your workspace root folder.*
Expand All @@ -42,7 +40,7 @@ your User `settings.json` or Workspace `settings.json`.
- `cssvar.extensions`: `["css", "scss", "sass", "less"]`
- `cssvar.themes`: `[]`
- `cssvar.excludeThemedVariables`: `false`
- `cssvar.unstable`: `[]`
- `cssvar.disableSort`: `false`

## Screeshots:

Expand Down
14 changes: 5 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"intellisense"
],
"description": "Intellisense support for CSS Variables",
"version": "0.1.1",
"version": "0.2.0",
"publisher": "phoenisx",
"license": "MIT",
"homepage": "https://github.com/willofindie/vscode-cssvar",
Expand Down Expand Up @@ -77,14 +77,10 @@
"default": false,
"description": "Exclude themed variables"
},
"cssvar.unstable": {
"type": [
"array",
"null"
],
"default": null,
"pattern": "no_sort",
"description": "Enable various unstable feature for the extension"
"cssvar.disableSort": {
"type": "boolean",
"default": false,
"description": "Disables default sorting applied by VSCode"
}
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,20 @@ export type SupportedExtensionNames =
| "javascript"
| "javascriptreact";

export const UNSTABLE_FEATURES = {
no_sort: false,
};
export type UnstableFeatures = (keyof typeof UNSTABLE_FEATURES)[];

export interface Config {
files: string[] | Record<string, string[]>;
extensions: SupportedExtensionNames[];
themes: string[];
excludeThemedVariables: boolean;
unstable: UnstableFeatures;
disableSort: boolean;
}

export const DEFAULT_CONFIG: Config = {
files: ["index.css"],
extensions: ["css", "scss", "sass", "less"],
themes: [],
excludeThemedVariables: false,
unstable: [],
disableSort: false,
};

export const mapShortToFullExtension = (
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
`\n\n${relativePaths.join("\n\n")}`
);
}
const completionItems = createCompletionItems(cssVars, {
const completionItems = createCompletionItems(config, cssVars, {
region,
languageId: language,
});
Expand Down
12 changes: 2 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
SUFFIX,
SupportedExtensionNames,
SupportedLanguageIds,
UNSTABLE_FEATURES,
} from "./constants";
import { getCSSDeclarationArray, isCSSInJS, isObjectProperty } from "./utils";
import { disableDefaultSort } from "./unstable";
Expand Down Expand Up @@ -83,14 +82,6 @@ export async function setup(): Promise<{ config: Config }> {
});
break;
}
case "unstable": {
const value =
_config.get<Config[typeof key]>(key) || DEFAULT_CONFIG[key];
value.forEach(featureKey => {
UNSTABLE_FEATURES[featureKey] = true;
});
break;
}
default: {
const value =
_config.get<Config[typeof key]>(key) || DEFAULT_CONFIG[key];
Expand Down Expand Up @@ -178,6 +169,7 @@ export const getRegion = (line: string, currentRange: Range): Region => {
};

export const createCompletionItems = (
config: Config,
cssVars: CSSVarRecord,
options: {
region?: Region | null;
Expand Down Expand Up @@ -210,7 +202,7 @@ export const createCompletionItems = (
}
item.insertText = insertText;
item.range = options.region.range;
disableDefaultSort(item, { size, index });
disableDefaultSort(config, item, { size, index });
}
items.push(item);
return items;
Expand Down
25 changes: 17 additions & 8 deletions src/test/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Position, Range } from "vscode";
import { CSSVarRecord, UNSTABLE_FEATURES } from "../constants";
import { Config, CSSVarRecord, DEFAULT_CONFIG } from "../constants";
import { createCompletionItems, getRegion, Region } from "../main";

jest.mock("../constants", () => ({
Expand Down Expand Up @@ -110,7 +110,10 @@ describe("Test Extension Main", () => {

describe(`Test createCompletion method`, () => {
it("Should return CompletionItems with Sorting On", async () => {
UNSTABLE_FEATURES.no_sort = false;
const config: Config = {
...DEFAULT_CONFIG,
disableSort: false,
}
const cssVars: CSSVarRecord = {
"./src/01.css": [
{
Expand All @@ -127,7 +130,7 @@ describe("Test Extension Main", () => {
},
],
};
const items = createCompletionItems(cssVars, {
const items = createCompletionItems(config, cssVars, {
region,
languageId: "css",
});
Expand All @@ -139,7 +142,10 @@ describe("Test Extension Main", () => {
);
});
it("Should return CompletionItems with Sorting Disabled", async () => {
UNSTABLE_FEATURES.no_sort = true;
const config: Config = {
...DEFAULT_CONFIG,
disableSort: true,
}
const cssVars: CSSVarRecord = {
"./src/01.css": [
{
Expand All @@ -157,7 +163,7 @@ describe("Test Extension Main", () => {
],
};

const items = createCompletionItems(cssVars, {
const items = createCompletionItems(config, cssVars, {
region,
languageId: "css",
});
Expand All @@ -171,7 +177,10 @@ describe("Test Extension Main", () => {
});
});
it("Should return CompletionItems with 3 and 2digit sortText", async () => {
UNSTABLE_FEATURES.no_sort = true;
const config: Config = {
...DEFAULT_CONFIG,
disableSort: true,
}
const cssVars1: CSSVarRecord = Array(11)
.fill([
{
Expand All @@ -197,11 +206,11 @@ describe("Test Extension Main", () => {
return acc;
}, {});

const items1 = createCompletionItems(cssVars1, {
const items1 = createCompletionItems(config, cssVars1, {
region,
languageId: "css",
});
const items2 = createCompletionItems(cssVars2, {
const items2 = createCompletionItems(config, cssVars2, {
region,
languageId: "css",
});
Expand Down
5 changes: 3 additions & 2 deletions src/unstable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
*/

import { CompletionItem } from "vscode";
import { UNSTABLE_FEATURES } from "./constants";
import { Config } from "./constants";

export const disableDefaultSort = (
config: Config,
item: CompletionItem,
options: {
size: number;
index: number;
}
) => {
if (UNSTABLE_FEATURES.no_sort) {
if (config.disableSort) {
const padSize = Math.floor(Math.log(options.size - 1) / Math.log(10) + 1);
item.sortText = `${options.index}`.padStart(padSize, "0");
}
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1851,9 +1851,9 @@ camelcase@^6.0.0:
integrity sha512-c7wVvbw3f37nuobQNtgsgG9POC9qMbNuMQmTCqZv23b6MIz0fcYpBiOlv9gEN/hdLdnZTDQhg6e9Dq5M1vKvfg==

caniuse-lite@^1.0.30001208:
version "1.0.30001208"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001208.tgz#a999014a35cebd4f98c405930a057a0d75352eb9"
integrity sha512-OE5UE4+nBOro8Dyvv0lfx+SRtfVIOM9uhKqFmJeUbGriqhhStgp1A0OyBpgy3OUF8AhYCT+PVwPC1gMl2ZcQMA==
version "1.0.30001269"
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001269.tgz"
integrity sha512-UOy8okEVs48MyHYgV+RdW1Oiudl1H6KolybD6ZquD0VcrPSgj25omXO1S7rDydjpqaISCwA8Pyx+jUQKZwWO5w==

capture-exit@^2.0.0:
version "2.0.0"
Expand Down

0 comments on commit 2d7eb18

Please sign in to comment.