Search in sources :

Example 1 with PerlStringMixin

use of com.perl5.lang.perl.psi.mixins.PerlStringMixin in project Perl5-IDEA by Camelcade.

the class PerlStringLiteralEscaper method decode.

@Override
public boolean decode(@NotNull TextRange rangeInsideHost, @NotNull StringBuilder outChars) {
    ElementManipulator<PerlStringMixin> manipulator = ElementManipulators.getNotNullManipulator(myHost);
    assert manipulator instanceof PerlStringManipulator;
    PsiElement openQuoteElement = ((PerlStringManipulator) manipulator).getOpeningQuote(myHost);
    char openQuote = openQuoteElement.getText().charAt(0);
    char closeQuote = PerlLexer.getQuoteCloseChar(openQuote);
    offsetsMap = new HashMap<>();
    CharSequence sourceText = rangeInsideHost.subSequence(myHost.getText());
    Integer sourceOffset = 0;
    Integer targetOffset = 0;
    Integer sourceLength = sourceText.length();
    boolean isEscaped = false;
    while (sourceOffset < sourceLength) {
        char currentChar = sourceText.charAt(sourceOffset);
        if (isEscaped) {
            if (currentChar != openQuote && currentChar != closeQuote) {
                assert sourceOffset > 0;
                outChars.append('\\');
                offsetsMap.put(targetOffset++, sourceOffset - 1);
            }
            outChars.append(currentChar);
            offsetsMap.put(targetOffset++, sourceOffset);
            isEscaped = false;
        } else if (currentChar == '\\') {
            isEscaped = true;
        } else {
            outChars.append(currentChar);
            offsetsMap.put(targetOffset++, sourceOffset);
        }
        sourceOffset++;
    }
    if (// end with escape, not sure if possible
    isEscaped) {
        outChars.append('\\');
        offsetsMap.put(targetOffset++, sourceOffset - 1);
    }
    // end marker
    offsetsMap.put(targetOffset, sourceOffset);
    return true;
}
Also used : PerlStringMixin(com.perl5.lang.perl.psi.mixins.PerlStringMixin) PsiElement(com.intellij.psi.PsiElement) PerlStringManipulator(com.perl5.lang.perl.idea.manipulators.PerlStringManipulator)

Aggregations

PsiElement (com.intellij.psi.PsiElement)1 PerlStringManipulator (com.perl5.lang.perl.idea.manipulators.PerlStringManipulator)1 PerlStringMixin (com.perl5.lang.perl.psi.mixins.PerlStringMixin)1