Skip to content

Commit

Permalink
feat: retrieve value from quoted/single quoted/unquoted psi element (#…
Browse files Browse the repository at this point in the history
…128)

Signed-off-by: Luca Stocchi <lstocchi@redhat.com>
  • Loading branch information
lstocchi authored Dec 13, 2021
1 parent a869780 commit 1d48048
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
******************************************************************************/
package com.redhat.devtools.intellij.common.utils;

import com.intellij.psi.PsiElement;
import org.jetbrains.yaml.psi.YAMLQuotedText;

public class StringHelper {

public static String beautify(String text) {
Expand All @@ -26,4 +29,11 @@ public static String getPlural(String kind) {
return kind + "s";
}
}

public static String getUnquotedValueFromPsi(PsiElement element) {
if (element instanceof YAMLQuotedText) {
return ((YAMLQuotedText) element).getTextValue();
}
return element.getText();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.redhat.devtools.intellij.common.utils;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.redhat.devtools.intellij.common.FixtureBaseTest;
import org.jetbrains.yaml.YAMLFileType;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

public class StringHelperTest extends FixtureBaseTest {

@Test
public void GetUnquotedValueFromPsi_DoubleQuotedValue_ValueWithoutQuotes() throws IOException {
String content = "\"foo\"";
PsiFile psiFile = myFixture.configureByText(YAMLFileType.YML, content);
ApplicationManager.getApplication().runReadAction(() -> {
PsiElement result = psiFile.getFirstChild().getLastChild();
assertEquals("foo", StringHelper.getUnquotedValueFromPsi(result));
});
}

@Test
public void GetUnquotedValueFromPsi_SingleQuotedValue_ValueWithoutQuotes() throws IOException {
String content = "'foo'";
PsiFile psiFile = myFixture.configureByText(YAMLFileType.YML, content);
ApplicationManager.getApplication().runReadAction(() -> {
PsiElement result = psiFile.getFirstChild().getLastChild();
assertEquals("foo", StringHelper.getUnquotedValueFromPsi(result));
});
}

@Test
public void GetUnquotedValueFromPsi_UnquotedValue_ValueAsIs() throws IOException {
String content = "foo";
PsiFile psiFile = myFixture.configureByText(YAMLFileType.YML, content);
ApplicationManager.getApplication().runReadAction(() -> {
PsiElement result = psiFile.getFirstChild().getLastChild();
assertEquals("foo", StringHelper.getUnquotedValueFromPsi(result));
});
}
}

0 comments on commit 1d48048

Please sign in to comment.