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

Feat: add title source hover settings #892

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 8 additions & 3 deletions src/languageservice/services/yamlHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { Hover, MarkupContent, MarkupKind, Position, Range } from 'vscode-languageserver-types';
import { matchOffsetToDocument } from '../utils/arrUtils';
import { LanguageSettings } from '../yamlLanguageService';
import { LanguageSettings, HoverSettings } from '../yamlLanguageService';
import { YAMLSchemaService } from './yamlSchemaService';
import { setKubernetesParserOption } from '../parser/isKubernetes';
import { TextDocument } from 'vscode-languageserver-textdocument';
Expand All @@ -24,6 +24,7 @@ import { stringify as stringifyYAML } from 'yaml';
export class YAMLHover {
private shouldHover: boolean;
private indentation: string;
private hoverSettings: HoverSettings;
private schemaService: YAMLSchemaService;

constructor(schemaService: YAMLSchemaService, private readonly telemetry?: Telemetry) {
Expand All @@ -35,6 +36,7 @@ export class YAMLHover {
if (languageSettings) {
this.shouldHover = languageSettings.hover;
this.indentation = languageSettings.indentation;
this.hoverSettings = languageSettings.hoverSettings ?? {};
}
}

Expand Down Expand Up @@ -103,6 +105,9 @@ export class YAMLHover {
return value.replace(/\|\|\s*$/, '');
};

const showSource = this.hoverSettings?.showSource ?? true; // showSource enabled by default
const showTitle = this.hoverSettings?.showTitle ?? true; // showTitle enabled by default

return this.schemaService.getSchemaForResource(document.uri, doc).then((schema) => {
if (schema && node && !schema.errors.length) {
const matchingSchemas = doc.getMatchingSchemas(schema.schema, node.offset);
Expand Down Expand Up @@ -159,7 +164,7 @@ export class YAMLHover {
return true;
});
let result = '';
if (title) {
if (showTitle && title) {
result = '#### ' + this.toMarkdown(title);
}
if (markdownDescription) {
Expand All @@ -184,7 +189,7 @@ export class YAMLHover {
result += `\`\`\`yaml\n${example}\`\`\`\n`;
});
}
if (result.length > 0 && schema.schema.url) {
if (showSource && result.length > 0 && schema.schema.url) {
result = ensureLineBreak(result);
result += `Source: [${getSchemaName(schema.schema)}](${schema.schema.url})`;
}
Expand Down
6 changes: 6 additions & 0 deletions src/languageservice/yamlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,15 @@ export interface SchemasSettings {
versions?: SchemaVersions;
}

export interface HoverSettings {
showSource?: boolean;
showTitle?: boolean;
}

export interface LanguageSettings {
validate?: boolean; //Setting for whether we want to validate the schema
hover?: boolean; //Setting for whether we want to have hover results
hoverSettings?: HoverSettings; // Settings for hover parts
completion?: boolean; //Setting for whether we want to have completion results
format?: boolean; //Setting for whether we want to have the formatter or not
isKubernetes?: boolean; //If true then its validating against kubernetes
Expand Down
101 changes: 101 additions & 0 deletions test/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,107 @@ Allowed Values:
* \`dog\`: Canis familiaris
* \`non\`

Source: [${SCHEMA_ID}](file:///${SCHEMA_ID})`
);
});

it('Hover hides title and source when disabled', async () => {
(() => {
languageSettingsSetup = new ServiceSetup()
.withHover()
.withHoverSettings({
showTitle: false,
showSource: false,
})
.withIndentation(' ')
.withSchemaFileMatch({
uri: 'http://google.com',
fileMatch: ['bad-schema.yaml'],
});
const {
languageHandler: langHandler,
yamlSettings: settings,
telemetry: testTelemetry,
schemaProvider: testSchemaProvider,
} = setupLanguageService(languageSettingsSetup.languageSettings);
languageHandler = langHandler;
yamlSettings = settings;
telemetry = testTelemetry;
schemaProvider = testSchemaProvider;
})();
schemaProvider.addSchema(SCHEMA_ID, {
type: 'object',
title: 'Living being',
properties: {
animal: {
type: 'string',
description: 'should return this description',
examples: ['cat'],
},
},
});
const content = 'animal:\n ca|t|'; // len: 13, pos: 12
const result = await parseSetup(content);

assert.strictEqual(MarkupContent.is(result.contents), true);
assert.strictEqual((result.contents as MarkupContent).kind, 'markdown');
assert.strictEqual(
(result.contents as MarkupContent).value,
`should return this description

Example:

\`\`\`yaml
cat
\`\`\`
`
);
});

it('Hover works when title and source explicitely enabled', async () => {
(() => {
languageSettingsSetup = new ServiceSetup()
.withHover()
.withSchemaFileMatch({
uri: 'http://google.com',
fileMatch: ['bad-schema.yaml'],
})
.withHoverSettings({
showTitle: true,
showSource: true,
});
const {
languageHandler: langHandler,
yamlSettings: settings,
telemetry: testTelemetry,
schemaProvider: testSchemaProvider,
} = setupLanguageService(languageSettingsSetup.languageSettings);
languageHandler = langHandler;
yamlSettings = settings;
telemetry = testTelemetry;
schemaProvider = testSchemaProvider;
})();
schemaProvider.addSchema(SCHEMA_ID, {
type: 'object',
title: 'Living being',
properties: {
animal: {
type: 'string',
description: 'should return this description',
},
},
});
const content = 'animal:\n ca|t|'; // len: 13, pos: 12
const result = await parseSetup(content);

assert.strictEqual(MarkupContent.is(result.contents), true);
assert.strictEqual((result.contents as MarkupContent).kind, 'markdown');
assert.strictEqual(
(result.contents as MarkupContent).value,
`#### Living being

should return this description

Source: [${SCHEMA_ID}](file:///${SCHEMA_ID})`
);
});
Expand Down
8 changes: 7 additions & 1 deletion test/utils/serviceSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Red Hat. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { LanguageSettings, SchemasSettings } from '../../src/languageservice/yamlLanguageService';
import { HoverSettings, LanguageSettings, SchemasSettings } from '../../src/languageservice/yamlLanguageService';

export class ServiceSetup {
/*
Expand All @@ -12,6 +12,7 @@ export class ServiceSetup {
languageSettings: LanguageSettings = {
validate: false,
hover: false,
hoverSettings: {},
completion: false,
format: false,
isKubernetes: false,
Expand All @@ -33,6 +34,11 @@ export class ServiceSetup {
return this;
}

withHoverSettings(hoverSettings: HoverSettings): ServiceSetup {
this.languageSettings.hoverSettings = hoverSettings;
return this;
}

withCompletion(): ServiceSetup {
this.languageSettings.completion = true;
return this;
Expand Down