[go: up one dir, main page]

Skip to content

Commit

Permalink
Added ‘stylesheet.strictMatch’ option
Browse files Browse the repository at this point in the history
This option will filter out abbreviations that were not matched with any existing snippet
  • Loading branch information
sergeche committed Sep 18, 2024
1 parent 4a16aa8 commit 1fb64ea
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
11 changes: 10 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,14 @@ export interface Options {
* Lower value will pick more abbreviations (and less accurate)
*/
'stylesheet.fuzzySearchMinScore': number;

/**
* Force strict abbreviation match. If Emmet is unable to match abbreviation
* with existing snippets, it will convert it to CSS property (`false`)
* or skip it (`true`). E.g. `foo-bar` will expand to `foo: bar` if this option
* is disabled or empty string if enabled
*/
'stylesheet.strictMatch': boolean;
}

/**
Expand Down Expand Up @@ -353,7 +361,8 @@ export const defaultOptions: Options = {
'stylesheet.unitAliases': { e: 'em', p: '%', x: 'ex', r: 'rem' },
'stylesheet.json': false,
'stylesheet.jsonDoubleQuotes': false,
'stylesheet.fuzzySearchMinScore': 0
'stylesheet.fuzzySearchMinScore': 0,
'stylesheet.strictMatch': false
};

export const defaultConfig: Config = {
Expand Down
28 changes: 18 additions & 10 deletions src/stylesheet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const gradientName = 'lg';
*/
export default function parse(abbr: string | CSSAbbreviation, config: Config): CSSAbbreviation {
const snippets = config.cache?.stylesheetSnippets || convertSnippets(config.snippets);
const result: CSSAbbreviation = []

if (config.cache) {
config.cache.stylesheetSnippets = snippets;
Expand All @@ -31,10 +32,13 @@ export default function parse(abbr: string | CSSAbbreviation, config: Config): C
const filteredSnippets = getSnippetsForScope(snippets, config);

for (const node of abbr) {
resolveNode(node, filteredSnippets, config);
const resolved = resolveNode(node, filteredSnippets, config);
if (resolved) {
result.push(resolved)
}
}

return abbr;
return result;
}

/**
Expand All @@ -53,7 +57,7 @@ export function convertSnippets(snippets: SnippetsMap): CSSSnippet[] {
* Resolves given node: finds matched CSS snippets using fuzzy match and resolves
* keyword aliases from node value
*/
function resolveNode(node: CSSProperty, snippets: CSSSnippet[], config: Config): CSSProperty {
function resolveNode(node: CSSProperty, snippets: CSSSnippet[], config: Config): CSSProperty | null {
if (!resolveGradient(node, config)) {
const score = config.options['stylesheet.fuzzySearchMinScore'];
if (isValueScope(config)) {
Expand All @@ -67,11 +71,15 @@ function resolveNode(node: CSSProperty, snippets: CSSSnippet[], config: Config):
node.snippet = snippet;

if (snippet) {
if (snippet.type === CSSSnippetType.Property) {
resolveAsProperty(node, snippet, config);
} else {
resolveAsSnippet(node, snippet);
const resolved = snippet.type === CSSSnippetType.Property
? resolveAsProperty(node, snippet, config)
: resolveAsSnippet(node, snippet)
if (resolved) {
node = resolved
} else if (config.options['stylesheet.strictMatch']) {
return null
}

}
}
}
Expand Down Expand Up @@ -125,7 +133,7 @@ function resolveGradient(node: CSSProperty, config: Config): boolean {
/**
* Resolves given parsed abbreviation node as CSS property
*/
function resolveAsProperty(node: CSSProperty, snippet: CSSSnippetProperty, config: Config): CSSProperty {
function resolveAsProperty(node: CSSProperty, snippet: CSSSnippetProperty, config: Config): CSSProperty | null {
const abbr = node.name!;

// Check for unmatched part of abbreviation
Expand All @@ -138,11 +146,11 @@ function resolveAsProperty(node: CSSProperty, snippet: CSSSnippetProperty, confi
if (inlineValue) {
if (node.value.length) {
// Already have value: unmatched part indicates matched snippet is invalid
return node;
return null;
}
const kw = resolveKeyword(inlineValue, config, snippet);
if (!kw) {
return node;
return null;
}
node.value.push(cssValue(kw));
}
Expand Down
11 changes: 11 additions & 0 deletions test/stylesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,15 @@ describe('Stylesheet abbreviations', () => {
equal(expand('spie'),'scroll-padding-inline-end: ${0};');

});

it('strict match', () => {
const config = resolveConfig({
type: 'stylesheet',
options: {
'stylesheet.strictMatch': true
}
});
equal(expand('max-inline-size:'), 'max: inline size;')
equal(expand('max-inline-size:', config), '')
})
});

0 comments on commit 1fb64ea

Please sign in to comment.