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