Skip to content

Commit c01968c

Browse files
authored
feat: chain single properties (#27)
1 parent bda342c commit c01968c

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

src/languageservice/services/yamlCompletion.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ interface InsertText {
6969
insertIndex: number;
7070
}
7171

72+
export const expressionSchemaName = 'expression';
73+
7274
export class YamlCompletion {
7375
private customTags: string[];
7476
private completionEnabled = true;
@@ -943,7 +945,9 @@ export class YamlCompletion {
943945
const isNodeNull =
944946
(isScalar(originalNode) && originalNode.value === null) ||
945947
(isMap(originalNode) && originalNode.items.length === 0);
946-
const existsParentCompletion = schema.schema.required?.length > 0;
948+
// jigx custom - exclude parent skeleton for expression completion, required prop made troubles
949+
const existsParentCompletion = schema.schema.required?.length > 0 && doc.uri !== expressionSchemaName;
950+
// end jigx custom
947951
if (!this.parentSkeletonSelectedFirst || !isNodeNull || !existsParentCompletion) {
948952
collector.add(
949953
{
@@ -957,7 +961,7 @@ export class YamlCompletion {
957961
);
958962
}
959963
// if the prop is required add it also to parent suggestion
960-
if (schema.schema.required?.includes(key)) {
964+
if (existsParentCompletion && schema.schema.required?.includes(key)) {
961965
collector.add({
962966
label: key,
963967
insertText: this.getInsertTextForProperty(

test/autoCompletionExtend.test.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
import assert = require('assert');
2121
import { expect } from 'chai';
2222
import { createExpectedCompletion } from './utils/verifyError';
23-
import { addUniquePostfix, removeUniquePostfix } from '../src/languageservice/services/yamlCompletion';
23+
import { addUniquePostfix, expressionSchemaName, removeUniquePostfix } from '../src/languageservice/services/yamlCompletion';
2424
import { JSONSchema } from 'vscode-json-languageservice';
2525

2626
describe('Auto Completion Tests Extended', () => {
@@ -51,8 +51,8 @@ describe('Auto Completion Tests Extended', () => {
5151
ensureExpressionSchema();
5252
});
5353

54-
function parseSetup(content: string, position: number): Promise<CompletionList> {
55-
const testTextDocument = setupSchemaIDTextDocument(content);
54+
function parseSetup(content: string, position: number, schemaName?: string): Promise<CompletionList> {
55+
const testTextDocument = setupSchemaIDTextDocument(content, schemaName);
5656
yamlSettings.documents = new TextDocumentTestManager();
5757
(yamlSettings.documents as TextDocumentTestManager).set(testTextDocument);
5858
return languageHandler.completionHandler({
@@ -68,10 +68,10 @@ describe('Auto Completion Tests Extended', () => {
6868
* For example, `content = 'ab|c|d'` places the caret over the `'c'`, at `position = 2`
6969
* @returns A list of valid completions.
7070
*/
71-
function parseCaret(content: string): Promise<CompletionList> {
71+
function parseCaret(content: string, schemaName?: string): Promise<CompletionList> {
7272
const { position, content: content2 } = caretPosition(content);
7373

74-
const testTextDocument = setupSchemaIDTextDocument(content2);
74+
const testTextDocument = setupSchemaIDTextDocument(content2, schemaName);
7575
yamlSettings.documents = new TextDocumentTestManager();
7676
(yamlSettings.documents as TextDocumentTestManager).set(testTextDocument);
7777
return languageHandler.completionHandler({
@@ -81,7 +81,7 @@ describe('Auto Completion Tests Extended', () => {
8181
}
8282

8383
function ensureExpressionSchema(): void {
84-
schemaProvider.addSchema('expression', {
84+
schemaProvider.addSchema('expression-schema', {
8585
properties: {
8686
expression: {
8787
...inlineObjectSchema.definitions.Expression,
@@ -441,4 +441,41 @@ describe('Auto Completion Tests Extended', () => {
441441
expect(completion.items.map((i) => i.insertText)).deep.equal(['entity1']);
442442
});
443443
});
444+
describe('Chain of single properties', () => {
445+
const schema: JSONSchema = {
446+
type: 'object',
447+
properties: {
448+
prop1: {
449+
type: 'object',
450+
properties: {
451+
prop2: {
452+
type: 'object',
453+
properties: {
454+
prop3: {
455+
type: 'object',
456+
properties: {
457+
prop4: {
458+
type: 'object',
459+
},
460+
},
461+
required: ['prop4'],
462+
},
463+
},
464+
required: ['prop3'],
465+
},
466+
},
467+
required: ['prop2'],
468+
},
469+
},
470+
required: ['prop1'],
471+
};
472+
it('should suggest chain of properties - without parent intellisense', async () => {
473+
// `expression` schema is important because client will use it to get completion
474+
schemaProvider.addSchema(expressionSchemaName, schema);
475+
const content = 'prop1:\n | |';
476+
const completion = await parseCaret(content, expressionSchemaName);
477+
expect(completion.items.length).to.be.equal(1);
478+
expect(completion.items[0].insertText).equal('prop2:\n prop3:\n prop4:\n ');
479+
});
480+
});
444481
});

0 commit comments

Comments
 (0)