Skip to content

Commit

Permalink
Refactor code to improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentHagu committed Jan 27, 2025
1 parent 7cb451b commit 2245743
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export class HighlightRuleComponent {
if (!lineNumber) return null;

const isUnbounded = groups.every(x => x === '');

if (isUnbounded) {
return new HighlightRuleComponent(lineNumber, true, []);
}
Expand Down Expand Up @@ -115,36 +114,17 @@ export class HighlightRuleComponent {
let [start, end] = bound;

if (start === UNBOUNDED) {
if (isAbsoluteIndexing) {
start = 0;
} else {
start = indents.length;
}
} else if (!isAbsoluteIndexing) {
start += indents.length;
// Clamp values
if (start < indents.length) {
start = indents.length;
} else if (start > line.length) {
start = line.length;
}
} else if (start > line.length) {
start = line.length;
start = isAbsoluteIndexing ? 0 : indents.length;
} else {
start = isAbsoluteIndexing ? start : Math.max(start + indents.length, indents.length);
start = Math.min(start, line.length);
}

if (end === UNBOUNDED) {
end = line.length;
} else if (!isAbsoluteIndexing) {
end += indents.length;

// Clamp values
if (end < indents.length) {
end = indents.length;
} else if (end > line.length) {
end = line.length;
}
} else if (end > line.length) {
end = line.length;
} else {
end = isAbsoluteIndexing ? end : Math.max(end + indents.length, indents.length);
end = Math.min(end, line.length);
}

return [start, end];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ describe('computeCharBounds, absolute value bounds', () => {
expect(bounds).toEqual([' some text'.length, ' some text'.length]);
});

test('handles line-length end correctly', () => {
const bounds = HighlightRuleComponent.computeCharBounds([0, 4], ' abcd', true);
expect(bounds).toEqual([0, 4]);
test('handles bounds spanning from start to line length correctly', () => {
const bounds = HighlightRuleComponent.computeCharBounds([0, 11], ' some text', true);
expect(bounds).toEqual([0, 11]);
});
});

Expand Down

0 comments on commit 2245743

Please sign in to comment.