use of org.eclipse.compare.rangedifferencer.RangeDifference in project translationstudio8 by heartsome.
the class Comparator method Compare.
/**
* 用于比较的参照文本(不含内部标记)
* @param referenceText
* @param text
* ;
*/
public static void Compare(String referenceText, StyledText text) {
ColorConfigBean colorCfgBean = ColorConfigBean.getInstance();
final Color differencefg = colorCfgBean.getSrcDiffFgColor();
final Color differencebg = colorCfgBean.getSrcDiffBgColor();
Position[] tagRanges = InnerTagUtil.getStyledTagRanges(text.getText());
TokenComparator left = new TokenComparator(InnerTagUtil.getDisplayValueWithoutTags(text.getText()));
TokenComparator right = new TokenComparator(referenceText);
ArrayList<StyleRange> styleRanges = new ArrayList<StyleRange>();
RangeDifference[] e = RangeDifferencer.findRanges(left, right);
for (int i = 0; i < e.length; i++) {
RangeDifference es = e[i];
int leftStart = es.leftStart();
int leftEnd = es.leftEnd();
int lStart = left.getTokenStart(leftStart);
int lEnd = left.getTokenStart(leftEnd);
if (es.kind() == RangeDifference.CHANGE) {
int start = lStart;
int end = lEnd;
if (tagRanges.length > 0) {
for (Position tagRange : tagRanges) {
int tagStart = tagRange.getOffset();
int tagEnd = tagRange.getOffset() + tagRange.getLength();
if (tagStart <= start) {
start += tagRange.getLength();
end += tagRange.getLength();
} else if (start < tagStart && tagStart < end) {
StyleRange range = new StyleRange(start, tagStart - start, differencefg, differencebg);
styleRanges.add(range);
start = tagEnd;
end += tagRange.getLength();
} else {
break;
}
}
}
StyleRange range = new StyleRange(start, end - start, differencefg, differencebg);
styleRanges.add(range);
}
}
for (int i = 0; i < styleRanges.size(); i++) {
text.setStyleRange(styleRanges.get(i));
text.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
differencefg.dispose();
differencebg.dispose();
}
});
}
}
use of org.eclipse.compare.rangedifferencer.RangeDifference in project translationstudio8 by heartsome.
the class Comparator method Compare.
public static List<Position> Compare(String referenceText, String targetText) {
List<Position> differencePosition = new ArrayList<Position>();
Position[] tagRanges = InnerTagUtil.getStyledTagRanges(targetText);
TokenComparator left = new TokenComparator(InnerTagUtil.getDisplayValueWithoutTags(targetText));
TokenComparator right = new TokenComparator(referenceText);
RangeDifference[] e = RangeDifferencer.findRanges(left, right);
for (int i = 0; i < e.length; i++) {
RangeDifference es = e[i];
int leftStart = es.leftStart();
int leftEnd = es.leftEnd();
int lStart = left.getTokenStart(leftStart);
int lEnd = left.getTokenStart(leftEnd);
if (es.kind() == RangeDifference.CHANGE) {
int start = lStart;
int end = lEnd;
if (tagRanges.length > 0) {
for (Position tagRange : tagRanges) {
int tagStart = tagRange.getOffset();
int tagEnd = tagRange.getOffset() + tagRange.getLength();
if (tagStart <= start) {
start += tagRange.getLength();
end += tagRange.getLength();
} else if (start < tagStart && tagStart < end) {
if (start >= 0 && end - 1 >= 0) {
Position position = new Position(start, tagStart - 1);
differencePosition.add(position);
}
start = tagEnd;
end += tagRange.getLength();
} else {
break;
}
}
}
if (start >= 0 && end - 1 >= 0) {
Position position = new Position(start, end - 1);
differencePosition.add(position);
}
}
}
return differencePosition;
}
use of org.eclipse.compare.rangedifferencer.RangeDifference 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.compare.rangedifferencer.RangeDifference 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