Search in sources :

Example 6 with PerlHeredocElementImpl

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;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) PerlHeredocElementImpl(com.perl5.lang.perl.psi.impl.PerlHeredocElementImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with PerlHeredocElementImpl

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();
}
Also used : Language(com.intellij.lang.Language) PerlHeredocElementImpl(com.perl5.lang.perl.psi.impl.PerlHeredocElementImpl)

Example 8 with PerlHeredocElementImpl

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);
}
Also used : PerlHeredocTerminatorElement(com.perl5.lang.perl.psi.PerlHeredocTerminatorElement) Language(com.intellij.lang.Language) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with PerlHeredocElementImpl

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);
}
Also used : Project(com.intellij.openapi.project.Project) PerlHeredocElementImpl(com.perl5.lang.perl.psi.impl.PerlHeredocElementImpl)

Example 10 with PerlHeredocElementImpl

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();
}
Also used : PerlHeredocLanguageInjector(com.perl5.lang.perl.idea.intellilang.PerlHeredocLanguageInjector) Language(com.intellij.lang.Language) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) TextRange(com.intellij.openapi.util.TextRange) MultiHostRegistrar(com.intellij.lang.injection.MultiHostRegistrar) NotNull(org.jetbrains.annotations.NotNull) PerlHeredocElementImpl(com.perl5.lang.perl.psi.impl.PerlHeredocElementImpl)

Aggregations

PerlHeredocElementImpl (com.perl5.lang.perl.psi.impl.PerlHeredocElementImpl)9 NotNull (org.jetbrains.annotations.NotNull)4 Language (com.intellij.lang.Language)3 TextRange (com.intellij.openapi.util.TextRange)3 PerlHeredocTerminatorElement (com.perl5.lang.perl.psi.PerlHeredocTerminatorElement)3 Project (com.intellij.openapi.project.Project)2 PsiElement (com.intellij.psi.PsiElement)2 IElementType (com.intellij.psi.tree.IElementType)2 PerlHeredocOpener (com.perl5.lang.perl.psi.PerlHeredocOpener)2 Nullable (org.jetbrains.annotations.Nullable)2 ASTNode (com.intellij.lang.ASTNode)1 MultiHostRegistrar (com.intellij.lang.injection.MultiHostRegistrar)1 CaretModel (com.intellij.openapi.editor.CaretModel)1 Document (com.intellij.openapi.editor.Document)1 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)1 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)1 PerlHeredocLanguageInjector (com.perl5.lang.perl.idea.intellilang.PerlHeredocLanguageInjector)1 PerlFileImpl (com.perl5.lang.perl.psi.impl.PerlFileImpl)1 PerlPolyNamedNestedCallElementBase (com.perl5.lang.perl.psi.impl.PerlPolyNamedNestedCallElementBase)1 PerlPolyNamedElementType (com.perl5.lang.perl.psi.stubs.PerlPolyNamedElementType)1