Search in sources :

Example 1 with DartSourceEditException

use of com.jetbrains.lang.dart.assists.DartSourceEditException in project intellij-plugins by JetBrains.

the class DartQuickFix method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
    if (mySuppressActionDelegate != null) {
        mySuppressActionDelegate.invoke(project, editor, file);
    }
    if (mySourceChange == null)
        return;
    final SourceFileEdit fileEdit = mySourceChange.getEdits().get(0);
    final String filePath = FileUtil.toSystemIndependentName(fileEdit.getFile());
    final VirtualFile virtualFile;
    // Create the file if it does not exist.
    if (fileEdit.getFileStamp() == -1) {
        try {
            final String directoryPath = PathUtil.getParentPath(filePath);
            if (directoryPath.isEmpty())
                throw new IOException("empty folder path");
            final VirtualFile directory = VfsUtil.createDirectoryIfMissing(directoryPath);
            if (directory == null)
                throw new IOException("failed to create folder " + FileUtil.toSystemDependentName(directoryPath));
            virtualFile = directory.createChildData(this, PathUtil.getFileName(filePath));
        } catch (IOException e) {
            final String message = DartBundle.message("failed.to.create.file.0.1", FileUtil.toSystemDependentName(filePath), e.getMessage());
            CommonRefactoringUtil.showErrorHint(project, editor, message, CommonBundle.getErrorTitle(), null);
            return;
        }
    } else {
        virtualFile = LocalFileSystem.getInstance().findFileByPath(filePath);
    }
    if (virtualFile == null)
        return;
    if (!FileModificationService.getInstance().prepareVirtualFilesForWrite(project, Collections.singletonList(virtualFile)))
        return;
    final Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
    if (document == null)
        return;
    try {
        AssistUtils.applySourceChange(project, mySourceChange, true);
    } catch (DartSourceEditException e) {
        CommonRefactoringUtil.showErrorHint(project, editor, e.getMessage(), CommonBundle.getErrorTitle(), null);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SourceFileEdit(org.dartlang.analysis.server.protocol.SourceFileEdit) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) DartSourceEditException(com.jetbrains.lang.dart.assists.DartSourceEditException)

Example 2 with DartSourceEditException

use of com.jetbrains.lang.dart.assists.DartSourceEditException in project intellij-plugins by JetBrains.

the class DartServerStatementCompletionProcessor method process.

@Override
public boolean process(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile psiFile) {
    final int offset = editor.getCaretModel().getOffset();
    final DartAnalysisServerService service = DartAnalysisServerService.getInstance(psiFile.getProject());
    service.updateFilesContent();
    SourceChange sourceChange = service.edit_getStatementCompletion(psiFile.getVirtualFile(), offset);
    if (sourceChange != null && !isNoop(sourceChange)) {
        try {
            AssistUtils.applySourceChange(project, sourceChange, true);
            Position position = sourceChange.getSelection();
            if (position != null) {
                // The position should never be null but it might be if unit tests are flaky.
                editor.getCaretModel().moveToOffset(service.getConvertedOffset(psiFile.getVirtualFile(), position.getOffset()));
            }
        } catch (DartSourceEditException e) {
            CommonRefactoringUtil.showErrorHint(project, editor, e.getMessage(), CommonBundle.getErrorTitle(), null);
        }
        return true;
    }
    return false;
}
Also used : Position(org.dartlang.analysis.server.protocol.Position) SourceChange(org.dartlang.analysis.server.protocol.SourceChange) DartAnalysisServerService(com.jetbrains.lang.dart.analyzer.DartAnalysisServerService) DartSourceEditException(com.jetbrains.lang.dart.assists.DartSourceEditException)

Example 3 with DartSourceEditException

use of com.jetbrains.lang.dart.assists.DartSourceEditException in project intellij-plugins by JetBrains.

the class DartExtractLocalVariableRefactoringTest method doTest.

private void doTest(String filePath, boolean all) {
    final ServerExtractLocalVariableRefactoring refactoring = createRefactoring(filePath);
    // check initial conditions
    final RefactoringStatus initialConditions = refactoring.checkInitialConditions();
    assertNotNull(initialConditions);
    assertTrue(initialConditions.isOK());
    // configure
    //refactoring.setName("test");
    refactoring.setExtractAll(all);
    // 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, true);
        } catch (DartSourceEditException e) {
            fail(e.getMessage());
        }
    });
    // validate
    myFixture.checkResultByFile(getTestName(false) + ".after.dart");
}
Also used : ServerExtractLocalVariableRefactoring(com.jetbrains.lang.dart.ide.refactoring.ServerExtractLocalVariableRefactoring) RefactoringStatus(com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus) SourceChange(org.dartlang.analysis.server.protocol.SourceChange) DartSourceEditException(com.jetbrains.lang.dart.assists.DartSourceEditException)

Example 4 with DartSourceEditException

use of com.jetbrains.lang.dart.assists.DartSourceEditException 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 5 with DartSourceEditException

use of com.jetbrains.lang.dart.assists.DartSourceEditException in project intellij-plugins by JetBrains.

the class DartInlineMethodRefactoringTest method doTest.

private void doTest(String filePath, boolean all) {
    final ServerInlineMethodRefactoring refactoring = createRefactoring(filePath);
    // check initial conditions
    final RefactoringStatus initialConditions = refactoring.checkInitialConditions();
    assertNotNull(initialConditions);
    assertTrue(initialConditions.isOK());
    // all
    if (all) {
        refactoring.setInlineAll(true);
        refactoring.setDeleteSource(true);
    }
    // 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 : RefactoringStatus(com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus) SourceChange(org.dartlang.analysis.server.protocol.SourceChange) ServerInlineMethodRefactoring(com.jetbrains.lang.dart.ide.refactoring.ServerInlineMethodRefactoring) DartSourceEditException(com.jetbrains.lang.dart.assists.DartSourceEditException)

Aggregations

DartSourceEditException (com.jetbrains.lang.dart.assists.DartSourceEditException)9 SourceChange (org.dartlang.analysis.server.protocol.SourceChange)8 RefactoringStatus (com.jetbrains.lang.dart.ide.refactoring.status.RefactoringStatus)7 Document (com.intellij.openapi.editor.Document)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 DartAnalysisServerService (com.jetbrains.lang.dart.analyzer.DartAnalysisServerService)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 IOException (java.io.IOException)1 Position (org.dartlang.analysis.server.protocol.Position)1 SourceFileEdit (org.dartlang.analysis.server.protocol.SourceFileEdit)1