Add an element to an YAMLArray PSI item

Answered

Hi there,

I'm trying to develop an IntelliJ plugin that works with Yaml Files. And I'd like to implement a quickfix which will add a yaml object to a yaml array, but I can't get it done.

Currently, I try to do the following:

val elementGenerator = YAMLElementGenerator.getInstance(project)

@Language("yaml")
val psiFile = elementGenerator.createDummyYamlWithText("""
parameters:
- name: "name"
""".trimIndent())

parametersElement
.psiElement
.value
?.add(psiFile.documents[0].topLevelValue["parameters"]?.value?.children?.get(0)!!)

(see here: https://gitlab.com/vilmosnagy/argo-intellij-plugin/-/blob/master/src/main/kotlin/me/vnagy/intellijplugins/argo/annotators/parameters/ParameterNameMissingInTemplateCallQuickFix.kt#L49)

But this code creates from this YAML

arguments:
parameters: []

this:

arguments:
parameters: - name: "name"

A newline character and the indentation is missing from the YAML Array.

Could you help me with how should I modify YAMLPsi elements correctly?

Thanks,

Vilmos

0
2 comments

Vilmos,

Try inserting a new line before the content you set to your parameters.

0

Hi Jakub Chrzanowski

 

Thanks for your answer. Could you help me how do I do that?

 - I tried to create a new instance of the `PsiWhitespaceImpl`, but that just isn't valid, and throws an Exception

 - If I add a `\n` to the beginning of the text when I create the `psiFile`, it just gets trimmed

 

And I'd like to keep the correct indentation - at this moment I'm not aware of how many indentation is there at the beginning of these lines, and I'm a bit lost here.

 

Thanks for your help,

Vilmos

0

Please sign in to leave a comment.