Search in sources :

Example 11 with CaretModel

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

the class CurrentLineMarker method attach.

public void attach(EditorSource editorSource) {
    if (myEditor != null)
        hide();
    myEditor = editorSource.getEditor();
    if (myEditor == null)
        return;
    final CaretModel caretModel = myEditor.getCaretModel();
    caretModel.addCaretListener(this);
    editorSource.addDisposable(new Disposable() {

        public void dispose() {
            caretModel.removeCaretListener(CurrentLineMarker.this);
        }
    });
}
Also used : Disposable(com.intellij.openapi.Disposable) CaretModel(com.intellij.openapi.editor.CaretModel)

Example 12 with CaretModel

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

the class ReplaceOctalEscapeWithUnicodeEscapeIntention method processIntention.

@Override
protected void processIntention(Editor editor, @NotNull PsiElement element) {
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (selectionModel.hasSelection()) {
        // does not check if octal escape is inside char or string literal (garbage in, garbage out)
        final Document document = editor.getDocument();
        final int start = selectionModel.getSelectionStart();
        final int end = selectionModel.getSelectionEnd();
        final String text = document.getText(new TextRange(start, end));
        final int textLength = end - start;
        final StringBuilder replacement = new StringBuilder(textLength);
        int anchor = 0;
        while (true) {
            final int index = indexOfOctalEscape(text, anchor + 1);
            if (index < 0) {
                break;
            }
            replacement.append(text.substring(anchor, index));
            anchor = appendUnicodeEscape(text, index, replacement);
        }
        replacement.append(text.substring(anchor, textLength));
        document.replaceString(start, end, replacement);
    } else if (element instanceof PsiLiteralExpression) {
        final PsiLiteralExpression literalExpression = (PsiLiteralExpression) element;
        final String text = literalExpression.getText();
        final CaretModel model = editor.getCaretModel();
        final int offset = model.getOffset() - literalExpression.getTextOffset();
        final StringBuilder newLiteralText = new StringBuilder();
        final int index1 = indexOfOctalEscape(text, offset);
        final int index2 = indexOfOctalEscape(text, offset + 1);
        final int escapeStart = index2 == offset ? index2 : index1;
        newLiteralText.append(text.substring(0, escapeStart));
        final int escapeEnd = appendUnicodeEscape(text, escapeStart, newLiteralText);
        newLiteralText.append(text.substring(escapeEnd, text.length()));
        PsiReplacementUtil.replaceExpression(literalExpression, newLiteralText.toString());
    }
}
Also used : PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) CaretModel(com.intellij.openapi.editor.CaretModel) SelectionModel(com.intellij.openapi.editor.SelectionModel) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document)

Example 13 with CaretModel

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

the class CreateSubclassAction method isAvailable.

@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    final CaretModel caretModel = editor.getCaretModel();
    final int position = caretModel.getOffset();
    PsiElement element = file.findElementAt(position);
    PsiClass psiClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
    if (psiClass == null || psiClass.isAnnotationType() || psiClass.isEnum() || psiClass instanceof PsiAnonymousClass || psiClass.hasModifierProperty(PsiModifier.FINAL)) {
        return false;
    }
    VirtualFile virtualFile = PsiUtilCore.getVirtualFile(psiClass);
    if (virtualFile == null || virtualFile.getFileType() == ScratchFileType.INSTANCE) {
        return false;
    }
    if (!isSupportedLanguage(psiClass))
        return false;
    final PsiMethod[] constructors = psiClass.getConstructors();
    if (constructors.length > 0) {
        boolean hasNonPrivateConstructor = false;
        for (PsiMethod constructor : constructors) {
            if (!constructor.hasModifierProperty(PsiModifier.PRIVATE)) {
                hasNonPrivateConstructor = true;
                break;
            }
        }
        if (!hasNonPrivateConstructor)
            return false;
    }
    PsiElement lBrace = psiClass.getLBrace();
    if (lBrace == null)
        return false;
    if (element.getTextOffset() >= lBrace.getTextOffset())
        return false;
    TextRange declarationRange = HighlightNamesUtil.getClassDeclarationTextRange(psiClass);
    final TextRange elementTextRange = element.getTextRange();
    if (!declarationRange.contains(elementTextRange)) {
        if (!(element instanceof PsiWhiteSpace) || (declarationRange.getStartOffset() != elementTextRange.getEndOffset() && declarationRange.getEndOffset() != elementTextRange.getStartOffset())) {
            return false;
        }
    }
    myText = getTitle(psiClass);
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) CaretModel(com.intellij.openapi.editor.CaretModel) TextRange(com.intellij.openapi.util.TextRange)

Example 14 with CaretModel

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

the class ExtractClassHandler method invoke.

public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
    final ScrollingModel scrollingModel = editor.getScrollingModel();
    scrollingModel.scrollToCaret(ScrollType.MAKE_VISIBLE);
    final CaretModel caretModel = editor.getCaretModel();
    final int position = caretModel.getOffset();
    final PsiElement element = file.findElementAt(position);
    final PsiMember selectedMember = PsiTreeUtil.getParentOfType(element, PsiMember.class, true);
    if (selectedMember == null) {
        //todo
        return;
    }
    PsiClass containingClass = selectedMember.getContainingClass();
    if (containingClass == null && selectedMember instanceof PsiClass) {
        containingClass = (PsiClass) selectedMember;
    }
    final String cannotRefactorMessage = getCannotRefactorMessage(containingClass);
    if (cannotRefactorMessage != null) {
        CommonRefactoringUtil.showErrorHint(project, editor, RefactorJBundle.message("cannot.perform.the.refactoring") + cannotRefactorMessage, ExtractClassProcessor.REFACTORING_NAME, getHelpID());
        return;
    }
    new ExtractClassDialog(containingClass, selectedMember).show();
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) ScrollingModel(com.intellij.openapi.editor.ScrollingModel)

Example 15 with CaretModel

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

the class CreateLocalVarFromInstanceofAction method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) {
    PsiInstanceOfExpression instanceOfExpression = getInstanceOfExpressionAtCaret(editor, file);
    assert instanceOfExpression.getContainingFile() == file : instanceOfExpression.getContainingFile() + "; file=" + file;
    try {
        final PsiStatement statementInside = isNegated(instanceOfExpression) ? null : getExpressionStatementInside(file, editor, instanceOfExpression.getOperand());
        PsiDeclarationStatement decl = createLocalVariableDeclaration(instanceOfExpression, statementInside);
        if (decl == null)
            return;
        decl = (PsiDeclarationStatement) CodeStyleManager.getInstance(project).reformat(decl);
        decl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(decl);
        PsiLocalVariable localVariable = (PsiLocalVariable) decl.getDeclaredElements()[0];
        TemplateBuilderImpl builder = new TemplateBuilderImpl(localVariable);
        builder.setEndVariableAfter(localVariable.getNameIdentifier());
        Template template = generateTemplate(project, localVariable.getInitializer(), localVariable.getType());
        Editor newEditor = CreateFromUsageBaseFix.positionCursor(project, file, localVariable.getNameIdentifier());
        if (newEditor == null)
            return;
        TextRange range = localVariable.getNameIdentifier().getTextRange();
        newEditor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
        CreateFromUsageBaseFix.startTemplate(newEditor, template, project, new TemplateEditingAdapter() {

            @Override
            public void templateFinished(Template template, boolean brokenOff) {
                ApplicationManager.getApplication().runWriteAction(() -> {
                    PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
                    CaretModel caretModel = editor.getCaretModel();
                    PsiElement elementAt = file.findElementAt(caretModel.getOffset());
                    PsiDeclarationStatement declarationStatement = PsiTreeUtil.getParentOfType(elementAt, PsiDeclarationStatement.class);
                    if (declarationStatement != null) {
                        caretModel.moveToOffset(declarationStatement.getTextRange().getEndOffset());
                    }
                    new EnterAction().actionPerformed(editor, DataManager.getInstance().getDataContext());
                });
            }
        });
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) EnterAction(com.intellij.openapi.editor.actions.EnterAction) TextRange(com.intellij.openapi.util.TextRange) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor)

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