Skip to content

Commit

Permalink
Merge pull request #117 from jeronimoek/avoid-duplicate-picker
Browse files Browse the repository at this point in the history
feat: allow to disable color picker only on duplicated pickers
  • Loading branch information
jeronimoek authored Jul 28, 2024
2 parents 27b5b78 + 529df18 commit a993da3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 17 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ This extension can be used through its color pickers, commands, or context menu

To see settings press `CTRL + ,` OR `⌘ + ,`

| Id | Description | Default | Available values | Example |
| ----------------------------------------- | ------------------------------------------------------------- | ------- | ----------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
| color-picker-universal.disable | Disables the extension | false | true false | true |
| color-picker-universal.ignoreVariableName | If enabled, ignores colors in variables names | true | true false | false |
| color-picker-universal.preferLegacy | If enabled, colors are displayed in legacy mode when possible | false | true false | true |
| color-picker-universal.languages | Enabled language identifiers. Use "!" to exclude languages | ["*"] | [Default identifiers](https://code.visualstudio.com/docs/languages/identifiers#_known-language-identifiers) | ["*", "!css", "!less", "!sass", "!scss"] |
| color-picker-universal.formatsFrom | Enabled formats to translate from. Use "!" to exclude formats | ["*"] | "\*" "device-cmyk" "hex" "hsl" "hwb" "named" "rgb" "hex0x" "lab" "lch" "oklab" "oklch" "a98" | ["*", "!hex_0x", "!named"] |
| color-picker-universal.formatsTo | Enabled formats to translate into. Use "!" to exclude formats | ["*"] | "\*" "cmyk" "hex" "hsl" "hwb" "named" "rgb" "hex0x" "lab" "lch" "oklab" "oklch" "a98" | ["*", "!hex_0x", "!cmyk", "!hwb"] |
| color-picker-universal.maxDigits | Max number of decimal digits | 2 | <integer> | 5 |
| color-picker-universal.customRegexes | Set custom regexes for given formats | {} | See [Custom Regexes](#-custom-regexes) | { "a98": ["my-a98\\((\\d+(?:\\.\\d+)?) \\$ (\\d+(?:\\.\\d+)?) \\$ (\\d+(?:\\.\\d+)?)(?: \\$ (\\d+(?:\\.\\d+)?))?\\)",]} |
| Id | Description | Default | Available values | Example |
| ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | ------- | ----------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
| color-picker-universal.disable | Disables the extension | false | true false | true |
| color-picker-universal.ignoreVariableName | If enabled, ignores colors in variables names | true | true false | false |
| color-picker-universal.preferLegacy | If enabled, colors are displayed in legacy mode when possible | false | true false | true |
| color-picker-universal.languages | Enabled language identifiers. Use "!" to exclude languages | ["*"] | [Default identifiers](https://code.visualstudio.com/docs/languages/identifiers#_known-language-identifiers) | ["*", "!css", "!less", "!sass", "!scss"] |
| color-picker-universal.formatsFrom | Enabled formats to translate from. Use "!" to exclude formats | ["*"] | "\*" "device-cmyk" "hex" "hsl" "hwb" "named" "rgb" "hex0x" "lab" "lch" "oklab" "oklch" "a98" | ["*", "!hex_0x", "!named"] |
| color-picker-universal.formatsTo | Enabled formats to translate into. Use "!" to exclude formats | ["*"] | "\*" "cmyk" "hex" "hsl" "hwb" "named" "rgb" "hex0x" "lab" "lch" "oklab" "oklch" "a98" | ["*", "!hex_0x", "!cmyk", "!hwb"] |
| color-picker-universal.maxDigits | Max number of decimal digits | 2 | <integer> | 5 |
| color-picker-universal.customRegexes | Set custom regexes for given formats | {} | See [Custom Regexes](#-custom-regexes) | { "a98": ["my-a98\\((\\d+(?:\\.\\d+)?) \\$ (\\d+(?:\\.\\d+)?) \\$ (\\d+(?:\\.\\d+)?)(?: \\$ (\\d+(?:\\.\\d+)?))?\\)",]} |
| color-picker-universal.avoidDuplicate | If enabled, duplicate color pickers will be avoided. Note: On these built-in color pickers, the extension formats and configurations won't apply | false | true false | true |

## ✍ Commands

Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,11 @@
]
}
]
},
"color-picker-universal.avoidDuplicate": {
"default": false,
"description": "If enabled, duplicate color pickers will be avoided. Note: On these built-in color pickers, the extension formats and configurations won't apply",
"type": "boolean"
}
}
}
Expand Down
59 changes: 52 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from "vscode";
import { colorFormatsTo } from "./shared/constants";
import { colorFormatsTo, formatsBuiltinPicker } from "./shared/constants";
import { getCustomMatches, getMatches } from "./getMatches";
import {
findCustomFormat,
Expand All @@ -19,10 +19,7 @@ class Picker implements vscode.Disposable {
}

private register() {
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) {
return;
}
let disabled = false;

const command = `color-picker-universal.translateColors`;

Expand Down Expand Up @@ -77,12 +74,60 @@ class Picker implements vscode.Disposable {
vscode.commands.registerCommand(command, commandHandler);
vscode.languages.registerColorProvider("*", {
async provideDocumentColors(document: vscode.TextDocument) {
if (disabled) {
disabled = false;
return;
}

if (!isValidDocument(document)) return;

const text = document.getText();

return [...(await getMatches(text)), ...getCustomMatches(text)];
let matches = [...(await getMatches(text)), ...getCustomMatches(text)];

const avoidDuplicate = getSetting<boolean>("avoidDuplicate");
if (
avoidDuplicate &&
matches.length &&
formatsBuiltinPicker.includes(document.languageId)
) {
disabled = true;

const builtinPickerColors = await vscode.commands.executeCommand<
vscode.ColorInformation[]
>("vscode.executeDocumentColorProvider", document.uri);

const lineCharBuiltinColors = builtinPickerColors.reduce<
Record<string, true>
>((acc, curr) => {
const { start, end } = curr.range;
const values = [
start.line,
start.character,
end.line,
end.character,
];
const key = values.join("-");
acc[key] = true;
return acc;
}, {});

matches = matches.filter((match) => {
const { start, end } = match.range;
const values = [
start.line,
start.character,
end.line,
end.character,
];
const key = values.join("-");
return !lineCharBuiltinColors[key];
});
}

return matches;
},

provideColorPresentations(colorRaw, { range, document }) {
if (!isValidDocument(document)) return;

Expand Down Expand Up @@ -136,7 +181,7 @@ class Picker implements vscode.Disposable {
const finalRepresentations: vscode.ColorPresentation[] = [];

// Add custom format representation
// TODO: ugly asf
// TODO: improve code quality
if (isCustomFormat) {
const customFormat = findCustomFormat(text);
if (customFormat) {
Expand Down
2 changes: 2 additions & 0 deletions src/shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,5 @@ export const namedColorsLAB = {
yellow: [97.61, -15.75, 93.38],
yellowgreen: [76.77, -33.1, 65.61],
} satisfies Record<string, [number, number, number]>;

export const formatsBuiltinPicker = ["css", "less", "sass", "scss"];

0 comments on commit a993da3

Please sign in to comment.