use of org.eclipse.linuxtools.internal.changelog.core.LineComparator in project linuxtools by eclipse.
the class PrepareChangeLogAction method getChangedLines.
private void getChangedLines(Subscriber s, PatchFile p, IProgressMonitor monitor) {
try {
// For an outgoing changed resource, find out which lines
// differ from the local file and its previous local version
// (i.e. we don't want to force a diff with the repository).
IDiff d = s.getDiff(p.getResource());
if (d instanceof IThreeWayDiff && ((IThreeWayDiff) d).getDirection() == IThreeWayDiff.OUTGOING) {
IThreeWayDiff diff = (IThreeWayDiff) d;
monitor.beginTask(null, 100);
IResourceDiff localDiff = (IResourceDiff) diff.getLocalChange();
IResource resource = localDiff.getResource();
if (resource instanceof IFile) {
IFile file = (IFile) resource;
// $NON-NLS-1$
monitor.subTask(Messages.getString("ChangeLog.MergingDiffs"));
String osEncoding = file.getCharset();
IFileRevision ancestorState = localDiff.getBeforeState();
IStorage ancestorStorage;
if (ancestorState != null) {
ancestorStorage = ancestorState.getStorage(monitor);
p.setStorage(ancestorStorage);
} else {
return;
}
try {
// We compare using a standard differencer to get ranges
// of changes. We modify them to be document-based (i.e.
// first line is line 1) and store them for later parsing.
LineComparator left = new LineComparator(ancestorStorage.getContents(), osEncoding);
LineComparator right = new LineComparator(file.getContents(), osEncoding);
for (RangeDifference tmp : RangeDifferencer.findDifferences(left, right)) {
if (tmp.kind() == RangeDifference.CHANGE) {
// Right side of diff are all changes found in local file.
int rightLength = tmp.rightLength() > 0 ? tmp.rightLength() : tmp.rightLength() + 1;
// We also want to store left side of the diff which are changes to the ancestor as it may contain
// functions/methods that have been removed.
int leftLength = tmp.leftLength() > 0 ? tmp.leftLength() : tmp.leftLength() + 1;
// Only store left side changes if the storage exists and we add one to the start line number
if (p.getStorage() != null)
p.addLineRange(tmp.leftStart(), tmp.leftStart() + leftLength, false);
p.addLineRange(tmp.rightStart(), tmp.rightStart() + rightLength, true);
}
}
} catch (UnsupportedEncodingException e) {
// do nothing for now
}
}
monitor.done();
}
} catch (CoreException e) {
// Do nothing if error occurs
}
}
use of org.eclipse.linuxtools.internal.changelog.core.LineComparator in project linuxtools by eclipse.
the class PrepareCommitHandler method loadClipboard.
private void loadClipboard(IProgressMonitor monitor) {
IEditorPart currentEditor;
try {
currentEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
} catch (Exception e) {
// no editor is active now so do nothing
return;
}
if (currentEditor == null) {
return;
}
IFile changelog = getChangelogFile(getDocumentLocation(currentEditor, false));
if (changelog == null) {
return;
}
String diffResult = "";
IProject project = null;
IResource[] resources = new IResource[] { changelog };
project = changelog.getProject();
RepositoryProvider r = RepositoryProvider.getProvider(project);
if (r == null) {
// There is no repository provider for this project, i.e
return;
// it's not shared.
}
SyncInfoSet set = new SyncInfoSet();
Subscriber s = r.getSubscriber();
try {
s.refresh(resources, IResource.DEPTH_ZERO, monitor);
} catch (TeamException e1) {
// Ignore, continue anyways
}
s.collectOutOfSync(resources, IResource.DEPTH_ZERO, set, monitor);
SyncInfo[] infos = set.getSyncInfos();
if (infos.length == 1) {
int kind = SyncInfo.getChange(infos[0].getKind());
if (kind == SyncInfo.CHANGE) {
try {
IDiff d = s.getDiff(infos[0].getLocal());
if (d instanceof IThreeWayDiff && ((IThreeWayDiff) d).getDirection() == IThreeWayDiff.OUTGOING) {
IThreeWayDiff diff = (IThreeWayDiff) d;
monitor.beginTask(null, 100);
IResourceDiff localDiff = (IResourceDiff) diff.getLocalChange();
IFile file = (IFile) localDiff.getResource();
monitor.subTask(Messages.getString(// $NON-NLS-1$
"ChangeLog.MergingDiffs"));
String osEncoding = file.getCharset();
IFileRevision ancestorState = localDiff.getBeforeState();
IStorage ancestorStorage;
if (ancestorState != null)
ancestorStorage = ancestorState.getStorage(monitor);
else {
ancestorStorage = null;
return;
}
try {
LineComparator left = new LineComparator(ancestorStorage.getContents(), osEncoding);
LineComparator right = new LineComparator(file.getContents(), osEncoding);
for (RangeDifference tmp : RangeDifferencer.findDifferences(left, right)) {
if (tmp.kind() == RangeDifference.CHANGE) {
LineNumberReader l = new LineNumberReader(new InputStreamReader(file.getContents()));
int rightLength = tmp.rightLength() > 0 ? tmp.rightLength() : tmp.rightLength() + 1;
String line0 = null;
String preDiffResult = "";
for (int i = 0; i < tmp.rightStart(); ++i) {
// usage if needed.
try {
String line = l.readLine();
if (line0 == null)
line0 = line;
preDiffResult += line + "\n";
} catch (IOException e) {
break;
}
}
for (int i = 0; i < rightLength; ++i) {
try {
String line = l.readLine();
// used as a commit comment.
if (i == rightLength - tmp.rightStart()) {
if (tmp.rightStart() != 0 && line.equals(line0)) {
diffResult = preDiffResult += diffResult;
// stop
i = rightLength;
// loop
} else
diffResult += line + "\n";
} else
// $NON-NLS-1$
diffResult += line + "\n";
} catch (IOException e) {
// do nothing
}
}
}
}
} catch (UnsupportedEncodingException e) {
// do nothing for now
}
monitor.done();
}
} catch (CoreException e) {
// do nothing
}
}
}
if (!diffResult.equals(""))
populateClipboardBuffer(diffResult);
}
Aggregations