-
Notifications
You must be signed in to change notification settings - Fork 120
/
extension.ts
168 lines (148 loc) · 5.6 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';
import * as lsp from 'vscode-languageclient';
import {registerCommands} from './commands';
import {projectLoadingNotification} from './protocol';
export function activate(context: vscode.ExtensionContext) {
// If the extension is launched in debug mode then the debug server options are used
// Otherwise the run options are used
const serverOptions: lsp.ServerOptions = {
run: getServerOptions(context, false /* debug */),
debug: getServerOptions(context, true /* debug */),
};
// Options to control the language client
const clientOptions: lsp.LanguageClientOptions = {
// Register the server for Angular templates and TypeScript documents
documentSelector: [
// scheme: 'file' means listen to changes to files on disk only
// other option is 'untitled', for buffer in the editor (like a new doc)
{scheme: 'file', language: 'html'},
{scheme: 'file', language: 'typescript'},
],
synchronize: {
fileEvents: [
// Notify the server about file changes to tsconfig.json contained in the workspace
vscode.workspace.createFileSystemWatcher('**/tsconfig.json'),
]
},
// Don't let our output console pop open
revealOutputChannelOn: lsp.RevealOutputChannelOn.Never
};
// Create the language client and start the client.
const forceDebug = process.env['NG_DEBUG'] === 'true';
const client =
new lsp.LanguageClient('Angular Language Service', serverOptions, clientOptions, forceDebug);
// Push the disposable to the context's subscriptions so that the
// client can be deactivated on extension deactivation
context.subscriptions.push(...registerCommands(client), client.start());
client.onDidChangeState((e) => {
let task: {resolve: () => void}|undefined;
if (e.newState == lsp.State.Running) {
client.onNotification(projectLoadingNotification.start, () => {
if (task) {
task.resolve();
task = undefined;
}
vscode.window.withProgress(
{
location: vscode.ProgressLocation.Window,
title: 'Initializing Angular language features',
},
() => new Promise((resolve) => {
task = {resolve};
}));
});
client.onNotification(projectLoadingNotification.finish, () => {
if (task) {
task.resolve();
task = undefined;
}
});
}
});
}
/**
* Return the paths for the module that corresponds to the specified `configValue`,
* and use the specified `bundled` as fallback if none is provided.
* @param configName
* @param bundled
*/
function getProbeLocations(configValue: string|null, bundled: string): string[] {
const locations = [];
// Always use config value if it's specified
if (configValue) {
locations.push(configValue);
}
// Prioritize the bundled version
locations.push(bundled);
// Look in workspaces currently open
const workspaceFolders = vscode.workspace.workspaceFolders || [];
for (const folder of workspaceFolders) {
locations.push(folder.uri.fsPath);
}
return locations;
}
/**
* Construct the arguments that's used to spawn the server process.
* @param ctx vscode extension context
* @param debug true if debug mode is on
*/
function constructArgs(ctx: vscode.ExtensionContext, debug: boolean): string[] {
const config = vscode.workspace.getConfiguration();
const args: string[] = [];
const ngLog: string = config.get('angular.log', 'off');
if (ngLog !== 'off') {
// Log file does not yet exist on disk. It is up to the server to create the file.
const logFile = path.join(ctx.logPath, 'nglangsvc.log');
args.push('--logFile', logFile);
args.push('--logVerbosity', debug ? 'verbose' : ngLog);
}
const ngdk: string|null = config.get('angular.ngdk', null);
const ngProbeLocations = getProbeLocations(ngdk, ctx.asAbsolutePath('server'));
args.push('--ngProbeLocations', ngProbeLocations.join(','));
const tsdk: string|null = config.get('typescript.tsdk', null);
const tsProbeLocations = getProbeLocations(tsdk, ctx.extensionPath);
args.push('--tsProbeLocations', tsProbeLocations.join(','));
return args;
}
function getServerOptions(ctx: vscode.ExtensionContext, debug: boolean): lsp.NodeModule {
// Environment variables for server process
const prodEnv = {
// Force TypeScript to use the non-polling version of the file watchers.
TSC_NONPOLLING_WATCHER: true,
};
const devEnv = {
...prodEnv,
NG_DEBUG: true,
};
// Node module for the language server
const prodBundle = ctx.asAbsolutePath('server');
const devBundle = ctx.asAbsolutePath(path.join('server', 'out', 'server.js'));
// Argv options for Node.js
const prodExecArgv: string[] = [];
const devExecArgv: string[] = [
// do not lazily evaluate the code so all breakpoints are respected
'--nolazy',
// If debugging port is changed, update .vscode/launch.json as well
'--inspect=6009',
];
return {
// VS Code Insider launches extensions in debug mode by default but users
// install prod bundle so we have to check whether dev bundle exists.
module: debug && fs.existsSync(devBundle) ? devBundle : prodBundle,
transport: lsp.TransportKind.ipc,
args: constructArgs(ctx, debug),
options: {
env: debug ? devEnv : prodEnv,
execArgv: debug ? devExecArgv : prodExecArgv,
},
};
}