Search in sources :

Example 1 with SourceFileEdit

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);
    }
}
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 SourceFileEdit

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SourceFileEdit(org.dartlang.analysis.server.protocol.SourceFileEdit) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex)

Example 3 with SourceFileEdit

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());
                    }
                }
            }
        }
    }
}
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 4 with SourceFileEdit

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;
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SourceFileEdit(org.dartlang.analysis.server.protocol.SourceFileEdit) Document(com.intellij.openapi.editor.Document) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with SourceFileEdit

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

Aggregations

SourceFileEdit (org.dartlang.analysis.server.protocol.SourceFileEdit)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 Document (com.intellij.openapi.editor.Document)3 JsonObject (com.google.gson.JsonObject)2 DartAnalysisServerService (com.jetbrains.lang.dart.analyzer.DartAnalysisServerService)2 SourceEdit (org.dartlang.analysis.server.protocol.SourceEdit)2 PsiElement2UsageTargetAdapter (com.intellij.find.findUsages.PsiElement2UsageTargetAdapter)1 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)1 TextRange (com.intellij.openapi.util.TextRange)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 DartSourceEditException (com.jetbrains.lang.dart.assists.DartSourceEditException)1 IOException (java.io.IOException)1 SourceChange (org.dartlang.analysis.server.protocol.SourceChange)1 NotNull (org.jetbrains.annotations.NotNull)1