Search in sources :

Example 1 with SourceEdit

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());
                    }
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SourceFileEdit(org.dartlang.analysis.server.protocol.SourceFileEdit) PsiManager(com.intellij.psi.PsiManager) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) TextRange(com.intellij.openapi.util.TextRange) SourceEdit(org.dartlang.analysis.server.protocol.SourceEdit) SourceChange(org.dartlang.analysis.server.protocol.SourceChange) PsiFile(com.intellij.psi.PsiFile) DartAnalysisServerService(com.jetbrains.lang.dart.analyzer.DartAnalysisServerService) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo)

Example 2 with SourceEdit

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));
}
Also used : SourceFileEdit(org.dartlang.analysis.server.protocol.SourceFileEdit) Document(com.intellij.openapi.editor.Document) DartAnalysisServerService(com.jetbrains.lang.dart.analyzer.DartAnalysisServerService) SourceEdit(org.dartlang.analysis.server.protocol.SourceEdit)

Example 3 with SourceEdit

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());
}
Also used : DartAnalysisServerService(com.jetbrains.lang.dart.analyzer.DartAnalysisServerService) SourceEdit(org.dartlang.analysis.server.protocol.SourceEdit)

Aggregations

DartAnalysisServerService (com.jetbrains.lang.dart.analyzer.DartAnalysisServerService)3 SourceEdit (org.dartlang.analysis.server.protocol.SourceEdit)3 SourceFileEdit (org.dartlang.analysis.server.protocol.SourceFileEdit)2 PsiElement2UsageTargetAdapter (com.intellij.find.findUsages.PsiElement2UsageTargetAdapter)1 Document (com.intellij.openapi.editor.Document)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 PsiManager (com.intellij.psi.PsiManager)1 UsageInfo (com.intellij.usageView.UsageInfo)1 SourceChange (org.dartlang.analysis.server.protocol.SourceChange)1