@@ -7,7 +7,7 @@ import { prepareExecutable, awaitServerConnection } from "./javaServerStarter";
7
7
import { getJavaConfig , applyWorkspaceEdit } from "./extension" ;
8
8
import { LanguageClientOptions , Position as LSPosition , Location as LSLocation , MessageType , TextDocumentPositionParams , ConfigurationRequest , ConfigurationParams } from "vscode-languageclient" ;
9
9
import { LanguageClient , StreamInfo } from "vscode-languageclient/node" ;
10
- import { CompileWorkspaceRequest , CompileWorkspaceStatus , SourceAttachmentRequest , SourceAttachmentResult , SourceAttachmentAttribute , ProjectConfigurationUpdateRequest , FeatureStatus , StatusNotification , ProgressReportNotification , ActionableNotification , ExecuteClientCommandRequest , ServerNotification , EventNotification , EventType , LinkLocation , FindLinks , GradleCompatibilityInfo } from "./protocol" ;
10
+ import { CompileWorkspaceRequest , CompileWorkspaceStatus , SourceAttachmentRequest , SourceAttachmentResult , SourceAttachmentAttribute , ProjectConfigurationUpdateRequest , FeatureStatus , StatusNotification , ProgressReportNotification , ActionableNotification , ExecuteClientCommandRequest , ServerNotification , EventNotification , EventType , LinkLocation , FindLinks , GradleCompatibilityInfo , UpgradeGradleWrapperInfo } from "./protocol" ;
11
11
import { setGradleWrapperChecksum , excludeProjectSettingsFiles , ServerMode } from "./settings" ;
12
12
import { onExtensionChange , collectBuildFilePattern } from "./plugin" ;
13
13
import { activationProgressNotification , serverTaskPresenter } from "./serverTaskPresenter" ;
@@ -35,6 +35,7 @@ import { pomCodeActionMetadata, PomCodeActionProvider } from "./pom/pomCodeActio
35
35
import { findRuntimes , IJavaRuntime } from "jdk-utils" ;
36
36
import { snippetCompletionProvider } from "./snippetCompletionProvider" ;
37
37
import { JavaInlayHintsProvider } from "./inlayHintsProvider" ;
38
+ import { gradleCodeActionMetadata , GradleCodeActionProvider } from "./gradle/gradleCodeActionProvider" ;
38
39
39
40
const extensionName = 'Language Support for Java' ;
40
41
const GRADLE_CHECKSUM = "gradle/checksum/prompt" ;
@@ -184,7 +185,22 @@ export class StandardLanguageClient {
184
185
} else {
185
186
options . push ( USE_JAVA + runtimes [ 0 ] . version . major + AS_GRADLE_JVM ) ;
186
187
}
187
- this . showGradleCompatibilityIssueNotification ( info . message , options , info . projectUri , runtimes [ 0 ] ?. homedir ) ;
188
+ this . showGradleCompatibilityIssueNotification ( info . message , options , info . projectUri , info . recommendedGradleVersion , runtimes [ 0 ] ?. homedir ) ;
189
+ break ;
190
+ case EventType . UpgradeGradleWrapper :
191
+ const neverShow : boolean | undefined = context . globalState . get < boolean > ( "java.neverShowUpgradeWrapperNotification" ) ;
192
+ if ( ! neverShow ) {
193
+ const upgradeInfo = notification . data as UpgradeGradleWrapperInfo ;
194
+ const option = `Upgrade to ${ upgradeInfo . recommendedGradleVersion } ` ;
195
+ window . showWarningMessage ( upgradeInfo . message , option , "Don't show again" ) . then ( async ( choice ) => {
196
+ if ( choice === option ) {
197
+ await upgradeGradle ( upgradeInfo . projectUri , upgradeInfo . recommendedGradleVersion ) ;
198
+ } else if ( choice === "Don't show again" ) {
199
+ context . globalState . update ( "java.neverShowUpgradeWrapperNotification" , true ) ;
200
+ }
201
+ } ) ;
202
+ }
203
+ break ;
188
204
default :
189
205
break ;
190
206
}
@@ -258,7 +274,7 @@ export class StandardLanguageClient {
258
274
this . status = ClientStatus . Initialized ;
259
275
}
260
276
261
- private showGradleCompatibilityIssueNotification ( message : string , options : string [ ] , projectUri : string , newJavaHome : string ) {
277
+ private showGradleCompatibilityIssueNotification ( message : string , options : string [ ] , projectUri : string , gradleVersion : string , newJavaHome : string ) {
262
278
window . showErrorMessage ( message + " [Learn More](https://docs.gradle.org/current/userguide/compatibility.html)" , ...options ) . then ( async ( choice ) => {
263
279
if ( choice === GET_JDK ) {
264
280
commands . executeCommand ( Commands . OPEN_BROWSER , Uri . parse ( getJdkUrl ( ) ) ) ;
@@ -267,31 +283,7 @@ export class StandardLanguageClient {
267
283
commands . executeCommand ( "workbench.action.openSettings" , GRADLE_IMPORT_JVM ) ;
268
284
commands . executeCommand ( Commands . IMPORT_PROJECTS_CMD ) ;
269
285
} else if ( choice . startsWith ( UPGRADE_GRADLE ) ) {
270
- const useWrapper = workspace . getConfiguration ( ) . get < boolean > ( "java.import.gradle.wrapper.enabled" ) ;
271
- if ( ! useWrapper ) {
272
- await workspace . getConfiguration ( ) . update ( "java.import.gradle.wrapper.enabled" , true , ConfigurationTarget . Workspace ) ;
273
- }
274
- const result = await window . withProgress ( {
275
- location : ProgressLocation . Notification ,
276
- title : "Upgrading Gradle wrapper..." ,
277
- cancellable : true ,
278
- } , ( _progress , token ) => {
279
- return commands . executeCommand ( Commands . EXECUTE_WORKSPACE_COMMAND , "java.project.upgradeGradle" , projectUri , token ) ;
280
- } ) ;
281
- if ( result ) {
282
- const propertiesFile = path . join ( Uri . parse ( projectUri ) . fsPath , "gradle" , "wrapper" , "gradle-wrapper.properties" ) ;
283
- if ( fse . pathExists ( propertiesFile ) ) {
284
- const content = await fse . readFile ( propertiesFile ) ;
285
- const offset = content . toString ( ) . indexOf ( "distributionUrl" ) ;
286
- if ( offset >= 0 ) {
287
- const document = await workspace . openTextDocument ( propertiesFile ) ;
288
- const position = document . positionAt ( offset ) ;
289
- const distributionUrlRange = document . getWordRangeAtPosition ( position ) ;
290
- window . showTextDocument ( document , { selection : new Range ( distributionUrlRange . start , new Position ( distributionUrlRange . start . line + 1 , 0 ) ) } ) ;
291
- }
292
- }
293
- commands . executeCommand ( Commands . IMPORT_PROJECTS_CMD ) ;
294
- }
286
+ await upgradeGradle ( projectUri , gradleVersion ) ;
295
287
}
296
288
} ) ;
297
289
}
@@ -504,6 +496,11 @@ export class StandardLanguageClient {
504
496
pattern : "**/pom.xml"
505
497
} , new PomCodeActionProvider ( context ) , pomCodeActionMetadata ) ;
506
498
499
+ languages . registerCodeActionsProvider ( {
500
+ scheme : "file" ,
501
+ pattern : "**/gradle/wrapper/gradle-wrapper.properties"
502
+ } , new GradleCodeActionProvider ( context ) , gradleCodeActionMetadata ) ;
503
+
507
504
if ( languages . registerInlayHintsProvider ) {
508
505
context . subscriptions . push ( languages . registerInlayHintsProvider ( [
509
506
{ scheme : "file" , language : "java" , pattern : "**/*.java" } ,
@@ -634,3 +631,31 @@ export function showNoLocationFound(message: string): void {
634
631
message
635
632
) ;
636
633
}
634
+
635
+ export async function upgradeGradle ( projectUri : string , version ?: string ) : Promise < void > {
636
+ const useWrapper = workspace . getConfiguration ( ) . get < boolean > ( "java.import.gradle.wrapper.enabled" ) ;
637
+ if ( ! useWrapper ) {
638
+ await workspace . getConfiguration ( ) . update ( "java.import.gradle.wrapper.enabled" , true , ConfigurationTarget . Workspace ) ;
639
+ }
640
+ const result = await window . withProgress ( {
641
+ location : ProgressLocation . Notification ,
642
+ title : "Upgrading Gradle wrapper..." ,
643
+ cancellable : true ,
644
+ } , ( _progress , token ) => {
645
+ return commands . executeCommand ( Commands . EXECUTE_WORKSPACE_COMMAND , "java.project.upgradeGradle" , projectUri , version , token ) ;
646
+ } ) ;
647
+ if ( result ) {
648
+ const propertiesFile = path . join ( Uri . parse ( projectUri ) . fsPath , "gradle" , "wrapper" , "gradle-wrapper.properties" ) ;
649
+ if ( fse . pathExists ( propertiesFile ) ) {
650
+ const content = await fse . readFile ( propertiesFile ) ;
651
+ const offset = content . toString ( ) . indexOf ( "distributionUrl" ) ;
652
+ if ( offset >= 0 ) {
653
+ const document = await workspace . openTextDocument ( propertiesFile ) ;
654
+ const position = document . positionAt ( offset ) ;
655
+ const distributionUrlRange = document . getWordRangeAtPosition ( position ) ;
656
+ window . showTextDocument ( document , { selection : new Range ( distributionUrlRange . start , new Position ( distributionUrlRange . start . line + 1 , 0 ) ) } ) ;
657
+ }
658
+ }
659
+ commands . executeCommand ( Commands . IMPORT_PROJECTS_CMD ) ;
660
+ }
661
+ }
0 commit comments