11import * as vscode from 'vscode' ;
2- import { DevProxyInstall } from './types' ;
2+ import { DevProxyInstall } from './types' ;
33
44export const registerCodeActions = ( context : vscode . ExtensionContext ) => {
5- const devProxyInstall = context . globalState . get < DevProxyInstall > ( 'devProxyInstall' ) ;
6- if ( ! devProxyInstall ) {
7- return ;
8- }
9- const devProxyVersion = devProxyInstall . isBeta ? devProxyInstall . version . split ( '-' ) [ 0 ] : devProxyInstall . version ;
10- context . subscriptions . push (
11- vscode . languages . registerCodeActionsProvider ( 'json' , {
12- provideCodeActions : ( document , range , context , token ) => {
13- const diagnostic = context . diagnostics . find ( diagnostic => {
14- return diagnostic . code === 'invalidSchema' ;
15- } ) ;
16- if ( diagnostic ) {
17- const fix = new vscode . CodeAction ( 'Update schema' , vscode . CodeActionKind . QuickFix ) ;
18- fix . edit = new vscode . WorkspaceEdit ( ) ;
19- fix . edit . replace (
20- document . uri ,
21- new vscode . Range (
22- diagnostic . range . start ,
23- diagnostic . range . end
24- ) ,
25- `$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v${ devProxyVersion } /rc.schema.json",`
26- ) ;
27- return [ fix ] ;
28- }
29- }
30- } ) ) ;
31- } ;
5+ const devProxyInstall =
6+ context . globalState . get < DevProxyInstall > ( 'devProxyInstall' ) ;
7+ if ( ! devProxyInstall ) {
8+ return ;
9+ }
10+ const devProxyVersion = devProxyInstall . isBeta
11+ ? devProxyInstall . version . split ( '-' ) [ 0 ]
12+ : devProxyInstall . version ;
13+
14+ // Code action for invalid schema
15+ context . subscriptions . push (
16+ vscode . languages . registerCodeActionsProvider ( 'json' , {
17+ provideCodeActions : ( document , range , context , token ) => {
18+ const diagnostic = context . diagnostics . find ( diagnostic => {
19+ return diagnostic . code === 'invalidSchema' ;
20+ } ) ;
21+ if ( diagnostic ) {
22+ const fix = new vscode . CodeAction (
23+ 'Update schema' ,
24+ vscode . CodeActionKind . QuickFix ,
25+ ) ;
26+ fix . edit = new vscode . WorkspaceEdit ( ) ;
27+ fix . edit . replace (
28+ document . uri ,
29+ new vscode . Range ( diagnostic . range . start , diagnostic . range . end ) ,
30+ `https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v${ devProxyVersion } /rc.schema.json` ,
31+ ) ;
32+ return [ fix ] ;
33+ }
34+ } ,
35+ } ) ,
36+ ) ;
37+
38+ // Code action for deprecated plugin paths (individual and bulk updates)
39+ context . subscriptions . push (
40+ vscode . languages . registerCodeActionsProvider ( 'json' , {
41+ provideCodeActions : ( document , range , context , token ) => {
42+ const correctPluginPath = '~appFolder/plugins/DevProxy.Plugins.dll' ;
43+
44+ // Check if the current range intersects with a deprecated plugin path diagnostic
45+ const currentDiagnostic = context . diagnostics . find ( diagnostic => {
46+ return diagnostic . code === 'deprecatedPluginPath' &&
47+ diagnostic . range . intersection ( range ) ;
48+ } ) ;
49+
50+ // Only provide deprecated plugin path actions if user is on a deprecated plugin path diagnostic
51+ if ( ! currentDiagnostic ) {
52+ return [ ] ;
53+ }
54+
55+ const fixes : vscode . CodeAction [ ] = [ ] ;
56+
57+ // Individual fix for the current diagnostic
58+ const individualFix = new vscode . CodeAction (
59+ 'Update plugin path' ,
60+ vscode . CodeActionKind . QuickFix ,
61+ ) ;
62+ individualFix . edit = new vscode . WorkspaceEdit ( ) ;
63+ individualFix . edit . replace (
64+ document . uri ,
65+ new vscode . Range (
66+ currentDiagnostic . range . start ,
67+ currentDiagnostic . range . end ,
68+ ) ,
69+ correctPluginPath ,
70+ ) ;
71+ fixes . push ( individualFix ) ;
72+
73+ // Bulk fix for all deprecated plugin paths in the document (only show when on a deprecated plugin path)
74+ const allDeprecatedPluginPathDiagnostics = vscode . languages
75+ . getDiagnostics ( document . uri )
76+ . filter ( diagnostic => {
77+ return diagnostic . code === 'deprecatedPluginPath' ;
78+ } ) ;
79+
80+ if ( allDeprecatedPluginPathDiagnostics . length > 1 ) {
81+ const bulkFix = new vscode . CodeAction (
82+ `Update all plugin paths` ,
83+ vscode . CodeActionKind . QuickFix ,
84+ ) ;
85+ bulkFix . edit = new vscode . WorkspaceEdit ( ) ;
86+
87+ // Update all deprecated plugin paths
88+ allDeprecatedPluginPathDiagnostics . forEach ( diagnostic => {
89+ bulkFix . edit ! . replace (
90+ document . uri ,
91+ new vscode . Range ( diagnostic . range . start , diagnostic . range . end ) ,
92+ correctPluginPath ,
93+ ) ;
94+ } ) ;
95+
96+ bulkFix . isPreferred = true ;
97+ fixes . push ( bulkFix ) ;
98+ }
99+
100+ return fixes ;
101+ } ,
102+ } ) ,
103+ ) ;
104+ } ;
0 commit comments