Skip to content

Commit 77e2f6c

Browse files
p-spacekPetr Spacek
andauthored
fix/suggestion item title (#659)
* chore: add script to pull from remote * fix: suggestion item title * fix: suggestion item test * feat: add schema description to the parent suggestion * fix: schema title for hover Co-authored-by: Petr Spacek <p-spacek@email.cz>
1 parent 2f34acf commit 77e2f6c

File tree

7 files changed

+32
-20
lines changed

7 files changed

+32
-20
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@
8181
"build:libs": "yarn compile:umd && yarn compile:esm",
8282
"compile:umd": "tsc -p ./tsconfig.umd.json",
8383
"compile:esm": "tsc -p ./tsconfig.esm.json",
84-
"check-dependencies": "node ./scripts/check-dependencies.js"
84+
"check-dependencies": "node ./scripts/check-dependencies.js",
85+
"pull-remote": "git pull https://github.com/redhat-developer/yaml-language-server.git main"
8586
},
8687
"nyc": {
8788
"extension": [

src/languageservice/jsonSchema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface JSONSchema {
1414
url?: string;
1515
type?: string | string[];
1616
title?: string;
17+
closestTitle?: string;
1718
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1819
default?: any;
1920
definitions?: { [name: string]: JSONSchema };

src/languageservice/parser/jsonParser07.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -618,9 +618,8 @@ function validate(
618618
if (!schema.url) {
619619
schema.url = originalSchema.url;
620620
}
621-
if (!schema.title) {
622-
schema.title = originalSchema.title;
623-
}
621+
622+
schema.closestTitle = schema.title || originalSchema.closestTitle;
624623

625624
switch (node.type) {
626625
case 'object':
@@ -1074,6 +1073,7 @@ function validate(
10741073
const subSchemaRef = itemSchema.oneOf[0];
10751074
const subSchema = asSchema(subSchemaRef);
10761075
subSchema.title = schema.title;
1076+
subSchema.closestTitle = schema.closestTitle;
10771077
validate(item, subSchema, schema, itemValidationResult, matchingSchemas, options);
10781078
validationResult.mergePropertyMatch(itemValidationResult);
10791079
validationResult.mergeEnumValues(itemValidationResult);
@@ -1469,8 +1469,10 @@ function getSchemaSource(schema: JSONSchema, originalSchema: JSONSchema): string
14691469
let label: string;
14701470
if (schema.title) {
14711471
label = schema.title;
1472-
} else if (originalSchema.title) {
1473-
label = originalSchema.title;
1472+
} else if (schema.closestTitle) {
1473+
label = schema.closestTitle;
1474+
} else if (originalSchema.closestTitle) {
1475+
label = originalSchema.closestTitle;
14741476
} else {
14751477
const uriString = schema.url ?? originalSchema.url;
14761478
if (uriString) {

src/languageservice/services/yamlCompletion.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const parentCompletionKind = CompletionItemKind.Class;
4646

4747
interface ParentCompletionItemOptions {
4848
schemaType: string;
49+
schemaDescription?: string;
4950
indent?: string;
5051
insertTexts?: string[];
5152
}
@@ -171,6 +172,7 @@ export class YamlCompletion {
171172
parentCompletion = {
172173
...completionItem,
173174
label: schemaType,
175+
documentation: completionItem.parent.schemaDescription,
174176
sortText: '_' + schemaType, // this parent completion goes first,
175177
kind: parentCompletionKind,
176178
};
@@ -645,6 +647,7 @@ export class YamlCompletion {
645647
documentation: this.fromMarkup(propertySchema.markdownDescription) || propertySchema.description || '',
646648
parent: {
647649
schemaType,
650+
schemaDescription: schema.schema.markdownDescription || schema.schema.description,
648651
indent: identCompensation,
649652
},
650653
});

src/languageservice/services/yamlHover.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class YAMLHover {
106106
enumValue: string | undefined = undefined;
107107
matchingSchemas.every((s) => {
108108
if (s.node === node && !s.inverted && s.schema) {
109-
title = title || s.schema.title;
109+
title = title || s.schema.title || s.schema.closestTitle;
110110
markdownDescription = markdownDescription || s.schema.markdownDescription || toMarkdown(s.schema.description);
111111
if (s.schema.enum) {
112112
const idx = s.schema.enum.indexOf(getNodeValue(node));

src/languageservice/utils/schemaUtils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { JSONSchema } from '../jsonSchema';
22

33
export function getSchemaTypeName(schema: JSONSchema): string {
4+
if (schema.title) {
5+
return schema.title;
6+
}
47
if (schema.$id) {
58
const type = getSchemaRefTypeTitle(schema.$id);
69
return type;
@@ -9,7 +12,7 @@ export function getSchemaTypeName(schema: JSONSchema): string {
912
const type = getSchemaRefTypeTitle(schema.$ref || schema._$ref);
1013
return type;
1114
}
12-
const typeStr = schema.title || (Array.isArray(schema.type) ? schema.type.join(' | ') : schema.type); //object
15+
const typeStr = schema.closestTitle || (Array.isArray(schema.type) ? schema.type.join(' | ') : schema.type); //object
1316
return typeStr;
1417
}
1518

test/autoCompletion.test.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,6 +2541,8 @@ describe('Auto Completion Tests', () => {
25412541
},
25422542
required: ['type', 'options'],
25432543
type: 'object',
2544+
description: 'Description1',
2545+
title: 'Object1',
25442546
};
25452547
const obj2 = {
25462548
properties: {
@@ -2581,12 +2583,12 @@ describe('Auto Completion Tests', () => {
25812583
createExpectedCompletion('type', 'type: typeObj1', 0, 0, 0, 0, 10, 2, { documentation: '' })
25822584
);
25832585
expect(result.items[1]).to.deep.equal(
2584-
createExpectedCompletion('obj1', 'type: typeObj1\noptions:\n label: ', 0, 0, 0, 0, 7, 2, {
2586+
createExpectedCompletion('Object1', 'type: typeObj1\noptions:\n label: ', 0, 0, 0, 0, 7, 2, {
25852587
documentation: {
25862588
kind: 'markdown',
2587-
value: '```yaml\ntype: typeObj1\noptions:\n label: \n```',
2589+
value: 'Description1\n\n----\n\n```yaml\ntype: typeObj1\noptions:\n label: \n```',
25882590
},
2589-
sortText: '_obj1',
2591+
sortText: '_Object1',
25902592
})
25912593
);
25922594
expect(result.items[2]).to.deep.equal(
@@ -2627,12 +2629,12 @@ describe('Auto Completion Tests', () => {
26272629
createExpectedCompletion('type', 'type: typeObj1', 0, 2, 0, 2, 10, 2, { documentation: '' })
26282630
);
26292631
expect(result.items[1]).to.deep.equal(
2630-
createExpectedCompletion('obj1', 'type: typeObj1\n options:\n label: ', 0, 2, 0, 2, 7, 2, {
2632+
createExpectedCompletion('Object1', 'type: typeObj1\n options:\n label: ', 0, 2, 0, 2, 7, 2, {
26312633
documentation: {
26322634
kind: 'markdown',
2633-
value: '```yaml\n type: typeObj1\n options:\n label: \n```',
2635+
value: 'Description1\n\n----\n\n```yaml\n type: typeObj1\n options:\n label: \n```',
26342636
},
2635-
sortText: '_obj1',
2637+
sortText: '_Object1',
26362638
})
26372639
);
26382640
expect(result.items[2]).to.deep.equal(
@@ -2666,12 +2668,12 @@ describe('Auto Completion Tests', () => {
26662668

26672669
expect(result.items.length).equal(3);
26682670
expect(result.items[1]).to.deep.equal(
2669-
createExpectedCompletion('obj1', 'type: typeObj1\noptions:\n label: ', 0, 0, 0, 0, 7, 2, {
2671+
createExpectedCompletion('Object1', 'type: typeObj1\noptions:\n label: ', 0, 0, 0, 0, 7, 2, {
26702672
documentation: {
26712673
kind: 'markdown',
2672-
value: '```yaml\ntype: typeObj1\noptions:\n label: \n```',
2674+
value: 'Description1\n\n----\n\n```yaml\ntype: typeObj1\noptions:\n label: \n```',
26732675
},
2674-
sortText: '_obj1',
2676+
sortText: '_Object1',
26752677
})
26762678
);
26772679
});
@@ -2686,12 +2688,12 @@ describe('Auto Completion Tests', () => {
26862688

26872689
expect(result.items.length).equal(2);
26882690
expect(result.items[1]).to.deep.equal(
2689-
createExpectedCompletion('obj1', 'options:\n label: ', 1, 0, 1, 0, 7, 2, {
2691+
createExpectedCompletion('Object1', 'options:\n label: ', 1, 0, 1, 0, 7, 2, {
26902692
documentation: {
26912693
kind: 'markdown',
2692-
value: '```yaml\noptions:\n label: \n```',
2694+
value: 'Description1\n\n----\n\n```yaml\noptions:\n label: \n```',
26932695
},
2694-
sortText: '_obj1',
2696+
sortText: '_Object1',
26952697
})
26962698
);
26972699
});

0 commit comments

Comments
 (0)