com.intellij.psi.LiteralTextEscaper Java Examples

The following examples show how to use com.intellij.psi.LiteralTextEscaper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BashIdentityStringLiteralEscaperTest.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncoding() throws Exception {
    PsiFile psiFile = myFixture.configureByText(BashFileType.BASH_FILE_TYPE, " 'abc\n'");

    BashWordImpl word = PsiTreeUtil.findChildOfType(psiFile, BashWordImpl.class);
    Assert.assertNotNull(word);

    LiteralTextEscaper<? extends PsiLanguageInjectionHost> escaper = word.createLiteralTextEscaper();
    Assert.assertTrue(escaper instanceof BashIdentityStringLiteralEscaper<?>);

    TextRange range = TextRange.allOf(word.getUnwrappedCharSequence());
    Assert.assertEquals(0, escaper.getOffsetInHost(0, range));
    Assert.assertEquals(1, escaper.getOffsetInHost(1, range));
    Assert.assertEquals(2, escaper.getOffsetInHost(2, range));
    Assert.assertEquals(3, escaper.getOffsetInHost(3, range));
    Assert.assertEquals(4, escaper.getOffsetInHost(4, range));
}
 
Example #2
Source File: BashEnhancedLiteralTextEscaperTest.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
@Test
public void testEscaped1() throws Exception {
    BashWordImpl psiElement = (BashWordImpl) BashPsiElementFactory.createWord(myProject, "$'a\\'a'");
    Assert.assertNotNull(psiElement);

    LiteralTextEscaper<? extends PsiLanguageInjectionHost> textEscaper = psiElement.createLiteralTextEscaper();

    StringBuilder content = new StringBuilder();
    TextRange range = psiElement.getTextContentRange();
    textEscaper.decode(range, content);
    Assert.assertEquals("a'a", content.toString());

    //check the offsets
    Assert.assertEquals(2, textEscaper.getOffsetInHost(0, range)); // a at 2
    Assert.assertEquals(3, textEscaper.getOffsetInHost(1, range)); // ' at 3-4
    Assert.assertEquals(5, textEscaper.getOffsetInHost(2, range)); // a at 5
}
 
Example #3
Source File: BashEnhancedLiteralTextEscaperTest.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
@Test
public void testEscaped2() throws Exception {
    // unescpaed content: a\\"'a
    // java escapes \\ to \\\\
    // bash escapes \\\\ to \\\\\\\\
    BashWordImpl psiElement = (BashWordImpl) BashPsiElementFactory.createWord(myProject, "$'a\\\\\\\\\"\\'a\\'");
    Assert.assertNotNull(psiElement);

    LiteralTextEscaper<? extends PsiLanguageInjectionHost> textEscaper = psiElement.createLiteralTextEscaper();

    StringBuilder content = new StringBuilder();
    TextRange range = psiElement.getTextContentRange();
    textEscaper.decode(range, content);

    //decoded text is a\\"'a
    Assert.assertEquals("a\\\\\"'a", content.toString());

    //check the offsets
    Assert.assertEquals(2, textEscaper.getOffsetInHost(0, range)); // a at 2
    Assert.assertEquals(3, textEscaper.getOffsetInHost(1, range)); // \ at 3-4
    Assert.assertEquals(5, textEscaper.getOffsetInHost(2, range)); // \ at 5-6
    Assert.assertEquals(7, textEscaper.getOffsetInHost(3, range)); // " at 7
    Assert.assertEquals(8, textEscaper.getOffsetInHost(4, range)); // ' at 8-9
    Assert.assertEquals(10, textEscaper.getOffsetInHost(5, range)); // a at 10
}
 
Example #4
Source File: CSharpConstantExpressionImpl.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
@RequiredReadAction
public LiteralTextEscaper<? extends PsiLanguageInjectionHost> createLiteralTextEscaper()
{
	IElementType elementType = getLiteralType();
	if(elementType == CSharpTokens.STRING_LITERAL)
	{
		return new CSharpStringLiteralEscaper<>(this);
	}
	else if(elementType == CSharpTokens.VERBATIM_STRING_LITERAL)
	{
		return LiteralTextEscaper.createSimple(this);
	}
	throw new IllegalArgumentException("Unknown " + elementType);
}
 
Example #5
Source File: PsiRawBody.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
@NotNull
@Override
public LiteralTextEscaper<? extends PsiLanguageInjectionHost> createLiteralTextEscaper() {
    return new JSStringLiteralEscaper<PsiLanguageInjectionHost>(this) {
        @Override
        protected boolean isRegExpLiteral() {
            return false;
        }
    };
}
 
Example #6
Source File: FluidStringLiteralMixin.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
@NotNull
@Override
public LiteralTextEscaper<? extends PsiLanguageInjectionHost> createLiteralTextEscaper() {
    return new JSStringLiteralEscaper<PsiLanguageInjectionHost>(this) {
        @Override
        protected boolean isRegExpLiteral() {
            return false;
        }
    };
}
 
Example #7
Source File: BashEnhancedLiteralTextEscaperTest.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnescpaed() throws Exception {
    BashWordImpl psiElement = (BashWordImpl) BashPsiElementFactory.createWord(myProject, "$'a'");
    Assert.assertNotNull(psiElement);

    LiteralTextEscaper<? extends PsiLanguageInjectionHost> textEscaper = psiElement.createLiteralTextEscaper();

    StringBuilder content = new StringBuilder();
    TextRange range = psiElement.getTextContentRange();
    textEscaper.decode(range, content);
    Assert.assertEquals("a", content.toString());

    //check the offsets
    Assert.assertEquals(2, textEscaper.getOffsetInHost(0, range));
}
 
Example #8
Source File: BashEnhancedLiteralTextEscaperTest.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@NotNull
private StringBuilder unescapeContent(String source) {
    BashWordImpl psiElement = (BashWordImpl) BashPsiElementFactory.createWord(myProject, source);

    LiteralTextEscaper<? extends PsiLanguageInjectionHost> textEscaper = psiElement.createLiteralTextEscaper();

    StringBuilder content = new StringBuilder();
    TextRange range = psiElement.getTextContentRange();
    textEscaper.decode(range, content);
    return content;
}
 
Example #9
Source File: FusionValueDslContentImplMixin.java    From intellij-neos with GNU General Public License v3.0 4 votes vote down vote up
@NotNull
@Override
public LiteralTextEscaper<? extends PsiLanguageInjectionHost> createLiteralTextEscaper() {
    return new FusionDslTextEscaper(this);
}
 
Example #10
Source File: BashHereDocImpl.java    From BashSupport with Apache License 2.0 4 votes vote down vote up
@NotNull
@Override
public LiteralTextEscaper<? extends PsiLanguageInjectionHost> createLiteralTextEscaper() {
    return new HeredocLiteralEscaper<PsiLanguageInjectionHost>(this);
}
 
Example #11
Source File: ShaderCGScript.java    From consulo-unity3d with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public LiteralTextEscaper<? extends PsiLanguageInjectionHost> createLiteralTextEscaper()
{
	return LiteralTextEscaper.createSimple(this);
}
 
Example #12
Source File: PsiCommentImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
@Nonnull
public LiteralTextEscaper<PsiCommentImpl> createLiteralTextEscaper() {
  return new CommentLiteralEscaper(this);
}