-
-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathLanguageClient.js
More file actions
232 lines (195 loc) · 8.15 KB
/
Copy pathLanguageClient.js
File metadata and controls
232 lines (195 loc) · 8.15 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* Copyright (c) 2019 - present Adobe. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*global exports, Promise, LanguageClientInfo */
/*eslint no-console: 0*/
/*eslint strict: ["error", "global"]*/
/*eslint max-len: ["error", { "code": 200 }]*/
var ProtocolAdapter = require("./ProtocolAdapter"),
ServerUtils = require("./ServerUtils"),
Connection = require("./Connection"),
NodeToBracketsInterface = require("./NodeToBracketsInterface").NodeToBracketsInterface,
ToolingInfo = LanguageClientInfo.toolingInfo,
MESSAGE_TYPE = {
BRACKETS: "brackets",
SERVER: "server"
};
function validateHandler(handler) {
var retval = false;
if (handler && typeof handler === "function") {
retval = true;
} else {
console.warn("Handler validation failed. Handler should be of type 'function'. Provided handler is of type :", typeof handler);
}
return retval;
}
function LanguageClient(clientName, domainManager, options) {
this._clientName = clientName;
this._bracketsInterface = null;
this._notifyBrackets = null;
this._requestBrackets = null;
this._connection = null,
this._startUpParams = null, //_projectRoot, capabilties, workspaceFolders etc.
this._initialized = false,
this._onRequestHandler = {};
this._onNotificationHandlers = {};
this._options = options || null;
this._init(domainManager);
}
LanguageClient.prototype._createConnection = function () {
if (!this._options || !this._options.serverOptions) {
return Promise.reject("No valid options provided for client :", this._clientName);
}
var restartLanguageClient = this.start.bind(this),
stopLanguageClient = this.stop.bind(this);
var serverOptions = this._options.serverOptions;
return ServerUtils.startServerAndGetConnectionArgs(serverOptions)
.then(function (connectionArgs) {
return Connection.createConnection(connectionArgs.reader, connectionArgs.writer, restartLanguageClient, stopLanguageClient);
}).catch(function (err) {
console.error("Couldn't establish connection", err);
});
};
LanguageClient.prototype.setOptions = function (options) {
if (options && typeof options === "object") {
this._options = options;
} else {
console.error("Invalid options provided for client :", this._clientName);
}
};
LanguageClient.prototype.start = function (params) {
var self = this;
//Check to see if a connection to a language server already exists.
if (self._connection) {
return Promise.resolve(true);
}
self._connection = null;
self._startUpParams = params || self._startUpParams;
//We default to standard capabilties
if (!self._startUpParams.capabilities) {
self._startUpParams.capabilities = LanguageClientInfo.defaultBracketsCapabilities;
}
return self._createConnection()
.then(function (connection) {
connection.listen();
self._connection = connection;
return ProtocolAdapter.initialize(connection, self._startUpParams);
}).then(function (result) {
self._initialized = result;
ProtocolAdapter.attachOnNotificationHandlers(self._connection, self._notifyBrackets);
ProtocolAdapter.attachOnRequestHandlers(self._connection, self._requestBrackets);
ProtocolAdapter.initialized(self._connection);
return result;
}).catch(function (error) {
console.error('Starting client failed because :', error);
console.error('Couldn\'t start client :', self._clientName);
return error;
});
};
LanguageClient.prototype.stop = function () {
var self = this;
self._initialized = false;
if (!self._connection) {
return Promise.resolve(true);
}
return ProtocolAdapter.shutdown(self._connection).then(function () {
ProtocolAdapter.exit(self._connection);
self._connection.dispose();
self._connection = null;
});
};
LanguageClient.prototype.request = function (params) {
var messageParams = params.params;
if (messageParams && messageParams.messageType === MESSAGE_TYPE.BRACKETS) {
if (!messageParams.type) {
console.log("Invalid brackets request");
return Promise.reject();
}
var requestHandler = this._onRequestHandler[messageParams.type];
if(validateHandler(requestHandler)) {
return requestHandler.call(null, messageParams.params);
}
console.log("No handler provided for brackets request type : ", messageParams.type);
return Promise.reject();
}
return ProtocolAdapter.processRequest(this._connection, params);
};
LanguageClient.prototype.notify = function (params) {
var messageParams = params.params;
if (messageParams && messageParams.messageType === MESSAGE_TYPE.BRACKETS) {
if (!messageParams.type) {
console.log("Invalid brackets notification");
return;
}
var notificationHandlers = this._onNotificationHandlers[messageParams.type];
if(notificationHandlers && Array.isArray(notificationHandlers) && notificationHandlers.length) {
notificationHandlers.forEach(function (handler) {
if(validateHandler(handler)) {
handler.call(null, messageParams.params);
}
});
} else {
console.log("No handlers provided for brackets notification type : ", messageParams.type);
}
} else {
ProtocolAdapter.processNotification(this._connection, params);
}
};
LanguageClient.prototype.addOnRequestHandler = function (type, handler) {
if (validateHandler(handler)) {
this._onRequestHandler[type] = handler;
}
};
LanguageClient.prototype.addOnNotificationHandler = function (type, handler) {
if (validateHandler(handler)) {
if (!this._onNotificationHandlers[type]) {
this._onNotificationHandlers[type] = [];
}
this._onNotificationHandlers[type].push(handler);
}
};
LanguageClient.prototype._init = function (domainManager) {
this._bracketsInterface = new NodeToBracketsInterface(domainManager, this._clientName);
//Expose own methods for interfaceing. All these are async except notify.
this._bracketsInterface.registerMethods([
{
methodName: ToolingInfo.LANGUAGE_SERVICE.START,
methodHandle: this.start.bind(this)
},
{
methodName: ToolingInfo.LANGUAGE_SERVICE.STOP,
methodHandle: this.stop.bind(this)
},
{
methodName: ToolingInfo.LANGUAGE_SERVICE.REQUEST,
methodHandle: this.request.bind(this)
},
{
methodName: ToolingInfo.LANGUAGE_SERVICE.NOTIFY,
methodHandle: this.notify.bind(this)
}
]);
//create function interfaces for Brackets
this._notifyBrackets = this._bracketsInterface.createInterface(ToolingInfo.LANGUAGE_SERVICE.NOTIFY);
this._requestBrackets = this._bracketsInterface.createInterface(ToolingInfo.LANGUAGE_SERVICE.REQUEST, true);
};
exports.LanguageClient = LanguageClient;