Skip to content

Commit

Permalink
Merge pull request #589 from jplag/enforce-lines
Browse files Browse the repository at this point in the history
Enforce line and column indices for all language modules
  • Loading branch information
tsaglam authored Aug 23, 2022
2 parents 5bbee67 + 6b3db4d commit 6b8dd84
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 35 deletions.
8 changes: 0 additions & 8 deletions jplag.frontend-utils/src/main/java/de/jplag/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ public interface Language {
*/
boolean hasErrors();

/**
* Determines whether the parser provide column information. If that is the case, line and column indices are used
* instead of a single token index.
*/
default boolean supportsColumns() {
return true;
}

/**
* Determines whether a fixed-width font should be used to display that language.
*/
Expand Down
5 changes: 0 additions & 5 deletions jplag.frontend-utils/src/main/java/de/jplag/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ public String getFile() {
return file;
}

// this is made to distinguish the character front end. Maybe other front ends can use it too?
public int getIndex() {
return -1;
}

/**
* Gives the length if the code sections represented by this token.
* @return the length in characters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ public ComparisonReportWriter(Function<Submission, String> submissionToIdFunctio
public Map<String, Map<String, String>> writeComparisonReports(JPlagResult jPlagResult, String path) {
int numberOfComparisons = jPlagResult.getOptions().getMaximumNumberOfComparisons();
List<JPlagComparison> comparisons = jPlagResult.getComparisons(numberOfComparisons);
writeComparisons(jPlagResult, path, comparisons);
writeComparisons(path, comparisons);
return submissionIdToComparisonFileName;
}

private void writeComparisons(JPlagResult jPlagResult, String path, List<JPlagComparison> comparisons) {
private void writeComparisons(String path, List<JPlagComparison> comparisons) {
for (JPlagComparison comparison : comparisons) {
String firstSubmissionId = submissionToIdFunction.apply(comparison.getFirstSubmission());
String secondSubmissionId = submissionToIdFunction.apply(comparison.getSecondSubmission());
String fileName = generateComparisonName(firstSubmissionId, secondSubmissionId);
addToLookUp(firstSubmissionId, secondSubmissionId, fileName);
var comparisonReport = new ComparisonReport(firstSubmissionId, secondSubmissionId, comparison.similarity(),
convertMatchesToReportMatches(jPlagResult, comparison));
convertMatchesToReportMatches(comparison));
fileWriter.saveAsJSON(comparisonReport, path, fileName);
}
}
Expand Down Expand Up @@ -82,30 +82,20 @@ private String concatenate(String firstSubmissionId, String secondSubmissionId)
return concatenate(firstSubmissionId, secondSubmissionId, 0);
}

private List<Match> convertMatchesToReportMatches(JPlagResult result, JPlagComparison comparison) {
return comparison.getMatches().stream()
.map(match -> convertMatchToReportMatch(comparison, match, result.getOptions().getLanguage().supportsColumns())).toList();
private List<Match> convertMatchesToReportMatches(JPlagComparison comparison) {
return comparison.getMatches().stream().map(match -> convertMatchToReportMatch(comparison, match)).toList();
}

private Match convertMatchToReportMatch(JPlagComparison comparison, de.jplag.Match match, boolean languageSupportsColumnsAndLines) {
private Match convertMatchToReportMatch(JPlagComparison comparison, de.jplag.Match match) {
TokenList tokensFirst = comparison.getFirstSubmission().getTokenList();
TokenList tokensSecond = comparison.getSecondSubmission().getTokenList();
Token startTokenFirst = tokensFirst.getToken(match.startOfFirst());
Token endTokenFirst = tokensFirst.getToken(match.startOfFirst() + match.length() - 1);
Token startTokenSecond = tokensSecond.getToken(match.startOfSecond());
Token endTokenSecond = tokensSecond.getToken(match.startOfSecond() + match.length() - 1);

int startFirst = getPosition(languageSupportsColumnsAndLines, startTokenFirst);
int endFirst = getPosition(languageSupportsColumnsAndLines, endTokenFirst);
int startSecond = getPosition(languageSupportsColumnsAndLines, startTokenSecond);
int endSecond = getPosition(languageSupportsColumnsAndLines, endTokenSecond);
int tokens = match.length();

return new Match(startTokenFirst.getFile(), startTokenSecond.getFile(), startFirst, endFirst, startSecond, endSecond, tokens);
}
Token startOfFirst = tokensFirst.getToken(match.startOfFirst());
Token endOfFirst = tokensFirst.getToken(match.startOfFirst() + match.length() - 1);
Token startOfSecond = tokensSecond.getToken(match.startOfSecond());
Token endOfSecond = tokensSecond.getToken(match.startOfSecond() + match.length() - 1);

private int getPosition(boolean languageSupportsColumnsAndLines, Token token) {
return languageSupportsColumnsAndLines ? token.getLine() : token.getIndex();
return new Match(startOfFirst.getFile(), startOfSecond.getFile(), startOfFirst.getLine(), endOfFirst.getLine(), startOfSecond.getLine(),
endOfSecond.getLine(), match.length());
}

}

0 comments on commit 6b8dd84

Please sign in to comment.