Skip to content

The Unified and Autonomous Development Platform

mid work

robin shine192887d2d8c9
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/diff/AroundContext.java b/commons.lang/src/main/java/com/pmease/commons/lang/diff/AroundContext.java
deleted file mode 100644
index 2468fe2cb4..0000000000
--- a/commons.lang/src/main/java/com/pmease/commons/lang/diff/AroundContext.java
+++ /dev/null
@@ -1,44 +0,0 @@
-package com.pmease.commons.lang.diff;
-
-import java.io.Serializable;
-import java.util.List;
-
-@SuppressWarnings("serial")
-public class AroundContext implements Serializable {
-
- private List<DiffLine> diffs;
-
- private final int line;
-
- private final boolean aboveOmitted;
-
- private final boolean belowOmitted;
-
- public AroundContext(List<DiffLine> diffs, int line, boolean aboveOmitted, boolean belowOmitted) {
- this.diffs = diffs;
- this.line = line;
- this.aboveOmitted = aboveOmitted;
- this.belowOmitted = belowOmitted;
- }
-
- public List<DiffLine> getDiffs() {
- return diffs;
- }
-
- public void setDiffs(List<DiffLine> diffs) {
- this.diffs = diffs;
- }
-
- public int getLine() {
- return line;
- }
-
- public boolean isAboveOmitted() {
- return aboveOmitted;
- }
-
- public boolean isBelowOmitted() {
- return belowOmitted;
- }
-
-}
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/diff/DiffBlock.java b/commons.lang/src/main/java/com/pmease/commons/lang/diff/DiffBlock.java
index b0d8b20a5a..d3e0c0dcd6 100644
--- a/commons.lang/src/main/java/com/pmease/commons/lang/diff/DiffBlock.java
+++ b/commons.lang/src/main/java/com/pmease/commons/lang/diff/DiffBlock.java
@@ -3,6 +3,7 @@ package com.pmease.commons.lang.diff;
import java.io.Serializable;
import java.util.List;
+import com.pmease.commons.lang.diff.DiffMatchPatch.Operation;
import com.pmease.commons.lang.tokenizers.Token;
public class DiffBlock implements Serializable {
@@ -13,9 +14,14 @@ public class DiffBlock implements Serializable {
private final List<List<Token>> lines;
- public DiffBlock(DiffMatchPatch.Operation operation, List<List<Token>> lines) {
+ private final int oldStart, newStart;
+
+ public DiffBlock(DiffMatchPatch.Operation operation, List<List<Token>> lines,
+ int oldStart, int newStart) {
this.operation = operation;
this.lines = lines;
+ this.oldStart = oldStart;
+ this.newStart = newStart;
}
public DiffMatchPatch.Operation getOperation() {
@@ -25,5 +31,26 @@ public class DiffBlock implements Serializable {
public List<List<Token>> getLines() {
return lines;
}
+
+ public int getOldStart() {
+ return oldStart;
+ }
+
+ public int getNewStart() {
+ return newStart;
+ }
+
+ public int getOldEnd() {
+ if (operation == Operation.INSERT)
+ return oldStart;
+ else
+ return oldStart + lines.size();
+ }
+ public int getNewEnd() {
+ if (operation == Operation.DELETE)
+ return newStart;
+ else
+ return newStart + lines.size();
+ }
}
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/diff/DiffHunk.java b/commons.lang/src/main/java/com/pmease/commons/lang/diff/DiffHunk.java
deleted file mode 100644
index f4bd94eca5..0000000000
--- a/commons.lang/src/main/java/com/pmease/commons/lang/diff/DiffHunk.java
+++ /dev/null
@@ -1,106 +0,0 @@
-package com.pmease.commons.lang.diff;
-
-import java.io.Serializable;
-import java.util.List;
-
-import com.pmease.commons.lang.diff.DiffMatchPatch.Operation;
-
-@SuppressWarnings("serial")
-public class DiffHunk implements Serializable {
-
- private int oldStart, oldEnd, newStart, newEnd;
-
- private final List<DiffLine> diffLines;
-
- public DiffHunk(int oldStart, int newStart, List<DiffLine> diffLines) {
- this.oldStart = oldStart;
- this.newStart = newStart;
- this.diffLines = diffLines;
-
- oldEnd = oldStart;
- newEnd = newStart;
- for (DiffLine diffLine: diffLines) {
- if (diffLine.getOperation() == Operation.INSERT) {
- newEnd++;
- } else if (diffLine.getOperation() == Operation.DELETE) {
- oldEnd++;
- } else {
- oldEnd++;
- newEnd++;
- }
- }
- }
-
- /**
- * Get starting row number of original part of this diff chunk. <tt>0</tt> represents
- * the first row.
- *
- * @return
- * starting row number of original part of the diff chunk
- */
- public int getOldStart() {
- return oldStart;
- }
-
- /**
- * Get starting row number of revised part of this diff chunk. <tt>0</tt> represents
- * the first row.
- *
- * @return
- * starting row number of revised part of the diff chunk
- */
- public int getNewStart() {
- return newStart;
- }
-
- public int getOldEnd() {
- return oldEnd;
- }
-
- public void setOldEnd(int oldEnd) {
- this.oldEnd = oldEnd;
- }
-
- public int getNewEnd() {
- return newEnd;
- }
-
- public void setNewEnd(int newEnd) {
- this.newEnd = newEnd;
- }
-
- public void setOldStart(int oldStart) {
- this.oldStart = oldStart;
- }
-
- public void setNewStart(int newStart) {
- this.newStart = newStart;
- }
-
- public List<DiffLine> getDiffLines() {
- return diffLines;
- }
-
- /**
- * Get string representation of this diff chunk. It will mimics the unified diff format,
- * so row number will be increased by one, that is, the first row will be represented
- * by <tt>1</tt>.
- */
- @Override
- public String toString() {
- StringBuffer buffer = new StringBuffer(describe(oldStart, newStart, oldEnd, newEnd));
- buffer.append("\n");
- for (DiffLine line: diffLines)
- buffer.append(line).append("\n");
- return buffer.toString();
- }
-
- public static String describe(int oldStart, int newStart, int oldEnd, int newEnd) {
- StringBuffer buffer = new StringBuffer();
- int oldCount = oldEnd-oldStart;
- int newCount = newEnd-newStart;
- buffer.append("@@ -").append(oldCount!=0?oldStart+1:0).append(",").append(oldCount)
- .append(" +").append(newCount!=0?newStart+1:0).append(",").append(newCount).append(" @@");
- return buffer.toString();
- }
-}
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/diff/DiffLine.java b/commons.lang/src/main/java/com/pmease/commons/lang/diff/DiffLine.java
deleted file mode 100644
index 554e37ea4b..0000000000
--- a/commons.lang/src/main/java/com/pmease/commons/lang/diff/DiffLine.java
+++ /dev/null
@@ -1,66 +0,0 @@
-package com.pmease.commons.lang.diff;
-
-import java.io.Serializable;
-import java.util.List;
-
-import com.pmease.commons.lang.diff.DiffMatchPatch.Operation;
-import com.pmease.commons.lang.tokenizers.Token;
-
-@SuppressWarnings("serial")
-public class DiffLine implements Serializable {
-
- private final Operation operation;
-
- private final int oldLineNo;
-
- private final int newLineNo;
-
- private final List<Token> tokens;
-
- public DiffLine(Operation operation, List<Token> tokens, int oldLineNo, int newLineNo) {
- this.operation = operation;
- this.tokens = tokens;
- this.oldLineNo = oldLineNo;
- this.newLineNo = newLineNo;
- }
-
- public Operation getOperation() {
- return operation;
- }
-
- /**
- * Get list of partials of this diff line. Concatenate all partial contents will
- * form a line, and partials with {@link Token#isEmphasized()} indicates
- * whether or not a partial should be emphasized when displayed as they cause
- * the line to be considered as a diff line.
- *
- * @return
- * list of partials of this diff line
- */
- public List<Token> getTokens() {
- return tokens;
- }
-
- public int getOldLineNo() {
- return oldLineNo;
- }
-
- public int getNewLineNo() {
- return newLineNo;
- }
-
- @Override
- public String toString() {
- StringBuffer buffer = new StringBuffer();
- for (Token token: tokens)
- buffer.append(token.toString());
-
- if (operation == Operation.DELETE)
- return "-" + buffer.toString();
- else if (operation == Operation.EQUAL)
- return " " + buffer.toString();
- else
- return "+" + buffer.toString();
- }
-
-}
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/diff/DiffUtils.java b/commons.lang/src/main/java/com/pmease/commons/lang/diff/DiffUtils.java
index 0a1971ca59..7004928a46 100644
--- a/commons.lang/src/main/java/com/pmease/commons/lang/diff/DiffUtils.java
+++ b/commons.lang/src/main/java/com/pmease/commons/lang/diff/DiffUtils.java
@@ -13,7 +13,6 @@ import com.google.common.base.Preconditions;
import com.pmease.commons.lang.diff.DiffMatchPatch.Diff;
import com.pmease.commons.lang.diff.DiffMatchPatch.Operation;
import com.pmease.commons.lang.tokenizers.Token;
-import com.pmease.commons.lang.tokenizers.TokenizedLine;
import com.pmease.commons.lang.tokenizers.Tokenizers;
import com.pmease.commons.loader.AppLoader;
@@ -37,20 +36,20 @@ public class DiffUtils {
return tokens;
}
- private static List<TokenizedLine> tokenize(List<String> lines, @Nullable String fileName) {
- List<TokenizedLine> tokenizedLines = null;
+ private static List<List<Token>> tokenize(List<String> lines, @Nullable String fileName) {
+ List<List<Token>> tokenizedLines = null;
if (fileName != null)
tokenizedLines = AppLoader.getInstance(Tokenizers.class).tokenize(lines, fileName);
if (tokenizedLines != null) {
- List<TokenizedLine> refinedLines = new ArrayList<>();
- for (TokenizedLine tokenizedLine: tokenizedLines) {
- TokenizedLine refinedLine = new TokenizedLine();
- for (Token token: tokenizedLine.getTokens()) {
+ List<List<Token>> refinedLines = new ArrayList<>();
+ for (List<Token> tokenizedLine: tokenizedLines) {
+ List<Token> refinedLine = new ArrayList<>();
+ for (Token token: tokenizedLine) {
if (token.style().equals("") || token.isComment() || token.isString()
|| token.isMeta() || token.isLink() || token.isAttribute()
|| token.isProperty()) {
for (String each: splitByWords(token.text()))
- refinedLine.getTokens().add(new Token(token.style(), each));
+ refinedLine.add(new Token(token.style(), each));
}
}
refinedLines.add(refinedLine);
@@ -59,9 +58,9 @@ public class DiffUtils {
} else {
tokenizedLines = new ArrayList<>();
for (String line: lines) {
- TokenizedLine tokenizedLine = new TokenizedLine();
+ List<Token> tokenizedLine = new ArrayList<>();
for (String each: splitByWords(line))
- tokenizedLine.getTokens().add(new Token(each, ""));
+ tokenizedLine.add(new Token(each, ""));
tokenizedLines.add(tokenizedLine);
}
return tokenizedLines;
@@ -71,53 +70,65 @@ public class DiffUtils {
/**
* Diff two list of strings.
*/
- public static List<DiffLine> diff(List<String> oldLines, @Nullable String oldFileName,
+ public static List<DiffBlock> diff(List<String> oldLines, @Nullable String oldFileName,
List<String> newLines, @Nullable String newFileName) {
Preconditions.checkArgument(oldLines.size() + newLines.size() <= 65535,
"Total size of old lines and new lines should be less than 65535.");
- List<TokenizedLine> oldTokenizedLines = tokenize(oldLines, oldFileName);
- List<TokenizedLine> newTokenizedLines = tokenize(newLines, newFileName);
+ List<List<Token>> oldTokenizedLines = tokenize(oldLines, oldFileName);
+ List<List<Token>> newTokenizedLines = tokenize(newLines, newFileName);
DiffMatchPatch dmp = new DiffMatchPatch();
TokensToCharsResult result = tokensToChars(oldLines, newLines);
List<DiffMatchPatch.Diff> diffs = dmp.diff_main(result.chars1, result.chars2, false);
- List<DiffLine> diffLines = new ArrayList<>();
+ List<DiffBlock> diffBlocks = new ArrayList<>();
int oldLineNo = 0;
int newLineNo = 0;
- Diff prevDiff = null;
for (Diff diff : diffs) {
-
+ List<List<Token>> lines = new ArrayList<>();
if (diff.operation == Operation.EQUAL) {
for (int i = 0; i < diff.text.length(); i++) {
- List<Token> tokens;
- tokens = newTokenizedLines.get(newLineNo).getTokens();
- diffLines.add(new DiffLine(diff.operation, tokens, oldLineNo++, newLineNo++));
+ lines.add(newTokenizedLines.get(newLineNo));
+ oldLineNo++;
+ newLineNo++;
}
+ } else if (diff.operation == Operation.INSERT) {
+ for (int i = 0; i < diff.text.length(); i++)
+ lines.add(newTokenizedLines.get(newLineNo++));
+ } else {
+ for (int i = 0; i < diff.text.length(); i++)
+ lines.add(oldTokenizedLines.get(oldLineNo++));
}
- prevDiff = diff;
+ diffBlocks.add(new DiffBlock(diff.operation, lines, oldLineNo, newLineNo));
}
- return diffLines;
+
+ DiffBlock prevBlock = null;
+ for (DiffBlock block: diffBlocks) {
+ if (block.getOperation() == Operation.INSERT && prevBlock != null
+ && prevBlock.getOperation() == Operation.DELETE) {
+ for (List<Token> prevLine: prevBlock.getLines()) {
+ for (List<Token> line: block.getLines()) {
+ }
+ }
+ }
+ prevBlock = block;
+ }
+ return diffBlocks;
}
private static TokensToCharsResult tokensToChars(List<String> tokens1, List<String> tokens2) {
- List<String> tokenArray = new ArrayList<>();
Map<String, Integer> tokenHash = new HashMap<>();
// e.g. linearray[4] == "Hello\n"
// e.g. linehash.get("Hello\n") == 4
- // "\x00" is a valid character, but various debuggers don't like it.
- // So we'll insert a junk entry to avoid generating a null character.
- tokenArray.add("");
-
- String chars1 = tokensToCharsMunge(tokens1, tokenArray, tokenHash);
- String chars2 = tokensToCharsMunge(tokens2, tokenArray, tokenHash);
+ String chars1 = tokensToCharsMunge(tokens1, tokenHash);
+ String chars2 = tokensToCharsMunge(tokens2, tokenHash);
return new TokensToCharsResult(chars1, chars2);
}
- private static String tokensToCharsMunge(List<String> tokens, List<String> tokenArray, Map<String, Integer> tokenHash) {
+ private static String tokensToCharsMunge(List<String> tokens, Map<String, Integer> tokenHash) {
StringBuilder chars = new StringBuilder();
for (String token: tokens) {
if (tokenHash.containsKey(token)) {
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/diff/RemovableDiffLine.java b/commons.lang/src/main/java/com/pmease/commons/lang/diff/RemovableDiffLine.java
deleted file mode 100644
index f011ca3368..0000000000
--- a/commons.lang/src/main/java/com/pmease/commons/lang/diff/RemovableDiffLine.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package com.pmease.commons.lang.diff;
-
-class RemovableDiffLine {
-
- DiffLine diffLine;
-
- boolean removed;
-
- RemovableDiffLine(DiffLine diffLine, boolean removed) {
- this.diffLine = diffLine;
- this.removed = removed;
- }
-}
\ No newline at end of file
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/tokenizers/AbstractTokenizer.java b/commons.lang/src/main/java/com/pmease/commons/lang/tokenizers/AbstractTokenizer.java
index 330a53dbab..c93d267e32 100644
--- a/commons.lang/src/main/java/com/pmease/commons/lang/tokenizers/AbstractTokenizer.java
+++ b/commons.lang/src/main/java/com/pmease/commons/lang/tokenizers/AbstractTokenizer.java
@@ -15,13 +15,13 @@ public abstract class AbstractTokenizer<S> implements Tokenizer {
List<List<Token>> tokenizedLines = new ArrayList<>();
S state = startState();
for (String line: lines) {
- TokenizedLine tokenizedLine = new TokenizedLine();
+ List<Token> tokenizedLine = new ArrayList<>();
if (line.length() == 0)
blankLine(state);
StringStream stream = new StringStream(line);
while (!stream.eol()) {
String style = token(stream, state);
- tokenizedLine.getTokens().add(new Token(style, stream.current()));
+ tokenizedLine.add(new Token(style, stream.current()));
stream.start(stream.pos());
}
tokenizedLines.add(tokenizedLine);
@@ -29,7 +29,7 @@ public abstract class AbstractTokenizer<S> implements Tokenizer {
return tokenizedLines;
}
- public List<TokenizedLine> tokenize(String text) {
+ public List<List<Token>> tokenize(String text) {
return tokenize(Splitter.on("\n").splitToList(text));
}
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/tokenizers/Token.java b/commons.lang/src/main/java/com/pmease/commons/lang/tokenizers/Token.java
index 07ba141238..3aa460f758 100644
--- a/commons.lang/src/main/java/com/pmease/commons/lang/tokenizers/Token.java
+++ b/commons.lang/src/main/java/com/pmease/commons/lang/tokenizers/Token.java
@@ -12,6 +12,8 @@ public class Token implements Serializable {
private final String text;
+ private boolean changed;
+
public Token(String style, String text) {
this.style = style;
this.text = text;
@@ -24,6 +26,14 @@ public class Token implements Serializable {
public String text() {
return text;
}
+
+ public boolean isChanged() {
+ return changed;
+ }
+
+ public void setChanged(boolean changed) {
+ this.changed = changed;
+ }
public boolean isComment() {
return style.contains("cm-comment");
diff --git a/commons.lang/src/main/java/com/pmease/commons/lang/tokenizers/TokenizedLine.java b/commons.lang/src/main/java/com/pmease/commons/lang/tokenizers/TokenizedLine.java
deleted file mode 100644
index 640d848d8c..0000000000
--- a/commons.lang/src/main/java/com/pmease/commons/lang/tokenizers/TokenizedLine.java
+++ /dev/null
@@ -1,78 +0,0 @@
-package com.pmease.commons.lang.tokenizers;
-
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.commons.lang3.StringEscapeUtils;
-import org.apache.commons.lang3.builder.HashCodeBuilder;
-
-public class TokenizedLine implements Serializable {
-
- private static final long serialVersionUID = 1L;
-
- private List<Token> tokens = new ArrayList<>();
-
- public List<Token> getTokens() {
- return tokens;
- }
-
- public void setTokens(List<Token> tokens) {
- this.tokens = tokens;
- }
-
- public String toHtml(int tabSize) {
- StringBuilder html = new StringBuilder();
- int col = 0;
- for (Token token: tokens) {
- StringBuilder text = new StringBuilder();
- for (int pos = 0; ;) {
- int idx = token.text().indexOf('\t', pos);
- if (idx == -1) {
- text.append(token.text().substring(pos));
- col += token.text().length() - pos;
- break;
- } else {
- col += idx - pos;
- text.append(token.text().substring(pos, idx));
- int size = tabSize - col % tabSize;
- col += size;
- for (int i=0; i<size; ++i)
- text.append(" ");
- pos = idx + 1;
- }
- }
-
- String escapedText = StringEscapeUtils.escapeHtml4(token.text());
- if (token.style().length() != 0) {
- html.append(String.format("<span class='%s'>%s</span>",
- "cm-" + token.style().replaceAll(" +", " cm-"), escapedText));
- } else {
- html.append(escapedText);
- }
- }
-
- return html.toString();
- }
-
- @Override
- public String toString() {
- return tokens.toString();
- }
-
- @Override
- public boolean equals(Object other) {
- if (!(other instanceof TokenizedLine))
- return false;
- if (this == other)
- return true;
- TokenizedLine otherLine = (TokenizedLine) other;
- return getTokens().equals(otherLine.getTokens());
- }
-
- @Override
- public int hashCode() {
- return new HashCodeBuilder(17, 37).append(getTokens()).toHashCode();
- }
-
-}
diff --git a/commons.lang/src/test/java/com/pmease/commons/lang/tokenizers/AbstractTokenizerTest.java b/commons.lang/src/test/java/com/pmease/commons/lang/tokenizers/AbstractTokenizerTest.java
index 8df6dd4645..0aa327e112 100644
--- a/commons.lang/src/test/java/com/pmease/commons/lang/tokenizers/AbstractTokenizerTest.java
+++ b/commons.lang/src/test/java/com/pmease/commons/lang/tokenizers/AbstractTokenizerTest.java
@@ -70,26 +70,26 @@ public abstract class AbstractTokenizerTest {
public static class Callback {
- private List<TokenizedLine> tokenizedLines = new ArrayList<>();
+ private List<List<Token>> tokenizedLines = new ArrayList<>();
- private TokenizedLine currentLine = new TokenizedLine();
+ private List<Token> currentLine = new ArrayList<>();
public void token(String style, String text) {
if (style == null || style.equals("null") || style.equals("undefined"))
style = "";
if (text.equals("\n")) {
- if (!currentLine.getTokens().isEmpty()) {
+ if (!currentLine.isEmpty()) {
tokenizedLines.add(currentLine);
- currentLine = new TokenizedLine();
+ currentLine = new ArrayList<>();
}
} else {
- currentLine.getTokens().add(new Token(style, text));
+ currentLine.add(new Token(style, text));
}
}
- public List<TokenizedLine> getTokenizedLines() {
- List<TokenizedLine> copy = new ArrayList<>(tokenizedLines);
- if (!currentLine.getTokens().isEmpty())
+ public List<List<Token>> getTokenizedLines() {
+ List<List<Token>> copy = new ArrayList<>(tokenizedLines);
+ if (!currentLine.isEmpty())
copy.add(currentLine);
return copy;
}
diff --git a/gitplex.product/src/test/java/gitplex/product/Test.java b/gitplex.product/src/test/java/gitplex/product/Test.java
index b10dd53b80..804f4193e7 100644
--- a/gitplex.product/src/test/java/gitplex/product/Test.java
+++ b/gitplex.product/src/test/java/gitplex/product/Test.java
@@ -1,11 +1,20 @@
package gitplex.product;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import com.google.common.collect.Lists;
+import com.pmease.commons.lang.tokenizers.Token;
public class Test {
@org.junit.Test
public void test() throws IOException {
+ List<List<Token>> lines = new ArrayList<>();
+ lines.add(Lists.newArrayList(new Token("comment", "hello"), new Token("comment", "world")));
+ lines.add(Lists.newArrayList(new Token("keyword", "for"), new Token("keyword", "while")));
+ System.out.println(lines);
}
}
\ No newline at end of file