diff --git a/src/languageservice/services/yamlHover.ts b/src/languageservice/services/yamlHover.ts index a0e72ce3..1b452747 100644 --- a/src/languageservice/services/yamlHover.ts +++ b/src/languageservice/services/yamlHover.ts @@ -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'; @@ -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) { @@ -35,6 +36,7 @@ export class YAMLHover { if (languageSettings) { this.shouldHover = languageSettings.hover; this.indentation = languageSettings.indentation; + this.hoverSettings = languageSettings.hoverSettings ?? {}; } } @@ -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); @@ -159,7 +164,7 @@ export class YAMLHover { return true; }); let result = ''; - if (title) { + if (showTitle && title) { result = '#### ' + this.toMarkdown(title); } if (markdownDescription) { @@ -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})`; } diff --git a/src/languageservice/yamlLanguageService.ts b/src/languageservice/yamlLanguageService.ts index 877fde06..697c9d70 100644 --- a/src/languageservice/yamlLanguageService.ts +++ b/src/languageservice/yamlLanguageService.ts @@ -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 diff --git a/test/hover.test.ts b/test/hover.test.ts index 404ed676..da2caec3 100644 --- a/test/hover.test.ts +++ b/test/hover.test.ts @@ -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})` ); }); diff --git a/test/utils/serviceSetup.ts b/test/utils/serviceSetup.ts index 1a20386d..998913be 100644 --- a/test/utils/serviceSetup.ts +++ b/test/utils/serviceSetup.ts @@ -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 { /* @@ -12,6 +12,7 @@ export class ServiceSetup { languageSettings: LanguageSettings = { validate: false, hover: false, + hoverSettings: {}, completion: false, format: false, isKubernetes: false, @@ -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;