3
3
4
4
import { CancellationToken , CodeLens , CodeLensProvider , ProviderResult , Range , TextDocument } from "vscode" ;
5
5
import { Commands } from "../commands" ;
6
- import { buildFixPrompt } from "./utility" ;
6
+ import { buildFixPrompt , normalizePath , toFirstLine } from "./utility" ;
7
7
import issueManager from "./issueManager" ;
8
8
import metadataManager from "./metadataManager" ;
9
9
import { Upgrade } from "../constants" ;
10
+ import pomDataManager from "./pomDataManager" ;
10
11
11
12
export default class UpgradeCodeLensProvider implements CodeLensProvider {
12
13
provideCodeLenses ( document : TextDocument , _token : CancellationToken ) : ProviderResult < CodeLens [ ] > {
13
14
const documentPath = document . uri . toString ( ) ;
14
15
const issues = issueManager . getIssues ( documentPath ) ;
15
- return Object . values ( issues ) . map ( ( issue ) => {
16
+ const topOfFileCodeLens : CodeLens [ ] = [ ] ;
17
+ const inlineCodeLens : CodeLens [ ] = [ ] ;
18
+ Object . values ( issues ) . forEach ( ( issue ) => {
16
19
const metadata = metadataManager . getMetadataById ( issue . packageId ) ;
17
20
if ( ! metadata ) {
18
21
return ;
19
22
}
20
- const codeLens = new CodeLens ( new Range ( 0 , 0 , 0 , 0 ) , {
21
- title : "Upgrade" ,
22
- command : Commands . VIEW_TRIGGER_JAVA_UPGRADE_TOOL ,
23
- tooltip : `Upgrade ${ metadata . name } with GitHub Copilot` ,
24
- arguments : [ buildFixPrompt ( issue ) ] ,
25
- } ) ;
23
+ const range = pomDataManager . getPomRange ( normalizePath ( documentPath ) , issue . packageId ) ;
24
+ if ( range ) {
25
+ const codeLens = new CodeLens (
26
+ toFirstLine ( range ) ,
27
+ {
28
+ title : "Upgrade" ,
29
+ command : Commands . VIEW_TRIGGER_JAVA_UPGRADE_TOOL ,
30
+ tooltip : `Upgrade ${ metadata . name } with GitHub Copilot` ,
31
+ arguments : [ buildFixPrompt ( issue ) ] ,
32
+ } ) ;
33
+ inlineCodeLens . push ( codeLens ) ;
34
+ } else {
35
+ const codeLens = new CodeLens (
36
+ new Range ( 0 , 0 , 0 , 0 ) ,
37
+ {
38
+ title : "Upgrade" ,
39
+ command : Commands . VIEW_TRIGGER_JAVA_UPGRADE_TOOL ,
40
+ tooltip : `Upgrade ${ metadata . name } with GitHub Copilot` ,
41
+ arguments : [ buildFixPrompt ( issue ) ] ,
42
+ } ) ;
43
+ topOfFileCodeLens . push ( codeLens ) ;
44
+ }
45
+ } ) ;
26
46
27
- return codeLens ;
28
- } )
29
- . filter ( ( x ) : x is CodeLens => Boolean ( x ) )
30
- . sort ( ( a , b ) => {
31
- // always show Java engine upgrade first
47
+ return [
48
+ ...topOfFileCodeLens . sort ( ( a , b ) => {
49
+ // always show Java engine upgrade first, if any
32
50
if ( a . command ?. title === `Upgrade ${ Upgrade . DIAGNOSTICS_NAME_FOR_JAVA_ENGINE } ` ) return - 1 ;
33
51
if ( b . command ?. title === `Upgrade ${ Upgrade . DIAGNOSTICS_NAME_FOR_JAVA_ENGINE } ` ) return 1 ;
34
52
return ( a . command ?. title ?? "" ) < ( b . command ?. title ?? "" ) ? - 1 : 1 ;
35
53
} )
36
- . slice ( 0 , 1 ) ; // give 1 Code Lens action at most
54
+ // give 1 top-of-file Code Lens action at most
55
+ . slice ( 0 , 1 ) ,
56
+ ...inlineCodeLens ,
57
+ ] ;
37
58
}
38
59
}
0 commit comments