diff --git a/README.md b/README.md index fb4c632ef..1a41299bb 100755 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ The following settings are supported: - `yaml.style.flowMapping` : Forbids flow style mappings if set to `forbid` - `yaml.style.flowSequence` : Forbids flow style sequences if set to `forbid` - `yaml.keyOrdering` : Enforces alphabetical ordering of keys in mappings when set to `true`. Default is `false` +- `yaml.hoverSchemaSource`: Enable/disable showing the schema source in hover tooltips. Default is `true` ## Suppressing diagnostics diff --git a/src/languageserver/handlers/settingsHandlers.ts b/src/languageserver/handlers/settingsHandlers.ts index 581b3f0de..8372ac871 100644 --- a/src/languageserver/handlers/settingsHandlers.ts +++ b/src/languageserver/handlers/settingsHandlers.ts @@ -77,6 +77,9 @@ export class SettingsHandler { if (Object.prototype.hasOwnProperty.call(settings.yaml, 'completion')) { this.yamlSettings.yamlShouldCompletion = settings.yaml.completion; } + if (Object.prototype.hasOwnProperty.call(settings.yaml, 'hoverSchemaSource')) { + this.yamlSettings.yamlhoverSchemaSource = settings.yaml.hoverSchemaSource; + } this.yamlSettings.customTags = settings.yaml.customTags ? settings.yaml.customTags : []; this.yamlSettings.maxItemsComputed = Math.trunc(Math.max(0, Number(settings.yaml.maxItemsComputed))) || 5000; @@ -280,6 +283,7 @@ export class SettingsHandler { flowSequence: this.yamlSettings.style?.flowSequence, yamlVersion: this.yamlSettings.yamlVersion, keyOrdering: this.yamlSettings.keyOrdering, + hoverSchemaSource: this.yamlSettings.yamlhoverSchemaSource, }; if (this.yamlSettings.schemaAssociations) { diff --git a/src/languageservice/services/yamlHover.ts b/src/languageservice/services/yamlHover.ts index da0cc9a30..fdaa1eac7 100644 --- a/src/languageservice/services/yamlHover.ts +++ b/src/languageservice/services/yamlHover.ts @@ -27,6 +27,7 @@ export class YAMLHover { private shouldHover: boolean; private shouldHoverAnchor: boolean; private indentation: string; + private hoverSchemaSource: boolean; private schemaService: YAMLSchemaService; constructor( @@ -42,6 +43,7 @@ export class YAMLHover { this.shouldHover = languageSettings.hover; this.shouldHoverAnchor = languageSettings.hoverAnchor; this.indentation = languageSettings.indentation; + this.hoverSchemaSource = languageSettings.hoverSchemaSource ?? true; } } @@ -208,7 +210,7 @@ export class YAMLHover { result += `\`\`\`yaml\n${example}\`\`\`\n`; }); } - if (result.length > 0 && schema.schema.url) { + if (result.length > 0 && schema.schema.url && this.hoverSchemaSource) { result = ensureLineBreak(result); result += l10n.t('Source: [{0}]({1})', getSchemaName(schema.schema), schema.schema.url); } diff --git a/src/languageservice/yamlLanguageService.ts b/src/languageservice/yamlLanguageService.ts index 7d5877161..b21cb0aaa 100644 --- a/src/languageservice/yamlLanguageService.ts +++ b/src/languageservice/yamlLanguageService.ts @@ -125,6 +125,11 @@ export interface LanguageSettings { * If set enforce alphabetical ordering of keys in mappings. */ keyOrdering?: boolean; + + /** + * Show schema source URI in hover popups. Default is true. + */ + hoverSchemaSource?: boolean; } export interface WorkspaceContextService { diff --git a/src/yamlSettings.ts b/src/yamlSettings.ts index b46075958..5d8c1fbe0 100644 --- a/src/yamlSettings.ts +++ b/src/yamlSettings.ts @@ -37,6 +37,7 @@ export interface Settings { keyOrdering: boolean; maxItemsComputed: number; yamlVersion: YamlVersion; + hoverSchemaSource: boolean; }; http: { proxy: string; @@ -80,6 +81,7 @@ export class SettingsState { yamlShouldHover = true; yamlShouldHoverAnchor = true; yamlShouldCompletion = true; + yamlhoverSchemaSource = true; schemaStoreSettings = []; customTags = []; schemaStoreEnabled = true; diff --git a/test/hover.test.ts b/test/hover.test.ts index aca2f6b36..bc4e013a8 100644 --- a/test/hover.test.ts +++ b/test/hover.test.ts @@ -1287,4 +1287,76 @@ Source: [${SCHEMA_ID}](file:///${SCHEMA_ID})` expect((result.contents as MarkupContent).value).to.include('Disabled flag'); }); }); + + describe('hoverSchemaSource configuration', () => { + it('Hover should not show schema source when hoverSchemaSource is false', async () => { + const languageSettingsSetupWithoutSource = new ServiceSetup() + .withHover() + .withhoverSchemaSource(false) + .withSchemaFileMatch({ + uri: SCHEMA_ID, + fileMatch: ['*.yaml'], + }); + const { + languageHandler: langHandler, + yamlSettings: settings, + schemaProvider: testSchemaProvider, + } = setupLanguageService(languageSettingsSetupWithoutSource.languageSettings); + + testSchemaProvider.addSchema(SCHEMA_ID, { + type: 'object', + properties: { + cwd: { + type: 'string', + description: 'The directory from which bower should run.', + }, + }, + }); + const content = 'c|w|d: test'; + const { content: parsedContent, position } = caretPosition(content); + const testTextDocument = setupSchemaIDTextDocument(parsedContent); + settings.documents = new TextDocumentTestManager(); + (settings.documents as TextDocumentTestManager).set(testTextDocument); + const hover = await langHandler.hoverHandler({ + position: testTextDocument.positionAt(position), + textDocument: testTextDocument, + }); + + assert.strictEqual(MarkupContent.is(hover.contents), true); + assert.strictEqual((hover.contents as MarkupContent).kind, 'markdown'); + assert.strictEqual((hover.contents as MarkupContent).value, 'The directory from which bower should run.'); + assert.strictEqual((hover.contents as MarkupContent).value.includes('Source:'), false); + + testSchemaProvider.deleteSchema(SCHEMA_ID); + }); + + it('Hover should show schema source by default', async () => { + schemaProvider.addSchema(SCHEMA_ID, { + type: 'object', + properties: { + cwd: { + type: 'string', + description: 'The directory from which bower should run.', + }, + }, + }); + const content = 'c|w|d: test'; + const { content: parsedContent, position } = caretPosition(content); + const testTextDocument = setupSchemaIDTextDocument(parsedContent); + yamlSettings.documents = new TextDocumentTestManager(); + (yamlSettings.documents as TextDocumentTestManager).set(testTextDocument); + const hover = await languageHandler.hoverHandler({ + position: testTextDocument.positionAt(position), + textDocument: testTextDocument, + }); + + assert.strictEqual(MarkupContent.is(hover.contents), true); + assert.strictEqual((hover.contents as MarkupContent).kind, 'markdown'); + assert.strictEqual((hover.contents as MarkupContent).value.includes('Source:'), true); + assert.strictEqual( + (hover.contents as MarkupContent).value, + `The directory from which bower should run.\n\nSource: [${SCHEMA_ID}](file:///${SCHEMA_ID})` + ); + }); + }); }); diff --git a/test/utils/serviceSetup.ts b/test/utils/serviceSetup.ts index 2bfd68e36..a7bc5d906 100644 --- a/test/utils/serviceSetup.ts +++ b/test/utils/serviceSetup.ts @@ -22,6 +22,7 @@ export class ServiceSetup { yamlVersion: '1.2', flowMapping: 'allow', flowSequence: 'allow', + hoverSchemaSource: true, }; withValidate(): ServiceSetup { @@ -81,4 +82,9 @@ export class ServiceSetup { this.languageSettings.yamlVersion = version; return this; } + + withhoverSchemaSource(show: boolean): ServiceSetup { + this.languageSettings.hoverSchemaSource = show; + return this; + } }