Search in sources :

Example 46 with CaretModel

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

the class FlexLiveTemplatesTest method testPublicFunctionWithTypingInMxml.

public void testPublicFunctionWithTypingInMxml() throws Exception {
    doTest("pf", "mxml", segmentIndex -> {
        final CaretModel caretModel = getEditor().getCaretModel();
        switch(segmentIndex) {
            case 0:
                type("functionName");
                caretModel.moveToOffset(caretModel.getOffset() + 1);
                break;
            case 1:
                type("param:Object");
                caretModel.moveToOffset(caretModel.getOffset() + 1);
                break;
            case 2:
                type("String");
                caretModel.moveToOffset(caretModel.getOffset() + 1);
                break;
        }
    });
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel)

Example 47 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)47 Document (com.intellij.openapi.editor.Document)20 PsiElement (com.intellij.psi.PsiElement)11 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 Nullable (org.jetbrains.annotations.Nullable)6 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)5 EditorEx (com.intellij.openapi.editor.ex.EditorEx)3 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)3 List (java.util.List)3 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)2 Caret (com.intellij.openapi.editor.Caret)2 ScrollingModel (com.intellij.openapi.editor.ScrollingModel)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiFile (com.intellij.psi.PsiFile)2 XmlFile (com.intellij.psi.xml.XmlFile)2 XmlTag (com.intellij.psi.xml.XmlTag)2 ArrayList (java.util.ArrayList)2