33
44import { CancellationToken , CodeLens , CodeLensProvider , ProviderResult , Range , TextDocument } from "vscode" ;
55import { Commands } from "../commands" ;
6- import { buildFixPrompt } from "./utility" ;
6+ import { buildFixPrompt , normalizePath , toFirstLine } from "./utility" ;
77import issueManager from "./issueManager" ;
88import metadataManager from "./metadataManager" ;
99import { Upgrade } from "../constants" ;
10+ import pomDataManager from "./pomDataManager" ;
1011
1112export default class UpgradeCodeLensProvider implements CodeLensProvider {
1213 provideCodeLenses ( document : TextDocument , _token : CancellationToken ) : ProviderResult < CodeLens [ ] > {
1314 const documentPath = document . uri . toString ( ) ;
1415 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 ) => {
1619 const metadata = metadataManager . getMetadataById ( issue . packageId ) ;
1720 if ( ! metadata ) {
1821 return ;
1922 }
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+ } ) ;
2646
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
3250 if ( a . command ?. title === `Upgrade ${ Upgrade . DIAGNOSTICS_NAME_FOR_JAVA_ENGINE } ` ) return - 1 ;
3351 if ( b . command ?. title === `Upgrade ${ Upgrade . DIAGNOSTICS_NAME_FOR_JAVA_ENGINE } ` ) return 1 ;
3452 return ( a . command ?. title ?? "" ) < ( b . command ?. title ?? "" ) ? - 1 : 1 ;
3553 } )
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+ ] ;
3758 }
3859}
0 commit comments