-
Notifications
You must be signed in to change notification settings - Fork 463
Expand file tree
/
Copy pathserver.ts
More file actions
211 lines (178 loc) · 5.93 KB
/
server.ts
File metadata and controls
211 lines (178 loc) · 5.93 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import "apollo-env";
// FIXME: The global fetch dependency comes from `apollo-link-http` and should be removed there.
import "apollo-env/lib/fetch/global";
import {
createConnection,
ProposedFeatures,
TextDocuments,
FileChangeType,
ServerCapabilities
} from "vscode-languageserver";
import { QuickPickItem } from "vscode";
import { GraphQLWorkspace } from "./workspace";
import { GraphQLLanguageProvider } from "./languageProvider";
import { LanguageServerLoadingHandler } from "./loadingHandler";
const connection = createConnection(ProposedFeatures.all);
let hasWorkspaceFolderCapability = false;
// Awaitable promise for sending messages before the connection is initialized
let initializeConnection: () => void;
const whenConnectionInitialized: Promise<void> = new Promise(
resolve => (initializeConnection = resolve)
);
const workspace = new GraphQLWorkspace(
new LanguageServerLoadingHandler(connection),
{
clientIdentity: {
name: process.env["APOLLO_CLIENT_NAME"],
version: process.env["APOLLO_CLIENT_VERSION"],
referenceID: process.env["APOLLO_CLIENT_REFERENCE_ID"]
}
}
);
workspace.onDiagnostics(params => {
connection.sendDiagnostics(params);
});
workspace.onDecorations(params => {
connection.sendNotification("apollographql/engineDecorations", params);
});
workspace.onSchemaTags(params => {
connection.sendNotification(
"apollographql/tagsLoaded",
JSON.stringify(params)
);
});
workspace.onConfigFilesFound(async params => {
await whenConnectionInitialized;
connection.sendNotification(
"apollographql/configFilesFound",
params instanceof Error
? // Can't stringify Errors, just results in "{}"
JSON.stringify({ message: params.message, stack: params.stack })
: JSON.stringify(params)
);
});
connection.onInitialize(async ({ capabilities, workspaceFolders }) => {
hasWorkspaceFolderCapability = !!(
capabilities.workspace && capabilities.workspace.workspaceFolders
);
if (workspaceFolders) {
// We wait until all projects are added, because after `initialize` returns we can get additional requests
// like `textDocument/codeLens`, and that way these can await `GraphQLProject#whenReady` to make sure
// we provide them eventually.
await Promise.all(
workspaceFolders.map(folder => workspace.addProjectsInFolder(folder))
);
}
return {
capabilities: {
hoverProvider: true,
completionProvider: {
resolveProvider: false,
triggerCharacters: ["..."]
},
definitionProvider: true,
referencesProvider: true,
documentSymbolProvider: true,
workspaceSymbolProvider: true,
codeLensProvider: {
resolveProvider: false
},
executeCommandProvider: {
commands: []
},
textDocumentSync: documents.syncKind
} as ServerCapabilities
};
});
connection.onInitialized(async () => {
initializeConnection();
if (hasWorkspaceFolderCapability) {
connection.workspace.onDidChangeWorkspaceFolders(async event => {
await Promise.all([
...event.removed.map(folder =>
workspace.removeProjectsInFolder(folder)
),
...event.added.map(folder => workspace.addProjectsInFolder(folder))
]);
});
}
});
const documents: TextDocuments = new TextDocuments();
// Make the text document manager listen on the connection
// for open, change and close text document events
documents.listen(connection);
documents.onDidChangeContent(params => {
const project = workspace.projectForFile(params.document.uri);
if (!project) return;
project.documentDidChange(params.document);
});
connection.onDidChangeWatchedFiles(params => {
for (const { uri, type } of params.changes) {
if (uri.endsWith("apollo.config.js") || uri.endsWith(".env")) {
workspace.reloadProjectForConfig(uri);
}
// Don't respond to changes in files that are currently open,
// because we'll get content change notifications instead
if (type === FileChangeType.Changed) {
continue;
}
const project = workspace.projectForFile(uri);
if (!project) continue;
switch (type) {
case FileChangeType.Created:
project.fileDidChange(uri);
break;
case FileChangeType.Deleted:
project.fileWasDeleted(uri);
break;
}
}
});
const languageProvider = new GraphQLLanguageProvider(workspace);
connection.onHover((params, token) =>
languageProvider.provideHover(params.textDocument.uri, params.position, token)
);
connection.onDefinition((params, token) =>
languageProvider.provideDefinition(
params.textDocument.uri,
params.position,
token
)
);
connection.onReferences((params, token) =>
languageProvider.provideReferences(
params.textDocument.uri,
params.position,
params.context,
token
)
);
connection.onDocumentSymbol((params, token) =>
languageProvider.provideDocumentSymbol(params.textDocument.uri, token)
);
connection.onWorkspaceSymbol((params, token) =>
languageProvider.provideWorkspaceSymbol(params.query, token)
);
connection.onCompletion((params, token) =>
languageProvider.provideCompletionItems(
params.textDocument.uri,
params.position,
token
)
);
connection.onCodeLens((params, token) =>
languageProvider.provideCodeLenses(params.textDocument.uri, token)
);
connection.onNotification("apollographql/reloadService", () =>
workspace.reloadService()
);
connection.onNotification(
"apollographql/tagSelected",
(selection: QuickPickItem) => workspace.updateSchemaTag(selection)
);
connection.onNotification("apollographql/getStats", async ({ uri }) => {
const status = await languageProvider.provideStats(uri);
connection.sendNotification("apollographql/statsLoaded", status);
});
// Listen on the connection
connection.listen();