Search in sources :

Example 56 with SelectionModel

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

the class GrIntroduceHandlerBase method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file, @Nullable final DataContext dataContext) {
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (!selectionModel.hasSelection()) {
        final int offset = editor.getCaretModel().getOffset();
        final List<GrExpression> expressions = collectExpressions(file, editor, offset, false);
        if (expressions.isEmpty()) {
            updateSelectionForVariable(editor, file, selectionModel, offset);
        } else if (expressions.size() == 1 || ApplicationManager.getApplication().isUnitTestMode()) {
            final TextRange textRange = expressions.get(0).getTextRange();
            selectionModel.setSelection(textRange.getStartOffset(), textRange.getEndOffset());
        } else {
            IntroduceTargetChooser.showChooser(editor, expressions, new Pass<GrExpression>() {

                @Override
                public void pass(final GrExpression selectedValue) {
                    invoke(project, editor, file, selectedValue.getTextRange().getStartOffset(), selectedValue.getTextRange().getEndOffset());
                }
            }, GR_EXPRESSION_RENDERER);
            return;
        }
    }
    invoke(project, editor, file, selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel)

Example 57 with SelectionModel

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

the class I18nizeQuickFix method checkApplicability.

@Override
public void checkApplicability(final PsiFile psiFile, final Editor editor) throws IncorrectOperationException {
    PsiLiteralExpression literalExpression = I18nizeAction.getEnclosingStringLiteral(psiFile, editor);
    if (literalExpression != null) {
        SelectionModel selectionModel = editor.getSelectionModel();
        if (!selectionModel.hasSelection())
            return;
        int start = selectionModel.getSelectionStart();
        int end = selectionModel.getSelectionEnd();
        TextRange textRange = literalExpression.getTextRange();
        if (textRange.contains(start) && textRange.contains(end)) {
            mySelectionRange = new TextRange(start, end);
            return;
        }
    }
    String message = CodeInsightBundle.message("i18nize.error.message");
    throw new IncorrectOperationException(message);
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) TextRange(com.intellij.openapi.util.TextRange) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 58 with SelectionModel

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

the class Intention method findMatchingElement.

@Nullable
PsiElement findMatchingElement(PsiFile file, Editor editor) {
    if (!file.getViewProvider().getLanguages().contains(GroovyLanguage.INSTANCE)) {
        return null;
    }
    SelectionModel selectionModel = editor.getSelectionModel();
    if (selectionModel.hasSelection()) {
        int start = selectionModel.getSelectionStart();
        int end = selectionModel.getSelectionEnd();
        if (0 <= start && start <= end) {
            TextRange selectionRange = new TextRange(start, end);
            PsiElement element = PsiImplUtil.findElementInRange(file, start, end, PsiElement.class);
            while (element != null && element.getTextRange() != null && selectionRange.contains(element.getTextRange())) {
                if (predicate.satisfiedBy(element))
                    return element;
                element = element.getParent();
            }
        }
    }
    final int position = editor.getCaretModel().getOffset();
    PsiElement element = file.findElementAt(position);
    while (element != null) {
        if (predicate.satisfiedBy(element))
            return element;
        if (isStopElement(element))
            break;
        element = element.getParent();
    }
    element = file.findElementAt(position - 1);
    while (element != null) {
        if (predicate.satisfiedBy(element))
            return element;
        if (isStopElement(element))
            return null;
        element = element.getParent();
    }
    return null;
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 59 with SelectionModel

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

the class ExtractMethodHandler method getElements.

public static PsiElement[] getElements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (selectionModel.hasSelection()) {
        int startOffset = selectionModel.getSelectionStart();
        int endOffset = selectionModel.getSelectionEnd();
        PsiElement[] elements;
        PsiExpression expr = CodeInsightUtil.findExpressionInRange(file, startOffset, endOffset);
        if (expr != null) {
            elements = new PsiElement[] { expr };
        } else {
            elements = CodeInsightUtil.findStatementsInRange(file, startOffset, endOffset);
            if (elements.length == 0) {
                final PsiExpression expression = IntroduceVariableBase.getSelectedExpression(project, file, startOffset, endOffset);
                if (expression != null && IntroduceVariableBase.getErrorMessage(expression) == null) {
                    final PsiType originalType = RefactoringUtil.getTypeByExpressionWithExpectedType(expression);
                    if (originalType != null) {
                        elements = new PsiElement[] { expression };
                    }
                }
            }
        }
        return elements;
    }
    final List<PsiExpression> expressions = IntroduceVariableBase.collectExpressions(file, editor, editor.getCaretModel().getOffset());
    return expressions.toArray(new PsiElement[expressions.size()]);
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel)

Example 60 with SelectionModel

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

the class EscapeHandler method execute.

@Override
public void execute(Editor editor, DataContext dataContext) {
    TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
    if (templateState != null && !templateState.isFinished()) {
        SelectionModel selectionModel = editor.getSelectionModel();
        LookupImpl lookup = (LookupImpl) LookupManager.getActiveLookup(editor);
        // the idea behind lookup checking is that if there is a preselected value in lookup 
        // then user might want just to close lookup but not finish a template.
        // E.g. user wants to move to the next template segment by Tab without completion invocation.
        // If there is no selected value in completion that user definitely wants to finish template
        boolean lookupIsEmpty = lookup == null || lookup.getCurrentItem() == null;
        if (!selectionModel.hasSelection() && lookupIsEmpty) {
            CommandProcessor.getInstance().setCurrentCommandName(CodeInsightBundle.message("finish.template.command"));
            templateState.gotoEnd(true);
            return;
        }
    }
    if (myOriginalHandler.isEnabled(editor, dataContext)) {
        myOriginalHandler.execute(editor, dataContext);
    }
}
Also used : LookupImpl(com.intellij.codeInsight.lookup.impl.LookupImpl) SelectionModel(com.intellij.openapi.editor.SelectionModel) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Aggregations

SelectionModel (com.intellij.openapi.editor.SelectionModel)76 TextRange (com.intellij.openapi.util.TextRange)21 Document (com.intellij.openapi.editor.Document)19 PsiElement (com.intellij.psi.PsiElement)19 NotNull (org.jetbrains.annotations.NotNull)16 Editor (com.intellij.openapi.editor.Editor)14 Nullable (org.jetbrains.annotations.Nullable)11 CaretModel (com.intellij.openapi.editor.CaretModel)10 PsiFile (com.intellij.psi.PsiFile)8 Project (com.intellij.openapi.project.Project)7 ArrayList (java.util.ArrayList)6 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)3 SurroundDescriptor (com.intellij.lang.surroundWith.SurroundDescriptor)3 Pass (com.intellij.openapi.util.Pass)3 List (java.util.List)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 EditorWindow (com.intellij.injected.editor.EditorWindow)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)2 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)2