1 package com
.intellij
.openapi
.vcs
.changes
;
3 import com
.intellij
.openapi
.project
.Project
;
4 import com
.intellij
.openapi
.util
.Pair
;
5 import com
.intellij
.openapi
.vcs
.AbstractVcs
;
6 import com
.intellij
.openapi
.vcs
.FilePathImpl
;
7 import com
.intellij
.openapi
.vcs
.ProjectLevelVcsManager
;
8 import com
.intellij
.openapi
.vcs
.VcsKey
;
9 import com
.intellij
.openapi
.vcs
.impl
.CollectionsDelta
;
10 import org
.jetbrains
.annotations
.Nullable
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Collection
;
15 import java
.util
.List
;
18 public class ChangesDelta
{
19 private final PlusMinus
<Pair
<String
, AbstractVcs
>> myDeltaListener
;
20 private ProjectLevelVcsManager myVcsManager
;
21 private boolean myInitialized
;
23 public ChangesDelta(final Project project
, final PlusMinus
<Pair
<String
, AbstractVcs
>> deltaListener
) {
24 myDeltaListener
= deltaListener
;
25 myVcsManager
= ProjectLevelVcsManager
.getInstance(project
);
28 public void step(final ChangeListsIndexes was
, final ChangeListsIndexes became
) {
29 List
<Pair
<String
, VcsKey
>> wasAffected
= was
.getAffectedFilesUnderVcs();
30 if (! myInitialized
) {
31 wasAffected
= fillVcsKeys(wasAffected
);
32 sendPlus(wasAffected
);
36 final List
<Pair
<String
, VcsKey
>> becameAffected
= became
.getAffectedFilesUnderVcs();
38 final Set
<Pair
<String
,VcsKey
>> toRemove
= CollectionsDelta
.notInSecond(wasAffected
, becameAffected
);
39 final Set
<Pair
<String
, VcsKey
>> toAdd
= CollectionsDelta
.notInSecond(becameAffected
, wasAffected
);
41 if (toRemove
!= null) {
42 for (Pair
<String
, VcsKey
> pair
: toRemove
) {
43 myDeltaListener
.minus(convertPair(pair
));
49 private void sendPlus(final Collection
<Pair
<String
, VcsKey
>> toAdd
) {
51 for (Pair
<String
, VcsKey
> pair
: toAdd
) {
52 myDeltaListener
.plus(convertPair(pair
));
57 private Pair
<String
, AbstractVcs
> convertPair(final Pair
<String
, VcsKey
> pair
) {
58 return new Pair
<String
, AbstractVcs
>(pair
.getFirst(), myVcsManager
.findVcsByName(pair
.getSecond().getName()));
61 // we expect only "was" to don't know what vcses are - after deserialization
62 private List
<Pair
<String
, VcsKey
>> fillVcsKeys(final List
<Pair
<String
, VcsKey
>> coll
) {
63 List
<Pair
<String
, VcsKey
>> converted
= null;
64 for (int i
= 0; i
< coll
.size(); i
++) {
65 final Pair
<String
, VcsKey
> pair
= coll
.get(i
);
66 if (pair
.getSecond() == null) {
67 final String fileKey
= pair
.getFirst();
68 final VcsKey vcsKey
= findVcs(fileKey
);
69 if (converted
== null) {
70 converted
= new ArrayList
<Pair
<String
, VcsKey
>>(coll
.size());
72 for (int j
= 0; j
< i
; j
++) {
73 converted
.add(coll
.get(j
));
77 converted
.add(new Pair
<String
, VcsKey
>(fileKey
, vcsKey
));
80 return converted
!= null ? converted
: coll
;
84 private VcsKey
findVcs(final String path
) {
85 // does not matter directory or not
86 final AbstractVcs vcs
= myVcsManager
.getVcsFor(FilePathImpl
.create(new File(path
), false));
87 return vcs
== null ?
null : vcs
.getKeyInstanceMethod();