forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLanguageClient.js
More file actions
1 lines (1 loc) · 5.04 KB
/
LanguageClient.js
File metadata and controls
1 lines (1 loc) · 5.04 KB
1
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=!1;return handler&&"function"==typeof handler?retval=!0:console.warn("Handler validation failed. Handler should be of type 'function'. Provided handler is of type :",typeof handler),retval}function LanguageClient(clientName,domainManager,options){this._clientName=clientName,this._bracketsInterface=null,this._notifyBrackets=null,this._requestBrackets=null,this._connection=null,this._startUpParams=null,this._initialized=!1,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),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){options&&"object"==typeof options?this._options=options:console.error("Invalid options provided for client :",this._clientName)},LanguageClient.prototype.start=function(params){var self=this;return self._connection?Promise.resolve(!0):(self._connection=null,self._startUpParams=params||self._startUpParams,self._startUpParams.capabilities||(self._startUpParams.capabilities=LanguageClientInfo.defaultBracketsCapabilities),self._createConnection().then(function(connection){return connection.listen(),self._connection=connection,ProtocolAdapter.initialize(connection,self._startUpParams)}).then(function(result){return self._initialized=result,ProtocolAdapter.attachOnNotificationHandlers(self._connection,self._notifyBrackets),ProtocolAdapter.attachOnRequestHandlers(self._connection,self._requestBrackets),ProtocolAdapter.initialized(self._connection),result}).catch(function(error){return console.error("Starting client failed because :",error),console.error("Couldn't start client :",self._clientName),error}))},LanguageClient.prototype.stop=function(){var self=this;return self._initialized=!1,self._connection?ProtocolAdapter.shutdown(self._connection).then(function(){ProtocolAdapter.exit(self._connection),self._connection.dispose(),self._connection=null}):Promise.resolve(!0)},LanguageClient.prototype.request=function(params){var messageParams=params.params;if(messageParams&&messageParams.messageType===MESSAGE_TYPE.BRACKETS){if(!messageParams.type)return console.log("Invalid brackets request"),Promise.reject();var requestHandler=this._onRequestHandler[messageParams.type];return validateHandler(requestHandler)?requestHandler.call(null,messageParams.params):(console.log("No handler provided for brackets request type : ",messageParams.type),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)return void console.log("Invalid brackets notification");var notificationHandlers=this._onNotificationHandlers[messageParams.type];notificationHandlers&&Array.isArray(notificationHandlers)&¬ificationHandlers.length?notificationHandlers.forEach(function(handler){validateHandler(handler)&&handler.call(null,messageParams.params)}):console.log("No handlers provided for brackets notification type : ",messageParams.type)}else ProtocolAdapter.processNotification(this._connection,params)},LanguageClient.prototype.addOnRequestHandler=function(type,handler){validateHandler(handler)&&(this._onRequestHandler[type]=handler)},LanguageClient.prototype.addOnNotificationHandler=function(type,handler){validateHandler(handler)&&(this._onNotificationHandlers[type]||(this._onNotificationHandlers[type]=[]),this._onNotificationHandlers[type].push(handler))},LanguageClient.prototype._init=function(domainManager){this._bracketsInterface=new NodeToBracketsInterface(domainManager,this._clientName),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)}]),this._notifyBrackets=this._bracketsInterface.createInterface(ToolingInfo.LANGUAGE_SERVICE.NOTIFY),this._requestBrackets=this._bracketsInterface.createInterface(ToolingInfo.LANGUAGE_SERVICE.REQUEST,!0)},exports.LanguageClient=LanguageClient;