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 13, 2024
1 parent 2d8653f commit 8f8f9bb
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 11 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;
}
40 changes: 32 additions & 8 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,11 +44,13 @@ 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
// Based on the pair & the given side.
const file = computed(() => props.side === "left" ? props.pair.leftFile : props.pair.rightFile);
const ignoredKgrams = computed(() => props.side === "left" ? props.pair.leftIgnoredKgrams : props.pair.rightIgnoredKgrams);
// List of matches, sorted on the start line & column.
const matches = computed(() => {
Expand Down Expand Up @@ -83,7 +85,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 +179,20 @@ const initializeSelections = (): void => {
isWholeLine: true,
});
}
// Convert the ignored kgrams into selections
for (const ignored of ignoredKgrams.value) {
selections.value.push({
match: "ignored",
range: {
startLineNumber: ignored.startRow + 1,
startColumn: ignored.startCol + 1,
endLineNumber: ignored.endRow + 1,
endColumn: ignored.endCol + 1,
},
isWholeLine: false,
});
}
};
const areMatchesEqual = (match1: Fragment | null, match2: Fragment | null) => {
Expand All @@ -196,15 +211,20 @@ 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 === "ignored") {
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;
}
return {
range: selection.range,
Expand Down Expand Up @@ -367,6 +387,10 @@ watch(
&--hovering {
background-color: v-bind("colors.matchHovering");
}
&--ignored {
background-color: v-bind("colors.matchIgnored");
}
}
}
</style>
Expand Down

0 comments on commit 8f8f9bb

Please sign in to comment.