forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJsonLsp.js
More file actions
1 lines (1 loc) · 3.66 KB
/
Copy pathJsonLsp.js
File metadata and controls
1 lines (1 loc) · 3.66 KB
1
define(function(require,exports,module){const EditorManager=require("editor/EditorManager"),ProjectManager=require("project/ProjectManager"),NodeConnector=require("NodeConnector"),PreferencesManager=require("preferences/PreferencesManager"),Strings=require("strings"),SchemaAssociations=require("./schemaAssociations"),SERVER_ID="json",SUPPORTED_LANGUAGES=["json"],PREF_JSON_CODE_INTELLIGENCE="codeIntelligence."+SERVER_ID;PreferencesManager.definePreference(PREF_JSON_CODE_INTELLIGENCE,"boolean",!0,{description:Strings.DESCRIPTION_JSON_CODE_INTELLIGENCE});let lspClientPromise=null,client=null,registered=!1,starting=!1,pendingRepoint=!1,initErrorReported=!1,_schemaAssociationsOverride=null;function canRun(){return Phoenix.isNativeApp&&NodeConnector.isNodeAvailable()}function loadLSPClient(){return lspClientPromise||(lspClientPromise=new Promise(function(resolve,reject){require(["languageTools/LSPClient"],resolve,function(){setTimeout(function(){require(["languageTools/LSPClient"],resolve,reject)},500)})})),lspClientPromise}function waitForNodeReady(timeout){return new Promise(function(resolve){const deadline=Date.now()+timeout;!function check(){NodeConnector.isNodeReady()?resolve(!0):Date.now()>deadline?resolve(!1):setTimeout(check,300)}()})}function _pushConfiguration(){client&&client.sendCustomNotification("workspace/didChangeConfiguration",{settings:{json:{validate:{enable:!0},format:{enable:!1},schemas:_schemaAssociationsOverride||SchemaAssociations.SCHEMA_ASSOCIATIONS}}})}async function start(){if(registered||!canRun())return;const ready=await waitForNodeReady(3e4);if(!ready)return void console.error("[JSONSupport] Node not ready - JSON LSP disabled");const LSPClient=await loadLSPClient();LSPClient.on(LSPClient.EVENT_LANGUAGE_SERVER_STARTED+".jsonSupport",function(_evt,data){data.serverId===SERVER_ID&&_pushConfiguration()}),(client=await LSPClient.registerLanguageServer({serverId:SERVER_ID,command:"vscode-json-language-server",args:["--stdio"],languages:SUPPORTED_LANGUAGES,languageIdMap:{json:"jsonc"},initializationOptions:{provideFormatter:!1},completionSnippetSupport:!0,documentFilter:function(fullPath){const name=fullPath.substring(fullPath.lastIndexOf("/")+1);return!/^\.?(brackets|phcode)\.json$/.test(name)}}))&&(registered=!0,_pushConfiguration())}function _isServedLanguageActive(){const editor=EditorManager.getActiveEditor();return!!editor&&-1!==SUPPORTED_LANGUAGES.indexOf(editor.getLanguageForSelection().getId())}function _ensureServerForActiveEditor(){if(canRun()&&_isServedLanguageActive()&&!1!==PreferencesManager.get(PREF_JSON_CODE_INTELLIGENCE)){if(!registered){if(starting)return;return starting=!0,pendingRepoint=!1,void start().catch(function(err){initErrorReported||(initErrorReported=!0,window.logger&&window.logger.reportError(err,"[JSONSupport] JSON LSP init failed"))}).finally(function(){starting=!1})}pendingRepoint&&(pendingRepoint=!1,loadLSPClient().then(function(LSPClient){LSPClient.changeWorkspaceRoot(SERVER_ID)}))}}function init(){canRun()&&(loadLSPClient(),EditorManager.on("activeEditorChange.jsonSupport",_ensureServerForActiveEditor),_ensureServerForActiveEditor(),ProjectManager.on(ProjectManager.EVENT_PROJECT_OPEN+".jsonSupport",function(){pendingRepoint=!0,_ensureServerForActiveEditor()}),PreferencesManager.on("change",PREF_JSON_CODE_INTELLIGENCE,function(){!1!==PreferencesManager.get(PREF_JSON_CODE_INTELLIGENCE)&&_ensureServerForActiveEditor()}))}function _setTestSchemaAssociations(associations){_schemaAssociationsOverride=associations,_pushConfiguration()}exports.init=init,exports.canRun=canRun,exports.SERVER_ID=SERVER_ID,exports._setTestSchemaAssociations=_setTestSchemaAssociations,Phoenix.isTestWindow&&(exports._getClient=function(){return client})});