Search in sources :

Example 11 with FileAnnotation

use of com.intellij.openapi.vcs.annotate.FileAnnotation in project intellij-community by JetBrains.

the class AnnotateLocalFileAction method doAnnotate.

private static void doAnnotate(@NotNull final Editor editor, @NotNull final Project project) {
    final VirtualFile file = FileDocumentManager.getInstance().getFile(editor.getDocument());
    if (file == null)
        return;
    final AbstractVcs vcs = ProjectLevelVcsManager.getInstance(project).getVcsFor(file);
    if (vcs == null)
        return;
    final AnnotationProvider annotationProvider = vcs.getAnnotationProvider();
    assert annotationProvider != null;
    final Ref<FileAnnotation> fileAnnotationRef = new Ref<>();
    final Ref<VcsException> exceptionRef = new Ref<>();
    VcsAnnotateUtil.getBackgroundableLock(project, file).lock();
    final Task.Backgroundable annotateTask = new Task.Backgroundable(project, VcsBundle.message("retrieving.annotations"), true) {

        @Override
        public void run(@NotNull final ProgressIndicator indicator) {
            try {
                fileAnnotationRef.set(annotationProvider.annotate(file));
            } catch (VcsException e) {
                exceptionRef.set(e);
            } catch (ProcessCanceledException pce) {
                throw pce;
            } catch (Throwable t) {
                exceptionRef.set(new VcsException(t));
            }
        }

        @Override
        public void onCancel() {
            onSuccess();
        }

        @Override
        public void onSuccess() {
            VcsAnnotateUtil.getBackgroundableLock(project, file).unlock();
            if (!exceptionRef.isNull()) {
                LOG.warn(exceptionRef.get());
                AbstractVcsHelper.getInstance(project).showErrors(Collections.singletonList(exceptionRef.get()), VcsBundle.message("message.title.annotate"));
            }
            if (!fileAnnotationRef.isNull()) {
                AnnotateToggleAction.doAnnotate(editor, project, file, fileAnnotationRef.get(), vcs);
            }
        }
    };
    ProgressManager.getInstance().run(annotateTask);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Task(com.intellij.openapi.progress.Task) AnnotationProvider(com.intellij.openapi.vcs.annotate.AnnotationProvider) ObjectUtils.assertNotNull(com.intellij.util.ObjectUtils.assertNotNull) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) FileAnnotation(com.intellij.openapi.vcs.annotate.FileAnnotation) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 12 with FileAnnotation

use of com.intellij.openapi.vcs.annotate.FileAnnotation in project intellij-community by JetBrains.

the class AnnotateRevisionActionBase method annotate.

public static void annotate(@NotNull VirtualFile file, @NotNull VcsFileRevision fileRevision, @NotNull AbstractVcs vcs, @Nullable Editor editor, int annotatedLine) {
    final CharSequence oldContent = editor == null ? null : editor.getDocument().getImmutableCharSequence();
    final AnnotationProvider annotationProvider = vcs.getAnnotationProvider();
    assert annotationProvider != null;
    final Ref<FileAnnotation> fileAnnotationRef = new Ref<>();
    final Ref<Integer> newLineRef = new Ref<>();
    final Ref<VcsException> exceptionRef = new Ref<>();
    VcsAnnotateUtil.getBackgroundableLock(vcs.getProject(), file).lock();
    Semaphore semaphore = new Semaphore(0);
    AtomicBoolean shouldOpenEditorInSync = new AtomicBoolean(true);
    ProgressManager.getInstance().run(new Task.Backgroundable(vcs.getProject(), VcsBundle.message("retrieving.annotations"), true) {

        public void run(@NotNull ProgressIndicator indicator) {
            try {
                FileAnnotation fileAnnotation = annotationProvider.annotate(file, fileRevision);
                int newLine = translateLine(oldContent, fileAnnotation.getAnnotatedContent(), annotatedLine);
                fileAnnotationRef.set(fileAnnotation);
                newLineRef.set(newLine);
                shouldOpenEditorInSync.set(false);
                semaphore.release();
            } catch (VcsException e) {
                exceptionRef.set(e);
            }
        }

        @Override
        public void onFinished() {
            VcsAnnotateUtil.getBackgroundableLock(vcs.getProject(), file).unlock();
        }

        @Override
        public void onSuccess() {
            if (!exceptionRef.isNull()) {
                AbstractVcsHelper.getInstance(myProject).showError(exceptionRef.get(), VcsBundle.message("operation.name.annotate"));
            }
            if (fileAnnotationRef.isNull())
                return;
            AbstractVcsHelper.getInstance(myProject).showAnnotation(fileAnnotationRef.get(), file, vcs, newLineRef.get());
        }
    });
    try {
        semaphore.tryAcquire(ProgressWindow.DEFAULT_PROGRESS_DIALOG_POSTPONE_TIME_MILLIS, TimeUnit.MILLISECONDS);
        // This will remove blinking on editor opening (step 1 - editor opens, step 2 - annotations are shown).
        if (shouldOpenEditorInSync.get()) {
            CharSequence content = LoadTextUtil.loadText(file);
            int newLine = translateLine(oldContent, content, annotatedLine);
            OpenFileDescriptor openFileDescriptor = new OpenFileDescriptor(vcs.getProject(), file, newLine, 0);
            FileEditorManager.getInstance(vcs.getProject()).openTextEditor(openFileDescriptor, true);
        }
    } catch (InterruptedException ignore) {
    }
}
Also used : Task(com.intellij.openapi.progress.Task) AnnotationProvider(com.intellij.openapi.vcs.annotate.AnnotationProvider) Semaphore(java.util.concurrent.Semaphore) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Ref(com.intellij.openapi.util.Ref) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) VcsException(com.intellij.openapi.vcs.VcsException) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) FileAnnotation(com.intellij.openapi.vcs.annotate.FileAnnotation)

Example 13 with FileAnnotation

use of com.intellij.openapi.vcs.annotate.FileAnnotation in project intellij-community by JetBrains.

the class AnnotateToggleAction method computeBgColors.

@Nullable
private static Couple<Map<VcsRevisionNumber, Color>> computeBgColors(@NotNull FileAnnotation fileAnnotation, @NotNull Editor editor) {
    Map<VcsRevisionNumber, Color> commitOrderColors = new HashMap<>();
    Map<VcsRevisionNumber, Color> commitAuthorColors = new HashMap<>();
    EditorColorsScheme colorScheme = editor.getColorsScheme();
    AnnotationsSettings settings = AnnotationsSettings.getInstance();
    List<Color> authorsColorPalette = settings.getAuthorsColors(colorScheme);
    List<Color> orderedColorPalette = settings.getOrderedColors(colorScheme);
    FileAnnotation.AuthorsMappingProvider authorsMappingProvider = fileAnnotation.getAuthorsMappingProvider();
    if (authorsMappingProvider != null) {
        Map<VcsRevisionNumber, String> authorsMap = authorsMappingProvider.getAuthors();
        Map<String, Color> authorColors = new HashMap<>();
        for (String author : ContainerUtil.sorted(authorsMap.values(), Comparing::compare)) {
            int index = authorColors.size();
            Color color = authorsColorPalette.get(index % authorsColorPalette.size());
            authorColors.put(author, color);
        }
        for (Map.Entry<VcsRevisionNumber, String> entry : authorsMap.entrySet()) {
            VcsRevisionNumber revision = entry.getKey();
            String author = entry.getValue();
            Color color = authorColors.get(author);
            commitAuthorColors.put(revision, color);
        }
    }
    FileAnnotation.RevisionsOrderProvider revisionsOrderProvider = fileAnnotation.getRevisionsOrderProvider();
    if (revisionsOrderProvider != null) {
        List<List<VcsRevisionNumber>> orderedRevisions = revisionsOrderProvider.getOrderedRevisions();
        int revisionsCount = orderedRevisions.size();
        for (int index = 0; index < revisionsCount; index++) {
            Color color = orderedColorPalette.get(orderedColorPalette.size() * index / revisionsCount);
            for (VcsRevisionNumber number : orderedRevisions.get(index)) {
                commitOrderColors.put(number, color);
            }
        }
    }
    return Couple.of(commitOrderColors.size() > 1 ? commitOrderColors : null, commitAuthorColors.size() > 1 ? commitAuthorColors : null);
}
Also used : HashMap(java.util.HashMap) VcsRevisionNumber(com.intellij.openapi.vcs.history.VcsRevisionNumber) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) ArrayList(java.util.ArrayList) List(java.util.List) FileAnnotation(com.intellij.openapi.vcs.annotate.FileAnnotation) HashMap(java.util.HashMap) Map(java.util.Map) Comparing(com.intellij.openapi.util.Comparing) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with FileAnnotation

use of com.intellij.openapi.vcs.annotate.FileAnnotation in project intellij-community by JetBrains.

the class VcsAnnotationCachedProxy method annotate.

@Override
public FileAnnotation annotate(final VirtualFile file) throws VcsException {
    final DiffProvider diffProvider = myVcs.getDiffProvider();
    final VcsRevisionNumber currentRevision = diffProvider.getCurrentRevision(file);
    return annotate(file, currentRevision, true, new ThrowableComputable<FileAnnotation, VcsException>() {

        @Override
        public FileAnnotation compute() throws VcsException {
            return myAnnotationProvider.annotate(file);
        }
    });
}
Also used : DiffProvider(com.intellij.openapi.vcs.diff.DiffProvider) VcsException(com.intellij.openapi.vcs.VcsException) FileAnnotation(com.intellij.openapi.vcs.annotate.FileAnnotation)

Example 15 with FileAnnotation

use of com.intellij.openapi.vcs.annotate.FileAnnotation in project intellij-community by JetBrains.

the class CvsAnnotationProvider method annotate.

public FileAnnotation annotate(VirtualFile cvsVirtualFile, String revision, CvsEnvironment environment) throws VcsException {
    // the VirtualFile has a full path if annotate is called from history (when we have a real file on disk),
    // and has the path equal to a CVS module name if annotate is called from the CVS repository browser
    // (when there's no real path)
    boolean hasLocalFile = false;
    File cvsFile = new File(cvsVirtualFile.getPath());
    if (cvsFile.isAbsolute()) {
        hasLocalFile = true;
        cvsFile = new File(CvsUtil.getModuleName(cvsVirtualFile));
    }
    final boolean binary = annotateBinary(cvsVirtualFile, environment);
    final AnnotateOperation annotateOperation = executeOperation(cvsFile, revision, environment, binary, true);
    final Annotation[] lineAnnotations = annotateOperation.getLineAnnotations();
    final List<VcsFileRevision> revisions;
    if (hasLocalFile) {
        final FilePath filePath = VcsContextFactory.SERVICE.getInstance().createFilePathOn(cvsVirtualFile);
        revisions = myCvsHistoryProvider.createRevisions(filePath);
        // in annotation cvs returns only 8 symbols of username
        // try to find usernames in history and use them
        adjustAnnotation(revisions, lineAnnotations);
    } else {
        // imitation
        revisions = new ArrayList<>();
        final Set<String> usedRevisions = new HashSet<>();
        for (Annotation annotation : lineAnnotations) {
            if (!usedRevisions.contains(annotation.getRevision())) {
                revisions.add(new RevisionPresentation(annotation.getRevision(), annotation.getUserName(), annotation.getDate()));
                usedRevisions.add(annotation.getRevision());
            }
        }
    }
    return new CvsFileAnnotation(annotateOperation.getContent(), lineAnnotations, revisions, cvsVirtualFile, revision, myProject);
}
Also used : FilePath(com.intellij.openapi.vcs.FilePath) AnnotateOperation(com.intellij.cvsSupport2.cvsoperations.cvsAnnotate.AnnotateOperation) FileAnnotation(com.intellij.openapi.vcs.annotate.FileAnnotation) Annotation(com.intellij.cvsSupport2.cvsoperations.cvsAnnotate.Annotation) VcsFileRevision(com.intellij.openapi.vcs.history.VcsFileRevision) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Aggregations

FileAnnotation (com.intellij.openapi.vcs.annotate.FileAnnotation)28 VcsAnnotationLocalChangesListener (com.intellij.openapi.vcs.changes.VcsAnnotationLocalChangesListener)16 Test (org.junit.Test)16 Change (com.intellij.openapi.vcs.changes.Change)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 VcsException (com.intellij.openapi.vcs.VcsException)7 File (java.io.File)7 LocalFileSystem (com.intellij.openapi.vfs.LocalFileSystem)4 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)3 Task (com.intellij.openapi.progress.Task)3 Ref (com.intellij.openapi.util.Ref)3 FilePath (com.intellij.openapi.vcs.FilePath)3 ProjectLevelVcsManager (com.intellij.openapi.vcs.ProjectLevelVcsManager)3 AnnotationProvider (com.intellij.openapi.vcs.annotate.AnnotationProvider)3 List (java.util.List)3 AnnotateOperation (com.intellij.cvsSupport2.cvsoperations.cvsAnnotate.AnnotateOperation)2 Annotation (com.intellij.cvsSupport2.cvsoperations.cvsAnnotate.Annotation)2 ActionManager (com.intellij.openapi.actionSystem.ActionManager)2 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)2 CommonDataKeys (com.intellij.openapi.actionSystem.CommonDataKeys)2