use of org.dartlang.analysis.server.protocol.SourceEdit 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.SourceEdit 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));
}
use of org.dartlang.analysis.server.protocol.SourceEdit in project flutter-intellij by flutter.
the class FlutterSaveActionsManager method performFormat.
private void performFormat(@NotNull Document document, @NotNull VirtualFile file, boolean reSave) {
final int lineLength = getRightMargin(myProject);
final DartAnalysisServerService das = DartAnalysisServerService.getInstance(myProject);
das.updateFilesContent();
final DartAnalysisServerService.FormatResult formatResult = das.edit_format(file, 0, 0, lineLength);
if (formatResult == null) {
if (reSave) {
FileDocumentManager.getInstance().saveDocument(document);
}
return;
}
ApplicationManager.getApplication().invokeLater(() -> new WriteCommandAction.Simple(myProject) {
@Override
protected void run() {
boolean didFormat = false;
final List<SourceEdit> edits = formatResult.getEdits();
if (edits != null && edits.size() == 1) {
final String replacement = StringUtil.convertLineSeparators(edits.get(0).getReplacement());
document.replaceString(0, document.getTextLength(), replacement);
PsiDocumentManager.getInstance(myProject).commitDocument(document);
didFormat = true;
}
if (reSave || didFormat) {
FileDocumentManager.getInstance().saveDocument(document);
}
}
}.execute());
}
Aggregations