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

replace multiple space into single space on markdown text in hover #844

3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/out/server/**/*.ts"],
"protocol": "inspector",
"trace": true
"trace": true,
"preLaunchTask": "watch typescript",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added pre launch check

},
// A launch configuration that compiles the server and runs mocha unit tests
{
Expand Down
9 changes: 6 additions & 3 deletions src/languageservice/services/yamlHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*--------------------------------------------------------------------------------------------*/
'use strict';

import { Hover, MarkupContent, Position, Range } from 'vscode-languageserver-types';
import { Hover, MarkupContent, MarkupKind, Position, Range } from 'vscode-languageserver-types';
import { matchOffsetToDocument } from '../utils/arrUtils';
import { LanguageSettings } from '../yamlLanguageService';
import { YAMLSchemaService } from './yamlSchemaService';
Expand All @@ -23,6 +23,7 @@ import { ASTNode } from 'vscode-json-languageservice';

export class YAMLHover {
private shouldHover: boolean;
private indentation: string;
private schemaService: YAMLSchemaService;

constructor(schemaService: YAMLSchemaService, private readonly telemetry: Telemetry) {
Expand All @@ -33,6 +34,7 @@ export class YAMLHover {
configure(languageSettings: LanguageSettings): void {
if (languageSettings) {
this.shouldHover = languageSettings.hover;
this.indentation = languageSettings.indentation;
}
}

Expand Down Expand Up @@ -86,9 +88,10 @@ export class YAMLHover {
);

const createHover = (contents: string): Hover => {
const regex = new RegExp(this.indentation, 'g');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rgrunber
Tabs and whitespace have a special meaning in Markdown. So converted spaces into non braking space as a workaround here.

const markupContent: MarkupContent = {
kind: 'markdown',
value: contents,
kind: MarkupKind.Markdown,
value: contents.replace(regex, ' '),
};
const result: Hover = {
contents: markupContent,
Expand Down
33 changes: 29 additions & 4 deletions test/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ describe('Hover Tests', () => {
let telemetry: TestTelemetry;

before(() => {
languageSettingsSetup = new ServiceSetup().withHover().withSchemaFileMatch({
uri: 'http://google.com',
fileMatch: ['bad-schema.yaml'],
});
languageSettingsSetup = new ServiceSetup()
.withHover()
.withIndentation(' ')
.withSchemaFileMatch({
uri: 'http://google.com',
fileMatch: ['bad-schema.yaml'],
});
const {
languageService: langService,
languageHandler: langHandler,
Expand Down Expand Up @@ -510,6 +513,28 @@ users:
);
});

it('hover on value and its description has multiline, indentationa and special string', async () => {
//https://github.com/redhat-developer/vscode-yaml/issues/886
languageService.addSchema(SCHEMA_ID, {
type: 'object',
title: 'Person',
properties: {
firstName: {
type: 'string',
description: 'At the top level my_var is shown properly.\n\n Issue with my_var2\n here my_var3',
},
},
});
const content = 'fi|r|stName: '; // len: 12, pos: 1
const result = await parseSetup(content);

assert.strictEqual(MarkupContent.is(result.contents), true);
assert.strictEqual(
(result.contents as MarkupContent).value,
`#### Person\n\nAt the top level my\\_var is shown properly\\.\n\n  Issue with my\\_var2\n\n   here my\\_var3\n\nSource: [${SCHEMA_ID}](file:///${SCHEMA_ID})`
);
});

it('Hover works on examples', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
Expand Down
5 changes: 3 additions & 2 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('Kubernetes Integration Tests', () => {
const fileMatch = ['*.yml', '*.yaml'];
languageSettingsSetup = new ServiceSetup()
.withHover()
.withIndentation(' ')
.withValidate()
.withCompletion()
.withSchemaFileMatch({
Expand Down Expand Up @@ -318,8 +319,8 @@ describe('Kubernetes Integration Tests', () => {
it('Hover on incomplete kubernetes document', async () => {
const content = 'apiVersion: v1\nmetadata:\n name: test\nkind: Deployment\nspec:\n ';
const hover = await parseSetup(content, 58);
assert.strictEqual(MarkupContent.is(hover.contents), true);
assert.strictEqual((hover.contents as MarkupContent).value, '');
assert.strictEqual(MarkupContent.is(hover?.contents), true);
assert.strictEqual((hover?.contents as MarkupContent).value, '');
});
});
});
1 change: 1 addition & 0 deletions test/multipleDocuments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('Multiple Documents Validation Tests', () => {
const fileMatch = ['*.yml', '*.yaml'];
languageSettingsSetup = new ServiceSetup()
.withHover()
.withIndentation(' ')
.withValidate()
.withSchemaFileMatch({
fileMatch,
Expand Down