Skip to content

Commit e0fc88e

Browse files
authored
Add code actions and code lens support for JSONC files. Closes #296 (#312)
1 parent 35fa051 commit e0fc88e

File tree

4 files changed

+135
-79
lines changed

4 files changed

+135
-79
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
1010
## [1.0.1] - Unreleased
1111

12+
### Added:
13+
14+
- Code actions: Support for JSONC files
15+
- Code lens: Support for JSONC files
16+
1217
### Changed:
1318

1419
- Snippets: All snippets that reference schemas updated to use `v1.0.0` schema

src/codeactions.ts

Lines changed: 107 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -4,101 +4,129 @@ import {DevProxyInstall} from './types';
44
export const registerCodeActions = (context: vscode.ExtensionContext) => {
55
const devProxyInstall =
66
context.globalState.get<DevProxyInstall>('devProxyInstall');
7+
78
if (!devProxyInstall) {
89
return;
910
}
11+
1012
const devProxyVersion = devProxyInstall.isBeta
1113
? devProxyInstall.version.split('-')[0]
1214
: devProxyInstall.version;
1315

16+
registerInvalidSchemaFixes(devProxyVersion, context);
17+
registerDeprecatedPluginPathFixes(context);
18+
};
19+
20+
function registerInvalidSchemaFixes(
21+
devProxyVersion: string,
22+
context: vscode.ExtensionContext,
23+
) {
24+
const invalidSchema: vscode.CodeActionProvider = {
25+
provideCodeActions: (document, range, context, token) => {
26+
const diagnostic = context.diagnostics.find(diagnostic => {
27+
return diagnostic.code === 'invalidSchema';
28+
});
29+
if (diagnostic) {
30+
const fix = new vscode.CodeAction(
31+
'Update schema',
32+
vscode.CodeActionKind.QuickFix,
33+
);
34+
fix.edit = new vscode.WorkspaceEdit();
35+
fix.edit.replace(
36+
document.uri,
37+
new vscode.Range(diagnostic.range.start, diagnostic.range.end),
38+
`https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v${devProxyVersion}/rc.schema.json`,
39+
);
40+
return [fix];
41+
}
42+
},
43+
};
44+
1445
// Code action for invalid schema
1546
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-
}),
47+
vscode.languages.registerCodeActionsProvider('json', invalidSchema),
3648
);
3749

38-
// Code action for deprecated plugin paths (individual and bulk updates)
3950
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-
});
51+
vscode.languages.registerCodeActionsProvider('jsonc', invalidSchema),
52+
);
53+
}
4954

50-
// Only provide deprecated plugin path actions if user is on a deprecated plugin path diagnostic
51-
if (!currentDiagnostic) {
52-
return [];
53-
}
55+
function registerDeprecatedPluginPathFixes(context: vscode.ExtensionContext) {
56+
const deprecatedPluginPaths: vscode.CodeActionProvider = {
57+
provideCodeActions: (document, range, context, token) => {
58+
const correctPluginPath = '~appFolder/plugins/DevProxy.Plugins.dll';
59+
60+
// Check if the current range intersects with a deprecated plugin path diagnostic
61+
const currentDiagnostic = context.diagnostics.find(diagnostic => {
62+
return (
63+
diagnostic.code === 'deprecatedPluginPath' &&
64+
diagnostic.range.intersection(range)
65+
);
66+
});
5467

55-
const fixes: vscode.CodeAction[] = [];
68+
// Only provide deprecated plugin path actions if user is on a deprecated plugin path diagnostic
69+
if (!currentDiagnostic) {
70+
return [];
71+
}
5672

57-
// Individual fix for the current diagnostic
58-
const individualFix = new vscode.CodeAction(
59-
'Update plugin path',
73+
const fixes: vscode.CodeAction[] = [];
74+
75+
// Individual fix for the current diagnostic
76+
const individualFix = new vscode.CodeAction(
77+
'Update plugin path',
78+
vscode.CodeActionKind.QuickFix,
79+
);
80+
individualFix.edit = new vscode.WorkspaceEdit();
81+
individualFix.edit.replace(
82+
document.uri,
83+
new vscode.Range(
84+
currentDiagnostic.range.start,
85+
currentDiagnostic.range.end,
86+
),
87+
correctPluginPath,
88+
);
89+
fixes.push(individualFix);
90+
91+
// Bulk fix for all deprecated plugin paths in the document (only show when on a deprecated plugin path)
92+
const allDeprecatedPluginPathDiagnostics = vscode.languages
93+
.getDiagnostics(document.uri)
94+
.filter(diagnostic => {
95+
return diagnostic.code === 'deprecatedPluginPath';
96+
});
97+
98+
if (allDeprecatedPluginPathDiagnostics.length > 1) {
99+
const bulkFix = new vscode.CodeAction(
100+
`Update all plugin paths`,
60101
vscode.CodeActionKind.QuickFix,
61102
);
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,
103+
bulkFix.edit = new vscode.WorkspaceEdit();
104+
105+
// Update all deprecated plugin paths
106+
allDeprecatedPluginPathDiagnostics.forEach(diagnostic => {
107+
bulkFix.edit!.replace(
108+
document.uri,
109+
new vscode.Range(diagnostic.range.start, diagnostic.range.end),
110+
correctPluginPath,
84111
);
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-
}),
112+
});
113+
114+
bulkFix.isPreferred = true;
115+
fixes.push(bulkFix);
116+
}
117+
118+
return fixes;
119+
},
120+
};
121+
// Code action for deprecated plugin paths (individual and bulk updates)
122+
context.subscriptions.push(
123+
vscode.languages.registerCodeActionsProvider('json', deprecatedPluginPaths),
103124
);
104-
};
125+
126+
context.subscriptions.push(
127+
vscode.languages.registerCodeActionsProvider(
128+
'jsonc',
129+
deprecatedPluginPaths,
130+
),
131+
);
132+
}

src/codelens.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ export const registerCodeLens = (context: vscode.ExtensionContext) => {
1010
pluginLensProvider
1111
)
1212
);
13+
14+
context.subscriptions.push(
15+
vscode.languages.registerCodeLensProvider(
16+
{scheme: 'file', language: 'jsonc'},
17+
pluginLensProvider,
18+
),
19+
);
1320
};
1421

1522
export const pluginLensProvider: vscode.CodeLensProvider = {

src/test/examples/devproxyrc.jsonc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.29.2/rc.schema.json",
3+
"plugins": [
4+
{
5+
"name": "LatencyPlugin",
6+
"enabled": true,
7+
"pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll"
8+
}
9+
],
10+
"urlsToWatch": [
11+
"https://jsonplaceholder.typicode.com/*"
12+
],
13+
"logLevel": "information",
14+
"newVersionNotification": "stable",
15+
"showSkipMessages": true
16+
}

0 commit comments

Comments
 (0)