Search in sources :

Example 1 with ServerExtractMethodRefactoring

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

the class DartServerExtractMethodDialog method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (!selectionModel.hasSelection())
        selectionModel.selectLineAtCaret();
    final int offset = selectionModel.getSelectionStart();
    final int length = selectionModel.getSelectionEnd() - offset;
    final ServerExtractMethodRefactoring refactoring = new ServerExtractMethodRefactoring(project, file.getVirtualFile(), offset, length);
    // Validate initial status.
    {
        final RefactoringStatus initialStatus = refactoring.checkInitialConditions();
        if (initialStatus == null) {
            return;
        }
        if (initialStatus.hasError()) {
            final String title = DartBundle.message("dart.refactoring.extract.method.error");
            CommonRefactoringUtil.showErrorHint(project, editor, initialStatus.getMessage(), title, null);
            return;
        }
    }
    new DartServerExtractMethodDialog(project, editor, refactoring).show();
}
Also used : ServerExtractMethodRefactoring(com.jetbrains.lang.dart.ide.refactoring.ServerExtractMethodRefactoring) SelectionModel(com.intellij.openapi.editor.SelectionModel) RefactoringStatus(com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus)

Example 2 with ServerExtractMethodRefactoring

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

the class DartExtractMethodRefactoringTest method doTest.

private void doTest(String filePath, boolean all, boolean asGetter) {
    final ServerExtractMethodRefactoring refactoring = createRefactoring(filePath);
    // check initial conditions
    final RefactoringStatus initialConditions = refactoring.checkInitialConditions();
    assertNotNull(initialConditions);
    assertTrue(initialConditions.isOK());
    // configure
    refactoring.setName("test");
    refactoring.setExtractAll(all);
    refactoring.setCreateGetter(asGetter);
    // 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 : ServerExtractMethodRefactoring(com.jetbrains.lang.dart.ide.refactoring.ServerExtractMethodRefactoring) RefactoringStatus(com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus) SourceChange(org.dartlang.analysis.server.protocol.SourceChange) DartSourceEditException(com.jetbrains.lang.dart.assists.DartSourceEditException)

Example 3 with ServerExtractMethodRefactoring

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

the class DartExtractMethodRefactoringTest method createRefactoring.

@NotNull
private ServerExtractMethodRefactoring createRefactoring(String filePath) {
    ((CodeInsightTestFixtureImpl) myFixture).canChangeDocumentDuringHighlighting(true);
    final PsiFile psiFile = myFixture.configureByFile(filePath);
    // make sure server is warmed up
    myFixture.doHighlighting();
    // find the Element to rename
    final SelectionModel selectionModel = getEditor().getSelectionModel();
    int offset = selectionModel.getSelectionStart();
    final int length = selectionModel.getSelectionEnd() - offset;
    return new ServerExtractMethodRefactoring(getProject(), psiFile.getVirtualFile(), offset, length);
}
Also used : CodeInsightTestFixtureImpl(com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl) ServerExtractMethodRefactoring(com.jetbrains.lang.dart.ide.refactoring.ServerExtractMethodRefactoring) SelectionModel(com.intellij.openapi.editor.SelectionModel) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ServerExtractMethodRefactoring (com.jetbrains.lang.dart.ide.refactoring.ServerExtractMethodRefactoring)3 SelectionModel (com.intellij.openapi.editor.SelectionModel)2 RefactoringStatus (com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus)2 PsiFile (com.intellij.psi.PsiFile)1 CodeInsightTestFixtureImpl (com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl)1 DartSourceEditException (com.jetbrains.lang.dart.assists.DartSourceEditException)1 SourceChange (org.dartlang.analysis.server.protocol.SourceChange)1 NotNull (org.jetbrains.annotations.NotNull)1