Skip to content

Commit

Permalink
[css] Redundant css suggestion for color. Fixes microsoft/vscode#43464
Browse files Browse the repository at this point in the history
  • Loading branch information
aeschli committed Mar 26, 2018
1 parent 3a64289 commit fa272ca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/services/cssCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ export class CSSCompletion {
});
}
let colorValues = new Set();
this.styleSheet.acceptVisitor(new ColorValueCollector(colorValues));
this.styleSheet.acceptVisitor(new ColorValueCollector(colorValues, this.offset));
for (let color of colorValues.getEntries()) {
result.items.push({
label: color,
Expand Down Expand Up @@ -912,13 +912,15 @@ function collectValues(styleSheet: nodes.Stylesheet, declaration: nodes.Declarat

class ColorValueCollector implements nodes.IVisitor {

constructor(public entries: Set) {
constructor(public entries: Set, private currentOffset: number) {
// nothing to do
}

public visitNode(node: nodes.Node): boolean {
if (node instanceof nodes.HexColorValue || (node instanceof nodes.Function && languageFacts.isColorConstructor(<nodes.Function>node))) {
this.entries.add(node.getText());
if (this.currentOffset < node.offset || node.end < this.currentOffset) {
this.entries.add(node.getText());
}
}
return true;
}
Expand Down
7 changes: 6 additions & 1 deletion src/test/css/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export let assertCompletion = function (completions: CompletionList, expected: I

suite('CSS - Completion', () => {

let testCompletionFor = function (value: string, expected: { count?: number, items?: ItemDescription[], participant?: { onProperty?, onPropertValue?, onURILiteralValue? } }) {
let testCompletionFor = function (value: string, expected: { count?: number, items?: ItemDescription[], participant?: { onProperty?, onPropertValue?, onURILiteralValue?} }) {
let offset = value.indexOf('|');
value = value.substr(0, offset) + value.substr(offset + 1);

Expand Down Expand Up @@ -341,6 +341,11 @@ suite('CSS - Completion', () => {
{ label: '#123456', kind: CompletionItemKind.Color, resultText: '.foo { background-color: #123456; } .bar { background-color:#123456 }' }
]
});
testCompletionFor('.bar { background-color: #123| }', {
items: [
{ label: '#123', notAvailable: true }
]
});
testCompletionFor('.foo { background-color: r|', {
items: [
{ label: 'rgb', kind: CompletionItemKind.Function, resultText: '.foo { background-color: rgb(${1:red}, ${2:green}, ${3:blue})' },
Expand Down

0 comments on commit fa272ca

Please sign in to comment.