1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT license.
3
3
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" ;
6
8
import { Commands , contextManager } from "../extension.bundle" ;
7
9
import { BuildTaskProvider } from "./tasks/build/buildTaskProvider" ;
8
- import { Context , ExtensionName } from "./constants" ;
10
+ import { buildFiles , Context , ExtensionName } from "./constants" ;
9
11
import { LibraryController } from "./controllers/libraryController" ;
10
12
import { ProjectController } from "./controllers/projectController" ;
11
13
import { init as initExpService } from "./ExperimentationService" ;
@@ -40,6 +42,28 @@ async function activateExtension(_operationId: string, context: ExtensionContext
40
42
context . subscriptions . push ( syncHandler ) ;
41
43
context . subscriptions . push ( tasks . registerTaskProvider ( ExportJarTaskProvider . exportJarType , new ExportJarTaskProvider ( ) ) ) ;
42
44
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_ALL , ( 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
+ } ) ;
43
67
}
44
68
45
69
// this method is called when your extension is deactivated
@@ -61,3 +85,23 @@ function addExtensionChangeListener(context: ExtensionContext): void {
61
85
context . subscriptions . push ( extensionChangeListener ) ;
62
86
}
63
87
}
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