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");
}
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);
}
}
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);
}
});
}
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);
}
});
}
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);
}
Aggregations