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;
}
Aggregations