Skip to content

Commit de465ad

Browse files
committed
Fix diagnostics removal code action
1 parent 43d570b commit de465ad

File tree

5 files changed

+29
-20
lines changed

5 files changed

+29
-20
lines changed

src/handlers/ExecutionHandler.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ExecuteCommandParams, MessageType } from 'vscode-languageserver';
1+
import { ExecuteCommandParams, MessageType, Range } from 'vscode-languageserver';
22
import { ServerRequestHandler } from 'vscode-languageserver/lib/common/server';
33
import { LspDocuments } from '../protocol/LspDocuments';
44
import { ServerComponents } from '../server/ServerComponents';
@@ -67,9 +67,10 @@ export function executionHandler(
6767
const args = params.arguments ?? [];
6868
if (args.length >= 2) {
6969
const uri = args[0] as string;
70-
const diagnosticId = args[1] as string;
70+
const diagnosticRange = args[1] as Range;
71+
const diagnosticMessage = args[2] as string;
7172
components.diagnosticCoordinator
72-
.handleClearCfnDiagnostic(uri, diagnosticId)
73+
.handleClearCfnDiagnostic(uri, diagnosticRange, diagnosticMessage)
7374
.catch((err) => log.error(`Error clearing diagnostic: ${extractErrorMessage(err)}`));
7475
}
7576
break;

src/services/CodeActionService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { SyntaxTreeManager } from '../context/syntaxtree/SyntaxTreeManager';
1515
import { NodeSearch } from '../context/syntaxtree/utils/NodeSearch';
1616
import { NodeType } from '../context/syntaxtree/utils/NodeType';
1717
import { DocumentManager } from '../document/DocumentManager';
18-
import { ANALYZE_DIAGNOSTIC } from '../handlers/ExecutionHandler';
18+
import { ANALYZE_DIAGNOSTIC, CLEAR_DIAGNOSTIC } from '../handlers/ExecutionHandler';
1919
import { CfnInfraCore } from '../server/CfnInfraCore';
2020
import { CFN_VALIDATION_SOURCE } from '../stacks/actions/ValidationWorkflow';
2121
import { LoggerFactory } from '../telemetry/LoggerFactory';
@@ -127,8 +127,8 @@ export class CodeActionService {
127127
textEdits: [],
128128
command: {
129129
title: CodeActionService.REMOVE_ERROR_TITLE,
130-
command: '/command/template/clear-diagnostic',
131-
arguments: [uri, diagnostic.data],
130+
command: CLEAR_DIAGNOSTIC,
131+
arguments: [uri, diagnostic.range, diagnostic.message],
132132
},
133133
},
134134
];

src/services/DiagnosticCoordinator.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Diagnostic, PublishDiagnosticsParams } from 'vscode-languageserver';
1+
import { Diagnostic, PublishDiagnosticsParams, Range } from 'vscode-languageserver';
22
import { LspDiagnostics } from '../protocol/LspDiagnostics';
33
import { CFN_VALIDATION_SOURCE } from '../stacks/actions/ValidationWorkflow';
44
import { LoggerFactory } from '../telemetry/LoggerFactory';
@@ -92,16 +92,23 @@ export class DiagnosticCoordinator {
9292
}
9393

9494
/**
95-
* Handle clearing a CFN diagnostic by ID.
95+
* Handle clearing a CFN diagnostic by range and message.
9696
*/
97-
async handleClearCfnDiagnostic(uri: string, diagnosticId: string): Promise<void> {
97+
async handleClearCfnDiagnostic(uri: string, range: Range, message: string): Promise<void> {
9898
const collection = this.urisToDiagnostics.get(uri);
9999
if (!collection) return;
100100

101101
const sourceDiagnostics = collection.get(CFN_VALIDATION_SOURCE);
102102
if (!sourceDiagnostics) return;
103103

104-
const filteredDiagnostics = sourceDiagnostics.filter((d) => d.data !== diagnosticId);
104+
const filteredDiagnostics = sourceDiagnostics.filter(
105+
(d) =>
106+
!(
107+
d.range.start.line === range.start.line &&
108+
d.range.start.character === range.start.character &&
109+
d.message === message
110+
),
111+
);
105112
collection.set(CFN_VALIDATION_SOURCE, filteredDiagnostics);
106113

107114
const mergedDiagnostics = this.mergeDiagnostics(collection);

tst/unit/services/CodeActionService.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ describe('CodeActionService', () => {
243243
message: 'Validation failed',
244244
severity: DiagnosticSeverity.Error,
245245
source: CFN_VALIDATION_SOURCE,
246-
data: 'test-uuid-123',
247246
};
248247

249248
const params: CodeActionParams = {
@@ -266,7 +265,7 @@ describe('CodeActionService', () => {
266265
command: {
267266
title: 'Remove validation error',
268267
command: '/command/template/clear-diagnostic',
269-
arguments: [params.textDocument.uri, 'test-uuid-123'],
268+
arguments: [params.textDocument.uri, diagnostic.range, diagnostic.message],
270269
},
271270
});
272271
});

tst/unit/services/DiagnosticCoordinator.test.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,14 @@ describe('DiagnosticCoordinator', () => {
379379
});
380380

381381
describe('handleClearCfnDiagnostic', () => {
382-
it('should clear diagnostic by ID', async () => {
383-
const diagnostic1 = { ...createDiagnostic(0, 0, 'Error 1'), data: 'id-1' };
384-
const diagnostic2 = { ...createDiagnostic(1, 0, 'Error 2'), data: 'id-2' };
382+
it('should clear diagnostic by range and message', async () => {
383+
const diagnostic1 = createDiagnostic(0, 0, 'Error 1');
384+
const diagnostic2 = createDiagnostic(1, 0, 'Error 2');
385385

386386
await coordinator.publishDiagnostics(CFN_VALIDATION_SOURCE, testUri, [diagnostic1, diagnostic2]);
387387
vi.clearAllMocks();
388388

389-
await coordinator.handleClearCfnDiagnostic(testUri, 'id-1');
389+
await coordinator.handleClearCfnDiagnostic(testUri, diagnostic1.range, 'Error 1');
390390

391391
expect(mockPublishDiagnostics).toHaveBeenCalledWith({
392392
uri: testUri,
@@ -395,16 +395,18 @@ describe('DiagnosticCoordinator', () => {
395395
});
396396

397397
it('should handle non-existent URI gracefully', async () => {
398-
await coordinator.handleClearCfnDiagnostic('non-existent', 'id-1');
398+
const range = Range.create(Position.create(0, 0), Position.create(0, 10));
399+
await coordinator.handleClearCfnDiagnostic('non-existent', range, 'Error');
399400
expect(mockPublishDiagnostics).not.toHaveBeenCalled();
400401
});
401402

402-
it('should handle non-existent diagnostic ID gracefully', async () => {
403-
const diagnostic = { ...createDiagnostic(0, 0, 'Error'), data: 'id-1' };
403+
it('should handle non-matching diagnostic gracefully', async () => {
404+
const diagnostic = createDiagnostic(0, 0, 'Error');
404405
await coordinator.publishDiagnostics(CFN_VALIDATION_SOURCE, testUri, [diagnostic]);
405406
vi.clearAllMocks();
406407

407-
await coordinator.handleClearCfnDiagnostic(testUri, 'non-existent-id');
408+
const differentRange = Range.create(Position.create(1, 0), Position.create(1, 10));
409+
await coordinator.handleClearCfnDiagnostic(testUri, differentRange, 'Different Error');
408410

409411
expect(mockPublishDiagnostics).toHaveBeenCalledWith({
410412
uri: testUri,

0 commit comments

Comments
 (0)