Search in sources :

Example 26 with CaretModel

use of com.intellij.openapi.editor.CaretModel in project Perl5-IDEA by Camelcade.

the class PerlEnterHeredocClosingHandler method postProcessEnter.

@Override
public Result postProcessEnter(@NotNull PsiFile file, @NotNull Editor editor, @NotNull DataContext dataContext) {
    if (!file.getLanguage().is(PerlLanguage.INSTANCE)) {
        return Result.Continue;
    }
    final CaretModel caretModel = editor.getCaretModel();
    int offset = caretModel.getOffset();
    PsiElement currentElement = file.findElementAt(offset);
    if (currentElement != null && (!(currentElement.getParent() instanceof PerlHeredocElementImpl) || !Perl5CodeInsightSettings.getInstance().HEREDOC_AUTO_INSERTION)) {
        return Result.Continue;
    }
    LogicalPosition currentPosition = caretModel.getLogicalPosition();
    final int enterLine = currentPosition.line - 1;
    final int currentOffset = caretModel.getOffset();
    if (enterLine <= -1 || currentOffset <= 0) {
        return Result.Continue;
    }
    final Document document = editor.getDocument();
    final PsiDocumentManager manager = PsiDocumentManager.getInstance(file.getProject());
    manager.commitDocument(document);
    final int lineStartOffset = document.getLineStartOffset(enterLine);
    PsiElement firstLineElement = file.findElementAt(lineStartOffset);
    if (firstLineElement == null) {
        return Result.Continue;
    }
    HeredocCollector collector = new HeredocCollector(currentOffset - 1);
    PerlPsiUtil.iteratePsiElementsRight(firstLineElement, collector);
    SmartPsiElementPointer<PerlHeredocOpener> lastOpenerPointer = null;
    for (SmartPsiElementPointer<PerlHeredocOpener> currentOpenerPointer : collector.getResult()) {
        PerlHeredocOpener currentOpener = currentOpenerPointer.getElement();
        if (currentOpener == null) {
            // System.err.println("Opener invalidated on reparse");
            return Result.Continue;
        }
        String openerName = currentOpener.getName();
        boolean emptyOpener = StringUtil.isEmpty(openerName);
        PsiReference inboundReference = ReferencesSearch.search(currentOpener).findFirst();
        if (inboundReference != null) {
            boolean falseAlarm = false;
            PsiElement run = inboundReference.getElement().getPrevSibling();
            while (run instanceof PsiWhiteSpace) {
                run = run.getPrevSibling();
            }
            if (run instanceof PerlHeredocElementImpl) {
                Pattern openerPattern = EMPTY_OPENER_PATTERN;
                if (!emptyOpener) {
                    openerPattern = Pattern.compile("<<~?(\\s*)(?:" + "\"" + openerName + "\"" + "|" + "`" + openerName + "`" + "|" + "'" + openerName + "'" + "|" + "\\\\" + openerName + "|" + openerName + ")");
                }
                falseAlarm = openerPattern.matcher(run.getNode().getChars()).find();
            }
            if (// looks like overlapping heredocs
            falseAlarm) {
                inboundReference = null;
            } else {
                lastOpenerPointer = currentOpenerPointer;
            }
        }
        if (// disclosed marker
        inboundReference == null) {
            int addOffset;
            String closeMarker = "\n" + openerName + "\n";
            if (// first one
            lastOpenerPointer == null) {
                addOffset = currentOffset;
            } else // sequentional
            {
                PerlHeredocOpener lastOpener = lastOpenerPointer.getElement();
                if (lastOpener == null) {
                    return Result.Continue;
                }
                PsiReference lastOpenerReference = ReferencesSearch.search(lastOpener).findFirst();
                if (lastOpenerReference != null) {
                    PsiElement element = lastOpenerReference.getElement();
                    addOffset = element.getTextRange().getEndOffset();
                    if (!emptyOpener) {
                        closeMarker = "\n" + closeMarker;
                    }
                } else {
                    return Result.Continue;
                }
            }
            document.insertString(addOffset, closeMarker);
            manager.commitDocument(document);
            CodeStyleManager.getInstance(file.getProject()).reformatRange(file, addOffset, addOffset + closeMarker.length());
            currentOpener = currentOpenerPointer.getElement();
            if (currentOpener == null) {
                return Result.Continue;
            }
            inboundReference = ReferencesSearch.search(currentOpener).findFirst();
            if (inboundReference != null) {
                lastOpenerPointer = currentOpenerPointer;
            } else {
                return Result.Continue;
            }
        }
    }
    return Result.Continue;
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) Pattern(java.util.regex.Pattern) CaretModel(com.intellij.openapi.editor.CaretModel) PerlHeredocOpener(com.perl5.lang.perl.psi.PerlHeredocOpener) Document(com.intellij.openapi.editor.Document) PerlHeredocElementImpl(com.perl5.lang.perl.psi.impl.PerlHeredocElementImpl)

Example 27 with CaretModel

use of com.intellij.openapi.editor.CaretModel in project Perl5-IDEA by Camelcade.

the class PerlTypedHandler method beforeCharTyped.

@NotNull
@Override
public Result beforeCharTyped(char c, @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file, @NotNull FileType fileType) {
    CaretModel caretModel = editor.getCaretModel();
    int currentOffset = caretModel.getOffset();
    Document document = editor.getDocument();
    CharSequence documentSequence = document.getCharsSequence();
    if (currentOffset > documentSequence.length()) {
        return Result.CONTINUE;
    }
    EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    HighlighterIterator iterator = highlighter.createIterator(currentOffset);
    IElementType nextTokenType = iterator.atEnd() ? null : iterator.getTokenType();
    char nextChar = currentOffset == documentSequence.length() ? 0 : documentSequence.charAt(currentOffset);
    if (c == '<' && nextTokenType == QUOTE_DOUBLE_CLOSE && nextChar == '>' && currentOffset > 0 && documentSequence.charAt(currentOffset - 1) == '<' && (currentOffset < 3 || PerlEditorUtil.getPreviousTokenType(highlighter.createIterator(currentOffset - 2)) != RESERVED_QQ)) {
        document.replaceString(currentOffset, currentOffset + 1, "<");
        caretModel.moveToOffset(currentOffset + 1);
        return Result.STOP;
    }
    if (QUOTE_CLOSE_FIRST_ANY.contains(nextTokenType) && c == nextChar) {
        caretModel.moveToOffset(currentOffset + 1);
        return Result.STOP;
    }
    if (c == ':' && nextTokenType == PACKAGE && Perl5CodeInsightSettings.getInstance().AUTO_INSERT_COLON) {
        document.insertString(currentOffset, "::");
        caretModel.moveToOffset(currentOffset + 2);
        AutoPopupController.getInstance(project).scheduleAutoPopup(editor);
        return Result.STOP;
    }
    if (c == ' ') {
        Result result = tryToAddFatComma(editor, file, currentOffset);
        if (result != null) {
            return result;
        }
    }
    return Result.CONTINUE;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) CaretModel(com.intellij.openapi.editor.CaretModel) EditorEx(com.intellij.openapi.editor.ex.EditorEx) Document(com.intellij.openapi.editor.Document) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) NotNull(org.jetbrains.annotations.NotNull)

Example 28 with CaretModel

use of com.intellij.openapi.editor.CaretModel in project Perl5-IDEA by Camelcade.

the class PodTypedHandler method beforeCharTyped.

@Override
public Result beforeCharTyped(char c, Project project, Editor editor, PsiFile file, FileType fileType) {
    if (file instanceof PodFile) {
        if (c == '<') {
            CaretModel caretModel = editor.getCaretModel();
            int offset = caretModel.getOffset() - 1;
            PsiElement element = file.findElementAt(offset);
            IElementType elementType = element == null ? null : element.getNode().getElementType();
            String text = element == null ? null : element.getText();
            if (elementType == POD_IDENTIFIER && text != null) {
                if (text.length() == 1 && StringUtil.containsChar(POD_COMMANDS, text.charAt(0))) {
                    EditorModificationUtil.insertStringAtCaret(editor, ">", false, false, 0);
                }
            } else if (elementType == POD_ANGLE_LEFT || POD_COMMANDS_TOKENSET.contains(elementType)) {
                // noinspection ConstantConditions
                extendAngles(element.getParent());
                caretModel.moveToOffset(offset + 2);
                return Result.STOP;
            }
        } else if (// '>'
        c == '>') {
            CaretModel caretModel = editor.getCaretModel();
            int offset = caretModel.getOffset();
            PsiElement element = file.findElementAt(offset);
            IElementType elementType = element == null ? null : element.getNode().getElementType();
            int extraOffset = 0;
            if (elementType != POD_ANGLE_RIGHT) {
                offset--;
                element = file.findElementAt(offset);
                elementType = element == null ? null : element.getNode().getElementType();
                extraOffset++;
            }
            if (elementType == POD_ANGLE_RIGHT) {
                // noinspection ConstantConditions
                caretModel.moveToOffset(offset + extendAngles(element.getParent()) + extraOffset);
                return Result.STOP;
            }
        }
    }
    return super.beforeCharTyped(c, project, editor, file, fileType);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) CaretModel(com.intellij.openapi.editor.CaretModel) PodFile(com.perl5.lang.pod.parser.psi.PodFile) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiElement(com.intellij.psi.PsiElement)

Example 29 with CaretModel

use of com.intellij.openapi.editor.CaretModel in project android by JetBrains.

the class CreateTypedResourceFileAction method doCreateAndNavigate.

PsiElement[] doCreateAndNavigate(String newName, PsiDirectory directory, String rootTagName, boolean chooseTagName, boolean navigate) throws Exception {
    final XmlFile file = AndroidResourceUtil.createFileResource(newName, directory, rootTagName, myResourceType.getName(), myValuesResourceFile);
    if (navigate) {
        doNavigate(file);
    }
    if (chooseTagName) {
        XmlDocument document = file.getDocument();
        if (document != null) {
            XmlTag rootTag = document.getRootTag();
            if (rootTag != null) {
                final Project project = file.getProject();
                final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
                if (editor != null) {
                    CaretModel caretModel = editor.getCaretModel();
                    caretModel.moveToOffset(rootTag.getTextOffset() + 1);
                    XmlTagInplaceRenamer.rename(editor, rootTag);
                }
            }
        }
    }
    return new PsiElement[] { file };
}
Also used : Project(com.intellij.openapi.project.Project) CaretModel(com.intellij.openapi.editor.CaretModel) XmlFile(com.intellij.psi.xml.XmlFile) XmlDocument(com.intellij.psi.xml.XmlDocument) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 30 with CaretModel

use of com.intellij.openapi.editor.CaretModel in project intellij-plugins by JetBrains.

the class DartIntroduceHandler method performAction.

public void performAction(DartIntroduceOperation operation) {
    final PsiFile file = operation.getFile();
    if (!CommonRefactoringUtil.checkReadOnlyStatus(file)) {
        return;
    }
    final Editor editor = operation.getEditor();
    if (editor.getSettings().isVariableInplaceRenameEnabled()) {
        final TemplateState templateState = TemplateManagerImpl.getTemplateState(operation.getEditor());
        if (templateState != null && !templateState.isFinished()) {
            return;
        }
    }
    PsiElement element1 = null;
    PsiElement element2 = null;
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (selectionModel.hasSelection()) {
        element1 = file.findElementAt(selectionModel.getSelectionStart());
        element2 = file.findElementAt(selectionModel.getSelectionEnd() - 1);
        if (element1 instanceof PsiWhiteSpace) {
            int startOffset = element1.getTextRange().getEndOffset();
            element1 = file.findElementAt(startOffset);
        }
        if (element2 instanceof PsiWhiteSpace) {
            int endOffset = element2.getTextRange().getStartOffset();
            element2 = file.findElementAt(endOffset - 1);
        }
    } else {
        if (smartIntroduce(operation)) {
            return;
        }
        final CaretModel caretModel = editor.getCaretModel();
        final Document document = editor.getDocument();
        int lineNumber = document.getLineNumber(caretModel.getOffset());
        if ((lineNumber >= 0) && (lineNumber < document.getLineCount())) {
            element1 = file.findElementAt(document.getLineStartOffset(lineNumber));
            element2 = file.findElementAt(document.getLineEndOffset(lineNumber) - 1);
        }
    }
    final Project project = operation.getProject();
    if (element1 == null || element2 == null) {
        showCannotPerformError(project, editor);
        return;
    }
    element1 = DartRefactoringUtil.getSelectedExpression(project, file, element1, element2);
    if (element1 == null) {
        showCannotPerformError(project, editor);
        return;
    }
    if (!checkIntroduceContext(file, editor, element1)) {
        return;
    }
    operation.setElement(element1);
    performActionOnElement(operation);
}
Also used : Project(com.intellij.openapi.project.Project) CaretModel(com.intellij.openapi.editor.CaretModel) SelectionModel(com.intellij.openapi.editor.SelectionModel) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Aggregations

CaretModel (com.intellij.openapi.editor.CaretModel)57 Document (com.intellij.openapi.editor.Document)23 PsiElement (com.intellij.psi.PsiElement)14 SelectionModel (com.intellij.openapi.editor.SelectionModel)10 TextRange (com.intellij.openapi.util.TextRange)9 Editor (com.intellij.openapi.editor.Editor)8 Project (com.intellij.openapi.project.Project)7 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)6 Nullable (org.jetbrains.annotations.Nullable)6 EditorEx (com.intellij.openapi.editor.ex.EditorEx)5 IElementType (com.intellij.psi.tree.IElementType)5 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)3 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)3 PsiFile (com.intellij.psi.PsiFile)3 List (java.util.List)3 InsertionContext (com.intellij.codeInsight.completion.InsertionContext)2 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)2 Caret (com.intellij.openapi.editor.Caret)2 ScrollingModel (com.intellij.openapi.editor.ScrollingModel)2