Skip to content

Commit

Permalink
Add template code highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelGusse committed Nov 1, 2024
1 parent 2d8653f commit 7f0a1bd
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 10 deletions.
7 changes: 6 additions & 1 deletion web/src/api/models/pair.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { File, Fragment } from "@/api/models";
import { File, Fragment, Kgram } from "@/api/models";

Check warning on line 1 in web/src/api/models/pair.ts

View workflow job for this annotation

GitHub Actions / Web 🌐 - lint 📏 (22)

'Kgram' is defined but never used
import { FileEntry, Region } from "@dodona/dolos-core";

export interface Pair {
id: number;
Expand All @@ -10,4 +11,8 @@ export interface Pair {
leftCovered: number;
rightCovered: number;
fragments: Fragment[] | null;
leftEntry: FileEntry;
rightEntry: FileEntry;
leftIgnoredKgrams: Region[];
rightIgnoredKgrams: Region[];
}
34 changes: 32 additions & 2 deletions web/src/api/workers/data.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
PairedOccurrence,
Hash,
} from "@/api/models";
import { Fragment as DolosFragment, FingerprintIndex } from "@dodona/dolos-core";
import { Fragment as DolosFragment, FingerprintIndex, TokenizedFile, Pair as DolosPair } from "@dodona/dolos-core";
import { report } from "process";

Check warning on line 12 in web/src/api/workers/data.worker.ts

View workflow job for this annotation

GitHub Actions / Web 🌐 - lint 📏 (22)

'report' is defined but never used

// Parse a list of Dolos fragments into a list of fragment models.
export function parseFragments(
Expand Down Expand Up @@ -37,6 +38,30 @@ export function parseFragments(
});
}

function getIgnoredKgrams(reportPair: DolosPair, leftFile: TokenizedFile, rightFile: TokenizedFile) {
const leftIgnoredKgrams = [];
const rightIgnoredKgrams = [];

for (const ignoredKgram of reportPair.leftEntry.ignored) {
const occurrences = ignoredKgram.occurrencesOf(leftFile);
if (occurrences.length > 0) {
leftIgnoredKgrams.push(occurrences[0].side.location);
}
}

for (const ignoredKgram of reportPair.rightEntry.ignored) {
const occurrences = ignoredKgram.occurrencesOf(rightFile);
if (occurrences.length > 0) {
rightIgnoredKgrams.push(occurrences[0].side.location);
}
}

return {
leftIgnoredKgrams,
rightIgnoredKgrams
};
}

// Populate the fragments for a given pair.
export function populateFragments(
pair: Pair,
Expand Down Expand Up @@ -64,7 +89,12 @@ export function populateFragments(
const kmer = kmers[kmerKey];
kmersMap.set(kmer.hash, kmer);
}
pair.fragments = parseFragments(reportPair.buildFragments(), kmersMap);
const ignoredKgramsMap = getIgnoredKgrams(reportPair, leftFile, rightFile);

pair.fragments = parseFragments(reportPair.buildFragments(), kmersMap);
pair.leftEntry = reportPair.leftEntry;
pair.rightEntry = reportPair.rightEntry;
pair.leftIgnoredKgrams = ignoredKgramsMap.leftIgnoredKgrams;
pair.rightIgnoredKgrams = ignoredKgramsMap.rightIgnoredKgrams;
return pair;
}
66 changes: 59 additions & 7 deletions web/src/components/pair/PairCodeMatchEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface Props {
}
interface Selection {
match: Fragment | null;
match: Fragment | null | string;
range: monaco.IRange | null;
isWholeLine: boolean;
}
Expand All @@ -44,6 +44,7 @@ const colors = {
match: "rgba(60, 115, 168, 0.2)",
matchHovering: "rgba(60, 115, 168, 0.3)",
matchSelected: "rgba(26, 188, 156, 0.5)",
matchIgnored: "rgba(0, 0, 0, 0.05)",
};
// File to display
Expand Down Expand Up @@ -83,7 +84,6 @@ const getMatchAtPosition = (row: number, col: number): Fragment | null => {
for (const match of matches.value) {
const side = match[props.side];
// If the row/col is within the match row range.
const inRowRange = side.startRow + 1 <= row && row <= side.endRow + 1;
// If the row/col is within the match col range.
Expand Down Expand Up @@ -178,6 +178,43 @@ const initializeSelections = (): void => {
isWholeLine: true,
});
}
for (const leftIgnored of props.pair.leftIgnoredKgrams) {
const fakeMatch: Fragment = {

Check warning on line 183 in web/src/components/pair/PairCodeMatchEditor.vue

View workflow job for this annotation

GitHub Actions / Web 🌐 - lint 📏 (22)

'fakeMatch' is assigned a value but never used
left: leftIgnored,
right: {
startRow: 0,
startCol: 0,
endRow: 0,
endCol: 0,
},
data: [],
occurrences: [],
active: false,
};
selections.value.push({
match: "leftIgnored",
range: {
startLineNumber: leftIgnored.startRow + 1,
startColumn: leftIgnored.startCol + 1,
endLineNumber: leftIgnored.endRow + 1,
endColumn: leftIgnored.endCol + 1,
},
isWholeLine: false,
});
}
for (const rightIgnored of props.pair.rightIgnoredKgrams) {
selections.value.push({
match: "rightIgnored",
range: {
startLineNumber: rightIgnored.startRow + 1,
startColumn: rightIgnored.startCol + 1,
endLineNumber: rightIgnored.endRow + 1,
endColumn: rightIgnored.endCol + 1,
},
isWholeLine: false,
});
}
};
const areMatchesEqual = (match1: Fragment | null, match2: Fragment | null) => {
Expand All @@ -196,16 +233,27 @@ const initializeDecorations = (): void => {
decorations.value,
selections.value.map((selection) => {
const match = selection.match;
let classname = "highlight-match";
if (areMatchesEqual(match, selectedMatch?.value)) classname += " highlight-match--selected";
else if (areMatchesEqual(match, hoveringMatch?.value)) classname += " highlight-match--hovering";
if (typeof match !== "string") {
if (areMatchesEqual(match, selectedMatch?.value)) classname += " highlight-match--selected";
else if (areMatchesEqual(match, hoveringMatch?.value)) classname += " highlight-match--hovering";
}
else if (match === "leftIgnored") {
classname += " highlight-match--ignored";
}
else if (match === "rightIgnored") {
classname += " highlight-match--ignored";
}
let color = colors.match;
if (areMatchesEqual(match, selectedMatch?.value)) color = colors.matchSelected;
else if (areMatchesEqual(match, hoveringMatch?.value)) color = colors.matchHovering;
if (typeof match !== "string") {
if (areMatchesEqual(match, selectedMatch?.value)) color = colors.matchSelected;
else if (areMatchesEqual(match, hoveringMatch?.value)) color = colors.matchHovering;
}
console.log(selections)
return {
range: selection.range,
options: {
Expand Down Expand Up @@ -367,6 +415,10 @@ watch(
&--hovering {
background-color: v-bind("colors.matchHovering");
}
&--ignored {
background-color: v-bind("colors.matchIgnored");
}
}
}
</style>
Expand Down

0 comments on commit 7f0a1bd

Please sign in to comment.