mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Show differences actions
This commit is contained in:
@@ -13,6 +13,7 @@ import com.intellij.openapi.vcs.fileView.FileViewEnvironment;
|
||||
import com.intellij.openapi.vcs.history.VcsHistoryProvider;
|
||||
import com.intellij.openapi.vcs.update.UpdateEnvironment;
|
||||
import com.intellij.openapi.vcs.annotate.AnnotationProvider;
|
||||
import com.intellij.openapi.vcs.diff.DiffProvider;
|
||||
|
||||
public abstract class AbstractVcs {
|
||||
|
||||
@@ -135,4 +136,8 @@ public abstract class AbstractVcs {
|
||||
public AnnotationProvider getAnnotationProvider(){
|
||||
return null;
|
||||
}
|
||||
|
||||
public DiffProvider getDiffProvider() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.intellij.openapi.vcs.diff;
|
||||
|
||||
import com.intellij.openapi.vcs.history.VcsFileContent;
|
||||
import com.intellij.openapi.vcs.history.VcsRevisionNumber;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
|
||||
public interface DiffProvider {
|
||||
|
||||
VcsRevisionNumber getCurrentRevision(VirtualFile file);
|
||||
|
||||
VcsRevisionNumber getLastRevision(VirtualFile virtualFile);
|
||||
|
||||
VcsFileContent createFileContent(VcsRevisionNumber revisionNumber, VirtualFile selectedFile);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.intellij.openapi.vcs.history;
|
||||
|
||||
import com.intellij.openapi.vcs.VcsException;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface VcsFileContent {
|
||||
void loadContent() throws VcsException;
|
||||
|
||||
byte[] getContent() throws IOException;
|
||||
}
|
||||
@@ -4,12 +4,9 @@
|
||||
*/
|
||||
package com.intellij.openapi.vcs.history;
|
||||
|
||||
import com.intellij.openapi.vcs.VcsException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
public interface VcsFileRevision {
|
||||
public interface VcsFileRevision extends VcsFileContent {
|
||||
VcsFileRevision NULL = new VcsFileRevision() {
|
||||
public VcsRevisionNumber getRevisionNumber() {
|
||||
return VcsRevisionNumber.NULL;
|
||||
@@ -43,6 +40,5 @@ public interface VcsFileRevision {
|
||||
Date getRevisionDate();
|
||||
String getAuthor();
|
||||
String getCommitMessage();
|
||||
void loadContent() throws VcsException;
|
||||
byte[] getContent() throws IOException;
|
||||
|
||||
}
|
||||
|
||||
@@ -48,6 +48,10 @@ public interface VcsRevisionNumber extends Comparable<VcsRevisionNumber>{
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public long getLongValue() {
|
||||
return myValue;
|
||||
}
|
||||
}
|
||||
|
||||
String asString();
|
||||
|
||||
@@ -148,6 +148,9 @@
|
||||
<action id="UpdateFiles" class="com.intellij.openapi.vcs.update.CommonUpdateFileOrDirectoryAction" text="Update"/>
|
||||
<action id="CheckStatusForFiles" class="com.intellij.openapi.vcs.update.CommonStatusFileOrDirectoryAction" text="Check Status"/>
|
||||
<action id="Annotate" class="com.intellij.openapi.vcs.actions.AnnotateToggleAction" text="A_nnotate" description="Show information about last modification date and author for each line"/>
|
||||
<action id="Compare.SameVersion" class="com.intellij.openapi.vcs.actions.CompareWithTheSameVersionAction" text="Compare with The Same Repository Version"/>
|
||||
<action id="Compare.LastVersion" class="com.intellij.openapi.vcs.actions.CompareWithLastVersion" text="Compare with The Last Repository Version"/>
|
||||
<group id="Compare.Selected" class="com.intellij.openapi.vcs.actions.CompareWithSelectedRevisionAction" text="Compare with..." popup="true"/>
|
||||
|
||||
<group id="NewGroup" text="New" popup="true">
|
||||
<action id="NewClass" text="Class" class="com.intellij.ide.actions.CreateClassAction"/>
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
package com.intellij.openapi.vcs.actions;
|
||||
|
||||
import com.intellij.openapi.actionSystem.Presentation;
|
||||
import com.intellij.openapi.diff.DiffManager;
|
||||
import com.intellij.openapi.diff.DocumentContent;
|
||||
import com.intellij.openapi.diff.SimpleContent;
|
||||
import com.intellij.openapi.diff.SimpleDiffRequest;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vcs.*;
|
||||
import com.intellij.openapi.vcs.diff.DiffProvider;
|
||||
import com.intellij.openapi.vcs.history.VcsFileContent;
|
||||
import com.intellij.openapi.vcs.history.VcsRevisionNumber;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class AbstractShowDiffAction extends AbstractVcsAction{
|
||||
|
||||
protected void update(VcsContext vcsContext, Presentation presentation) {
|
||||
updateDiffAction(presentation, vcsContext);
|
||||
}
|
||||
|
||||
protected static void updateDiffAction(final Presentation presentation, final VcsContext vcsContext) {
|
||||
presentation.setEnabled(isEnabled(vcsContext));
|
||||
presentation.setVisible(isVisible(vcsContext));
|
||||
}
|
||||
|
||||
protected static boolean isVisible(final VcsContext vcsContext) {
|
||||
final Project project = vcsContext.getProject();
|
||||
if (project == null) return false;
|
||||
final VirtualFile[] selectedFilePaths = vcsContext.getSelectedFiles();
|
||||
if (selectedFilePaths == null || selectedFilePaths.length != 1) return false;
|
||||
|
||||
final VirtualFile selectedFile = selectedFilePaths[0];
|
||||
|
||||
final AbstractVcs vcs = ProjectLevelVcsManager.getInstance(project).getVcsFor(selectedFile);
|
||||
if (vcs == null) return false;
|
||||
|
||||
final DiffProvider diffProvider = vcs.getDiffProvider();
|
||||
|
||||
if (diffProvider == null) return false;
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
protected static boolean isEnabled(final VcsContext vcsContext) {
|
||||
if (!(isVisible(vcsContext))) return false;
|
||||
|
||||
final Project project = vcsContext.getProject();
|
||||
if (project == null) return false;
|
||||
final VirtualFile[] selectedFilePaths = vcsContext.getSelectedFiles();
|
||||
if (selectedFilePaths == null || selectedFilePaths.length != 1) return false;
|
||||
|
||||
final VirtualFile selectedFile = selectedFilePaths[0];
|
||||
|
||||
final AbstractVcs vcs = ProjectLevelVcsManager.getInstance(project).getVcsFor(selectedFile);
|
||||
if (vcs == null) return false;
|
||||
|
||||
final DiffProvider diffProvider = vcs.getDiffProvider();
|
||||
|
||||
if (diffProvider == null) return false;
|
||||
|
||||
return vcs.fileExistsInVcs(new FilePathImpl(selectedFile)) ;
|
||||
}
|
||||
|
||||
|
||||
protected void actionPerformed(VcsContext vcsContext) {
|
||||
final Project project = vcsContext.getProject();
|
||||
final VirtualFile selectedFile = vcsContext.getSelectedFiles()[0];
|
||||
|
||||
final AbstractVcs vcs = ProjectLevelVcsManager.getInstance(project).getVcsFor(selectedFile);
|
||||
final DiffProvider diffProvider = vcs.getDiffProvider();
|
||||
|
||||
VcsRevisionNumber revisionNumber = getRevisionNumber(diffProvider, selectedFile);
|
||||
|
||||
try {
|
||||
final VcsFileContent fileRevision = diffProvider.createFileContent(revisionNumber, selectedFile);
|
||||
fileRevision.loadContent();
|
||||
|
||||
final SimpleDiffRequest request =
|
||||
new SimpleDiffRequest(project, "Compare local and " + revisionNumber.asString() + " versions");
|
||||
final SimpleContent content1 = new SimpleContent(new String(fileRevision.getContent(), selectedFile.getCharset().name()), selectedFile.getFileType());
|
||||
final DocumentContent content2 = new DocumentContent(project, FileDocumentManager.getInstance().getDocument(selectedFile));
|
||||
|
||||
final VcsRevisionNumber currentRevision = diffProvider.getCurrentRevision(selectedFile);
|
||||
final VcsRevisionNumber lastRevision = diffProvider.getLastRevision(selectedFile);
|
||||
|
||||
if (lastRevision.compareTo(currentRevision) > 0) {
|
||||
request.setContents(content2, content1);
|
||||
request.setContentTitles("Local", revisionNumber.asString());
|
||||
} else {
|
||||
request.setContents(content1, content2);
|
||||
request.setContentTitles(revisionNumber.asString(), "Local");
|
||||
}
|
||||
|
||||
DiffManager.getInstance().getDiffTool().show(request);
|
||||
}
|
||||
catch (VcsException e) {
|
||||
AbstractVcsHelper.getInstance(project).showError(e, "Diff");
|
||||
}
|
||||
catch (IOException e) {
|
||||
AbstractVcsHelper.getInstance(project).showError(new VcsException(e), "Diff");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
protected abstract VcsRevisionNumber getRevisionNumber(DiffProvider diffProvider, VirtualFile file);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.intellij.openapi.vcs.actions;
|
||||
|
||||
import com.intellij.openapi.vcs.history.VcsRevisionNumber;
|
||||
import com.intellij.openapi.vcs.diff.DiffProvider;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
|
||||
public class CompareWithLastVersion extends AbstractShowDiffAction{
|
||||
protected VcsRevisionNumber getRevisionNumber(DiffProvider diffProvider, VirtualFile file) {
|
||||
return diffProvider.getLastRevision(file);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.intellij.openapi.vcs.actions;
|
||||
|
||||
import com.intellij.openapi.actionSystem.ActionGroup;
|
||||
import com.intellij.openapi.actionSystem.AnAction;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.actionSystem.Presentation;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vcs.ProjectLevelVcsManager;
|
||||
import com.intellij.openapi.vcs.AbstractVcs;
|
||||
import com.intellij.openapi.vcs.FilePathImpl;
|
||||
import com.intellij.openapi.vcs.VcsException;
|
||||
import com.intellij.openapi.vcs.diff.DiffProvider;
|
||||
import com.intellij.openapi.vcs.history.VcsHistoryProvider;
|
||||
import com.intellij.openapi.vcs.history.VcsHistorySession;
|
||||
import com.intellij.openapi.vcs.history.VcsFileRevision;
|
||||
import com.intellij.openapi.vcs.history.VcsRevisionNumber;
|
||||
import com.intellij.peer.PeerFactory;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Iterator;
|
||||
import java.util.ArrayList;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.text.DateFormat;
|
||||
|
||||
public class CompareWithSelectedRevisionAction extends ActionGroup{
|
||||
private static final DateFormat DATE_FORMAT = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT);
|
||||
|
||||
public void update(AnActionEvent e) {
|
||||
final VcsContext vcsContext = createVcsContext(e);
|
||||
AbstractShowDiffAction.updateDiffAction(e.getPresentation(), vcsContext);
|
||||
}
|
||||
|
||||
private VcsContext createVcsContext(final AnActionEvent e) {
|
||||
return PeerFactory.getInstance().getVcsContextFactory().createOn(e);
|
||||
}
|
||||
|
||||
public AnAction[] getChildren(AnActionEvent e) {
|
||||
final VcsContext vcsContext = createVcsContext(e);
|
||||
final VirtualFile file = vcsContext.getSelectedFiles()[0];
|
||||
final Project project = vcsContext.getProject();
|
||||
final AbstractVcs vcs = ProjectLevelVcsManager.getInstance(project).getVcsFor(file);
|
||||
final VcsHistoryProvider vcsHistoryProvider = vcs.getVcsHistoryProvider();
|
||||
final ArrayList<AnAction> actions = new ArrayList<AnAction>();
|
||||
|
||||
try {
|
||||
final VcsHistorySession session = vcsHistoryProvider.createSessionFor(new FilePathImpl(file));
|
||||
final List<VcsFileRevision> revisions = session.getRevisionList();
|
||||
for (Iterator<VcsFileRevision> iterator = revisions.iterator(); iterator.hasNext();) {
|
||||
final VcsFileRevision vcsFileRevision = iterator.next();
|
||||
actions.add(new AbstractShowDiffAction() {
|
||||
protected VcsRevisionNumber getRevisionNumber(DiffProvider diffProvider, VirtualFile file) {
|
||||
return vcsFileRevision.getRevisionNumber();
|
||||
}
|
||||
|
||||
protected void update(VcsContext vcsContext, Presentation presentation) {
|
||||
presentation.setText(vcsFileRevision.getRevisionNumber().asString() +" " + DATE_FORMAT.format(vcsFileRevision.getRevisionDate()) + " " + vcsFileRevision.getAuthor());
|
||||
super.update(vcsContext, presentation);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
catch (VcsException e1) {
|
||||
|
||||
}
|
||||
return actions.toArray(new AnAction[actions.size()]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.intellij.openapi.vcs.actions;
|
||||
|
||||
import com.intellij.openapi.vcs.history.VcsRevisionNumber;
|
||||
import com.intellij.openapi.vcs.diff.DiffProvider;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
|
||||
public class CompareWithTheSameVersionAction extends AbstractShowDiffAction{
|
||||
protected VcsRevisionNumber getRevisionNumber(DiffProvider diffProvider, VirtualFile file) {
|
||||
return diffProvider.getCurrentRevision(file);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user