Skip to content
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

addCustomWord #75

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
"description": "Highlight color words in all files (grey, green, etc.)",
"type": "boolean"
},
"color-highlight.customWords": {
"default": {},
"description": "add the color name and code you want",
"type": "object"
},
"color-highlight.markerType": {
"default": "background",
"description": "Style of the highlight. Can be 'dot-before', 'dot-after', 'foreground', 'background', 'outline', 'underline'",
Expand Down
8 changes: 6 additions & 2 deletions src/color-highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { findCssVars } from './strategies/css-vars';
import { findFn } from './strategies/functions';
import { findHex } from './strategies/hex';
import { findHwb } from './strategies/hwb';
import { findWords } from './strategies/words';
import { findWords, addCustomWord } from './strategies/words';
import { DecorationMap } from './lib/decoration-map';
import { dirname } from 'path';

Expand All @@ -35,7 +35,11 @@ export class DocumentHighlight {
this.strategies = [findHex, findFn, findHwb];

if (colorWordsLanguages.indexOf(colorWordsLanguages) > -1 || viewConfig.matchWords) {
this.strategies.push(findWords);
const customWords = viewConfig.customWords;
if (Object.entries(customWords).length !== 0){
addCustomWord(customWords);
}
this.strategies.push(text => findWords(text,customWords));
}

switch (document.languageId) {
Expand Down
23 changes: 17 additions & 6 deletions src/strategies/words.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const Color = require('color');
const webColors = require('color-name');

const preparedRePart = Object.keys(webColors)
let preparedRePart = Object.keys(webColors)
.map(color => `\\b${color}\\b`)
.join('|');

const colorWeb = new RegExp('.?(' + preparedRePart + ')(?!-)', 'g');
let colorWeb = new RegExp('.?(' + preparedRePart + ')(?!-)', 'g');


/**
* @export
Expand All @@ -16,7 +17,7 @@ const colorWeb = new RegExp('.?(' + preparedRePart + ')(?!-)', 'g');
* color: string
* }}
*/
export async function findWords(text) {
export async function findWords(text,customWords) {
let match = colorWeb.exec(text);
let result = [];

Expand All @@ -32,19 +33,29 @@ export async function findWords(text) {
}

try {
const color = Color(matchedColor)
let color = null;
if (customWords.hasOwnProperty(matchedColor)){
color = customWords[matchedColor];
}else{
color = Color(matchedColor)
.rgb()
.string();

}
result.push({
start,
end,
color
});
} catch (e) { }

match = colorWeb.exec(text);
}

return result;
}

export function addCustomWord(customWords){
Object.keys(customWords).forEach((word)=>{
preparedRePart = preparedRePart+'|\\b'+word+'\\b';
});
colorWeb = new RegExp('.?(' + preparedRePart + ')(?!-)', 'g');
}