Skip to content

Commit

Permalink
replace multiple space into single space on markdown text in hover (#844
Browse files Browse the repository at this point in the history
)

* replace multiple space into single space on markdown text in hover

Signed-off-by: msivasubramaniaan <msivasub@redhat.com>

* removed escape sequece when string has more number of indentation

Signed-off-by: msivasubramaniaan <msivasub@redhat.com>

* updated test case

Signed-off-by: msivasubramaniaan <msivasub@redhat.com>

* replace space with indetation

Signed-off-by: msivasubramaniaan <msivasub@redhat.com>

---------

Signed-off-by: msivasubramaniaan <msivasub@redhat.com>
  • Loading branch information
msivasubramaniaan committed Feb 27, 2023
1 parent ba7452e commit 47e9259
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
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",
},
// 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');
const markupContent: MarkupContent = {
kind: 'markdown',
value: contents,
kind: MarkupKind.Markdown,
value: contents.replace(regex, '&emsp;'),
};
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&emsp;&emsp;Issue with my\\_var2\n\n&emsp;&emsp;&emsp;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

0 comments on commit 47e9259

Please sign in to comment.