Search in sources :

Example 6 with SourceChange

use of org.dartlang.analysis.server.protocol.SourceChange in project intellij-plugins by JetBrains.

the class DartServerRenameTest method doTest.

private void doTest(@NotNull final ServerRenameRefactoring refactoring, @NotNull final String newName) {
    // check initial conditions
    final RefactoringStatus initialConditions = refactoring.checkInitialConditions();
    assertNotNull(initialConditions);
    assertTrue(initialConditions.isOK());
    // check final conditions
    refactoring.setNewName(newName);
    final RefactoringStatus finalConditions = refactoring.checkFinalConditions();
    assertNotNull(finalConditions);
    assertTrue(finalConditions.isOK());
    // apply the SourceChange
    final SourceChange change = refactoring.getChange();
    assertNotNull(change);
    ApplicationManager.getApplication().runWriteAction(() -> {
        final Set<String> excludedIds = refactoring.getPotentialEdits();
        try {
            AssistUtils.applySourceChange(myFixture.getProject(), change, false, excludedIds);
        } catch (DartSourceEditException e) {
            fail(e.getMessage());
        }
    });
    // validate
    myFixture.checkResultByFile(getTestName(false) + ".after.dart");
}
Also used : RefactoringStatus(com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus) SourceChange(org.dartlang.analysis.server.protocol.SourceChange) DartSourceEditException(com.jetbrains.lang.dart.assists.DartSourceEditException)

Example 7 with SourceChange

use of org.dartlang.analysis.server.protocol.SourceChange in project intellij-plugins by JetBrains.

the class GetRefactoringProcessor method process.

public void process(String requestId, JsonObject resultObject, RequestError requestError) {
    if (resultObject != null) {
        try {
            // problems
            List<RefactoringProblem> initialProblems = getRefactoringProblems(resultObject, "initialProblems");
            List<RefactoringProblem> optionsProblems = getRefactoringProblems(resultObject, "optionsProblems");
            List<RefactoringProblem> finalProblems = getRefactoringProblems(resultObject, "finalProblems");
            // change
            SourceChange change = null;
            if (resultObject.has("change")) {
                change = SourceChange.fromJson(resultObject.get("change").getAsJsonObject());
            }
            // potential edits
            List<String> potentialEdits = JsonUtilities.decodeStringList(resultObject.get("potentialEdits") != null ? resultObject.get("potentialEdits").getAsJsonArray() : null);
            //
            // Compute all refactoring-kind specific "Feedback" and put them into the feedback map
            //
            RefactoringFeedback feedback = null;
            if (resultObject.has("feedback")) {
                JsonObject feedbackObject = resultObject.get("feedback").getAsJsonObject();
                String kind = requestToRefactoringKindMap.remove(requestId);
                if (RefactoringKind.EXTRACT_LOCAL_VARIABLE.equals(kind)) {
                    feedback = ExtractLocalVariableFeedback.fromJson(feedbackObject);
                } else if (RefactoringKind.EXTRACT_METHOD.equals(kind)) {
                    feedback = ExtractMethodFeedback.fromJson(feedbackObject);
                } else if (RefactoringKind.INLINE_LOCAL_VARIABLE.equals(kind)) {
                    feedback = InlineLocalVariableFeedback.fromJson(feedbackObject);
                } else if (RefactoringKind.INLINE_METHOD.equals(kind)) {
                    feedback = InlineMethodFeedback.fromJson(feedbackObject);
                } else if (RefactoringKind.RENAME.equals(kind)) {
                    feedback = RenameFeedback.fromJson(feedbackObject);
                }
            }
            consumer.computedRefactorings(initialProblems, optionsProblems, finalProblems, feedback, change, potentialEdits);
        } catch (Exception exception) {
            // catch any exceptions in the formatting of this response
            requestError = generateRequestError(exception);
        }
    }
    if (requestError != null) {
        consumer.onError(requestError);
    }
}
Also used : RefactoringProblem(org.dartlang.analysis.server.protocol.RefactoringProblem) JsonObject(com.google.gson.JsonObject) SourceChange(org.dartlang.analysis.server.protocol.SourceChange) RefactoringFeedback(org.dartlang.analysis.server.protocol.RefactoringFeedback)

Example 8 with SourceChange

use of org.dartlang.analysis.server.protocol.SourceChange in project intellij-plugins by JetBrains.

the class InlineMethodDialog method inlineElement.

@Override
public void inlineElement(@NotNull final Project project, @Nullable final Editor editor, PsiElement element) {
    final InlineRefactoringContext context = findContext(editor);
    if (context == null) {
        return;
    }
    // create refactoring
    final ServerRefactoring refactoring;
    if (ElementKind.LOCAL_VARIABLE.equals(context.kind)) {
        refactoring = new ServerInlineLocalRefactoring(project, context.virtualFile, context.offset, 0);
    } else {
        refactoring = new ServerInlineMethodRefactoring(project, context.virtualFile, context.offset, 0);
    }
    // validate initial status
    {
        final RefactoringStatus initialConditions = refactoring.checkInitialConditions();
        if (showMessageIfError(editor, initialConditions)) {
            return;
        }
    }
    // configure using dialog
    if (refactoring instanceof ServerInlineMethodRefactoring) {
        boolean dialogOK = new InlineMethodDialog(project, element, (ServerInlineMethodRefactoring) refactoring).showAndGet();
        if (!dialogOK) {
            return;
        }
    }
    // validate final status
    {
        final RefactoringStatus finalConditions = refactoring.checkFinalConditions();
        if (showMessageIfError(editor, finalConditions)) {
            return;
        }
    }
    // Apply the change.
    ApplicationManager.getApplication().runWriteAction(() -> {
        final SourceChange change = refactoring.getChange();
        assert change != null;
        try {
            AssistUtils.applySourceChange(project, change, false);
        } catch (DartSourceEditException e) {
            CommonRefactoringUtil.showErrorHint(project, editor, e.getMessage(), CommonBundle.getErrorTitle(), null);
        }
    });
}
Also used : RefactoringStatus(com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus) SourceChange(org.dartlang.analysis.server.protocol.SourceChange) DartSourceEditException(com.jetbrains.lang.dart.assists.DartSourceEditException)

Example 9 with SourceChange

use of org.dartlang.analysis.server.protocol.SourceChange in project intellij-plugins by JetBrains.

the class DartServerExtractLocalVariableDialog method performInPlace.

private void performInPlace() {
    final String[] names = refactoring.getNames();
    if (names.length != 0) {
        refactoring.setName(names[0]);
    }
    // validate final status
    {
        final RefactoringStatus finalConditions = refactoring.checkFinalConditions();
        if (showMessageIfError(finalConditions)) {
            return;
        }
    }
    // Apply the change.
    ApplicationManager.getApplication().runWriteAction(() -> {
        final SourceChange change = refactoring.getChange();
        assert change != null;
        try {
            AssistUtils.applySourceChange(project, change, true);
        } catch (DartSourceEditException e) {
            CommonRefactoringUtil.showErrorHint(project, editor, e.getMessage(), CommonBundle.getErrorTitle(), null);
        }
    });
}
Also used : RefactoringStatus(com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus) SourceChange(org.dartlang.analysis.server.protocol.SourceChange) DartSourceEditException(com.jetbrains.lang.dart.assists.DartSourceEditException)

Example 10 with SourceChange

use of org.dartlang.analysis.server.protocol.SourceChange in project intellij-plugins by JetBrains.

the class DartRenameDialog method previewRefactoring.

@Override
protected void previewRefactoring() {
    final UsageViewPresentation presentation = new UsageViewPresentation();
    presentation.setTabText(RefactoringBundle.message("usageView.tabText"));
    presentation.setShowCancelButton(true);
    presentation.setTargetsNodeText(RefactoringBundle.message("0.to.be.renamed.to.1.2", myRefactoring.getElementKindName(), "", getNewName()));
    presentation.setNonCodeUsagesString(DartBundle.message("usages.in.comments.to.rename"));
    presentation.setCodeUsagesString(DartBundle.message("usages.in.code.to.rename"));
    presentation.setDynamicUsagesString(DartBundle.message("dynamic.usages.to.rename"));
    presentation.setUsageTypeFilteringAvailable(false);
    final List<UsageTarget> usageTargets = new SmartList<>();
    final Map<Usage, String> usageToEditIdMap = new THashMap<>();
    fillTargetsAndUsageToEditIdMap(usageTargets, usageToEditIdMap);
    final UsageTarget[] targets = usageTargets.toArray(new UsageTarget[usageTargets.size()]);
    final Set<Usage> usageSet = usageToEditIdMap.keySet();
    final Usage[] usages = usageSet.toArray(new Usage[usageSet.size()]);
    final UsageView usageView = UsageViewManager.getInstance(myProject).showUsages(targets, usages, presentation);
    final SourceChange sourceChange = myRefactoring.getChange();
    assert sourceChange != null;
    usageView.addPerformOperationAction(createRefactoringRunnable(usageView, usageToEditIdMap), sourceChange.getMessage(), DartBundle.message("rename.need.reRun"), RefactoringBundle.message("usageView.doAction"), false);
}
Also used : THashMap(gnu.trove.THashMap) SourceChange(org.dartlang.analysis.server.protocol.SourceChange) SmartList(com.intellij.util.SmartList)

Aggregations

SourceChange (org.dartlang.analysis.server.protocol.SourceChange)12 DartSourceEditException (com.jetbrains.lang.dart.assists.DartSourceEditException)8 RefactoringStatus (com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus)7 DartAnalysisServerService (com.jetbrains.lang.dart.analyzer.DartAnalysisServerService)2 JsonObject (com.google.gson.JsonObject)1 PsiElement2UsageTargetAdapter (com.intellij.find.findUsages.PsiElement2UsageTargetAdapter)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 PsiManager (com.intellij.psi.PsiManager)1 UsageInfo (com.intellij.usageView.UsageInfo)1 SmartList (com.intellij.util.SmartList)1 ServerExtractLocalVariableRefactoring (com.jetbrains.lang.dart.ide.refactoring.ServerExtractLocalVariableRefactoring)1 ServerExtractMethodRefactoring (com.jetbrains.lang.dart.ide.refactoring.ServerExtractMethodRefactoring)1 ServerInlineLocalRefactoring (com.jetbrains.lang.dart.ide.refactoring.ServerInlineLocalRefactoring)1 ServerInlineMethodRefactoring (com.jetbrains.lang.dart.ide.refactoring.ServerInlineMethodRefactoring)1 THashMap (gnu.trove.THashMap)1 Position (org.dartlang.analysis.server.protocol.Position)1 RefactoringFeedback (org.dartlang.analysis.server.protocol.RefactoringFeedback)1