import { commands , Disposable , Event , Extension , extensions , Uri } from "vscode" ; import { Commands } from "../commands" ; import { Context , ExtensionName } from "../constants" ; import { contextManager } from "../contextManager" ; import { Settings } from "../settings" ; import { syncHandler } from "../syncHandler" ; import { LanguageServerMode } from "./LanguageServerMode" ; class LanguageServerApiManager implements Disposable { /**
     * undefined means a legacy version language server
     * null means the JDT.LS is not activated
     */ private serverMode : LanguageServerMode | null | undefined = null ; private extensionChangeListener : Disposable ; public async ready ( ) { await this . checkServerMode ( ) ; if ( this . serverMode === null || this . serverMode === LanguageServerMode . LightWeight ) { return false ; } if ( this . serverMode === LanguageServerMode . Hybrid ) { await new Promise < void > ( ( resolve ) : void => { extensions . getExtension ( "redhat.java" ) ! . exports . onDidServerModeChange ( resolve ) ; } ) ; } return true ; } public async initializeJavaLanguageServerApi ( forceActivate = true ) { if ( this . isLanguageServerActivated ( ) ) { return ; } if ( ! this . extensionChangeListener ) { this . extensionChangeListener = extensions . onDidChange ( ( ) => { if ( this . serverMode === null ) { commands . executeCommand ( Commands . VIEW_PACKAGE_REFRESH , false ) ; } } ) ; } const extension = extensions . getExtension ( ExtensionName . JAVA_LANGUAGE_SUPPORT ) ; if ( extension ) { contextManager . setContextValue ( Context . LANGUAGE_SUPPORT_INSTALLED , true ) ; if ( ! forceActivate ) { return ; } await extension . activate ( ) ; const extensionApi = extension . exports ; if ( ! extensionApi ) { return ; } this . serverMode = extensionApi . serverMode ; if ( this . serverMode === LanguageServerMode . Standard ) { syncHandler . updateFileWatcher ( Settings . autoRefresh ( ) ) ; } if ( extensionApi . onDidClasspathUpdate ) { const onDidClasspathUpdate = extensionApi . onDidClasspathUpdate ; contextManager . context . subscriptions . push ( onDidClasspathUpdate ( ( ) => { commands . executeCommand ( Commands . VIEW_PACKAGE_REFRESH , true ) ; syncHandler . updateFileWatcher ( Settings . autoRefresh ( ) ) ; } ) ) ; } if ( extensionApi . onDidServerModeChange ) { const onDidServerModeChange = extensionApi . onDidServerModeChange ; contextManager . context . subscriptions . push ( onDidServerModeChange ( ( mode ) => { if ( this . serverMode !== mode ) { if ( mode === LanguageServerMode . Hybrid ) { commands . executeCommand ( Commands . VIEW_PACKAGE_REFRESH , false ) ; } else if ( mode === LanguageServerMode . Standard ) { syncHandler . updateFileWatcher ( Settings . autoRefresh ( ) ) ; } this . serverMode = mode ; } } ) ) ; } if ( extensionApi . onDidProjectsImport ) { const onDidProjectsImport = extensionApi . onDidProjectsImport ; contextManager . context . subscriptions . push ( onDidProjectsImport ( ( ) => { commands . executeCommand ( Commands . VIEW_PACKAGE_REFRESH , true ) ; syncHandler . updateFileWatcher ( Settings . autoRefresh ( ) ) ; } ) ) ; } } } public dispose ( ) { this . extensionChangeListener . dispose ( ) ; } private isLanguageServerActivated ( ) { return this . serverMode !== null ; } private async checkServerMode ( ) { if ( ! this . isLanguageServerActivated ( ) ) { await this . initializeJavaLanguageServerApi ( ) ; } } } export const languageServerApiManager = new LanguageServerApiManager ( ) ;