Search in sources :

Example 71 with Editor

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

the class GrIntroduceParameterHandler method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file, @Nullable final DataContext dataContext) {
    if (editor == null || file == null)
        return;
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (!selectionModel.hasSelection()) {
        final int offset = editor.getCaretModel().getOffset();
        final List<GrExpression> expressions = GrIntroduceHandlerBase.collectExpressions(file, editor, offset, false);
        if (expressions.isEmpty()) {
            GrIntroduceHandlerBase.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());
                }
            }, grExpression -> grExpression.getText());
            return;
        }
    }
    invoke(project, editor, file, selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
}
Also used : GroovyRefactoringBundle(org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringBundle) HelpID(org.jetbrains.plugins.groovy.refactoring.HelpID) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) DataContext(com.intellij.openapi.actionSystem.DataContext) SelectionModel(com.intellij.openapi.editor.SelectionModel) GrParametersOwner(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) RefactoringBundle(com.intellij.refactoring.RefactoringBundle) GrIntroduceVariableHandler(org.jetbrains.plugins.groovy.refactoring.introduce.variable.GrIntroduceVariableHandler) MethodOrClosureScopeChooser(org.jetbrains.plugins.groovy.refactoring.ui.MethodOrClosureScopeChooser) ArrayList(java.util.ArrayList) InitialInfo(org.jetbrains.plugins.groovy.refactoring.extract.InitialInfo) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) IntroduceOccurrencesChooser(org.jetbrains.plugins.groovy.refactoring.introduce.IntroduceOccurrencesChooser) GrIntroduceHandlerBase(org.jetbrains.plugins.groovy.refactoring.introduce.GrIntroduceHandlerBase) GrRefactoringError(org.jetbrains.plugins.groovy.refactoring.GrRefactoringError) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) IntroduceTargetChooser(com.intellij.refactoring.IntroduceTargetChooser) GrIntroduceContext(org.jetbrains.plugins.groovy.refactoring.introduce.GrIntroduceContext) PsiMethod(com.intellij.psi.PsiMethod) TextRange(com.intellij.openapi.util.TextRange) SuperMethodWarningUtil(com.intellij.ide.util.SuperMethodWarningUtil) Editor(com.intellij.openapi.editor.Editor) JBPopup(com.intellij.openapi.ui.popup.JBPopup) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Pass(com.intellij.openapi.util.Pass) OccurrencesChooser(com.intellij.refactoring.introduce.inplace.OccurrencesChooser) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ApplicationManager(com.intellij.openapi.application.ApplicationManager) RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler) NotNull(org.jetbrains.annotations.NotNull) GroovyExtractChooser(org.jetbrains.plugins.groovy.refactoring.extract.GroovyExtractChooser) Pass(com.intellij.openapi.util.Pass) SelectionModel(com.intellij.openapi.editor.SelectionModel) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TextRange(com.intellij.openapi.util.TextRange)

Example 72 with Editor

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

the class GrIntroduceClosureParameterProcessor method processInternalUsages.

private static void processInternalUsages(UsageInfo[] usages, GrIntroduceParameterSettings settings) {
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(settings.getProject());
    // Replacing expression occurrences
    for (UsageInfo usage : usages) {
        if (usage instanceof ChangedMethodCallInfo) {
            PsiElement element = usage.getElement();
            processChangedMethodCall(element, settings);
        } else if (usage instanceof InternalUsageInfo) {
            PsiElement element = usage.getElement();
            if (element == null)
                continue;
            GrExpression newExpr = factory.createExpressionFromText(settings.getName());
            if (element instanceof GrExpression) {
                ((GrExpression) element).replaceWithExpression(newExpr, true);
            } else {
                element.replace(newExpr);
            }
        }
    }
    final StringPartInfo info = settings.getStringPartInfo();
    if (info != null) {
        final GrExpression expr = info.replaceLiteralWithConcatenation(settings.getName());
        final Editor editor = PsiUtilBase.findEditor(expr);
        if (editor != null) {
            editor.getSelectionModel().removeSelection();
            editor.getCaretModel().moveToOffset(expr.getTextRange().getEndOffset());
        }
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) ChangedMethodCallInfo(com.intellij.refactoring.introduceParameter.ChangedMethodCallInfo) Editor(com.intellij.openapi.editor.Editor) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) InternalUsageInfo(com.intellij.refactoring.introduceParameter.InternalUsageInfo) InternalUsageInfo(com.intellij.refactoring.introduceParameter.InternalUsageInfo)

Example 73 with Editor

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

the class PropertyRenameHandler method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiElement[] elements, @Nullable DataContext dataContext) {
    PsiElement element = elements.length == 1 ? elements[0] : null;
    if (element == null)
        element = getElement(dataContext);
    Editor editor = dataContext == null ? null : CommonDataKeys.EDITOR.getData(dataContext);
    invokeInner(project, editor, element);
}
Also used : Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 74 with Editor

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

the class PropertiesAnnotator method highlightTokens.

private static void highlightTokens(final Property property, final ASTNode node, final AnnotationHolder holder, PropertiesHighlighter highlighter) {
    Lexer lexer = highlighter.getHighlightingLexer();
    final String s = node.getText();
    lexer.start(s);
    while (lexer.getTokenType() != null) {
        IElementType elementType = lexer.getTokenType();
        TextAttributesKey[] keys = highlighter.getTokenHighlights(elementType);
        for (TextAttributesKey key : keys) {
            Pair<String, HighlightSeverity> pair = PropertiesHighlighter.DISPLAY_NAMES.get(key);
            String displayName = pair.getFirst();
            HighlightSeverity severity = pair.getSecond();
            if (severity != null) {
                int start = lexer.getTokenStart() + node.getTextRange().getStartOffset();
                int end = lexer.getTokenEnd() + node.getTextRange().getStartOffset();
                TextRange textRange = new TextRange(start, end);
                final Annotation annotation;
                if (severity == HighlightSeverity.WARNING) {
                    annotation = holder.createWarningAnnotation(textRange, displayName);
                } else if (severity == HighlightSeverity.ERROR) {
                    annotation = holder.createErrorAnnotation(textRange, displayName);
                } else {
                    annotation = holder.createInfoAnnotation(textRange, displayName);
                }
                TextAttributes attributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(key);
                annotation.setEnforcedTextAttributes(attributes);
                if (key == PropertiesHighlighter.PROPERTIES_INVALID_STRING_ESCAPE) {
                    annotation.registerFix(new IntentionAction() {

                        @NotNull
                        public String getText() {
                            return PropertiesBundle.message("unescape");
                        }

                        @NotNull
                        public String getFamilyName() {
                            return getText();
                        }

                        public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
                            if (!property.isValid() || !property.getManager().isInProject(property))
                                return false;
                            String text = property.getPropertiesFile().getContainingFile().getText();
                            int startOffset = annotation.getStartOffset();
                            return text.length() > startOffset && text.charAt(startOffset) == '\\';
                        }

                        public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
                            int offset = annotation.getStartOffset();
                            if (property.getPropertiesFile().getContainingFile().getText().charAt(offset) == '\\') {
                                editor.getDocument().deleteString(offset, offset + 1);
                            }
                        }

                        public boolean startInWriteAction() {
                            return true;
                        }
                    });
                }
            }
        }
        lexer.advance();
    }
}
Also used : HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) TextRange(com.intellij.openapi.util.TextRange) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) NotNull(org.jetbrains.annotations.NotNull) Annotation(com.intellij.lang.annotation.Annotation) IElementType(com.intellij.psi.tree.IElementType) Project(com.intellij.openapi.project.Project) Lexer(com.intellij.lexer.Lexer) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor)

Example 75 with Editor

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

the class PySmartEnterTest method doTest.

public void doTest() {
    myFixture.configureByFile("codeInsight/smartEnter/" + getTestName(true) + ".py");
    final List<SmartEnterProcessor> processors = getSmartProcessors(PythonLanguage.getInstance());
    new WriteCommandAction(myFixture.getProject()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            final Editor editor = myFixture.getEditor();
            for (SmartEnterProcessor processor : processors) {
                processor.process(myFixture.getProject(), editor, myFixture.getFile());
            }
        }
    }.execute();
    myFixture.checkResultByFile("codeInsight/smartEnter/" + getTestName(true) + "_after.py", true);
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Editor(com.intellij.openapi.editor.Editor) SmartEnterProcessor(com.intellij.codeInsight.editorActions.smartEnter.SmartEnterProcessor) Result(com.intellij.openapi.application.Result)

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