Skip to content

Commit

Permalink
Merge pull request #4921 from dodona-edu/fix/annotations-past-end-of-…
Browse files Browse the repository at this point in the history
…line

Constrain annotations to start within the length of the line when rendering
  • Loading branch information
chvp authored Aug 28, 2023
2 parents b38435f + 9ba5a88 commit bf6a856
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions app/assets/javascripts/components/annotations/line_of_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export class LineOfCode extends ShadowlessLitElement {
.sort(compareAnnotationOrders);
}

hasValidColumn(annotation: Annotation): boolean {
return annotation.column !== undefined && annotation.column !== null && annotation.column >= 0 && annotation.column < this.codeLength;
}

/**
* Calculates the range of the code that is covered by the given annotation.
* If the annotation spans multiple lines, the range will be the whole line unless this is the first or last line.
Expand All @@ -81,12 +85,17 @@ export class LineOfCode extends ShadowlessLitElement {

let start = 0;
if (this.row === firstRow) {
start = annotation.column || 0;
// In rare cases, machine annotations start past the end of the line, resulting in errors if we don't constrain them to be in the line
if (this.hasValidColumn(annotation)) {
start = annotation.column;
} else {
start = 0;
}
}

let length = this.codeLength - start;
if (this.row === lastRow) {
if (annotation.column !== undefined && annotation.column !== null) {
if (this.hasValidColumn(annotation)) {
const defaultLength = isMachineAnnotation ? 0 : this.codeLength - start;
length = annotation.columns || defaultLength;
}
Expand Down

0 comments on commit bf6a856

Please sign in to comment.