Skip to content

Commit

Permalink
Remove data from unused anchor diagnostics
Browse files Browse the repository at this point in the history
The anchor name was added as custom data. It’s unnecessary, because the
name of the anchor can be determined from other values in the place
where it’s used.

Because Monaco Editor marker data doesn’t support custom data, this blocks
support code actions in monaco-yaml.
  • Loading branch information
remcohaszing authored and evidolob committed Feb 15, 2022
1 parent 77e2f6c commit cecc0e4
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 12 deletions.
1 change: 0 additions & 1 deletion src/languageservice/services/validation/unused-anchors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export class UnusedAnchorsValidator implements AdditionalValidator {
);
const warningDiagnostic = Diagnostic.create(range, `Unused anchor "${aToken.source}"`, DiagnosticSeverity.Hint, 0);
warningDiagnostic.tags = [DiagnosticTag.Unnecessary];
warningDiagnostic.data = { name: aToken.source };
result.push(warningDiagnostic);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/languageservice/services/yamlCodeActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ export class YamlCodeActions {
const buffer = new TextBuffer(document);
for (const diag of diagnostics) {
if (diag.message.startsWith('Unused anchor') && diag.source === YAML_SOURCE) {
const { name } = diag.data as { name: string };
const range = Range.create(diag.range.start, diag.range.end);
const actual = buffer.getText(range);
const lineContent = buffer.getLineContent(range.end.line);
const lastWhitespaceChar = getFirstNonWhitespaceCharacterAfterOffset(lineContent, range.end.character);
range.end.character = lastWhitespaceChar;
const action = CodeAction.create(
`Delete unused anchor: ${name}`,
`Delete unused anchor: ${actual}`,
createWorkspaceEdit(document.uri, [TextEdit.del(range)]),
CodeActionKind.QuickFix
);
Expand Down
2 changes: 0 additions & 2 deletions test/utils/verifyError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export function createDiagnosticWithData(

export function createUnusedAnchorDiagnostic(
message: string,
name: string,
startLine: number,
startCharacter: number,
endLine: number,
Expand All @@ -53,7 +52,6 @@ export function createUnusedAnchorDiagnostic(
'YAML'
);
diagnostic.tags = [DiagnosticTag.Unnecessary];
diagnostic.data = { name };
return diagnostic;
}

Expand Down
4 changes: 2 additions & 2 deletions test/yamlCodeActions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe('CodeActions Tests', () => {
describe('Remove Unused Anchor', () => {
it('should generate proper action', () => {
const doc = setupTextDocument('foo: &bar bar\n');
const diagnostics = [createUnusedAnchorDiagnostic('Unused anchor "&bar"', '&bar', 0, 5, 0, 9)];
const diagnostics = [createUnusedAnchorDiagnostic('Unused anchor "&bar"', 0, 5, 0, 9)];
const params: CodeActionParams = {
context: CodeActionContext.create(diagnostics),
range: undefined,
Expand All @@ -172,7 +172,7 @@ describe('CodeActions Tests', () => {

it('should delete all whitespace after unused anchor', () => {
const doc = setupTextDocument('foo: &bar \tbar\n');
const diagnostics = [createUnusedAnchorDiagnostic('Unused anchor "&bar"', '&bar', 0, 5, 0, 9)];
const diagnostics = [createUnusedAnchorDiagnostic('Unused anchor "&bar"', 0, 5, 0, 9)];
const params: CodeActionParams = {
context: CodeActionContext.create(diagnostics),
range: undefined,
Expand Down
10 changes: 5 additions & 5 deletions test/yamlValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('YAML Validation Tests', () => {
const result = await parseSetup(yaml);
expect(result).is.not.empty;
expect(result.length).to.be.equal(1);
expect(result[0]).deep.equal(createUnusedAnchorDiagnostic('Unused anchor "&bar"', '&bar', 0, 5, 0, 9));
expect(result[0]).deep.equal(createUnusedAnchorDiagnostic('Unused anchor "&bar"', 0, 5, 0, 9));
});

it('should not report used anchor', async () => {
Expand All @@ -85,10 +85,10 @@ some:
expect(result).is.not.empty;
expect(result.length).to.be.equal(4);
expect(result).to.include.deep.members([
createUnusedAnchorDiagnostic('Unused anchor "&bar"', '&bar', 0, 5, 0, 9),
createUnusedAnchorDiagnostic('Unused anchor "&a"', '&a', 4, 2, 4, 4),
createUnusedAnchorDiagnostic('Unused anchor "&aa"', '&aa', 5, 0, 5, 3),
createUnusedAnchorDiagnostic('Unused anchor "&e"', '&e', 8, 4, 8, 6),
createUnusedAnchorDiagnostic('Unused anchor "&bar"', 0, 5, 0, 9),
createUnusedAnchorDiagnostic('Unused anchor "&a"', 4, 2, 4, 4),
createUnusedAnchorDiagnostic('Unused anchor "&aa"', 5, 0, 5, 3),
createUnusedAnchorDiagnostic('Unused anchor "&e"', 8, 4, 8, 6),
]);
});
});
Expand Down

0 comments on commit cecc0e4

Please sign in to comment.