Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/emmet/src/reflectCssValue.ts
4772 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { window, TextEditor } from 'vscode';
7
import { getCssPropertyFromRule, getCssPropertyFromDocument, offsetRangeToVsRange } from './util';
8
import { Property, Rule } from 'EmmetFlatNode';
9
10
const vendorPrefixes = ['-webkit-', '-moz-', '-ms-', '-o-', ''];
11
12
export function reflectCssValue(): Thenable<boolean> | undefined {
13
const editor = window.activeTextEditor;
14
if (!editor) {
15
window.showInformationMessage('No editor is active.');
16
return;
17
}
18
19
const node = getCssPropertyFromDocument(editor, editor.selection.active);
20
if (!node) {
21
return;
22
}
23
24
return updateCSSNode(editor, node);
25
}
26
27
function updateCSSNode(editor: TextEditor, property: Property): Thenable<boolean> {
28
const rule: Rule = property.parent;
29
let currentPrefix = '';
30
31
// Find vendor prefix of given property node
32
for (const prefix of vendorPrefixes) {
33
if (property.name.startsWith(prefix)) {
34
currentPrefix = prefix;
35
break;
36
}
37
}
38
39
const propertyName = property.name.substr(currentPrefix.length);
40
const propertyValue = property.value;
41
42
return editor.edit(builder => {
43
// Find properties with vendor prefixes, update each
44
vendorPrefixes.forEach(prefix => {
45
if (prefix === currentPrefix) {
46
return;
47
}
48
const vendorProperty = getCssPropertyFromRule(rule, prefix + propertyName);
49
if (vendorProperty) {
50
const rangeToReplace = offsetRangeToVsRange(editor.document, vendorProperty.valueToken.start, vendorProperty.valueToken.end);
51
builder.replace(rangeToReplace, propertyValue);
52
}
53
});
54
});
55
}
56
57