Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce line and column indices for all language modules #589

Merged
merged 2 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}

}