Path: blob/main/extensions/emmet/src/reflectCssValue.ts
4772 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { window, TextEditor } from 'vscode';6import { getCssPropertyFromRule, getCssPropertyFromDocument, offsetRangeToVsRange } from './util';7import { Property, Rule } from 'EmmetFlatNode';89const vendorPrefixes = ['-webkit-', '-moz-', '-ms-', '-o-', ''];1011export function reflectCssValue(): Thenable<boolean> | undefined {12const editor = window.activeTextEditor;13if (!editor) {14window.showInformationMessage('No editor is active.');15return;16}1718const node = getCssPropertyFromDocument(editor, editor.selection.active);19if (!node) {20return;21}2223return updateCSSNode(editor, node);24}2526function updateCSSNode(editor: TextEditor, property: Property): Thenable<boolean> {27const rule: Rule = property.parent;28let currentPrefix = '';2930// Find vendor prefix of given property node31for (const prefix of vendorPrefixes) {32if (property.name.startsWith(prefix)) {33currentPrefix = prefix;34break;35}36}3738const propertyName = property.name.substr(currentPrefix.length);39const propertyValue = property.value;4041return editor.edit(builder => {42// Find properties with vendor prefixes, update each43vendorPrefixes.forEach(prefix => {44if (prefix === currentPrefix) {45return;46}47const vendorProperty = getCssPropertyFromRule(rule, prefix + propertyName);48if (vendorProperty) {49const rangeToReplace = offsetRangeToVsRange(editor.document, vendorProperty.valueToken.start, vendorProperty.valueToken.end);50builder.replace(rangeToReplace, propertyValue);51}52});53});54}555657