Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicate codelens entries #667

Merged
merged 1 commit into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions src/languageservice/services/yamlCodeLens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,23 @@ export class YamlCodeLens {
const result = [];
try {
const yamlDocument = yamlDocumentsCache.getYamlDocument(document);
let schemaUrls = new Map<string, JSONSchema>();
for (const currentYAMLDoc of yamlDocument.documents) {
const schema = await this.schemaService.getSchemaForResource(document.uri, currentYAMLDoc);
if (schema?.schema) {
const schemaUrls = getSchemaUrls(schema?.schema);
if (schemaUrls.size === 0) {
continue;
}
for (const urlToSchema of schemaUrls) {
const lens = CodeLens.create(Range.create(0, 0, 0, 0));
lens.command = {
title: getCommandTitle(urlToSchema[0], urlToSchema[1]),
command: YamlCommands.JUMP_TO_SCHEMA,
arguments: [urlToSchema[0]],
};
result.push(lens);
}
// merge schemas from all docs to avoid duplicates
schemaUrls = new Map([...getSchemaUrls(schema?.schema), ...schemaUrls]);
}
}
for (const urlToSchema of schemaUrls) {
const lens = CodeLens.create(Range.create(0, 0, 0, 0));
lens.command = {
title: getCommandTitle(urlToSchema[0], urlToSchema[1]),
command: YamlCommands.JUMP_TO_SCHEMA,
arguments: [urlToSchema[0]],
};
result.push(lens);
}
} catch (err) {
this.telemetry.sendError('yaml.codeLens.error', { error: convertErrorToTelemetryMsg(err) });
}
Expand Down
15 changes: 15 additions & 0 deletions test/yamlCodeLens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ describe('YAML CodeLens', () => {
);
});

it('should place one CodeLens at beginning of the file for multiple documents', async () => {
const doc = setupTextDocument('foo: bar\n---\nfoo: bar');
const schema: JSONSchema = {
url: 'some://url/to/schema.json',
};
yamlSchemaService.getSchemaForResource.resolves({ schema });
const codeLens = new YamlCodeLens((yamlSchemaService as unknown) as YAMLSchemaService, telemetry);
const result = await codeLens.getCodeLens(doc, { textDocument: { uri: doc.uri } });
expect(result.length).to.eq(1);
expect(result[0].range).is.deep.equal(Range.create(0, 0, 0, 0));
expect(result[0].command).is.deep.equal(
createCommand('schema.json', YamlCommands.JUMP_TO_SCHEMA, 'some://url/to/schema.json')
);
});

it('command name should contains schema title', async () => {
const doc = setupTextDocument('foo: bar');
const schema = {
Expand Down