11/* eslint-disable @typescript-eslint/ban-ts-comment */
2- import { CodeLens , CodeLensProvider , Disposable , Event , EventEmitter , ExtensionContext , TextDocument , Uri , languages } from "vscode" ;
2+ import { CodeLens , CodeLensProvider , Command , Range , Disposable , Event , EventEmitter , ExtensionContext , TextDocument , Uri , languages } from "vscode" ;
33import { Inspection } from "../Inspection" ;
44import { InspectionRenderer } from "./InspectionRenderer" ;
55import { logger , uncapitalize } from "../../../copilot/utils" ;
66import { COMMAND_IGNORE_INSPECTIONS , COMMAND_FIX_INSPECTION } from "../commands" ;
77import { capitalize } from "lodash" ;
8+ import _ from "lodash" ;
89
910export class CodeLensRenderer implements InspectionRenderer {
10- private readonly codeLenses : Map < Uri , CodeLens [ ] > = new Map ( ) ;
11+ private readonly codeLenses : Map < Uri , InspectionCodeLens [ ] > = new Map ( ) ;
1112 private readonly provider = new InspectionCodeLensProvider ( this . codeLenses ) ;
1213 private disposableRegistry : Disposable | undefined ;
1314
@@ -41,27 +42,28 @@ export class CodeLensRenderer implements InspectionRenderer {
4142 if ( inspections . length < 1 || ! this . codeLenses ) {
4243 return ;
4344 }
44- const newCodeLenses : CodeLens [ ] = inspections . flatMap ( s => CodeLensRenderer . toCodeLenses ( document , s ) ) ;
45- const newCodeLensesMessages = newCodeLenses . map ( c => c . command ?. title . trim ( ) ) ;
46- const existingCodeLenses = this . codeLenses . get ( document . uri ) ?? [ ] ;
47- const leftCodeLenses = existingCodeLenses . filter ( c => ! newCodeLensesMessages . includes ( c . command ?. title . trim ( ) ) ) ;
48- newCodeLenses . push ( ...leftCodeLenses ) ;
49- this . codeLenses . set ( document . uri , newCodeLenses ) ;
45+ const oldItems = this . codeLenses . get ( document . uri ) ?? [ ] ;
46+ const oldIds : string [ ] = _ . uniq ( oldItems ) . map ( c => c . inspection . id ) ;
47+ const newIds : string [ ] = inspections . map ( i => i . id ) ;
48+ const toKeep : InspectionCodeLens [ ] = _ . intersection ( oldIds , newIds ) . map ( id => oldItems . find ( c => c . inspection . id === id ) ! ) ?? [ ] ;
49+ const toAdd : InspectionCodeLens [ ] = _ . difference ( newIds , oldIds ) . map ( id => inspections . find ( i => i . id === id ) ! )
50+ . flatMap ( i => CodeLensRenderer . toCodeLenses ( document , i ) ) ;
51+ this . codeLenses . set ( document . uri , [ ...toKeep , ...toAdd ] ) ;
5052 this . provider . refresh ( ) ;
5153 }
5254
53- private static toCodeLenses ( document : TextDocument , inspection : Inspection ) : CodeLens [ ] {
55+ private static toCodeLenses ( document : TextDocument , inspection : Inspection ) : InspectionCodeLens [ ] {
5456 const codeLenses = [ ] ;
5557 const range = Inspection . getIndicatorRangeOfInspection ( inspection . problem ) ;
56- const inspectionCodeLens = new CodeLens ( range , {
58+ const inspectionCodeLens = new InspectionCodeLens ( inspection , range , {
5759 title : capitalize ( inspection . solution ) ,
5860 tooltip : inspection . problem . description ,
5961 command : COMMAND_FIX_INSPECTION ,
6062 arguments : [ inspection . problem , inspection . solution , 'codelenses' ]
6163 } ) ;
6264 codeLenses . push ( inspectionCodeLens ) ;
6365
64- const ignoreCodeLens = new CodeLens ( range , {
66+ const ignoreCodeLens = new InspectionCodeLens ( inspection , range , {
6567 title : 'Ignore' ,
6668 tooltip : `Ignore "${ uncapitalize ( inspection . problem . description ) } "` ,
6769 command : COMMAND_IGNORE_INSPECTIONS ,
@@ -72,6 +74,12 @@ export class CodeLensRenderer implements InspectionRenderer {
7274 }
7375}
7476
77+ class InspectionCodeLens extends CodeLens {
78+ public constructor ( public readonly inspection : Inspection , range : Range , command ?: Command ) {
79+ super ( range , command ) ;
80+ }
81+ }
82+
7583class InspectionCodeLensProvider implements CodeLensProvider {
7684 private readonly emitter : EventEmitter < void > = new EventEmitter < void > ( ) ;
7785 public readonly onDidChangeCodeLenses : Event < void > = this . emitter . event ;
0 commit comments