Skip to content

Commit

Permalink
fix/suggestion item title (#659)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
p-spacek and Petr Spacek authored Feb 15, 2022
1 parent 2f34acf commit 77e2f6c
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 20 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
"build:libs": "yarn compile:umd && yarn compile:esm",
"compile:umd": "tsc -p ./tsconfig.umd.json",
"compile:esm": "tsc -p ./tsconfig.esm.json",
"check-dependencies": "node ./scripts/check-dependencies.js"
"check-dependencies": "node ./scripts/check-dependencies.js",
"pull-remote": "git pull https://github.com/redhat-developer/yaml-language-server.git main"
},
"nyc": {
"extension": [
Expand Down
1 change: 1 addition & 0 deletions src/languageservice/jsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface JSONSchema {
url?: string;
type?: string | string[];
title?: string;
closestTitle?: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
default?: any;
definitions?: { [name: string]: JSONSchema };
Expand Down
12 changes: 7 additions & 5 deletions src/languageservice/parser/jsonParser07.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,9 +618,8 @@ function validate(
if (!schema.url) {
schema.url = originalSchema.url;
}
if (!schema.title) {
schema.title = originalSchema.title;
}

schema.closestTitle = schema.title || originalSchema.closestTitle;

switch (node.type) {
case 'object':
Expand Down Expand Up @@ -1074,6 +1073,7 @@ function validate(
const subSchemaRef = itemSchema.oneOf[0];
const subSchema = asSchema(subSchemaRef);
subSchema.title = schema.title;
subSchema.closestTitle = schema.closestTitle;
validate(item, subSchema, schema, itemValidationResult, matchingSchemas, options);
validationResult.mergePropertyMatch(itemValidationResult);
validationResult.mergeEnumValues(itemValidationResult);
Expand Down Expand Up @@ -1469,8 +1469,10 @@ function getSchemaSource(schema: JSONSchema, originalSchema: JSONSchema): string
let label: string;
if (schema.title) {
label = schema.title;
} else if (originalSchema.title) {
label = originalSchema.title;
} else if (schema.closestTitle) {
label = schema.closestTitle;
} else if (originalSchema.closestTitle) {
label = originalSchema.closestTitle;
} else {
const uriString = schema.url ?? originalSchema.url;
if (uriString) {
Expand Down
3 changes: 3 additions & 0 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const parentCompletionKind = CompletionItemKind.Class;

interface ParentCompletionItemOptions {
schemaType: string;
schemaDescription?: string;
indent?: string;
insertTexts?: string[];
}
Expand Down Expand Up @@ -171,6 +172,7 @@ export class YamlCompletion {
parentCompletion = {
...completionItem,
label: schemaType,
documentation: completionItem.parent.schemaDescription,
sortText: '_' + schemaType, // this parent completion goes first,
kind: parentCompletionKind,
};
Expand Down Expand Up @@ -645,6 +647,7 @@ export class YamlCompletion {
documentation: this.fromMarkup(propertySchema.markdownDescription) || propertySchema.description || '',
parent: {
schemaType,
schemaDescription: schema.schema.markdownDescription || schema.schema.description,
indent: identCompensation,
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/languageservice/services/yamlHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class YAMLHover {
enumValue: string | undefined = undefined;
matchingSchemas.every((s) => {
if (s.node === node && !s.inverted && s.schema) {
title = title || s.schema.title;
title = title || s.schema.title || s.schema.closestTitle;
markdownDescription = markdownDescription || s.schema.markdownDescription || toMarkdown(s.schema.description);
if (s.schema.enum) {
const idx = s.schema.enum.indexOf(getNodeValue(node));
Expand Down
5 changes: 4 additions & 1 deletion src/languageservice/utils/schemaUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { JSONSchema } from '../jsonSchema';

export function getSchemaTypeName(schema: JSONSchema): string {
if (schema.title) {
return schema.title;
}
if (schema.$id) {
const type = getSchemaRefTypeTitle(schema.$id);
return type;
Expand All @@ -9,7 +12,7 @@ export function getSchemaTypeName(schema: JSONSchema): string {
const type = getSchemaRefTypeTitle(schema.$ref || schema._$ref);
return type;
}
const typeStr = schema.title || (Array.isArray(schema.type) ? schema.type.join(' | ') : schema.type); //object
const typeStr = schema.closestTitle || (Array.isArray(schema.type) ? schema.type.join(' | ') : schema.type); //object
return typeStr;
}

Expand Down
26 changes: 14 additions & 12 deletions test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2541,6 +2541,8 @@ describe('Auto Completion Tests', () => {
},
required: ['type', 'options'],
type: 'object',
description: 'Description1',
title: 'Object1',
};
const obj2 = {
properties: {
Expand Down Expand Up @@ -2581,12 +2583,12 @@ describe('Auto Completion Tests', () => {
createExpectedCompletion('type', 'type: typeObj1', 0, 0, 0, 0, 10, 2, { documentation: '' })
);
expect(result.items[1]).to.deep.equal(
createExpectedCompletion('obj1', 'type: typeObj1\noptions:\n label: ', 0, 0, 0, 0, 7, 2, {
createExpectedCompletion('Object1', 'type: typeObj1\noptions:\n label: ', 0, 0, 0, 0, 7, 2, {
documentation: {
kind: 'markdown',
value: '```yaml\ntype: typeObj1\noptions:\n label: \n```',
value: 'Description1\n\n----\n\n```yaml\ntype: typeObj1\noptions:\n label: \n```',
},
sortText: '_obj1',
sortText: '_Object1',
})
);
expect(result.items[2]).to.deep.equal(
Expand Down Expand Up @@ -2627,12 +2629,12 @@ describe('Auto Completion Tests', () => {
createExpectedCompletion('type', 'type: typeObj1', 0, 2, 0, 2, 10, 2, { documentation: '' })
);
expect(result.items[1]).to.deep.equal(
createExpectedCompletion('obj1', 'type: typeObj1\n options:\n label: ', 0, 2, 0, 2, 7, 2, {
createExpectedCompletion('Object1', 'type: typeObj1\n options:\n label: ', 0, 2, 0, 2, 7, 2, {
documentation: {
kind: 'markdown',
value: '```yaml\n type: typeObj1\n options:\n label: \n```',
value: 'Description1\n\n----\n\n```yaml\n type: typeObj1\n options:\n label: \n```',
},
sortText: '_obj1',
sortText: '_Object1',
})
);
expect(result.items[2]).to.deep.equal(
Expand Down Expand Up @@ -2666,12 +2668,12 @@ describe('Auto Completion Tests', () => {

expect(result.items.length).equal(3);
expect(result.items[1]).to.deep.equal(
createExpectedCompletion('obj1', 'type: typeObj1\noptions:\n label: ', 0, 0, 0, 0, 7, 2, {
createExpectedCompletion('Object1', 'type: typeObj1\noptions:\n label: ', 0, 0, 0, 0, 7, 2, {
documentation: {
kind: 'markdown',
value: '```yaml\ntype: typeObj1\noptions:\n label: \n```',
value: 'Description1\n\n----\n\n```yaml\ntype: typeObj1\noptions:\n label: \n```',
},
sortText: '_obj1',
sortText: '_Object1',
})
);
});
Expand All @@ -2686,12 +2688,12 @@ describe('Auto Completion Tests', () => {

expect(result.items.length).equal(2);
expect(result.items[1]).to.deep.equal(
createExpectedCompletion('obj1', 'options:\n label: ', 1, 0, 1, 0, 7, 2, {
createExpectedCompletion('Object1', 'options:\n label: ', 1, 0, 1, 0, 7, 2, {
documentation: {
kind: 'markdown',
value: '```yaml\noptions:\n label: \n```',
value: 'Description1\n\n----\n\n```yaml\noptions:\n label: \n```',
},
sortText: '_obj1',
sortText: '_Object1',
})
);
});
Expand Down

0 comments on commit 77e2f6c

Please sign in to comment.