Search in sources :

Example 41 with Editor

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

the class IntroduceHandler method performIntroduceWithDialog.

protected void performIntroduceWithDialog(IntroduceOperation operation) {
    final Project project = operation.getProject();
    if (operation.getName() == null) {
        PyIntroduceDialog dialog = new PyIntroduceDialog(project, myDialogTitle, myValidator, getHelpId(), operation);
        if (!dialog.showAndGet()) {
            return;
        }
        operation.setName(dialog.getName());
        operation.setReplaceAll(dialog.doReplaceAllOccurrences());
        operation.setInitPlace(dialog.getInitPlace());
    }
    PsiElement declaration = performRefactoring(operation);
    final Editor editor = operation.getEditor();
    editor.getCaretModel().moveToOffset(declaration.getTextRange().getEndOffset());
    editor.getSelectionModel().removeSelection();
}
Also used : Project(com.intellij.openapi.project.Project) Editor(com.intellij.openapi.editor.Editor)

Example 42 with Editor

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

the class IntroduceHandler method performAction.

public void performAction(IntroduceOperation 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();
    boolean singleElementSelection = false;
    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);
        }
        if (element1 == element2) {
            singleElementSelection = true;
        }
    } 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 = PyRefactoringUtil.getSelectedExpression(project, file, element1, element2);
    final PyComprehensionElement comprehension = PsiTreeUtil.getParentOfType(element1, PyComprehensionElement.class, true);
    if (element1 == null || comprehension != null) {
        showCannotPerformError(project, editor);
        return;
    }
    if (singleElementSelection && element1 instanceof PyStringLiteralExpression) {
        final PyStringLiteralExpression literal = (PyStringLiteralExpression) element1;
        // Currently introduce for substrings of a multi-part string literals is not supported
        if (literal.getStringNodes().size() > 1) {
            showCannotPerformError(project, editor);
            return;
        }
        final int offset = element1.getTextOffset();
        final TextRange selectionRange = TextRange.create(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
        final TextRange elementRange = element1.getTextRange();
        if (!elementRange.equals(selectionRange) && elementRange.contains(selectionRange)) {
            final TextRange innerRange = literal.getStringValueTextRange();
            final TextRange intersection = selectionRange.shiftRight(-offset).intersection(innerRange);
            final TextRange finalRange = intersection != null ? intersection : selectionRange;
            final String text = literal.getText();
            if (getFormatValueExpression(literal) != null && breaksStringFormatting(text, finalRange) || getNewStyleFormatValueExpression(literal) != null && breaksNewStyleStringFormatting(text, finalRange) || breaksStringEscaping(text, finalRange)) {
                showCannotPerformError(project, editor);
                return;
            }
            element1.putUserData(PyReplaceExpressionUtil.SELECTION_BREAKS_AST_NODE, Pair.create(element1, finalRange));
        }
    }
    if (!checkIntroduceContext(file, editor, element1)) {
        return;
    }
    operation.setElement(element1);
    performActionOnElement(operation);
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) Project(com.intellij.openapi.project.Project) SelectionModel(com.intellij.openapi.editor.SelectionModel) Editor(com.intellij.openapi.editor.Editor)

Example 43 with Editor

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

the class IntroduceHandler method smartIntroduce.

private boolean smartIntroduce(final IntroduceOperation operation) {
    final Editor editor = operation.getEditor();
    final PsiFile file = operation.getFile();
    final int offset = editor.getCaretModel().getOffset();
    PsiElement elementAtCaret = file.findElementAt(offset);
    if ((elementAtCaret instanceof PsiWhiteSpace && offset == elementAtCaret.getTextOffset() || elementAtCaret == null) && offset > 0) {
        elementAtCaret = file.findElementAt(offset - 1);
    }
    if (!checkIntroduceContext(file, editor, elementAtCaret))
        return true;
    final List<PyExpression> expressions = new ArrayList<>();
    while (elementAtCaret != null) {
        if (elementAtCaret instanceof PyStatement || elementAtCaret instanceof PyFile) {
            break;
        }
        if (elementAtCaret instanceof PyExpression && isValidIntroduceVariant(elementAtCaret)) {
            expressions.add((PyExpression) elementAtCaret);
        }
        elementAtCaret = elementAtCaret.getParent();
    }
    if (expressions.size() == 1 || ApplicationManager.getApplication().isUnitTestMode()) {
        operation.setElement(expressions.get(0));
        performActionOnElement(operation);
        return true;
    } else if (expressions.size() > 1) {
        IntroduceTargetChooser.showChooser(editor, expressions, new Pass<PyExpression>() {

            @Override
            public void pass(PyExpression pyExpression) {
                operation.setElement(pyExpression);
                performActionOnElement(operation);
            }
        }, pyExpression -> pyExpression.getText());
        return true;
    }
    return false;
}
Also used : PyNoneType(com.jetbrains.python.psi.types.PyNoneType) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) PyNames(com.jetbrains.python.PyNames) java.util(java.util) DataContext(com.intellij.openapi.actionSystem.DataContext) SelectionModel(com.intellij.openapi.editor.SelectionModel) TemplateManagerImpl(com.intellij.codeInsight.template.impl.TemplateManagerImpl) Document(com.intellij.openapi.editor.Document) ScopeUtil(com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil) RefactoringEventListener(com.intellij.refactoring.listeners.RefactoringEventListener) CaretModel(com.intellij.openapi.editor.CaretModel) PyRefactoringUtil(com.jetbrains.python.refactoring.PyRefactoringUtil) PyUtil.as(com.jetbrains.python.psi.PyUtil.as) PyPsiUtils(com.jetbrains.python.psi.impl.PyPsiUtils) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) InplaceVariableIntroducer(com.intellij.refactoring.introduce.inplace.InplaceVariableIntroducer) Project(com.intellij.openapi.project.Project) com.jetbrains.python.psi(com.jetbrains.python.psi) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) IntroduceTargetChooser(com.intellij.refactoring.IntroduceTargetChooser) RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) NameSuggesterUtil(com.jetbrains.python.refactoring.NameSuggesterUtil) PyBundle(com.jetbrains.python.PyBundle) PyReplaceExpressionUtil(com.jetbrains.python.refactoring.PyReplaceExpressionUtil) PyType(com.jetbrains.python.psi.types.PyType) PyTokenTypes(com.jetbrains.python.PyTokenTypes) PyStringFormatParser(com.jetbrains.python.inspections.PyStringFormatParser) TextRange(com.intellij.openapi.util.TextRange) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) Editor(com.intellij.openapi.editor.Editor) ASTNode(com.intellij.lang.ASTNode) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) Nullable(org.jetbrains.annotations.Nullable) Pass(com.intellij.openapi.util.Pass) OccurrencesChooser(com.intellij.refactoring.introduce.inplace.OccurrencesChooser) StreamEx(one.util.streamex.StreamEx) Result(com.intellij.openapi.application.Result) Pair(com.intellij.openapi.util.Pair) ApplicationManager(com.intellij.openapi.application.ApplicationManager) RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler) com.intellij.psi(com.intellij.psi) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) NotNull(org.jetbrains.annotations.NotNull) CodeInsightUtilCore(com.intellij.codeInsight.CodeInsightUtilCore) Pass(com.intellij.openapi.util.Pass) Editor(com.intellij.openapi.editor.Editor)

Example 44 with Editor

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

the class XmlZenCodingGenerator method computeTemplateKey.

@Nullable
@Override
public String computeTemplateKey(@NotNull CustomTemplateCallback callback) {
    Editor editor = callback.getEditor();
    int currentOffset = editor.getCaretModel().getOffset();
    int startOffset = Math.min(editor.getDocument().getLineStartOffset(editor.getDocument().getLineNumber(currentOffset)), currentOffset);
    CharSequence documentText = editor.getDocument().getCharsSequence();
    PsiElement prevVisibleLeaf = callback.getContext();
    while (prevVisibleLeaf != null) {
        TextRange textRange = prevVisibleLeaf.getTextRange();
        final int endOffset = textRange.getEndOffset();
        if (endOffset <= currentOffset) {
            if (endOffset <= startOffset) {
                break;
            }
            IElementType prevType = prevVisibleLeaf.getNode().getElementType();
            if (prevType == XmlTokenType.XML_TAG_END || prevType == XmlTokenType.XML_EMPTY_ELEMENT_END) {
                startOffset = endOffset;
                break;
            }
        }
        prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
    }
    if (startOffset < 0 || currentOffset > documentText.length() || currentOffset < startOffset) {
        Logger.getInstance(getClass()).error("Error while calculating emmet abbreviation. Offset: " + currentOffset + "; Start: " + startOffset, AttachmentFactory.createAttachment(editor.getDocument()));
        return null;
    }
    String key = computeKey(documentText.subSequence(startOffset, currentOffset));
    return !StringUtil.isEmpty(key) && ZenCodingTemplate.checkTemplateKey(key, callback, this) ? key : null;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 45 with Editor

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

the class ZenCodingGenerator method computeTemplateKey.

@Nullable
public String computeTemplateKey(@NotNull CustomTemplateCallback callback) {
    Editor editor = callback.getEditor();
    int currentOffset = editor.getCaretModel().getOffset();
    int startOffset = editor.getDocument().getLineStartOffset(editor.getCaretModel().getLogicalPosition().line);
    String key = computeKey(editor.getDocument().getCharsSequence().subSequence(startOffset, currentOffset));
    return !StringUtil.isEmpty(key) && ZenCodingTemplate.checkTemplateKey(key, callback, this) ? key : null;
}
Also used : Editor(com.intellij.openapi.editor.Editor) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Editor (com.intellij.openapi.editor.Editor)748 Project (com.intellij.openapi.project.Project)281 PsiFile (com.intellij.psi.PsiFile)171 VirtualFile (com.intellij.openapi.vfs.VirtualFile)122 NotNull (org.jetbrains.annotations.NotNull)110 Document (com.intellij.openapi.editor.Document)108 PsiElement (com.intellij.psi.PsiElement)107 Nullable (org.jetbrains.annotations.Nullable)103 TextRange (com.intellij.openapi.util.TextRange)77 FileEditor (com.intellij.openapi.fileEditor.FileEditor)67 TextEditor (com.intellij.openapi.fileEditor.TextEditor)48 ArrayList (java.util.ArrayList)39 IncorrectOperationException (com.intellij.util.IncorrectOperationException)36 List (java.util.List)36 EditorEx (com.intellij.openapi.editor.ex.EditorEx)35 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)29 DataContext (com.intellij.openapi.actionSystem.DataContext)27 ApplicationManager (com.intellij.openapi.application.ApplicationManager)25 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)25 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)22