-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
extension.ts
138 lines (120 loc) · 3.88 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
import {
workspace,
ExtensionContext,
window,
commands,
OutputChannel,
} from 'vscode';
import {
LanguageClientOptions,
ServerOptions,
TransportKind,
RevealOutputChannelOn,
LanguageClient,
} from 'vscode-languageclient/node';
import * as path from 'path';
import { createStatusBar, initStatusBar } from './apis/statusBar';
let client: LanguageClient;
export async function activate(context: ExtensionContext) {
const outputChannel: OutputChannel = window.createOutputChannel(
'GraphQL Language Server',
);
const config = getConfig();
const { debug } = config;
if (debug) {
console.log('Extension "vscode-graphql" is now active!');
}
const serverPath = path.join('out', 'server', 'index.js');
const serverModule = context.asAbsolutePath(serverPath);
const debugOptions = {
execArgv: ['--nolazy', '--inspect=localhost:6009'],
};
const serverOptions: ServerOptions = {
run: {
module: serverModule,
transport: TransportKind.ipc,
},
debug: {
module: serverModule,
transport: TransportKind.ipc,
options: { ...(debug ? debugOptions : {}) },
},
};
const clientOptions: LanguageClientOptions = {
documentSelector: [
{ scheme: 'file', language: 'graphql' },
{ scheme: 'file', language: 'javascript' },
{ scheme: 'file', language: 'javascriptreact' },
{ scheme: 'file', language: 'typescript' },
{ scheme: 'file', language: 'typescriptreact' },
],
synchronize: {
// TODO: This should include any referenced graphql files inside the graphql-config
fileEvents: [
workspace.createFileSystemWatcher(
'/{graphql.config.*,.graphqlrc,.graphqlrc.*,package.json}',
false,
// Ignore change events for graphql config, we only care about create, delete and save events
// otherwise, the underlying language service is re-started on every key change.
// also, it makes sense that it should only re-load on file save, but we need to document that.
// TODO: perhaps we can intercept change events, and remind the user
// to save for the changes to take effect
true,
),
// These ignore node_modules and .git by default
workspace.createFileSystemWatcher(
'**/{*.graphql,*.graphqls,*.gql,*.js,*.mjs,*.cjs,*.esm,*.es,*.es6,*.jsx,*.ts,*.tsx}',
),
],
},
outputChannel,
outputChannelName: 'GraphQL Language Server',
revealOutputChannelOn: RevealOutputChannelOn.Never,
initializationFailedHandler: err => {
outputChannel.appendLine('Initialization failed');
outputChannel.appendLine(err.message);
if (err.stack) {
outputChannel.appendLine(err.stack);
}
if (debug) {
outputChannel.show();
}
return false;
},
};
client = new LanguageClient(
'vscode-graphql',
serverOptions,
clientOptions,
debug,
);
const statusBarItem = createStatusBar();
context.subscriptions.push(statusBarItem);
await client.start();
initStatusBar(statusBarItem, client, window.activeTextEditor);
const commandShowOutputChannel = commands.registerCommand(
'vscode-graphql.showOutputChannel',
() => outputChannel.show(),
);
context.subscriptions.push(commandShowOutputChannel);
commands.registerCommand('vscode-graphql.restart', async () => {
outputChannel.appendLine(`Stopping GraphQL LSP`);
await client.stop();
outputChannel.appendLine(`Restarting GraphQL LSP`);
await client.start();
outputChannel.appendLine(`GraphQL LSP restarted`);
});
}
export function deactivate() {
if (!client) {
return undefined;
}
console.log('Extension "vscode-graphql" will be de-activated!!');
return client.stop();
}
function getConfig() {
return workspace.getConfiguration(
'vscode-graphql',
window.activeTextEditor ? window.activeTextEditor.document.uri : null,
);
}