forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProtocolAdapter.js
More file actions
1 lines (1 loc) · 9.34 KB
/
ProtocolAdapter.js
File metadata and controls
1 lines (1 loc) · 9.34 KB
1
var protocol=require("vscode-languageserver-protocol"),Utils=require("./Utils"),ToolingInfo=LanguageClientInfo.toolingInfo,MESSAGE_FORMAT={BRACKETS:"brackets",LSP:"lsp"};function _constructParamsAndRelay(relay,type,params){var _params=null,handler=null;switch(params.format===MESSAGE_FORMAT.LSP&&(params.format=void 0,_params=JSON.parse(JSON.stringify(params))),type){case ToolingInfo.LANGUAGE_SERVICE.CUSTOM_REQUEST:return sendCustomRequest(relay,params.type,params.params);case ToolingInfo.LANGUAGE_SERVICE.CUSTOM_NOTIFICATION:sendCustomNotification(relay,params.type,params.params);break;case ToolingInfo.SERVICE_REQUESTS.SHOW_SELECT_MESSAGE:case ToolingInfo.SERVICE_REQUESTS.REGISTRATION_REQUEST:case ToolingInfo.SERVICE_REQUESTS.UNREGISTRATION_REQUEST:case ToolingInfo.SERVICE_REQUESTS.PROJECT_FOLDERS_REQUEST:return relay(_params={type:type,params:params});case ToolingInfo.SERVICE_NOTIFICATIONS.SHOW_MESSAGE:case ToolingInfo.SERVICE_NOTIFICATIONS.LOG_MESSAGE:case ToolingInfo.SERVICE_NOTIFICATIONS.TELEMETRY:case ToolingInfo.SERVICE_NOTIFICATIONS.DIAGNOSTICS:relay(_params={type:type,params:params});break;case ToolingInfo.SYNCHRONIZE_EVENTS.DOCUMENT_OPENED:didOpenTextDocument(relay,_params=_params||{textDocument:{uri:Utils.pathToUri(params.filePath),languageId:params.languageId,version:1,text:params.fileContent}});break;case ToolingInfo.SYNCHRONIZE_EVENTS.DOCUMENT_CHANGED:didChangeTextDocument(relay,_params=_params||{textDocument:{uri:Utils.pathToUri(params.filePath),version:1},contentChanges:[{text:params.fileContent}]});break;case ToolingInfo.SYNCHRONIZE_EVENTS.DOCUMENT_SAVED:_params||(_params={textDocument:{uri:Utils.pathToUri(params.filePath)}},params.fileContent&&(_params.text=params.fileContent)),didSaveTextDocument(relay,_params);break;case ToolingInfo.SYNCHRONIZE_EVENTS.DOCUMENT_CLOSED:didCloseTextDocument(relay,_params=_params||{textDocument:{uri:Utils.pathToUri(params.filePath)}});break;case ToolingInfo.SYNCHRONIZE_EVENTS.PROJECT_FOLDERS_CHANGED:var foldersAdded=params.foldersAdded||[],foldersRemoved=params.foldersRemoved||[];foldersAdded=Utils.convertToWorkspaceFolders(foldersAdded),foldersRemoved=Utils.convertToWorkspaceFolders(foldersRemoved),didChangeWorkspaceFolders(relay,_params=_params||{event:{added:foldersAdded,removed:foldersRemoved}});break;case ToolingInfo.FEATURES.CODE_HINTS:handler=completion;case ToolingInfo.FEATURES.PARAMETER_HINTS:handler=handler||signatureHelp;case ToolingInfo.FEATURES.JUMP_TO_DECLARATION:handler=handler||gotoDeclaration;case ToolingInfo.FEATURES.JUMP_TO_DEFINITION:handler=handler||gotoDefinition;case ToolingInfo.FEATURES.JUMP_TO_IMPL:return(handler=handler||gotoImplementation)(relay,_params=_params||{textDocument:{uri:Utils.pathToUri(params.filePath)},position:Utils.convertToLSPPosition(params.cursorPos)});case ToolingInfo.FEATURES.CODE_HINT_INFO:return completionItemResolve(relay,params);case ToolingInfo.FEATURES.FIND_REFERENCES:return findReferences(relay,_params=_params||{textDocument:{uri:Utils.pathToUri(params.filePath)},position:Utils.convertToLSPPosition(params.cursorPos),context:{includeDeclaration:params.includeDeclaration}});case ToolingInfo.FEATURES.DOCUMENT_SYMBOLS:return documentSymbol(relay,_params=_params||{textDocument:{uri:Utils.pathToUri(params.filePath)}});case ToolingInfo.FEATURES.PROJECT_SYMBOLS:return workspaceSymbol(relay,_params=_params||{query:params.query})}}function onCustom(connection,type,handler){connection.onNotification(type,handler)}function sendCustomRequest(connection,type,params){return connection.sendRequest(type,params)}function sendCustomNotification(connection,type,params){connection.sendNotification(type,params)}function didOpenTextDocument(connection,params){connection.sendNotification(protocol.DidOpenTextDocumentNotification.type,params)}function didChangeTextDocument(connection,params){connection.sendNotification(protocol.DidChangeTextDocumentNotification.type,params)}function didCloseTextDocument(connection,params){connection.sendNotification(protocol.DidCloseTextDocumentNotification.type,params)}function didSaveTextDocument(connection,params){connection.sendNotification(protocol.DidSaveTextDocumentNotification.type,params)}function didChangeWorkspaceFolders(connection,params){connection.sendNotification(protocol.DidChangeWorkspaceFoldersNotification.type,params)}function completion(connection,params){return connection.sendRequest(protocol.CompletionRequest.type,params)}function completionItemResolve(connection,params){return connection.sendRequest(protocol.CompletionResolveRequest.type,params)}function signatureHelp(connection,params){return connection.sendRequest(protocol.SignatureHelpRequest.type,params)}function gotoDefinition(connection,params){return connection.sendRequest(protocol.DefinitionRequest.type,params)}function gotoDeclaration(connection,params){return connection.sendRequest(protocol.DeclarationRequest.type,params)}function gotoImplementation(connection,params){return connection.sendRequest(protocol.ImplementationRequest.type,params)}function findReferences(connection,params){return connection.sendRequest(protocol.ReferencesRequest.type,params)}function documentSymbol(connection,params){return connection.sendRequest(protocol.DocumentSymbolRequest.type,params)}function workspaceSymbol(connection,params){return connection.sendRequest(protocol.WorkspaceSymbolRequest.type,params)}function initialize(connection,params){var rootPath=params.rootPath,workspaceFolders=params.rootPaths;!rootPath&&workspaceFolders&&Array.isArray(workspaceFolders)&&(rootPath=workspaceFolders[0]),workspaceFolders||(workspaceFolders=[rootPath]),workspaceFolders.length&&(workspaceFolders=Utils.convertToWorkspaceFolders(workspaceFolders));var _params={rootPath:rootPath,rootUri:Utils.pathToUri(rootPath),processId:process.pid,capabilities:params.capabilities,workspaceFolders:workspaceFolders};return connection.sendRequest(protocol.InitializeRequest.type,_params)}function initialized(connection){connection.sendNotification(protocol.InitializedNotification.type)}function shutdown(connection){return connection.sendRequest(protocol.ShutdownRequest.type)}function exit(connection){connection.sendNotification(protocol.ExitNotification.type)}function processRequest(connection,message){return _constructParamsAndRelay(connection,message.type,message.params)}function processNotification(connection,message){_constructParamsAndRelay(connection,message.type,message.params)}function attachOnNotificationHandlers(connection,handler){function _callbackFactory(type){switch(type){case protocol.ShowMessageNotification.type:return _constructParamsAndRelay.bind(null,handler,ToolingInfo.SERVICE_NOTIFICATIONS.SHOW_MESSAGE);case protocol.LogMessageNotification.type:return _constructParamsAndRelay.bind(null,handler,ToolingInfo.SERVICE_NOTIFICATIONS.LOG_MESSAGE);case protocol.TelemetryEventNotification.type:return _constructParamsAndRelay.bind(null,handler,ToolingInfo.SERVICE_NOTIFICATIONS.TELEMETRY);case protocol.PublishDiagnosticsNotification.type:return _constructParamsAndRelay.bind(null,handler,ToolingInfo.SERVICE_NOTIFICATIONS.DIAGNOSTICS)}}connection.onNotification(protocol.ShowMessageNotification.type,_callbackFactory(protocol.ShowMessageNotification.type)),connection.onNotification(protocol.LogMessageNotification.type,_callbackFactory(protocol.LogMessageNotification.type)),connection.onNotification(protocol.TelemetryEventNotification.type,_callbackFactory(protocol.TelemetryEventNotification.type)),connection.onNotification(protocol.PublishDiagnosticsNotification.type,_callbackFactory(protocol.PublishDiagnosticsNotification.type)),connection.onNotification(function(type,params){var _params;handler({type:type,params:params})})}function attachOnRequestHandlers(connection,handler){function _callbackFactory(type){switch(type){case protocol.ShowMessageRequest.type:return _constructParamsAndRelay.bind(null,handler,ToolingInfo.SERVICE_REQUESTS.SHOW_SELECT_MESSAGE);case protocol.RegistrationRequest.type:return _constructParamsAndRelay.bind(null,handler,ToolingInfo.SERVICE_REQUESTS.REGISTRATION_REQUEST);case protocol.UnregistrationRequest.type:return _constructParamsAndRelay.bind(null,handler,ToolingInfo.SERVICE_REQUESTS.UNREGISTRATION_REQUEST);case protocol.WorkspaceFoldersRequest.type:return _constructParamsAndRelay.bind(null,handler,ToolingInfo.SERVICE_REQUESTS.PROJECT_FOLDERS_REQUEST)}}connection.onRequest(protocol.ShowMessageRequest.type,_callbackFactory(protocol.ShowMessageRequest.type)),connection.onRequest(protocol.RegistrationRequest.type,_callbackFactory(protocol.RegistrationRequest.type)),connection.onRequest("client/registerFeature",_callbackFactory(protocol.RegistrationRequest.type)),connection.onRequest(protocol.UnregistrationRequest.type,_callbackFactory(protocol.UnregistrationRequest.type)),connection.onRequest("client/unregisterFeature",_callbackFactory(protocol.UnregistrationRequest.type)),connection.onRequest(protocol.WorkspaceFoldersRequest.type,_callbackFactory(protocol.WorkspaceFoldersRequest.type)),connection.onRequest(function(type,params){var _params;return handler({type:type,params:params})})}exports.initialize=initialize,exports.initialized=initialized,exports.shutdown=shutdown,exports.exit=exit,exports.onCustom=onCustom,exports.sendCustomRequest=sendCustomRequest,exports.sendCustomNotification=sendCustomNotification,exports.processRequest=processRequest,exports.processNotification=processNotification,exports.attachOnNotificationHandlers=attachOnNotificationHandlers,exports.attachOnRequestHandlers=attachOnRequestHandlers;