Search in sources :

Example 86 with RangeMarker

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

the class DocumentBasedReplaceHandler method prepare.

@Override
public void prepare(ReplacementInfo info) {
    assert info instanceof ReplacementInfoImpl;
    MatchResult result = ((ReplacementInfoImpl) info).getMatchResult();
    PsiElement element = result.getMatch();
    PsiFile file = element instanceof PsiFile ? (PsiFile) element : element.getContainingFile();
    Document document = PsiDocumentManager.getInstance(myProject).getDocument(file);
    TextRange textRange = result.getMatch().getTextRange();
    assert textRange != null;
    RangeMarker rangeMarker = document.createRangeMarker(textRange);
    rangeMarker.setGreedyToLeft(true);
    rangeMarker.setGreedyToRight(true);
    myRangeMarkers.put(info, rangeMarker);
}
Also used : ReplacementInfoImpl(com.intellij.structuralsearch.plugin.replace.impl.ReplacementInfoImpl) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement)

Example 87 with RangeMarker

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

the class GroovyCompletionUtil method addImportForClass.

public static int addImportForClass(PsiFile file, int startOffset, int endOffset, PsiClass aClass) throws IncorrectOperationException {
    //    LOG.assertTrue(CommandProcessor.getInstance().getCurrentCommand() != null);
    //    LOG.assertTrue(
    //      ApplicationManager.getApplication().isUnitTestMode() || ApplicationManager.getApplication().getCurrentWriteAction(null) != null);
    final PsiManager manager = file.getManager();
    final Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
    int newStartOffset = startOffset;
    final PsiReference reference = file.findReferenceAt(endOffset - 1);
    if (reference != null) {
        final PsiElement resolved = reference.resolve();
        if (resolved instanceof PsiClass) {
            if (((PsiClass) resolved).getQualifiedName() == null || manager.areElementsEquivalent(aClass, resolved)) {
                return newStartOffset;
            }
        }
    }
    String name = aClass.getName();
    document.replaceString(startOffset, endOffset, name);
    final RangeMarker toDelete = JavaCompletionUtil.insertTemporary(endOffset, document, " ");
    PsiDocumentManager.getInstance(manager.getProject()).commitAllDocuments();
    final PsiReference ref = file.findReferenceAt(startOffset);
    if (ref instanceof GrReferenceElement && aClass.isValid()) {
        PsiElement newElement = ref.bindToElement(aClass);
        RangeMarker marker = document.createRangeMarker(newElement.getTextRange());
        CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(newElement);
        newStartOffset = marker.getStartOffset();
    }
    if (toDelete.isValid()) {
        document.deleteString(toDelete.getStartOffset(), toDelete.getEndOffset());
    }
    return newStartOffset;
}
Also used : RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Example 88 with RangeMarker

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

the class ConstructorInsertHandler method insertParentheses.

public static boolean insertParentheses(InsertionContext context, LookupElement delegate, final PsiClass psiClass, final boolean forAnonymous) {
    if (context.getCompletionChar() == '[') {
        return false;
    }
    PsiMethod constructor = JavaConstructorCallElement.extractCalledConstructor(delegate);
    final PsiElement place = context.getFile().findElementAt(context.getStartOffset());
    assert place != null;
    boolean hasParams = constructor != null ? constructor.getParameterList().getParametersCount() > 0 : hasConstructorParameters(psiClass, place);
    RangeMarker refEnd = context.getDocument().createRangeMarker(context.getTailOffset(), context.getTailOffset());
    JavaCompletionUtil.insertParentheses(context, delegate, false, hasParams, forAnonymous);
    if (constructor != null) {
        PsiCallExpression call = JavaMethodCallElement.findCallAtOffset(context, refEnd.getStartOffset());
        if (call != null) {
            CompletionMemory.registerChosenMethod(constructor, call);
        }
    }
    return true;
}
Also used : RangeMarker(com.intellij.openapi.editor.RangeMarker)

Example 89 with RangeMarker

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

the class JavaCompletionUtil method insertClassReference.

public static int insertClassReference(PsiClass psiClass, PsiFile file, int startOffset, int endOffset) {
    final Project project = file.getProject();
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    documentManager.commitAllDocuments();
    final PsiManager manager = file.getManager();
    final Document document = FileDocumentManager.getInstance().getDocument(file.getViewProvider().getVirtualFile());
    final PsiReference reference = file.findReferenceAt(startOffset);
    if (reference != null) {
        final PsiElement resolved = reference.resolve();
        if (resolved instanceof PsiClass) {
            if (((PsiClass) resolved).getQualifiedName() == null || manager.areElementsEquivalent(psiClass, resolved)) {
                return endOffset;
            }
        }
    }
    String name = psiClass.getName();
    if (name == null) {
        return endOffset;
    }
    assert document != null;
    document.replaceString(startOffset, endOffset, name);
    int newEndOffset = startOffset + name.length();
    final RangeMarker toDelete = insertTemporary(newEndOffset, document, " ");
    documentManager.commitAllDocuments();
    PsiElement element = file.findElementAt(startOffset);
    if (element instanceof PsiIdentifier) {
        PsiElement parent = element.getParent();
        if (parent instanceof PsiJavaCodeReferenceElement && !((PsiJavaCodeReferenceElement) parent).isQualified() && !(parent.getParent() instanceof PsiPackageStatement)) {
            PsiJavaCodeReferenceElement ref = (PsiJavaCodeReferenceElement) parent;
            if (psiClass.isValid() && !psiClass.getManager().areElementsEquivalent(psiClass, resolveReference(ref))) {
                final boolean staticImport = ref instanceof PsiImportStaticReferenceElement;
                PsiElement newElement;
                try {
                    newElement = staticImport ? ((PsiImportStaticReferenceElement) ref).bindToTargetClass(psiClass) : ref.bindToElement(psiClass);
                } catch (IncorrectOperationException e) {
                    // can happen if fqn contains reserved words, for example
                    return endOffset;
                }
                final RangeMarker rangeMarker = document.createRangeMarker(newElement.getTextRange());
                documentManager.doPostponedOperationsAndUnblockDocument(document);
                documentManager.commitDocument(document);
                newElement = CodeInsightUtilCore.findElementInRange(file, rangeMarker.getStartOffset(), rangeMarker.getEndOffset(), PsiJavaCodeReferenceElement.class, JavaLanguage.INSTANCE);
                rangeMarker.dispose();
                if (newElement != null) {
                    newEndOffset = newElement.getTextRange().getEndOffset();
                    if (!(newElement instanceof PsiReferenceExpression)) {
                        PsiReferenceParameterList parameterList = ((PsiJavaCodeReferenceElement) newElement).getParameterList();
                        if (parameterList != null) {
                            newEndOffset = parameterList.getTextRange().getStartOffset();
                        }
                    }
                    if (!staticImport && !psiClass.getManager().areElementsEquivalent(psiClass, resolveReference((PsiReference) newElement)) && !PsiUtil.isInnerClass(psiClass)) {
                        final String qName = psiClass.getQualifiedName();
                        if (qName != null) {
                            document.replaceString(newElement.getTextRange().getStartOffset(), newEndOffset, qName);
                            newEndOffset = newElement.getTextRange().getStartOffset() + qName.length();
                        }
                    }
                }
            }
        }
    }
    if (toDelete.isValid()) {
        document.deleteString(toDelete.getStartOffset(), toDelete.getEndOffset());
    }
    return newEndOffset;
}
Also used : RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document) ElementClassHint(com.intellij.psi.scope.ElementClassHint) NameHint(com.intellij.psi.scope.NameHint) Project(com.intellij.openapi.project.Project) IncorrectOperationException(com.intellij.util.IncorrectOperationException) FakePsiElement(com.intellij.psi.impl.FakePsiElement)

Example 90 with RangeMarker

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

the class ExtractMethodObjectHandler method run.

public static void run(@NotNull final Project project, final Editor editor, @NotNull final ExtractMethodObjectProcessor processor, @NotNull final ExtractMethodObjectProcessor.MyExtractMethodProcessor extractProcessor) {
    final RangeMarker marker;
    if (editor != null) {
        final int offset = editor.getCaretModel().getOffset();
        marker = editor.getDocument().createRangeMarker(new TextRange(offset, offset));
    } else {
        marker = null;
    }
    CommandProcessor.getInstance().executeCommand(project, () -> PostprocessReformattingAspect.getInstance(project).postponeFormattingInside(() -> {
        try {
            ApplicationManager.getApplication().runWriteAction(() -> extractProcessor.doRefactoring());
            processor.run();
            ApplicationManager.getApplication().runWriteAction(() -> processor.runChangeSignature());
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
        PsiDocumentManager.getInstance(project).commitAllDocuments();
        if (processor.isCreateInnerClass()) {
            processor.moveUsedMethodsToInner();
            PsiDocumentManager.getInstance(project).commitAllDocuments();
            if (editor != null) {
                DuplicatesImpl.processDuplicates(extractProcessor, project, editor);
            }
        }
        ApplicationManager.getApplication().runWriteAction(() -> {
            if (processor.isCreateInnerClass()) {
                processor.changeInstanceAccess(project);
            }
            final PsiElement method = processor.getMethod();
            LOG.assertTrue(method != null);
            method.delete();
        });
    }), ExtractMethodObjectProcessor.REFACTORING_NAME, ExtractMethodObjectProcessor.REFACTORING_NAME);
    if (editor != null) {
        editor.getCaretModel().moveToOffset(marker.getStartOffset());
        marker.dispose();
        editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) IncorrectOperationException(com.intellij.util.IncorrectOperationException) RangeMarker(com.intellij.openapi.editor.RangeMarker) PsiElement(com.intellij.psi.PsiElement)

Aggregations

RangeMarker (com.intellij.openapi.editor.RangeMarker)111 Document (com.intellij.openapi.editor.Document)33 TextRange (com.intellij.openapi.util.TextRange)20 Project (com.intellij.openapi.project.Project)19 PsiFile (com.intellij.psi.PsiFile)14 PsiElement (com.intellij.psi.PsiElement)13 IncorrectOperationException (com.intellij.util.IncorrectOperationException)13 Editor (com.intellij.openapi.editor.Editor)11 Nullable (org.jetbrains.annotations.Nullable)11 NotNull (org.jetbrains.annotations.NotNull)10 Template (com.intellij.codeInsight.template.Template)8 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)7 TemplateEditingAdapter (com.intellij.codeInsight.template.TemplateEditingAdapter)6 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)6 RangeHighlighterEx (com.intellij.openapi.editor.ex.RangeHighlighterEx)5 THashMap (gnu.trove.THashMap)5 GutterMark (com.intellij.codeInsight.daemon.GutterMark)4 HighlightSeverity (com.intellij.lang.annotation.HighlightSeverity)4 ApplicationManager (com.intellij.openapi.application.ApplicationManager)4 RelativePoint (com.intellij.ui.awt.RelativePoint)4