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

Adding vendor prefixes to css abbreviations #13

Merged
merged 4 commits into from
Jan 26, 2018
Merged
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
42 changes: 31 additions & 11 deletions src/emmetHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let markupSnippetKeysRegex: RegExp[];
const stylesheetCustomSnippetsKeyCache = new Map<string, string[]>();
const htmlAbbreviationStartRegex = /^[a-z,A-Z,!,(,[,#,\.]/;
const htmlAbbreviationEndRegex = /[a-z,A-Z,!,),\],#,\.,},\d,*,$]$/;
const cssAbbreviationRegex = /^[a-z,A-Z,!,@,#]/;
const cssAbbreviationRegex = /^-?[a-z,A-Z,!,@,#]/;
const htmlAbbreviationRegex = /[a-z,A-Z]/;
const emmetModes = ['html', 'pug', 'slim', 'haml', 'xml', 'xsl', 'jsx', 'css', 'scss', 'sass', 'less', 'stylus'];
const commonlyUsedTags = ['div', 'span', 'p', 'b', 'i', 'body', 'html', 'ul', 'ol', 'li', 'head', 'section', 'canvas', 'dl', 'dt', 'dd', 'em', 'main', 'figure',
Expand Down Expand Up @@ -91,7 +91,7 @@ export function doComplete(document: TextDocument, position: Position, syntax: s
// If abbreviation is valid, then expand it and ensure the expanded value is not noise
if (isAbbreviationValid(syntax, abbreviation)) {
try {
expandedText = expand(abbreviation, expandOptions);
expandedText = expandAbbreviationHelper(abbreviation, expandOptions);
} catch (e) {
}

Expand Down Expand Up @@ -123,12 +123,13 @@ export function doComplete(document: TextDocument, position: Position, syntax: s
const stylesheetCustomSnippetsKeys = stylesheetCustomSnippetsKeyCache.has(syntax) ? stylesheetCustomSnippetsKeyCache.get(syntax) : stylesheetCustomSnippetsKeyCache.get('css');
completionItems = makeSnippetSuggestion(stylesheetCustomSnippetsKeys, currentWord, abbreviation, abbreviationRange, expandOptions, 'Emmet Custom Snippet', false);

if (!completionItems.find(x => x.textEdit.newText === expandedAbbr.textEdit.newText)) {

if (!completionItems.find(x => x.textEdit.newText === expandedAbbr.textEdit.newText)) {
// Fix for https://github.com/Microsoft/vscode/issues/28933#issuecomment-309236902
// When user types in propertyname, emmet uses it to match with snippet names, resulting in width -> widows or font-family -> font: family
// Filter out those cases here.
const abbrRegex = new RegExp('.*' + abbreviation.split('').map(x => x === '$' ? '\\$' : x).join('.*') + '.*', 'i');
let abbreviationWithoutDashPrefix = abbreviation[0] == '-' ? abbreviation.slice(1) : abbreviation;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is only applicable for css abbreviations. So I'd suggest to have a another function vendorPrefixCssAbbreviation that takes in abbreviation and syntax and returns a boolean. You can use isStylesheet to check the syntax as well as look for the preceeding -.

Re-use this function inside expandAbbreviationWithDash

  • rename this function as expandCssAbreviationWithVendorPrefixes
  • have a check on the syntax. Use the isStylesheet function.

Also rename expandAbbreviationWithDash to expandCssAbbreviationWithVendorPrefix

const abbrRegex = new RegExp('.*' + abbreviationWithoutDashPrefix.split('').map(x => x === '$' ? '\\$' : x).join('.*') + '.*', 'i');
if (/\d/.test(abbreviation) || abbrRegex.test(expandedAbbr.label)) {
completionItems.push(expandedAbbr);
}
Expand Down Expand Up @@ -220,8 +221,7 @@ function addFinalTabStop(text): string {
}

let maxTabStop = -1;
let maxTabStopStart = -1;
let maxTabStopEnd = -1;
let maxTabStopRanges = [];
let foundLastStop = false;
let replaceWithLastStop = false;
let i = 0;
Expand Down Expand Up @@ -270,17 +270,22 @@ function addFinalTabStop(text): string {
// Decide to replace currentTabStop with ${0} only if its the max among all tabstops and is not a placeholder
if (currentTabStop > maxTabStop) {
maxTabStop = currentTabStop;
maxTabStopStart = foundPlaceholder ? -1 : numberStart;
maxTabStopEnd = foundPlaceholder ? -1 : numberEnd;
maxTabStopRanges = [{ numberStart, numberEnd }];
replaceWithLastStop = !foundPlaceholder;
} else if (currentTabStop == maxTabStop) {
maxTabStopRanges.push({numberStart, numberEnd});
}
}
} catch (e) {

}

if (replaceWithLastStop && !foundLastStop) {
text = text.substr(0, maxTabStopStart) + '0' + text.substr(maxTabStopEnd);
for (let i = 0; i < maxTabStopRanges.length; i++) {
let rangeStart = maxTabStopRanges[i].numberStart;
let rangeEnd = maxTabStopRanges[i].numberEnd;
text = text.substr(0, rangeStart) + '0' + text.substr(rangeEnd);
}
}

return text;
Expand Down Expand Up @@ -520,13 +525,28 @@ export function getExpandOptions(syntax: string, emmetConfig?: object, filter?:
};
}

function expandAbbreviationHelper(abbreviation: string, options: any) {
let expandedText;
let prefixes = ["-webkit-", "-moz-", "-ms-", "-o-"];
abbreviation = abbreviation || "";
if (isStyleSheet(options.syntax) && abbreviation[0] == '-') {
let tmp = expand(abbreviation.substr(1), options);
expandedText = tmp;
for (let index = 0; index < prefixes.length; index++) {
expandedText += "\n" + prefixes[index] + tmp;
}
} else {
expandedText = expand(abbreviation, options);
}
return expandedText;
}
/**
* Expands given abbreviation using given options
* @param abbreviation string
* @param options
*/
export function expandAbbreviation(abbreviation: string, options: any) {
let expandedText = expand(abbreviation, options);
let expandedText = expandAbbreviationHelper(abbreviation, options);
return escapeNonTabStopDollar(addFinalTabStop(expandedText));
}

Expand Down
59 changes: 59 additions & 0 deletions src/test/emmetHelperTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,66 @@ describe('Test completions', () => {
assert.equal(matches[1].startsWith('Lorem'), true);

return Promise.resolve();
});
});

it('should provide completions using vendor prefixes', () => {
return updateExtensionsPath(extensionsPath).then(() => {
const testCases: [string, number, number, string, string, string][] = [

['brs', 0, 3, 'border-radius: ;', 'border-radius: |;', 'brs'],
['brs5', 0, 4, 'border-radius: 5px;', 'border-radius: 5px;', 'brs5'],
['-brs', 0, 4, 'border-radius: ;\n-webkit-border-radius: ;\n-moz-border-radius: ;\n-ms-border-radius: ;\n-o-border-radius: ;',
'border-radius: |;\n-webkit-border-radius: |;\n-moz-border-radius: |;\n-ms-border-radius: |;\n-o-border-radius: |;', '-brs'],
['-brs10', 0, 6, 'border-radius: 10px;\n-webkit-border-radius: 10px;\n-moz-border-radius: 10px;\n-ms-border-radius: 10px;\n-o-border-radius: 10px;',
'border-radius: 10px;\n-webkit-border-radius: 10px;\n-moz-border-radius: 10px;\n-ms-border-radius: 10px;\n-o-border-radius: 10px;', '-brs10'],
['-bdts', 0, 5, 'border-top-style: ;\n-webkit-border-top-style: ;\n-moz-border-top-style: ;\n-ms-border-top-style: ;\n-o-border-top-style: ;',
'border-top-style: |;\n-webkit-border-top-style: |;\n-moz-border-top-style: |;\n-ms-border-top-style: |;\n-o-border-top-style: |;', '-bdts'],
['-p', 0, 2, 'padding: ;\n-webkit-padding: ;\n-moz-padding: ;\n-ms-padding: ;\n-o-padding: ;',
'padding: |;\n-webkit-padding: |;\n-moz-padding: |;\n-ms-padding: |;\n-o-padding: |;', '-p'],
['-p10', 0, 4, 'padding: 10px;\n-webkit-padding: 10px;\n-moz-padding: 10px;\n-ms-padding: 10px;\n-o-padding: 10px;',
'padding: 10px;\n-webkit-padding: 10px;\n-moz-padding: 10px;\n-ms-padding: 10px;\n-o-padding: 10px;', '-p10'],
['-p10-20', 0, 7, 'padding: 10px 20px;\n-webkit-padding: 10px 20px;\n-moz-padding: 10px 20px;\n-ms-padding: 10px 20px;\n-o-padding: 10px 20px;',
'padding: 10px 20px;\n-webkit-padding: 10px 20px;\n-moz-padding: 10px 20px;\n-ms-padding: 10px 20px;\n-o-padding: 10px 20px;', '-p10-20'],
];

testCases.forEach(([content, positionLine, positionChar, expectedAbbr, expectedExpansion, expectedFilterText]) => {
const document = TextDocument.create('test://test/test.css', 'css', 0, content);
const position = Position.create(positionLine, positionChar);
const completionList = doComplete(document, position, 'css', {
preferences: {},
showExpandedAbbreviation: 'always',
showAbbreviationSuggestions: false,
syntaxProfiles: {},
variables: {}
});

assert.equal(completionList.items[0].label, expectedAbbr);
assert.equal(completionList.items[0].documentation, expectedExpansion);
assert.equal(completionList.items[0].filterText, expectedFilterText);
});
return Promise.resolve();
});
});


it('should expand with multiple vendor prefixes', () => {
return updateExtensionsPath(null).then(() => {
assert.equal(expandAbbreviation('brs', getExpandOptions('css', {})), 'border-radius: ${0};');
assert.equal(expandAbbreviation('brs5', getExpandOptions('css', {})), 'border-radius: 5px;');
assert.equal(expandAbbreviation('brs10px', getExpandOptions('css', {})), 'border-radius: 10px;');
assert.equal(expandAbbreviation('-brs', getExpandOptions('css', {})), 'border-radius: ${0};\n-webkit-border-radius: ${0};\n-moz-border-radius: ${0};\n-ms-border-radius: ${0};\n-o-border-radius: ${0};');
assert.equal(expandAbbreviation('-brs10', getExpandOptions('css', {})), 'border-radius: 10px;\n-webkit-border-radius: 10px;\n-moz-border-radius: 10px;\n-ms-border-radius: 10px;\n-o-border-radius: 10px;');
assert.equal(expandAbbreviation('-bdts', getExpandOptions('css', {})), 'border-top-style: ${0};\n-webkit-border-top-style: ${0};\n-moz-border-top-style: ${0};\n-ms-border-top-style: ${0};\n-o-border-top-style: ${0};');
assert.equal(expandAbbreviation('-bdts2px', getExpandOptions('css', {})), 'border-top-style: 2px;\n-webkit-border-top-style: 2px;\n-moz-border-top-style: 2px;\n-ms-border-top-style: 2px;\n-o-border-top-style: 2px;');
assert.equal(expandAbbreviation('-p10-20', getExpandOptions('css', {})), 'padding: 10px 20px;\n-webkit-padding: 10px 20px;\n-moz-padding: 10px 20px;\n-ms-padding: 10px 20px;\n-o-padding: 10px 20px;');
assert.equal(expandAbbreviation('-p10p20', getExpandOptions('css', {})), 'padding: 10% 20px;\n-webkit-padding: 10% 20px;\n-moz-padding: 10% 20px;\n-ms-padding: 10% 20px;\n-o-padding: 10% 20px;');


//assert.equal(expandAbbreviation('-bgp', getExpandOptions('css', {})), 'background-position:${1:0} ${2:0};\n-webkit-background-position:${1:0} ${2:0};\n-moz-background-position:${1:0} ${2:0};\n-ms-background-position:${1:0} ${2:0};\n-o-background-position:${1:0} ${2:0};');
//assert.equal(expandAbbreviation('bdr', getExpandOptions('css', {})), 'border-right: ${2:1px} ${3:solid} ${4:#000};');

return Promise.resolve();
});
});

Expand Down