-
Notifications
You must be signed in to change notification settings - Fork 275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: default value with special chars with anyOf #963
Open
p-spacek
wants to merge
5
commits into
redhat-developer:main
Choose a base branch
from
jigx-com:fix/default-value-with-special-chars-with-anyOf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fae39cd
fix: default value with special chars with anyOf
p-spacek 26ec17a
chore: accept null,0,emptyString in default or const property
p-spacek 10fcfed
Merge branch 'main' into fix/default-value-with-special-chars-with-anyOf
p-spacek 33ef09a
fix: special chars in property
p-spacek 67b24b1
fix: special chars in object completion
p-spacek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -640,8 +640,9 @@ export class YamlCompletion { | |
completionItem.textEdit.newText = completionItem.insertText; | ||
} | ||
// remove $x or use {$x:value} in documentation | ||
const mdText = insertText.replace(/\${[0-9]+[:|](.*)}/g, (s, arg) => arg).replace(/\$([0-9]+)/g, ''); | ||
|
||
let mdText = insertText.replace(/\${[0-9]+[:|](.*)}/g, (s, arg) => arg).replace(/\$([0-9]+)/g, ''); | ||
// unescape special chars for markdown, reverse operation to getInsertTextForPlainText | ||
mdText = getOriginalTextFromEscaped(mdText); | ||
const originalDocumentation = completionItem.documentation ? [completionItem.documentation, '', '----', ''] : []; | ||
completionItem.documentation = { | ||
kind: MarkupKind.Markdown, | ||
|
@@ -1095,6 +1096,7 @@ export class YamlCompletion { | |
|
||
Object.keys(schema.properties).forEach((key: string) => { | ||
const propertySchema = schema.properties[key] as JSONSchema; | ||
const keyEscaped = getInsertTextForPlainText(key); | ||
let type = Array.isArray(propertySchema.type) ? propertySchema.type[0] : propertySchema.type; | ||
if (!type) { | ||
if (propertySchema.anyOf) { | ||
|
@@ -1114,14 +1116,14 @@ export class YamlCompletion { | |
case 'number': | ||
case 'integer': | ||
case 'anyOf': { | ||
let value = propertySchema.default || propertySchema.const; | ||
if (value) { | ||
if (type === 'string') { | ||
let value = propertySchema.default === undefined ? propertySchema.const : propertySchema.default; | ||
if (isDefined(value)) { | ||
if (type === 'string' || typeof value === 'string') { | ||
value = convertToStringValue(value); | ||
} | ||
insertText += `${indent}${key}: \${${insertIndex++}:${value}}\n`; | ||
insertText += `${indent}${keyEscaped}: \${${insertIndex++}:${value}}\n`; | ||
} else { | ||
insertText += `${indent}${key}: $${insertIndex++}\n`; | ||
insertText += `${indent}${keyEscaped}: $${insertIndex++}\n`; | ||
} | ||
break; | ||
} | ||
|
@@ -1138,7 +1140,7 @@ export class YamlCompletion { | |
arrayTemplate = arrayInsertLines.join('\n'); | ||
} | ||
insertIndex = arrayInsertResult.insertIndex; | ||
insertText += `${indent}${key}:\n${indent}${this.indentation}- ${arrayTemplate}\n`; | ||
insertText += `${indent}${keyEscaped}:\n${indent}${this.indentation}- ${arrayTemplate}\n`; | ||
} | ||
break; | ||
case 'object': | ||
|
@@ -1150,7 +1152,7 @@ export class YamlCompletion { | |
insertIndex++ | ||
); | ||
insertIndex = objectInsertResult.insertIndex; | ||
insertText += `${indent}${key}:\n${objectInsertResult.insertText}\n`; | ||
insertText += `${indent}${keyEscaped}:\n${objectInsertResult.insertText}\n`; | ||
} | ||
break; | ||
} | ||
|
@@ -1165,7 +1167,7 @@ export class YamlCompletion { | |
}: \${${insertIndex++}:${propertySchema.default}}\n`; | ||
break; | ||
case 'string': | ||
insertText += `${indent}${key}: \${${insertIndex++}:${convertToStringValue(propertySchema.default)}}\n`; | ||
insertText += `${indent}${keyEscaped}: \${${insertIndex++}:${convertToStringValue(propertySchema.default)}}\n`; | ||
break; | ||
case 'array': | ||
case 'object': | ||
|
@@ -1230,7 +1232,7 @@ export class YamlCompletion { | |
case 'string': { | ||
let snippetValue = JSON.stringify(value); | ||
snippetValue = snippetValue.substr(1, snippetValue.length - 2); // remove quotes | ||
snippetValue = this.getInsertTextForPlainText(snippetValue); // escape \ and } | ||
snippetValue = getInsertTextForPlainText(snippetValue); // escape \ and } | ||
if (type === 'string') { | ||
snippetValue = convertToStringValue(snippetValue); | ||
} | ||
|
@@ -1243,10 +1245,6 @@ export class YamlCompletion { | |
return this.getInsertTextForValue(value, separatorAfter, type); | ||
} | ||
|
||
private getInsertTextForPlainText(text: string): string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved outside of the class to be able to use it from global functions |
||
return text.replace(/[\\$}]/g, '\\$&'); // escape $, \ and } | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
private getInsertTextForValue(value: any, separatorAfter: string, type: string | string[]): string { | ||
if (value === null) { | ||
|
@@ -1259,13 +1257,13 @@ export class YamlCompletion { | |
} | ||
case 'number': | ||
case 'boolean': | ||
return this.getInsertTextForPlainText(value + separatorAfter); | ||
return getInsertTextForPlainText(value + separatorAfter); | ||
} | ||
type = Array.isArray(type) ? type[0] : type; | ||
if (type === 'string') { | ||
value = convertToStringValue(value); | ||
} | ||
return this.getInsertTextForPlainText(value + separatorAfter); | ||
return getInsertTextForPlainText(value + separatorAfter); | ||
} | ||
|
||
private getInsertTemplateForValue( | ||
|
@@ -1290,14 +1288,14 @@ export class YamlCompletion { | |
if (typeof element === 'object') { | ||
valueTemplate = `${this.getInsertTemplateForValue(element, indent + this.indentation, navOrder, separatorAfter)}`; | ||
} else { | ||
valueTemplate = ` \${${navOrder.index++}:${this.getInsertTextForPlainText(element + separatorAfter)}}\n`; | ||
valueTemplate = ` \${${navOrder.index++}:${getInsertTextForPlainText(element + separatorAfter)}}\n`; | ||
} | ||
insertText += `${valueTemplate}`; | ||
} | ||
} | ||
return insertText; | ||
} | ||
return this.getInsertTextForPlainText(value + separatorAfter); | ||
return getInsertTextForPlainText(value + separatorAfter); | ||
} | ||
|
||
private addSchemaValueCompletions( | ||
|
@@ -1669,6 +1667,20 @@ export class YamlCompletion { | |
} | ||
} | ||
|
||
/** | ||
* escape $, \ and } | ||
*/ | ||
function getInsertTextForPlainText(text: string): string { | ||
return text.replace(/(\\?)([\\$}])/g, (match, escapeChar, specialChar) => { | ||
// If it's already escaped (has a backslash before it), return it as is | ||
return escapeChar ? match : `\\${specialChar}`; | ||
}); | ||
} | ||
|
||
function getOriginalTextFromEscaped(text: string): string { | ||
return text.replace(/\\([\\$}])/g, '$1'); | ||
} | ||
|
||
const isNumberExp = /^\d+$/; | ||
function convertToStringValue(param: unknown): string { | ||
let value: string; | ||
|
@@ -1681,6 +1693,8 @@ function convertToStringValue(param: unknown): string { | |
return value; | ||
} | ||
|
||
value = getInsertTextForPlainText(value); // escape $, \ and } | ||
|
||
if (value === 'true' || value === 'false' || value === 'null' || isNumberExp.test(value)) { | ||
return `"${value}"`; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixes condition to allow null,0,emptyString as a default or const
the same logic is correctly used in other places