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

Add support for nested CSS variables #90

Open
wants to merge 5 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 .changeset/thick-drinks-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"css-variables-language-server": minor
---

Add support for nested CSS variables.
35 changes: 20 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/css-variables-language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"dependencies": {
"axios": "^0.27.2",
"culori": "0.20.1",
"culori": "^4.0.1",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to be updated...! #95

"fast-glob": "^3.2.7",
"less": "^4.1.3",
"line-column": "^1.0.2",
Expand Down
94 changes: 65 additions & 29 deletions packages/css-variables-language-server/src/CSSVariableManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import path from 'path';
import postcssSCSS from 'postcss-scss';
import postcssLESS from 'postcss-less';
import CacheManager from './CacheManager';
import isColor from './utils/isColor';
import { culoriColorToVscodeColor } from './utils/culoriColorToVscodeColor';

export type CSSSymbol = {
Expand Down Expand Up @@ -55,7 +54,7 @@ const getAST = (filePath: string, content: string) => {
if (fileExtension === '.less') {
return postcssLESS.parse(content);
}

if (fileExtension === '.scss') {
return postcssSCSS.parse(content);
}
Expand All @@ -66,6 +65,54 @@ const getAST = (filePath: string, content: string) => {
export default class CSSVariableManager {
private cacheManager = new CacheManager<CSSVariable>();

private resolveCachedVariables = () => {
for (const filePath of this.cacheManager.getFiles()) {
this.cacheManager.getAll(filePath).forEach((variable, key) => {
this.setCssVariable(key, this.resolveRecursiveVariables(variable.symbol.value), filePath, variable.definition.range);
});
}
};

public resolveRecursiveVariables = (value: string) => {
for (let i = 0; i < 20; i++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrewda

Why does the for loop run 20? Is it because the maximum recursion depth is thought to be 20?

const variableReference = value.match(/^var\(\s*([a-zA-Z0-9-]+)\s*\)$/)?.[1];
if (variableReference) {
const newValue = this.cacheManager.get(variableReference)?.symbol?.value;
if (newValue) {
value = newValue;
} else {
break;
}
} else {
break;
}
Comment on lines +78 to +88

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const variableReference = value.match(/^var\(\s*([a-zA-Z0-9-]+)\s*\)$/)?.[1];
if (variableReference) {
const newValue = this.cacheManager.get(variableReference)?.symbol?.value;
if (newValue) {
value = newValue;
} else {
break;
}
} else {
break;
}
const variableReference = value.match(/^var\(\s*([a-zA-Z0-9-]+)\s*\)$/)?.[1];
if (!variableReference) {
break;
}
const newValue = this.cacheManager.get(variableReference)?.symbol?.value;
if (newValue) {
value = newValue;
continue;
}
break;

I think nested if statements are complicated. How about this?

}

return value;
};

public setCssVariable = (prop: string, value: string, filePath: string, range: Range) => {
const variable: CSSVariable = {
symbol: {
name: prop,
value: value,
},
definition: {
uri: filePath,
range: range,
},
};

const culoriColor = culori.parse(value);

if (culoriColor) {
variable.color = culoriColorToVscodeColor(culoriColor);
}

// add to cache
this.cacheManager.set(filePath, prop, variable);
};

public parseCSSVariablesFromText = async ({
content,
filePath,
Expand Down Expand Up @@ -117,36 +164,25 @@ export default class CSSVariableManager {

ast.walkDecls((decl) => {
if (decl.prop.startsWith('--')) {
const variable: CSSVariable = {
symbol: {
name: decl.prop,
value: decl.value,
},
definition: {
uri: fileURI,
range: Range.create(
Position.create(
decl.source.start.line - 1,
decl.source.start.column - 1
),
Position.create(
decl.source.end.line - 1,
decl.source.end.column - 1
)
this.setCssVariable(
decl.prop,
decl.value,
fileURI,
Range.create(
Position.create(
decl.source.start.line - 1,
decl.source.start.column - 1
),
},
};

const culoriColor = culori.parse(decl.value);

if (culoriColor) {
variable.color = culoriColorToVscodeColor(culoriColor);
}

// add to cache
this.cacheManager.set(filePath, decl.prop, variable);
Position.create(
decl.source.end.line - 1,
decl.source.end.column - 1
)
)
);
}
});

this.resolveCachedVariables();
} catch (error) {
console.error(filePath);
}
Expand Down
30 changes: 19 additions & 11 deletions packages/css-variables-language-server/src/CacheManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Cache Manager
*
*
* {
* src/styles/variables.css: {
* },
Expand All @@ -14,37 +14,45 @@
export default class CacheManager<T> {
private cachedVariables: Map<string, Map<string, T>> = new Map();
private allVariables: Map<string, T> = new Map();

public get(key: string, filePath?: string) {
if (filePath) {
return this.cachedVariables[filePath]?.get(key);
return this.cachedVariables.get(filePath)?.get(key);
}

return this.allVariables?.get(key);
}

public getAll() {
public getAll(filePath?: string) {
if (filePath) {
return this.cachedVariables.get(filePath);
}

return this.allVariables;
}

public getFiles() {
return this.cachedVariables.keys();
}

public set(filePath: string, key: string, value: T) {
if (!this.cachedVariables[filePath]) {
this.cachedVariables[filePath] = new Map();
if (!this.cachedVariables.get(filePath)) {
this.cachedVariables.set(filePath, new Map());
}

this.allVariables?.set(key, value);
this.cachedVariables[filePath].set(key, value);
this.allVariables.set(key, value);
this.cachedVariables.get(filePath).set(key, value);
}

public clearFileCache(filePath: string) {
this.cachedVariables[filePath]?.forEach((_, key) => {
this.cachedVariables.get(filePath)?.forEach((_, key) => {
this.allVariables?.delete(key);
});
this.cachedVariables[filePath]?.clear();
this.cachedVariables.get(filePath)?.clear();
}

public clearAllCache() {
this.allVariables?.clear();
this.cachedVariables.clear();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@

.five {
background-color: var(--main-bg-color);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
--h3: 18px;
--text-base: 16px;
--carousel-bg: var(--main-bg-color);
--carousel-bg-nested: var(--carousel-bg);
}

.one {
Expand Down Expand Up @@ -39,4 +40,4 @@

.five {
background-color: var(--main-bg-color);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@

.five {
background-color: var(--main-bg-color);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
--h3: 18px;
--text-base: 16px;
--carousel-bg: var(--main-bg-color);
--carousel-bg-nested: var(--carousel-bg);
}
}

Expand Down Expand Up @@ -43,4 +44,4 @@

.five {
background-color: var(--main-bg-color);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@

.five {
background-color: var(--main-bg-color);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
--h3: 18px;
--text-base: 16px;
--carousel-bg: var(--main-bg-color);
--carousel-bg-nested: var(--carousel-bg);
}

.one {
Expand Down Expand Up @@ -39,4 +40,4 @@

.five {
background-color: var(--main-bg-color);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@

.five {
background-color: var(--main-bg-color);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@

.five {
background-color: var(--main-bg-color);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
--h3: 18px;
--text-base: 16px;
--carousel-bg: var(--main-bg-color);
--carousel-bg-nested: var(--carousel-bg);
}

.one {
Expand Down Expand Up @@ -39,4 +40,4 @@

.five {
background-color: var(--main-bg-color);
}
}
Loading