use of com.perl5.lang.perl.psi.impl.PerlHeredocElementImpl in project Perl5-IDEA by Camelcade.
the class PerlElementFactory method createHeredocBodyReplacement.
/**
* Create a replacement heredoc element with new text
*
* @param originalElement element to replace
* @param newText new element text
* @return new generated element
*/
@NotNull
public static PerlHeredocElementImpl createHeredocBodyReplacement(@NotNull PerlHeredocElementImpl originalElement, @NotNull String newText) {
StringBuilder sb = new StringBuilder("<<");
String marker = getSafeHeredocMarker(newText);
IElementType originalElementType = PsiUtilCore.getElementType(originalElement);
char quote;
if (originalElementType == HEREDOC) {
quote = '\'';
} else if (originalElementType == HEREDOC_QQ) {
quote = '"';
} else {
quote = '`';
}
sb.append(quote).append(marker).append(quote).append(";\n").append(newText);
if (!newText.endsWith("\n")) {
sb.append("\n");
}
PerlFileImpl newFile = createFile(originalElement.getProject(), sb.append(marker).toString());
PerlHeredocElementImpl newElement = newFile.findChildByClass(originalElement.getClass());
assert newElement != null : "Can't find element " + originalElement.getClass() + " generated with: \n" + sb.toString();
return newElement;
}
use of com.perl5.lang.perl.psi.impl.PerlHeredocElementImpl in project Perl5-IDEA by Camelcade.
the class PerlHeredocLanguageInjector method getLanguagesToInject.
@Override
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) {
assert context instanceof PerlHeredocElementImpl;
if (context.getTextLength() == 0 || !PerlSharedSettings.getInstance(context.getProject()).AUTOMATIC_HEREDOC_INJECTIONS) {
return;
}
Language mappedLanguage = getInjectedLanguage((PerlHeredocElementImpl) context);
if (mappedLanguage == null) {
return;
}
registrar.startInjecting(mappedLanguage);
addPlace((PerlHeredocElementImpl) context, registrar);
registrar.doneInjecting();
}
use of com.perl5.lang.perl.psi.impl.PerlHeredocElementImpl in project Perl5-IDEA by Camelcade.
the class PerlHeredocLanguageInjector method getInjectedLanguage.
@Nullable
private Language getInjectedLanguage(@NotNull PerlHeredocElementImpl heredocElement) {
PerlHeredocTerminatorElement terminator = heredocElement.getTerminatorElement();
if (terminator == null) {
return null;
}
String terminatorText = terminator.getText();
Language mappedLanguage = LANGUAGE_MAP.get(terminatorText);
if (mappedLanguage != null) {
return mappedLanguage;
}
PerlTemporaryInjectedLanguageDetector temporaryInjectedLanguageDetector = PerlTemporaryInjectedLanguageDetector.getInstance();
return temporaryInjectedLanguageDetector == null ? null : temporaryInjectedLanguageDetector.getTemporaryInjectedLanguage(heredocElement);
}
use of com.perl5.lang.perl.psi.impl.PerlHeredocElementImpl in project Perl5-IDEA by Camelcade.
the class PerlHeredocElementManipulator method handleContentChange.
@Override
public PerlHeredocElementImpl handleContentChange(@NotNull PerlHeredocElementImpl element, @NotNull TextRange range, String newContent) throws IncorrectOperationException {
StringBuilder sb = new StringBuilder(newContent);
if (element.getTextLength() == range.getEndOffset() && !StringUtil.endsWith(newContent, "\n")) {
sb.append("\n");
}
Project project = element.getProject();
if (range.getStartOffset() > 0) {
sb.insert(0, getIndenter(project, range.getStartOffset()));
range = TextRange.from(0, range.getEndOffset());
}
normalizeIndentation(project, sb, element.getRealIndentSize());
String newElementText = range.replace(element.getText(), sb.toString());
PerlHeredocElementImpl replacement = PerlElementFactory.createHeredocBodyReplacement(element, newElementText);
return (PerlHeredocElementImpl) element.replace(replacement);
}
use of com.perl5.lang.perl.psi.impl.PerlHeredocElementImpl in project Perl5-IDEA by Camelcade.
the class PerlHeredocInjectionTestCase method getHeredocDecodedText.
protected String getHeredocDecodedText(@Nullable PerlHeredocElementImpl heredocElement) {
assertNotNull(heredocElement);
StringBuilder sb = new StringBuilder();
LiteralTextEscaper<PerlHeredocElementImpl> escaper = heredocElement.createLiteralTextEscaper();
// host MUST be auto-injected with our own injector
new PerlHeredocLanguageInjector().getLanguagesToInject(new MultiHostRegistrar() {
@NotNull
@Override
public MultiHostRegistrar startInjecting(@NotNull Language language) {
return this;
}
@NotNull
@Override
public MultiHostRegistrar addPlace(@Nullable String prefix, @Nullable String suffix, @NotNull PsiLanguageInjectionHost host, @NotNull TextRange rangeInsideHost) {
escaper.decode(rangeInsideHost, sb);
return this;
}
@Override
public void doneInjecting() {
}
}, heredocElement);
return sb.toString();
}
Aggregations