Search in sources :

Example 1 with PsiPerlHashIndex

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

the class PerlFormattingScalarDerefExpand method apply.

@Override
public int apply() {
    int delta = 0;
    if (myScalarDereference.isValid()) {
        PsiElement parent = myScalarDereference.getParent();
        PsiPerlScalarVariable scalarVariable = PsiTreeUtil.findChildOfType(myScalarDereference, PsiPerlScalarVariable.class);
        if (parent.isValid() && (parent instanceof PsiPerlHashElement || parent instanceof PsiPerlArrayElement) && scalarVariable != null && scalarVariable.isValid()) {
            PsiElement indexElement = parent.getLastChild();
            if (indexElement.isValid() && (indexElement instanceof PsiPerlHashIndex || indexElement instanceof PsiPerlArrayIndex)) {
                String newCode = scalarVariable.getNode().getText() + "->" + indexElement.getText();
                PerlFileImpl newFile = PerlElementFactory.createFile(myScalarDereference.getProject(), newCode);
                PsiPerlDerefExpr derefExpr = PsiTreeUtil.findChildOfType(newFile, PsiPerlDerefExpr.class);
                if (derefExpr != null) {
                    if (parent.getParent() instanceof PsiPerlDerefExpr) {
                        delta = new PerlFormattingReplace(parent, parent, derefExpr.getFirstChild(), derefExpr.getLastChild()).apply();
                    } else {
                        delta = new PerlFormattingReplace(parent, derefExpr).apply();
                    }
                }
            }
        }
    }
    return delta;
}
Also used : PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PsiPerlHashIndex

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

the class PerlStringContentCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull final CompletionResultSet result) {
    PsiElement element = parameters.getPosition();
    PsiElement parent = element.getParent();
    if (parent instanceof PsiLanguageInjectionHost && PerlInjectionUtil.hasInjections(parent)) {
        return;
    }
    if (// exporter assignments
    EXPORT_ASSIGNED_STRING_CONTENT.accepts(element)) {
        PerlStringCompletionUtil.fillWithExportableEntities(element, result);
    } else if (// hash indexes
    SIMPLE_HASH_INDEX.accepts(element)) {
        PsiPerlHashIndex indexElement = PsiTreeUtil.getParentOfType(element, PsiPerlHashIndex.class);
        if (indexElement != null && indexElement.getParent() instanceof PsiPerlGlobSlot) {
            PerlStringCompletionUtil.fillWithRefTypes(result);
        } else {
            PerlStringCompletionUtil.fillWithHashIndexes(element, result);
        }
    } else if (// use or no parameters
    USE_PARAMETERS_PATTERN.accepts(element)) {
        PerlStringCompletionUtil.fillWithUseParameters(element, result);
    } else if (// #@Inject some
    parent != null && parent.getParent() instanceof PsiPerlAnnotationInject) {
        PerlStringCompletionUtil.fillWithInjectableMarkers(element, result);
        result.stopHere();
    } else if (// HERE-DOC openers
    STRING_CONTENT_IN_HEREDOC_OPENER_PATTERN.accepts(element)) {
        PerlStringCompletionUtil.fillWithInjectableMarkers(element, result);
        PerlStringCompletionUtil.fillWithHeredocOpeners(element, result);
    } else if (// begin of string or qw element
    STRING_CONTENT_IN_LIST_OR_STRING_START.accepts(element)) {
        PerlStringCompletionUtil.fillWithRefTypes(result);
        PerlPackageCompletionUtil.fillWithAllPackageNames(element, result);
    }
}
Also used : PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) PsiPerlGlobSlot(com.perl5.lang.perl.psi.PsiPerlGlobSlot) PsiPerlHashIndex(com.perl5.lang.perl.psi.PsiPerlHashIndex) PsiPerlAnnotationInject(com.perl5.lang.perl.psi.PsiPerlAnnotationInject) PsiElement(com.intellij.psi.PsiElement)

Example 3 with PsiPerlHashIndex

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

the class PerlTypedHandler method charTyped.

@NotNull
@Override
public Result charTyped(char typedChar, @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
    final int offset = editor.getCaretModel().getOffset() - 1;
    if (offset < 0) {
        return Result.CONTINUE;
    }
    EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    HighlighterIterator iterator = highlighter.createIterator(offset);
    IElementType elementTokenType = iterator.getTokenType();
    Document document = editor.getDocument();
    if (QUOTE_OPEN_ANY.contains(elementTokenType) && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_QUOTE) {
        IElementType quotePrefixType = offset > 0 ? PerlEditorUtil.getPreviousTokenType(highlighter.createIterator(offset - 1)) : null;
        CharSequence text = document.getCharsSequence();
        if (offset > text.length() - 1 || text.charAt(offset) != typedChar) {
            return Result.CONTINUE;
        }
        if (elementTokenType == QUOTE_DOUBLE_OPEN || elementTokenType == QUOTE_SINGLE_OPEN) {
            AutoPopupController.getInstance(project).scheduleAutoPopup(editor);
        }
        char openChar = text.charAt(offset);
        char closeChar = PerlBaseLexer.getQuoteCloseChar(openChar);
        iterator.advance();
        IElementType possibleCloseQuoteType = iterator.atEnd() ? null : iterator.getTokenType();
        if (QUOTE_CLOSE_FIRST_ANY.contains(possibleCloseQuoteType) && closeChar == text.charAt(iterator.getStart())) {
            if (DOUBLE_QUOTE_OPENERS.contains(quotePrefixType) && StringUtil.containsChar(HANDLED_BY_BRACE_MATCHER, openChar)) {
                iterator.advance();
                if (iterator.atEnd() || !QUOTE_OPEN_ANY.contains(iterator.getTokenType())) {
                    EditorModificationUtil.insertStringAtCaret(editor, Character.toString(closeChar) + openChar, false, false);
                }
            }
            return Result.CONTINUE;
        }
        StringBuilder textToAppend = new StringBuilder();
        textToAppend.append(closeChar);
        if (DOUBLE_QUOTE_OPENERS.contains(quotePrefixType)) {
            textToAppend.append(openChar);
            if (openChar != closeChar) {
                textToAppend.append(closeChar);
            }
        }
        EditorModificationUtil.insertStringAtCaret(editor, textToAppend.toString(), false, false);
    } else if (elementTokenType == LEFT_BRACE) {
        AutoPopupController.getInstance(project).scheduleAutoPopup(editor, CompletionType.BASIC, psiFile -> {
            PsiElement newElement = psiFile.findElementAt(offset);
            return PsiUtilCore.getElementType(newElement) == elementTokenType && newElement.getParent() instanceof PsiPerlHashIndex;
        });
    }
    return Result.CONTINUE;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) QUOTE_CLOSE_FIRST_ANY(com.perl5.lang.perl.lexer.PerlTokenSets.QUOTE_CLOSE_FIRST_ANY) IElementType(com.intellij.psi.tree.IElementType) Document(com.intellij.openapi.editor.Document) PerlPsiUtil(com.perl5.lang.perl.psi.utils.PerlPsiUtil) CaretModel(com.intellij.openapi.editor.CaretModel) AutoPopupController(com.intellij.codeInsight.AutoPopupController) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) EditorEx(com.intellij.openapi.editor.ex.EditorEx) PsiPerlCommaSequenceExpr(com.perl5.lang.perl.psi.PsiPerlCommaSequenceExpr) QUOTE_OPEN_ANY(com.perl5.lang.perl.lexer.PerlTokenSets.QUOTE_OPEN_ANY) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) EditorModificationUtil(com.intellij.openapi.editor.EditorModificationUtil) PsiPerlHashIndex(com.perl5.lang.perl.psi.PsiPerlHashIndex) CompletionType(com.intellij.codeInsight.completion.CompletionType) TypedHandlerDelegate(com.intellij.codeInsight.editorActions.TypedHandlerDelegate) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) StringUtil(com.intellij.openapi.util.text.StringUtil) CodeInsightSettings(com.intellij.codeInsight.CodeInsightSettings) FileType(com.intellij.openapi.fileTypes.FileType) Editor(com.intellij.openapi.editor.Editor) Perl5CodeInsightSettings(com.perl5.lang.perl.idea.codeInsight.Perl5CodeInsightSettings) TokenSet(com.intellij.psi.tree.TokenSet) Nullable(org.jetbrains.annotations.Nullable) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) PerlElementTypes(com.perl5.lang.perl.lexer.PerlElementTypes) NotNull(org.jetbrains.annotations.NotNull) PerlBaseLexer(com.perl5.lang.perl.lexer.PerlBaseLexer) EditorEx(com.intellij.openapi.editor.ex.EditorEx) PsiPerlHashIndex(com.perl5.lang.perl.psi.PsiPerlHashIndex) Document(com.intellij.openapi.editor.Document) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) PsiElement(com.intellij.psi.PsiElement) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElement (com.intellij.psi.PsiElement)3 PsiPerlHashIndex (com.perl5.lang.perl.psi.PsiPerlHashIndex)2 AutoPopupController (com.intellij.codeInsight.AutoPopupController)1 CodeInsightSettings (com.intellij.codeInsight.CodeInsightSettings)1 CompletionType (com.intellij.codeInsight.completion.CompletionType)1 TypedHandlerDelegate (com.intellij.codeInsight.editorActions.TypedHandlerDelegate)1 CaretModel (com.intellij.openapi.editor.CaretModel)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 EditorModificationUtil (com.intellij.openapi.editor.EditorModificationUtil)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)1 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)1 FileType (com.intellij.openapi.fileTypes.FileType)1 Project (com.intellij.openapi.project.Project)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)1 PsiFile (com.intellij.psi.PsiFile)1 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1