cssMain.ts 5.7 KB
Newer Older
M
Martin Aeschlimann 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';
import * as path from 'path';

D
Dirk Baeumer 已提交
8 9 10
import * as nls from 'vscode-nls';
const localize = nls.loadMessageBundle();

P
Pine Wu 已提交
11
import { languages, window, commands, ExtensionContext, Range, Position, TextDocument, CompletionItem, CompletionItemKind, TextEdit, SnippetString, FoldingRangeList, FoldingRange, FoldingContext, CancellationToken } from 'vscode';
P
Pine Wu 已提交
12
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind, Disposable } from 'vscode-languageclient';
13
import { FoldingRangesRequest, FoldingRangeRequestParam } from 'vscode-languageserver-protocol-foldingprovider';
14

M
Martin Aeschlimann 已提交
15 16 17 18 19 20
// this method is called when vs code is activated
export function activate(context: ExtensionContext) {

	// The server is implemented in node
	let serverModule = context.asAbsolutePath(path.join('server', 'out', 'cssServerMain.js'));
	// The debug options for the server
21
	let debugOptions = { execArgv: ['--nolazy', '--inspect=6044'] };
M
Martin Aeschlimann 已提交
22 23 24 25 26 27 28 29

	// If the extension is launch in debug mode the debug server options are use
	// Otherwise the run options are used
	let serverOptions: ServerOptions = {
		run: { module: serverModule, transport: TransportKind.ipc },
		debug: { module: serverModule, transport: TransportKind.ipc, options: debugOptions }
	};

30 31
	let documentSelector = ['css', 'scss', 'less'];

M
Martin Aeschlimann 已提交
32 33
	// Options to control the language client
	let clientOptions: LanguageClientOptions = {
34
		documentSelector,
M
Martin Aeschlimann 已提交
35
		synchronize: {
36
			configurationSection: ['css', 'scss', 'less', 'emmet']
M
Martin Aeschlimann 已提交
37 38 39 40 41 42
		},
		initializationOptions: {
		}
	};

	// Create the language client and start the client.
43
	let client = new LanguageClient('css', localize('cssserver.name', 'CSS Language Server'), serverOptions, clientOptions);
44
	client.registerProposedFeatures();
M
Martin Aeschlimann 已提交
45 46 47 48 49 50

	let disposable = client.start();
	// Push the disposable to the context's subscriptions so that the
	// client can be deactivated on extension deactivation
	context.subscriptions.push(disposable);

51 52 53 54 55
	let indentationRules = {
		increaseIndentPattern: /(^.*\{[^}]*$)/,
		decreaseIndentPattern: /^\s*\}/
	};

M
Martin Aeschlimann 已提交
56
	languages.setLanguageConfiguration('css', {
57 58
		wordPattern: /(#?-?\d*\.\d\w*%?)|(::?[\w-]*(?=[^,{;]*[,{]))|(([@#.!])?[\w-?]+%?|[@#!.])/g,
		indentationRules: indentationRules
M
Martin Aeschlimann 已提交
59 60 61
	});

	languages.setLanguageConfiguration('less', {
62 63
		wordPattern: /(#?-?\d*\.\d\w*%?)|(::?[\w-]+(?=[^,{;]*[,{]))|(([@#.!])?[\w-?]+%?|[@#!.])/g,
		indentationRules: indentationRules
M
Martin Aeschlimann 已提交
64 65 66
	});

	languages.setLanguageConfiguration('scss', {
67 68
		wordPattern: /(#?-?\d*\.\d\w*%?)|(::?[\w-]*(?=[^,{;]*[,{]))|(([@$#.!])?[\w-?]+%?|[@#!$.])/g,
		indentationRules: indentationRules
M
Martin Aeschlimann 已提交
69 70
	});

P
Pine Wu 已提交
71 72 73
	client.onReady().then(() => {
		context.subscriptions.push(initCompletionProvider());
		context.subscriptions.push(initFoldingProvider());
74 75
	});

P
Pine Wu 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
	function initCompletionProvider(): Disposable {
		const regionCompletionRegExpr = /^(\s*)(\/(\*\s*(#\w*)?)?)?$/;

		return languages.registerCompletionItemProvider(documentSelector, {
			provideCompletionItems(doc, pos) {
				let lineUntilPos = doc.getText(new Range(new Position(pos.line, 0), pos));
				let match = lineUntilPos.match(regionCompletionRegExpr);
				if (match) {
					let range = new Range(new Position(pos.line, match[1].length), pos);
					let beginProposal = new CompletionItem('#region', CompletionItemKind.Snippet);
					beginProposal.range = range; TextEdit.replace(range, '/* #region */');
					beginProposal.insertText = new SnippetString('/* #region $1*/');
					beginProposal.documentation = localize('folding.start', 'Folding Region Start');
					beginProposal.filterText = match[2];
					beginProposal.sortText = 'za';
					let endProposal = new CompletionItem('#endregion', CompletionItemKind.Snippet);
					endProposal.range = range;
					endProposal.insertText = '/* #endregion */';
					endProposal.documentation = localize('folding.end', 'Folding Region End');
					endProposal.sortText = 'zb';
					endProposal.filterText = match[2];
					return [beginProposal, endProposal];
P
Pine Wu 已提交
98 99
				}
				return null;
P
Pine Wu 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
			}
		});
	}

	function initFoldingProvider(): Disposable {
		return languages.registerFoldingProvider(documentSelector, {
			provideFoldingRanges(document: TextDocument, context: FoldingContext, token: CancellationToken) {
				const param: FoldingRangeRequestParam = {
					textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(document),
					maxRanges: context.maxRanges
				};
				return client.sendRequest(FoldingRangesRequest.type, param, token).then(res => {
					if (res && Array.isArray(res.ranges)) {
						return new FoldingRangeList(res.ranges.map(r => new FoldingRange(r.startLine, r.endLine, r.type)));
					}
					return null;
				}, error => {
					client.logFailedRequest(FoldingRangesRequest.type, error);
					return null;
				});
			}
		});
	}
P
Pine Wu 已提交
123

M
Martin Aeschlimann 已提交
124 125
	commands.registerCommand('_css.applyCodeAction', applyCodeAction);

126 127 128 129 130
	function applyCodeAction(uri: string, documentVersion: number, edits: TextEdit[]) {
		let textEditor = window.activeTextEditor;
		if (textEditor && textEditor.document.uri.toString() === uri) {
			if (textEditor.document.version !== documentVersion) {
				window.showInformationMessage(`CSS fix is outdated and can't be applied to the document.`);
M
Martin Aeschlimann 已提交
131
			}
132 133 134 135 136 137 138 139 140 141
			textEditor.edit(mutator => {
				for (let edit of edits) {
					mutator.replace(client.protocol2CodeConverter.asRange(edit.range), edit.newText);
				}
			}).then(success => {
				if (!success) {
					window.showErrorMessage('Failed to apply CSS fix to the document. Please consider opening an issue with steps to reproduce.');
				}
			});
		}
M
Martin Aeschlimann 已提交
142 143 144
	}
}