Search in sources :

Example 11 with RefactoringStatus

use of com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus in project intellij-plugins by JetBrains.

the class DartServerExtractLocalVariableDialog method createRefactoring.

private void createRefactoring(int offset, int length) {
    refactoring = new ServerExtractLocalVariableRefactoring(project, file.getVirtualFile(), offset, length);
    final RefactoringStatus initialStatus = refactoring.checkInitialConditions();
    if (showMessageIfError(initialStatus)) {
        refactoring = null;
    }
}
Also used : ServerExtractLocalVariableRefactoring(com.jetbrains.lang.dart.ide.refactoring.ServerExtractLocalVariableRefactoring) RefactoringStatus(com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus)

Example 12 with RefactoringStatus

use of com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus 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 13 with RefactoringStatus

use of com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus in project intellij-plugins by JetBrains.

the class DartServerRenameHandler method showRenameDialog.

private static void showRenameDialog(@NotNull Project project, @NotNull Editor editor, DataContext context) {
    final PsiFile psiFile = CommonDataKeys.PSI_FILE.getData(context);
    final VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(context);
    if (psiFile == null || virtualFile == null) {
        return;
    }
    // Prepare the offset in the editor or of the selected element.
    final int offset;
    {
        final Caret caret = CommonDataKeys.CARET.getData(context);
        final PsiElement element = CommonDataKeys.PSI_ELEMENT.getData(context);
        if (caret != null) {
            offset = caret.getOffset();
        } else if (element != null) {
            offset = element.getTextOffset();
        } else {
            return;
        }
    }
    // Create the refactoring.
    final ServerRenameRefactoring refactoring = new ServerRenameRefactoring(project, virtualFile, offset, 0);
    // Validate initial status.
    {
        final RefactoringStatus initialStatus = refactoring.checkInitialConditions();
        if (initialStatus == null) {
            return;
        }
        if (initialStatus.hasError()) {
            final String message = initialStatus.getMessage();
            assert message != null;
            CommonRefactoringUtil.showErrorHint(project, editor, message, CommonBundle.getErrorTitle(), null);
            return;
        }
    }
    // Show the rename dialog.
    new DartRenameDialog(project, editor, refactoring).show();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) RefactoringStatus(com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus) PsiFile(com.intellij.psi.PsiFile) Caret(com.intellij.openapi.editor.Caret) PsiElement(com.intellij.psi.PsiElement)

Example 14 with RefactoringStatus

use of com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus in project intellij-plugins by JetBrains.

the class DartInlineLocalRefactoringTest method doTest.

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

Example 15 with RefactoringStatus

use of com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus in project intellij-plugins by JetBrains.

the class DartServerRenameTest method testCheckFinalConditionsNameFatalError.

public void testCheckFinalConditionsNameFatalError() throws Throwable {
    final ServerRenameRefactoring refactoring = createRenameRefactoring();
    // initial status OK
    final RefactoringStatus initialConditions = refactoring.checkInitialConditions();
    assertNotNull(initialConditions);
    assertTrue(initialConditions.isOK());
    // final (actually options) status has a fatal error
    refactoring.setNewName("bad name");
    final RefactoringStatus finalConditions = refactoring.checkFinalConditions();
    assertNotNull(finalConditions);
    assertTrue(finalConditions.hasFatalError());
}
Also used : RefactoringStatus(com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus) ServerRenameRefactoring(com.jetbrains.lang.dart.ide.refactoring.ServerRenameRefactoring)

Aggregations

RefactoringStatus (com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus)15 DartSourceEditException (com.jetbrains.lang.dart.assists.DartSourceEditException)7 SourceChange (org.dartlang.analysis.server.protocol.SourceChange)7 ServerExtractLocalVariableRefactoring (com.jetbrains.lang.dart.ide.refactoring.ServerExtractLocalVariableRefactoring)2 ServerExtractMethodRefactoring (com.jetbrains.lang.dart.ide.refactoring.ServerExtractMethodRefactoring)2 ServerRenameRefactoring (com.jetbrains.lang.dart.ide.refactoring.ServerRenameRefactoring)2 Caret (com.intellij.openapi.editor.Caret)1 SelectionModel (com.intellij.openapi.editor.SelectionModel)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Task (com.intellij.openapi.progress.Task)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 ServerInlineLocalRefactoring (com.jetbrains.lang.dart.ide.refactoring.ServerInlineLocalRefactoring)1 ServerInlineMethodRefactoring (com.jetbrains.lang.dart.ide.refactoring.ServerInlineMethodRefactoring)1 RefactoringStatusEntry (com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatusEntry)1 RefactoringStatusSeverity (com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatusSeverity)1 Nullable (org.jetbrains.annotations.Nullable)1