From 69a888a84c5ee1a7b64c74eccb5a491a4161d3f6 Mon Sep 17 00:00:00 2001 From: Quentin Rault Date: Sat, 3 Jun 2023 12:32:37 +0200 Subject: [PATCH 1/4] rebase nikalexxx work into main --- src/languageservice/services/yamlHover.ts | 10 ++++++++-- src/languageservice/yamlLanguageService.ts | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/languageservice/services/yamlHover.ts b/src/languageservice/services/yamlHover.ts index a0e72ce3..492ca1cc 100644 --- a/src/languageservice/services/yamlHover.ts +++ b/src/languageservice/services/yamlHover.ts @@ -24,6 +24,7 @@ import { stringify as stringifyYAML } from 'yaml'; export class YAMLHover { private shouldHover: boolean; private indentation: string; + private hoverSettings: LanguageSettings['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,10 @@ export class YAMLHover { return value.replace(/\|\|\s*$/, ''); }; + const hoverSettings = this.hoverSettings || {}; + const showSource = hoverSettings.hasOwnProperty('showSource') ? hoverSettings.showSource : true; + const showTitle = hoverSettings.hasOwnProperty('showTitle') ? hoverSettings.showTitle : true; + 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 +165,7 @@ export class YAMLHover { return true; }); let result = ''; - if (title) { + if (showTitle && title) { result = '#### ' + this.toMarkdown(title); } if (markdownDescription) { @@ -184,7 +190,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 539371d8..b7341976 100644 --- a/src/languageservice/yamlLanguageService.ts +++ b/src/languageservice/yamlLanguageService.ts @@ -74,6 +74,7 @@ export interface SchemasSettings { 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?: {showSource?: boolean; showTitle?: boolean;}; // 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 From e896d0faa57bcde4a07c0ae421d86b1b13b710fa Mon Sep 17 00:00:00 2001 From: Quentin Rault Date: Sat, 3 Jun 2023 12:33:20 +0200 Subject: [PATCH 2/4] fix lint --- src/languageservice/services/yamlHover.ts | 6 +++--- src/languageservice/yamlLanguageService.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/languageservice/services/yamlHover.ts b/src/languageservice/services/yamlHover.ts index 492ca1cc..4d75c10c 100644 --- a/src/languageservice/services/yamlHover.ts +++ b/src/languageservice/services/yamlHover.ts @@ -106,9 +106,9 @@ export class YAMLHover { }; const hoverSettings = this.hoverSettings || {}; - const showSource = hoverSettings.hasOwnProperty('showSource') ? hoverSettings.showSource : true; - const showTitle = hoverSettings.hasOwnProperty('showTitle') ? hoverSettings.showTitle : true; - + const showSource = !!hoverSettings?.showSource; + const showTitle = !!hoverSettings?.showTitle; + return this.schemaService.getSchemaForResource(document.uri, doc).then((schema) => { if (schema && node && !schema.errors.length) { const matchingSchemas = doc.getMatchingSchemas(schema.schema, node.offset); diff --git a/src/languageservice/yamlLanguageService.ts b/src/languageservice/yamlLanguageService.ts index b7341976..b5933843 100644 --- a/src/languageservice/yamlLanguageService.ts +++ b/src/languageservice/yamlLanguageService.ts @@ -74,7 +74,7 @@ export interface SchemasSettings { 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?: {showSource?: boolean; showTitle?: boolean;}; // Settings for hover parts + hoverSettings?: { showSource?: boolean; showTitle?: boolean }; // 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 From 94df20d7ca09d7fda07ccf880eca1373552d4cbb Mon Sep 17 00:00:00 2001 From: Quentin Rault Date: Sat, 3 Jun 2023 12:34:44 +0200 Subject: [PATCH 3/4] add tests --- src/languageservice/services/yamlHover.ts | 11 +++++------ src/languageservice/yamlLanguageService.ts | 7 ++++++- test/utils/serviceSetup.ts | 8 +++++++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/languageservice/services/yamlHover.ts b/src/languageservice/services/yamlHover.ts index 4d75c10c..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,7 +24,7 @@ import { stringify as stringifyYAML } from 'yaml'; export class YAMLHover { private shouldHover: boolean; private indentation: string; - private hoverSettings: LanguageSettings['hoverSettings']; + private hoverSettings: HoverSettings; private schemaService: YAMLSchemaService; constructor(schemaService: YAMLSchemaService, private readonly telemetry?: Telemetry) { @@ -36,7 +36,7 @@ export class YAMLHover { if (languageSettings) { this.shouldHover = languageSettings.hover; this.indentation = languageSettings.indentation; - this.hoverSettings = languageSettings.hoverSettings; + this.hoverSettings = languageSettings.hoverSettings ?? {}; } } @@ -105,9 +105,8 @@ export class YAMLHover { return value.replace(/\|\|\s*$/, ''); }; - const hoverSettings = this.hoverSettings || {}; - const showSource = !!hoverSettings?.showSource; - const showTitle = !!hoverSettings?.showTitle; + 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) { diff --git a/src/languageservice/yamlLanguageService.ts b/src/languageservice/yamlLanguageService.ts index b5933843..f688fd40 100644 --- a/src/languageservice/yamlLanguageService.ts +++ b/src/languageservice/yamlLanguageService.ts @@ -71,10 +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?: { showSource?: boolean; showTitle?: boolean }; // Settings for hover parts + 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/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; From 6344c54070290b3cdcf779f98d41b8df9b6c92e3 Mon Sep 17 00:00:00 2001 From: Quentin Rault Date: Fri, 26 Apr 2024 19:20:39 +0200 Subject: [PATCH 4/4] fix: update tests --- test/hover.test.ts | 101 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) 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})` ); });