Search in sources :

Example 66 with RangeMarker

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

the class CodeInsightUtilCore method forcePsiPostprocessAndRestoreElement.

public static <T extends PsiElement> T forcePsiPostprocessAndRestoreElement(@NotNull T element, boolean useFileLanguage) {
    final PsiFile psiFile = element.getContainingFile();
    final Document document = psiFile.getViewProvider().getDocument();
    //if (document == null) return element;
    final Language language = useFileLanguage ? psiFile.getLanguage() : PsiUtilCore.getDialect(element);
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(psiFile.getProject());
    final RangeMarker rangeMarker = document.createRangeMarker(element.getTextRange());
    documentManager.doPostponedOperationsAndUnblockDocument(document);
    documentManager.commitDocument(document);
    T elementInRange = findElementInRange(psiFile, rangeMarker.getStartOffset(), rangeMarker.getEndOffset(), (Class<? extends T>) element.getClass(), language, element);
    rangeMarker.dispose();
    return elementInRange;
}
Also used : Language(com.intellij.lang.Language) PsiFile(com.intellij.psi.PsiFile) RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 67 with RangeMarker

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

the class CreateClassFromNewFix method setupClassFromNewExpression.

protected void setupClassFromNewExpression(final PsiClass psiClass, final PsiNewExpression newExpression) {
    assert ApplicationManager.getApplication().isWriteAccessAllowed();
    final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(newExpression.getProject()).getElementFactory();
    PsiClass aClass = psiClass;
    if (aClass == null)
        return;
    final PsiJavaCodeReferenceElement classReference = newExpression.getClassReference();
    if (classReference != null) {
        classReference.bindToElement(aClass);
    }
    setupInheritance(newExpression, aClass);
    PsiExpressionList argList = newExpression.getArgumentList();
    final Project project = aClass.getProject();
    if (argList != null && argList.getExpressions().length > 0) {
        PsiMethod constructor = elementFactory.createConstructor();
        constructor = (PsiMethod) aClass.add(constructor);
        TemplateBuilderImpl templateBuilder = new TemplateBuilderImpl(aClass);
        CreateFromUsageUtils.setupMethodParameters(constructor, templateBuilder, argList, getTargetSubstitutor(newExpression));
        setupSuperCall(aClass, constructor, templateBuilder);
        getReferenceElement(newExpression).bindToElement(aClass);
        aClass = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(aClass);
        final Template template = templateBuilder.buildTemplate();
        template.setToReformat(true);
        final Editor editor = positionCursor(project, aClass.getContainingFile(), aClass);
        if (editor == null)
            return;
        final RangeMarker textRange = editor.getDocument().createRangeMarker(aClass.getTextRange());
        final Runnable runnable = () -> {
            new WriteCommandAction(project, getText(), getText()) {

                @Override
                protected void run(@NotNull Result result) throws Throwable {
                    try {
                        editor.getDocument().deleteString(textRange.getStartOffset(), textRange.getEndOffset());
                    } finally {
                        textRange.dispose();
                    }
                }
            }.execute();
            startTemplate(editor, template, project, null, getText());
        };
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            runnable.run();
        } else {
            ApplicationManager.getApplication().invokeLater(runnable);
        }
    } else {
        positionCursor(project, aClass.getContainingFile(), ObjectUtils.notNull(aClass.getNameIdentifier(), aClass));
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) RangeMarker(com.intellij.openapi.editor.RangeMarker) NotNull(org.jetbrains.annotations.NotNull) Template(com.intellij.codeInsight.template.Template) Result(com.intellij.openapi.application.Result) Project(com.intellij.openapi.project.Project) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) Editor(com.intellij.openapi.editor.Editor)

Example 68 with RangeMarker

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

the class CreateConstructorFromThisOrSuperFix method invokeImpl.

@Override
protected void invokeImpl(PsiClass targetClass) {
    final PsiFile callSite = myMethodCall.getContainingFile();
    final Project project = myMethodCall.getProject();
    PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
    IdeDocumentHistory.getInstance(project).includeCurrentPlaceAsChangePlace();
    try {
        PsiMethod constructor = elementFactory.createConstructor();
        constructor = (PsiMethod) targetClass.add(constructor);
        final TemplateBuilderImpl templateBuilder = new TemplateBuilderImpl(constructor);
        CreateFromUsageUtils.setupMethodParameters(constructor, templateBuilder, myMethodCall.getArgumentList(), getTargetSubstitutor(myMethodCall));
        final PsiFile psiFile = myMethodCall.getContainingFile();
        templateBuilder.setEndVariableAfter(constructor.getBody().getLBrace());
        final RangeMarker rangeMarker = psiFile.getViewProvider().getDocument().createRangeMarker(myMethodCall.getTextRange());
        constructor = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(constructor);
        targetClass = constructor.getContainingClass();
        myMethodCall = CodeInsightUtil.findElementInRange(psiFile, rangeMarker.getStartOffset(), rangeMarker.getEndOffset(), myMethodCall.getClass());
        rangeMarker.dispose();
        Template template = templateBuilder.buildTemplate();
        final Editor editor = positionCursor(project, targetClass.getContainingFile(), targetClass);
        if (editor == null)
            return;
        final TextRange textRange = constructor.getTextRange();
        final PsiFile file = targetClass.getContainingFile();
        editor.getDocument().deleteString(textRange.getStartOffset(), textRange.getEndOffset());
        editor.getCaretModel().moveToOffset(textRange.getStartOffset());
        startTemplate(editor, template, project, new TemplateEditingAdapter() {

            @Override
            public void templateFinished(Template template, boolean brokenOff) {
                ApplicationManager.getApplication().runWriteAction(() -> {
                    try {
                        PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
                        final int offset = editor.getCaretModel().getOffset();
                        PsiMethod constructor1 = PsiTreeUtil.findElementOfClassAtOffset(file, offset, PsiMethod.class, false);
                        CreateFromUsageUtils.setupMethodBody(constructor1);
                        CreateFromUsageUtils.setupEditor(constructor1, editor);
                        UndoUtil.markPsiFileForUndo(callSite);
                    } catch (IncorrectOperationException e) {
                        LOG.error(e);
                    }
                });
            }
        });
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : TemplateEditingAdapter(com.intellij.codeInsight.template.TemplateEditingAdapter) TextRange(com.intellij.openapi.util.TextRange) RangeMarker(com.intellij.openapi.editor.RangeMarker) Template(com.intellij.codeInsight.template.Template) Project(com.intellij.openapi.project.Project) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor)

Example 69 with RangeMarker

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

the class DefineParamsDefaultValueAction method startTemplate.

public static void startTemplate(@NotNull Project project, Editor editor, PsiExpression[] argsToBeDelegated, PsiMethod delegateMethod) {
    TemplateBuilderImpl builder = new TemplateBuilderImpl(delegateMethod);
    RangeMarker rangeMarker = editor.getDocument().createRangeMarker(delegateMethod.getTextRange());
    for (final PsiExpression exprToBeDefault : argsToBeDelegated) {
        builder.replaceElement(exprToBeDefault, new TextExpression(""));
    }
    Template template = builder.buildTemplate();
    editor.getCaretModel().moveToOffset(rangeMarker.getStartOffset());
    PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
    editor.getDocument().deleteString(rangeMarker.getStartOffset(), rangeMarker.getEndOffset());
    rangeMarker.dispose();
    CreateFromUsageBaseFix.startTemplate(editor, template, project);
}
Also used : TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) RangeMarker(com.intellij.openapi.editor.RangeMarker) TextExpression(com.intellij.codeInsight.template.impl.TextExpression) Template(com.intellij.codeInsight.template.Template)

Example 70 with RangeMarker

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

the class InlineMethodProcessor method performRefactoring.

protected void performRefactoring(@NotNull UsageInfo[] usages) {
    RangeMarker position = null;
    if (myEditor != null) {
        final int offset = myEditor.getCaretModel().getOffset();
        position = myEditor.getDocument().createRangeMarker(offset, offset + 1);
    }
    LocalHistoryAction a = LocalHistory.getInstance().startAction(getCommandName());
    try {
        doRefactoring(usages);
    } finally {
        a.finish();
    }
    if (position != null) {
        if (position.isValid()) {
            myEditor.getCaretModel().moveToOffset(position.getStartOffset());
        }
        position.dispose();
    }
}
Also used : LocalHistoryAction(com.intellij.history.LocalHistoryAction) RangeMarker(com.intellij.openapi.editor.RangeMarker)

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