use of org.dartlang.analysis.server.protocol.SourceFileEdit 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 org.dartlang.analysis.server.protocol.SourceFileEdit in project intellij-plugins by JetBrains.
the class DartQuickFix method isAvailable.
@Override
public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
myQuickFixSet.ensureInitialized();
if (mySuppressActionDelegate != null) {
return mySuppressActionDelegate.isAvailable(project, editor, file);
}
if (mySourceChange == null)
return false;
final List<SourceFileEdit> fileEdits = mySourceChange.getEdits();
if (fileEdits.size() != 1)
return false;
final SourceFileEdit fileEdit = fileEdits.get(0);
final VirtualFile virtualFile = AssistUtils.findVirtualFile(fileEdit);
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
if (fileEdit.getFileStamp() != -1) {
if (virtualFile == null || !fileIndex.isInContent(virtualFile))
return false;
}
return true;
}
use of org.dartlang.analysis.server.protocol.SourceFileEdit in project intellij-plugins by JetBrains.
the class DartRenameDialog method fillTargetsAndUsageToEditIdMap.
private void fillTargetsAndUsageToEditIdMap(@NotNull final List<UsageTarget> usageTargets, @NotNull final Map<Usage, String> usageToEditIdMap) {
final SourceChange change = myRefactoring.getChange();
assert change != null;
final DartAnalysisServerService service = DartAnalysisServerService.getInstance(myProject);
final PsiManager psiManager = PsiManager.getInstance(myProject);
for (SourceFileEdit fileEdit : change.getEdits()) {
final VirtualFile file = AssistUtils.findVirtualFile(fileEdit);
final PsiFile psiFile = file == null ? null : psiManager.findFile(file);
if (psiFile == null)
continue;
for (SourceEdit sourceEdit : fileEdit.getEdits()) {
final int offset = service.getConvertedOffset(file, sourceEdit.getOffset());
final int length = service.getConvertedOffset(file, sourceEdit.getOffset() + sourceEdit.getLength()) - offset;
final TextRange range = TextRange.create(offset, offset + length);
final boolean potentialUsage = myRefactoring.getPotentialEdits().contains(sourceEdit.getId());
final PsiElement usageElement = DartServerFindUsagesHandler.getUsagePsiElement(psiFile, range);
if (usageElement != null) {
if (DartComponentType.typeOf(usageElement) != null) {
usageTargets.add(new PsiElement2UsageTargetAdapter(usageElement));
} else {
final UsageInfo usageInfo = DartServerFindUsagesHandler.getUsageInfo(usageElement, range, potentialUsage);
if (usageInfo != null) {
usageToEditIdMap.put(new UsageInfo2UsageAdapter(usageInfo), sourceEdit.getId());
}
}
}
}
}
}
use of org.dartlang.analysis.server.protocol.SourceFileEdit in project intellij-plugins by JetBrains.
the class DartImportOptimizer method processFile.
@NotNull
@Override
public Runnable processFile(final PsiFile file) {
DartAnalysisServerService.getInstance(file.getProject()).serverReadyForRequest(file.getProject());
return new CollectingInfoRunnable() {
private boolean myFileChanged = false;
@Override
public void run() {
final VirtualFile vFile = DartResolveUtil.getRealVirtualFile(file);
if (vFile != null) {
final String filePath = vFile.getPath();
final SourceFileEdit fileEdit = DartAnalysisServerService.getInstance(file.getProject()).edit_organizeDirectives(filePath);
if (fileEdit != null) {
if (AssistUtils.applyFileEdit(file.getProject(), fileEdit)) {
myFileChanged = true;
final Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
if (document != null) {
// Tricky story. Committing a document here is required in oder to guarantee that DartPostFormatProcessor.processText() is called afterwards.
PsiDocumentManager.getInstance(file.getProject()).commitDocument(document);
}
}
}
}
}
@Nullable
@Override
public String getUserNotificationInfo() {
return myFileChanged ? DartBundle.message("organized.directives") : null;
}
};
}
use of org.dartlang.analysis.server.protocol.SourceFileEdit in project intellij-plugins by JetBrains.
the class DartSortMembersAction method runOverEditor.
protected void runOverEditor(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile psiFile) {
final Document document = editor.getDocument();
if (!ReadonlyStatusHandler.ensureDocumentWritable(project, document))
return;
final String path = psiFile.getVirtualFile().getPath();
final DartAnalysisServerService service = DartAnalysisServerService.getInstance(project);
service.updateFilesContent();
final SourceFileEdit fileEdit = service.edit_sortMembers(path);
if (fileEdit == null) {
showHintLater(editor, DartBundle.message("dart.sort.members.hint.failed"), true);
LOG.warn("Unexpected response from edit_sortMembers, fileEdit is null");
return;
}
final List<SourceEdit> edits = fileEdit.getEdits();
if (edits == null || edits.size() == 0) {
showHintLater(editor, DartBundle.message("dart.sort.members.hint.already.good"), false);
return;
}
final Runnable runnable = () -> {
AssistUtils.applySourceEdits(project, psiFile.getVirtualFile(), document, edits, Collections.emptySet());
showHintLater(editor, DartBundle.message("dart.sort.members.hint.success"), false);
};
ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance().executeCommand(project, runnable, DartBundle.message("dart.sort.members.action.name"), null));
}
Aggregations