|
| 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 | +'use strict'; |
| 6 | + |
| 7 | +import * as path from 'path'; |
| 8 | + |
| 9 | +import {languages, window, commands, ExtensionContext} from 'vscode'; |
| 10 | +import {LanguageClient, LanguageClientOptions, ServerOptions, TransportKind, RequestType, Range, TextEdit, Protocol2Code} from 'vscode-languageclient'; |
| 11 | +import {activateColorDecorations} from './colorDecorators'; |
| 12 | + |
| 13 | +namespace ColorSymbolRequest { |
| 14 | + export const type: RequestType<string, Range[], any> = { get method() { return 'css/colorSymbols'; } }; |
| 15 | +} |
| 16 | + |
| 17 | +// this method is called when vs code is activated |
| 18 | +export function activate(context: ExtensionContext) { |
| 19 | + |
| 20 | + // The server is implemented in node |
| 21 | + let serverModule = context.asAbsolutePath(path.join('server', 'out', 'cssServerMain.js')); |
| 22 | + // The debug options for the server |
| 23 | + let debugOptions = { execArgv: ['--nolazy', '--debug=6004'] }; |
| 24 | + |
| 25 | + // If the extension is launch in debug mode the debug server options are use |
| 26 | + // Otherwise the run options are used |
| 27 | + let serverOptions: ServerOptions = { |
| 28 | + run: { module: serverModule, transport: TransportKind.ipc }, |
| 29 | + debug: { module: serverModule, transport: TransportKind.ipc, options: debugOptions } |
| 30 | + }; |
| 31 | + |
| 32 | + // Options to control the language client |
| 33 | + let clientOptions: LanguageClientOptions = { |
| 34 | + documentSelector: ['css', 'less', 'scss'], |
| 35 | + synchronize: { |
| 36 | + configurationSection: ['css', 'scss', 'less'] |
| 37 | + }, |
| 38 | + initializationOptions: { |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + // Create the language client and start the client. |
| 43 | + let client = new LanguageClient('css', serverOptions, clientOptions); |
| 44 | + |
| 45 | + let disposable = client.start(); |
| 46 | + // Push the disposable to the context's subscriptions so that the |
| 47 | + // client can be deactivated on extension deactivation |
| 48 | + context.subscriptions.push(disposable); |
| 49 | + |
| 50 | + let colorRequestor = (uri: string) => { |
| 51 | + return client.sendRequest(ColorSymbolRequest.type, uri).then(ranges => ranges.map(Protocol2Code.asRange)); |
| 52 | + }; |
| 53 | + disposable = activateColorDecorations(colorRequestor, { css: true, scss: true, less: true }); |
| 54 | + context.subscriptions.push(disposable); |
| 55 | + |
| 56 | + languages.setLanguageConfiguration('css', { |
| 57 | + wordPattern: /(#?-?\d*\.\d\w*%?)|((::|[@#.!:])?[\w-?]+%?)|::|[@#.!:]/g, |
| 58 | + comments: { |
| 59 | + blockComment: ['/*', '*/'] |
| 60 | + }, |
| 61 | + brackets: [['{', '}'], ['[', ']'], ['(', ')']], |
| 62 | + autoClosingPairs: [ |
| 63 | + { open: '{', close: '}' }, |
| 64 | + { open: '[', close: ']' }, |
| 65 | + { open: '(', close: ')' }, |
| 66 | + { open: '"', close: '"', notIn: ['string'] }, |
| 67 | + { open: '\'', close: '\'', notIn: ['string'] } |
| 68 | + ] |
| 69 | + }); |
| 70 | + |
| 71 | + languages.setLanguageConfiguration('less', { |
| 72 | + wordPattern: /(#?-?\d*\.\d\w*%?)|([@#!.:]?[\w-?]+%?)|[@#!.]/g, |
| 73 | + comments: { |
| 74 | + blockComment: ['/*', '*/'], |
| 75 | + lineComment: '//' |
| 76 | + }, |
| 77 | + brackets: [['{', '}'], ['[', ']'], ['(', ')'], ['<', '>']], |
| 78 | + autoClosingPairs: [ |
| 79 | + { open: '"', close: '"', notIn: ['string', 'comment'] }, |
| 80 | + { open: '\'', close: '\'', notIn: ['string', 'comment'] }, |
| 81 | + { open: '{', close: '}', notIn: ['string', 'comment'] }, |
| 82 | + { open: '[', close: ']', notIn: ['string', 'comment'] }, |
| 83 | + { open: '(', close: ')', notIn: ['string', 'comment'] }, |
| 84 | + { open: '<', close: '>', notIn: ['string', 'comment'] }, |
| 85 | + ] |
| 86 | + }); |
| 87 | + |
| 88 | + languages.setLanguageConfiguration('scss', { |
| 89 | + wordPattern: /(#?-?\d*\.\d\w*%?)|([@#!.:]?[\w-?]+%?)|[@#!.]/g, |
| 90 | + comments: { |
| 91 | + blockComment: ['/*', '*/'], |
| 92 | + lineComment: '//' |
| 93 | + }, |
| 94 | + brackets: [['{', '}'], ['[', ']'], ['(', ')'], ['<', '>']], |
| 95 | + autoClosingPairs: [ |
| 96 | + { open: '"', close: '"', notIn: ['string', 'comment'] }, |
| 97 | + { open: '\'', close: '\'', notIn: ['string', 'comment'] }, |
| 98 | + { open: '{', close: '}', notIn: ['string', 'comment'] }, |
| 99 | + { open: '[', close: ']', notIn: ['string', 'comment'] }, |
| 100 | + { open: '(', close: ')', notIn: ['string', 'comment'] }, |
| 101 | + { open: '<', close: '>', notIn: ['string', 'comment'] }, |
| 102 | + ] |
| 103 | + }); |
| 104 | + |
| 105 | + commands.registerCommand('_css.applyCodeAction', applyCodeAction); |
| 106 | +} |
| 107 | + |
| 108 | +function applyCodeAction(uri: string, documentVersion: number, edits: TextEdit[]) { |
| 109 | + let textEditor = window.activeTextEditor; |
| 110 | + if (textEditor && textEditor.document.uri.toString() === uri) { |
| 111 | + if (textEditor.document.version !== documentVersion) { |
| 112 | + window.showInformationMessage(`CSS fix is outdated and can't be applied to the document.`); |
| 113 | + } |
| 114 | + textEditor.edit(mutator => { |
| 115 | + for (let edit of edits) { |
| 116 | + mutator.replace(Protocol2Code.asRange(edit.range), edit.newText); |
| 117 | + } |
| 118 | + }).then(success => { |
| 119 | + if (!success) { |
| 120 | + window.showErrorMessage('Failed to apply CSS fix to the document. Please consider opening an issue with steps to reproduce.'); |
| 121 | + } |
| 122 | + }); |
| 123 | + } |
| 124 | +} |
| 125 | + |
0 commit comments