yaml-language-server icon indicating copy to clipboard operation
yaml-language-server copied to clipboard

support propertyOrder for completions

Open harrylepotter-win opened this issue 3 years ago • 2 comments

Is your enhancement related to a problem? Please describe.

JSONSchema supports propertyOrder, which acts as an aid for UIs to perform completions in the order as was intended by the developer. This is incredibly useful for users as it makes creating a YAML from scratch "look" more understandable.

Describe the solution you would like

yamlcompletions that follow the propertyOrder directive in JSON schema

Describe alternatives you have considered

Patch proposed

Additional context

The following would provide this functionality:

diff --git a/src/languageservice/jsonSchema.ts b/src/languageservice/jsonSchema.ts
index e8b6f90..1688a0b 100644
--- a/src/languageservice/jsonSchema.ts
+++ b/src/languageservice/jsonSchema.ts
@@ -19,6 +19,7 @@ export interface JSONSchema {
   definitions?: { [name: string]: JSONSchema };
   description?: string;
   properties?: JSONSchemaMap;
+  propertyOrder?: string[];
   patternProperties?: JSONSchemaMap;
   additionalProperties?: boolean | JSONSchemaRef;
   minProperties?: number;
diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts
index 701c79f..c216f88 100644
--- a/src/languageservice/services/yamlCompletion.ts
+++ b/src/languageservice/services/yamlCompletion.ts
@@ -298,7 +298,6 @@ export class YamlCompletion {
             }
           }
         }
-
         this.addPropertyCompletions(schema, currentDoc, node, '', collector, textBuffer, overwriteRange);
 
         if (!schema && currentWord.length > 0 && document.getText().charAt(offset - currentWord.length - 1) !== '"') {
@@ -655,7 +654,8 @@ export class YamlCompletion {
       return { insertText, insertIndex };
     }
 
-    Object.keys(schema.properties).forEach((key: string) => {
+    const iterator = schema.propertyOrder !== undefined ? schema.propertyOrder : Object.keys(schema.properties);
+    iterator.forEach((key: string) => {
       const propertySchema = schema.properties[key] as JSONSchema;
       let type = Array.isArray(propertySchema.type) ? propertySchema.type[0] : propertySchema.type;
       if (!type) {

harrylepotter-win avatar Oct 08 '21 04:10 harrylepotter-win

@harrylepotter-win propertyOrder is not yet supported in jsonschema from what I can tell.

dudicoco avatar Nov 04 '21 13:11 dudicoco

@dudicoco You are correct to point out that the propertyOrder is not part of the jsonschema spec. It likely will not be part of the spec anytime soon, as the current focus of jsonschema is validation. The propertyOrder is deemed to be a feature related to GUIs and not validation. Given the YAML LS is used by VSCode (and other) extensions to offer code completion (in addition to validation), it would be a useful extension to the jsonschema standard.

Without this enhancement it is impractical to start from a blank yaml file, and go about authoring one purely through code completion, defaults and examples defined in the json schema.

https://github.com/redhat-developer/yaml-language-server/blob/main/src/languageservice/services/yamlCompletion.ts

arunura avatar Feb 08 '22 17:02 arunura