-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathprotocolFilter.ts
More file actions
72 lines (69 loc) · 3.57 KB
/
protocolFilter.ts
File metadata and controls
72 lines (69 loc) · 3.57 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
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */
'use strict';
import * as path from 'path';
import * as vscode from 'vscode';
import { Middleware } from 'vscode-languageclient';
import * as util from '../common';
import { logAndReturn } from '../Utility/Async/returns';
import { Client } from './client';
import { clients } from './extension';
import { hasFileAssociation } from './settings';
import { shouldChangeFromCToCpp } from './utils';
export const RequestCancelled: number = -32800;
export const ServerCancelled: number = -32802;
export function createProtocolFilter(): Middleware {
return {
didOpen: async (document, sendMessage) => {
if (!util.isCpp(document)) {
return;
}
util.setWorkspaceIsCpp();
const client: Client = clients.getClientFor(document.uri);
if (clients.checkOwnership(client, document)) {
const uriString: string = document.uri.toString();
if (!client.TrackedDocuments.has(uriString)) {
client.TrackedDocuments.set(uriString, document);
// Work around vscode treating ".C" or ".H" as c, by adding this file name to file associations as cpp
if (document.languageId === "c" && shouldChangeFromCToCpp(document)) {
// Don't override the user's setting.
if (!hasFileAssociation(path.basename(document.uri.fsPath))) {
const baseFileName: string = path.basename(document.fileName);
const mappingString: string = baseFileName + "@" + document.fileName;
client.addFileAssociations(mappingString, "cpp");
client.sendDidChangeSettings();
// The following will cause the file to be closed and reopened.
void vscode.languages.setTextDocumentLanguage(document, "cpp");
return;
}
}
// client.takeOwnership() will call client.TrackedDocuments.add() again, but that's ok. It's a Set.
client.takeOwnership(document);
void sendMessage(document);
client.ready.then(() => {
const cppEditors: vscode.TextEditor[] = vscode.window.visibleTextEditors.filter(e => util.isCpp(e.document));
client.onDidChangeVisibleTextEditors(cppEditors).catch(logAndReturn.undefined);
}).catch(logAndReturn.undefined);
}
}
},
willSaveWaitUntil: async (event, sendMessage) => {
const me: Client = clients.getClientFor(event.document.uri);
if (me.TrackedDocuments.has(event.document.uri.toString())) {
return sendMessage(event);
}
return [];
},
didClose: async (document, sendMessage) => {
const me: Client = clients.getClientFor(document.uri);
const uriString: string = document.uri.toString();
if (me.TrackedDocuments.has(uriString)) {
me.onDidCloseTextDocument(document);
me.TrackedDocuments.delete(uriString);
void sendMessage(document);
}
}
};
}