11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT license.
33
4- import { commands , Extension , ExtensionContext , extensions , tasks , Uri , workspace } from "vscode" ;
5- import { dispose as disposeTelemetryWrapper , initializeFromJsonFile , instrumentOperation , sendInfo } from "vscode-extension-telemetry-wrapper" ;
4+
5+ import * as path from "path" ;
6+ import { commands , Diagnostic , Extension , ExtensionContext , extensions , languages , tasks , TextDocument , TextEditor , Uri , window , workspace } from "vscode" ;
7+ import { dispose as disposeTelemetryWrapper , initializeFromJsonFile , instrumentOperation , instrumentOperationAsVsCodeCommand , sendInfo } from "vscode-extension-telemetry-wrapper" ;
68import { Commands , contextManager } from "../extension.bundle" ;
79import { BuildTaskProvider } from "./tasks/build/buildTaskProvider" ;
8- import { Context , ExtensionName } from "./constants" ;
10+ import { buildFiles , Context , ExtensionName } from "./constants" ;
911import { LibraryController } from "./controllers/libraryController" ;
1012import { ProjectController } from "./controllers/projectController" ;
1113import { init as initExpService } from "./ExperimentationService" ;
@@ -40,6 +42,28 @@ async function activateExtension(_operationId: string, context: ExtensionContext
4042 context . subscriptions . push ( syncHandler ) ;
4143 context . subscriptions . push ( tasks . registerTaskProvider ( ExportJarTaskProvider . exportJarType , new ExportJarTaskProvider ( ) ) ) ;
4244 context . subscriptions . push ( tasks . registerTaskProvider ( BuildTaskProvider . type , new BuildTaskProvider ( ) ) ) ;
45+
46+ context . subscriptions . push ( window . onDidChangeActiveTextEditor ( ( e : TextEditor | undefined ) => {
47+ setContextForReloadProject ( e ?. document ) ;
48+ } ) ) ;
49+ context . subscriptions . push ( languages . onDidChangeDiagnostics ( ( ) => {
50+ setContextForReloadProject ( window . activeTextEditor ?. document ) ;
51+ } ) ) ;
52+ instrumentOperationAsVsCodeCommand ( Commands . JAVA_PROJECT_RELOAD_ACTIVE_FILE , ( uri ?: Uri ) => {
53+ if ( ! uri ) {
54+ const activeDocument = window . activeTextEditor ?. document ;
55+ if ( ! activeDocument ) {
56+ return ;
57+ }
58+ uri = activeDocument . uri ;
59+ }
60+
61+ if ( ! buildFiles . includes ( path . basename ( uri . fsPath ) ) ) {
62+ return ;
63+ }
64+
65+ commands . executeCommand ( Commands . JAVA_PROJECT_CONFIGURATION_UPDATE , uri ) ;
66+ } ) ;
4367}
4468
4569// this method is called when your extension is deactivated
@@ -61,3 +85,23 @@ function addExtensionChangeListener(context: ExtensionContext): void {
6185 context . subscriptions . push ( extensionChangeListener ) ;
6286 }
6387}
88+
89+ /**
90+ * Set the context value when reload diagnostic is detected for the active
91+ * build file.
92+ */
93+ function setContextForReloadProject ( document : TextDocument | undefined ) : void {
94+ if ( ! document || ! buildFiles . includes ( path . basename ( document . fileName ) ) ) {
95+ contextManager . setContextValue ( Context . RELOAD_PROJECT_ACTIVE , false ) ;
96+ return ;
97+ }
98+
99+ const diagnostics : Diagnostic [ ] = languages . getDiagnostics ( document . uri ) ;
100+ for ( const diagnostic of diagnostics ) {
101+ if ( diagnostic . message . startsWith ( "The build file has been changed" ) ) {
102+ contextManager . setContextValue ( Context . RELOAD_PROJECT_ACTIVE , true ) ;
103+ return ;
104+ }
105+ }
106+ contextManager . setContextValue ( Context . RELOAD_PROJECT_ACTIVE , false ) ;
107+ }
0 commit comments