htmlMain.ts 7.8 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
5

6
import * as path from 'path';
M
Martin Aeschlimann 已提交
7
import * as fs from 'fs';
D
Dirk Baeumer 已提交
8 9
import * as nls from 'vscode-nls';
const localize = nls.loadMessageBundle();
10

P
Pine Wu 已提交
11 12
import { languages, ExtensionContext, IndentAction, Position, TextDocument, Range, CompletionItem, CompletionItemKind, SnippetString, workspace, SelectionRange, SelectionRangeKind } from 'vscode';
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind, RequestType, TextDocumentPositionParams, TextDocumentIdentifier } from 'vscode-languageclient';
J
Johannes Rieken 已提交
13
import { EMPTY_ELEMENTS } from './htmlEmptyTagsShared';
14
import { activateTagClosing } from './tagClosing';
15
import TelemetryReporter from 'vscode-extension-telemetry';
P
Pine Wu 已提交
16
import { getCustomDataPathsInAllWorkspaces } from './customData';
17

18 19
namespace TagCloseRequest {
	export const type: RequestType<TextDocumentPositionParams, string, any, any> = new RequestType('html/tag');
20 21
}

22 23 24 25 26 27
interface IPackageInfo {
	name: string;
	version: string;
	aiKey: string;
}

28 29
let telemetryReporter: TelemetryReporter | null;

30

31
export function activate(context: ExtensionContext) {
32
	let toDispose = context.subscriptions;
33

34
	let packageInfo = getPackageInfo(context);
35
	telemetryReporter = packageInfo && new TelemetryReporter(packageInfo.name, packageInfo.version, packageInfo.aiKey);
36

M
Martin Aeschlimann 已提交
37 38 39
	let serverMain = readJSONFile(context.asAbsolutePath('./server/package.json')).main;
	let serverModule = context.asAbsolutePath(path.join('server', serverMain));

40
	// The debug options for the server
41
	let debugOptions = { execArgv: ['--nolazy', '--inspect=6045'] };
42 43 44 45 46 47 48 49

	// 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 }
	};

50
	let documentSelector = ['html', 'handlebars', 'razor'];
51
	let embeddedLanguages = { css: true, javascript: true };
52

53
	let { dataPaths } = getCustomDataPathsInAllWorkspaces(workspace.workspaceFolders);
54

55 56
	// Options to control the language client
	let clientOptions: LanguageClientOptions = {
57
		documentSelector,
58
		synchronize: {
59
			configurationSection: ['html', 'css', 'javascript'], // the settings to synchronize
60 61
		},
		initializationOptions: {
62
			embeddedLanguages,
63
			dataPaths
64 65 66 67
		}
	};

	// Create the language client and start the client.
68
	let client = new LanguageClient('html', localize('htmlserver.name', 'HTML Language Server'), serverOptions, clientOptions);
69
	client.registerProposedFeatures();
70

71
	let disposable = client.start();
72
	toDispose.push(disposable);
73
	client.onReady().then(() => {
74 75 76 77
		let tagRequestor = (document: TextDocument, position: Position) => {
			let param = client.code2ProtocolConverter.asTextDocumentPositionParams(document, position);
			return client.sendRequest(TagCloseRequest.type, param);
		};
78
		disposable = activateTagClosing(tagRequestor, { html: true, handlebars: true, razor: true }, 'html.autoClosingTags');
79 80 81
		toDispose.push(disposable);

		disposable = client.onTelemetry(e => {
82 83 84 85
			if (telemetryReporter) {
				telemetryReporter.sendTelemetryEvent(e.key, e.data);
			}
		});
86
		toDispose.push(disposable);
87
	});
88

P
Pine Wu 已提交
89 90 91
	languages.registerSelectionRangeProvider('html', {
		async provideSelectionRanges(document: TextDocument, position: Position): Promise<SelectionRange[]> {
			const textDocument = TextDocumentIdentifier.create(document.uri.toString());
P
Pine Wu 已提交
92
			const rawRanges: Range[] = await client.sendRequest('$/textDocument/selectionRange', { textDocument, position });
P
Pine Wu 已提交
93 94 95 96 97 98 99 100 101 102 103

			return rawRanges.map(r => {
				const actualRange = new Range(new Position(r.start.line, r.start.character), new Position(r.end.line, r.end.character));
				return {
					range: actualRange,
					kind: SelectionRangeKind.Declaration
				};
			});
		}
	});

104
	languages.setLanguageConfiguration('html', {
105 106 107 108
		indentationRules: {
			increaseIndentPattern: /<(?!\?|(?:area|base|br|col|frame|hr|html|img|input|link|meta|param)\b|[^>]*\/>)([-_\.A-Za-z0-9]+)(?=\s|>)\b[^>]*>(?!.*<\/\1>)|<!--(?!.*-->)|\{[^}"']*$/,
			decreaseIndentPattern: /^\s*(<\/(?!html)[-_\.A-Za-z0-9]+\b[^>]*>|-->|\})/
		},
109
		wordPattern: /(-?\d*\.\d\w*)|([^\`\~\!\@\$\^\&\*\(\)\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\s]+)/g,
J
Johannes Rieken 已提交
110
		onEnterRules: [
111 112
			{
				beforeText: new RegExp(`<(?!(?:${EMPTY_ELEMENTS.join('|')}))([_:\\w][_:\\w-.\\d]*)([^/>]*(?!/)>)[^<]*$`, 'i'),
113
				afterText: /^<\/([_:\w][_:\w-.\d]*)\s*>/i,
114 115 116 117 118 119 120 121
				action: { indentAction: IndentAction.IndentOutdent }
			},
			{
				beforeText: new RegExp(`<(?!(?:${EMPTY_ELEMENTS.join('|')}))(\\w[\\w\\d]*)([^/>]*(?!/)>)[^<]*$`, 'i'),
				action: { indentAction: IndentAction.Indent }
			}
		],
	});
M
Martin Aeschlimann 已提交
122 123

	languages.setLanguageConfiguration('handlebars', {
124
		wordPattern: /(-?\d*\.\d\w*)|([^\`\~\!\@\$\^\&\*\(\)\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\s]+)/g,
J
Johannes Rieken 已提交
125
		onEnterRules: [
M
Martin Aeschlimann 已提交
126 127
			{
				beforeText: new RegExp(`<(?!(?:${EMPTY_ELEMENTS.join('|')}))([_:\\w][_:\\w-.\\d]*)([^/>]*(?!/)>)[^<]*$`, 'i'),
128
				afterText: /^<\/([_:\w][_:\w-.\d]*)\s*>/i,
M
Martin Aeschlimann 已提交
129 130 131 132 133 134 135 136
				action: { indentAction: IndentAction.IndentOutdent }
			},
			{
				beforeText: new RegExp(`<(?!(?:${EMPTY_ELEMENTS.join('|')}))(\\w[\\w\\d]*)([^/>]*(?!/)>)[^<]*$`, 'i'),
				action: { indentAction: IndentAction.Indent }
			}
		],
	});
E
Erich Gamma 已提交
137 138

	languages.setLanguageConfiguration('razor', {
139
		wordPattern: /(-?\d*\.\d\w*)|([^\`\~\!\@\$\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\s]+)/g,
J
Johannes Rieken 已提交
140
		onEnterRules: [
E
Erich Gamma 已提交
141 142
			{
				beforeText: new RegExp(`<(?!(?:${EMPTY_ELEMENTS.join('|')}))([_:\\w][_:\\w-.\\d]*)([^/>]*(?!/)>)[^<]*$`, 'i'),
143
				afterText: /^<\/([_:\w][_:\w-.\d]*)\s*>/i,
E
Erich Gamma 已提交
144 145 146 147 148 149 150 151
				action: { indentAction: IndentAction.IndentOutdent }
			},
			{
				beforeText: new RegExp(`<(?!(?:${EMPTY_ELEMENTS.join('|')}))(\\w[\\w\\d]*)([^/>]*(?!/)>)[^<]*$`, 'i'),
				action: { indentAction: IndentAction.Indent }
			}
		],
	});
152

153
	const regionCompletionRegExpr = /^(\s*)(<(!(-(-\s*(#\w*)?)?)?)?)?$/;
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	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;
				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 = new SnippetString('<!-- #endregion -->');
				endProposal.documentation = localize('folding.end', 'Folding Region End');
				endProposal.filterText = match[2];
				endProposal.sortText = 'zb';
				return [beginProposal, endProposal];
			}
			return null;
		}
	});
177 178
}

179
function getPackageInfo(context: ExtensionContext): IPackageInfo | null {
M
Martin Aeschlimann 已提交
180
	let extensionPackage = readJSONFile(context.asAbsolutePath('./package.json'));
181 182 183 184 185 186 187 188
	if (extensionPackage) {
		return {
			name: extensionPackage.name,
			version: extensionPackage.version,
			aiKey: extensionPackage.aiKey
		};
	}
	return null;
189
}
190

M
Martin Aeschlimann 已提交
191 192 193 194 195 196 197 198 199 200

function readJSONFile(location: string) {
	try {
		return JSON.parse(fs.readFileSync(location).toString());
	} catch (e) {
		console.log(`Problems reading ${location}: ${e}`);
		return {};
	}
}

201 202 203
export function deactivate(): Promise<any> {
	return telemetryReporter ? telemetryReporter.dispose() : Promise.resolve(null);
}