Skip to content

Commit

Permalink
Update documentation to follow idea of absolute char indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentHagu committed Jan 27, 2025
1 parent fe14d75 commit 7cb451b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { splitCodeAndIndentation } from './helper';

const LINESLICE_CHAR_REGEX = /(_?)(\d+)\[(\d*):(\d*)]/;
const LINESLICE_CHAR_REGEX = /(\+?)(\d+)\[(\d*):(\d*)]/;
const LINESLICE_WORD_REGEX = /(\d+)\[(\d*)::(\d*)]/;
const LINEPART_REGEX = /(\d+)\[(["'])((?:\\.|[^\\])*?)\2]/;
const UNBOUNDED = -1;
Expand Down Expand Up @@ -43,13 +43,13 @@ export class HighlightRuleComponent {
const linesliceWordMatch = compString.match(LINESLICE_WORD_REGEX);
const sliceMatch = linesliceCharMatch || linesliceWordMatch;
if (sliceMatch) {
// There are four/five capturing groups: [full match, is highlight spaces (for char slice),
// There are four/five capturing groups: [full match, is absolute indexing (for char match),
// line number, start bound, end bound]
const groups = sliceMatch.slice(1); // discard full match

let isHighlightSpaces = false;
let isAbsoluteIndexing = false;
if (sliceMatch === linesliceCharMatch) {
isHighlightSpaces = groups.shift() === '_';
isAbsoluteIndexing = groups.shift() === '+';
}

const lineNumber = HighlightRuleComponent
Expand All @@ -65,7 +65,7 @@ export class HighlightRuleComponent {
let bound = groups.map(x => (x !== '' ? parseInt(x, 10) : UNBOUNDED)) as [number, number];
const isCharSlice = sliceMatch === linesliceCharMatch;
bound = isCharSlice
? HighlightRuleComponent.computeCharBounds(bound, lines[lineNumber - 1], isHighlightSpaces)
? HighlightRuleComponent.computeCharBounds(bound, lines[lineNumber - 1], isAbsoluteIndexing)
: HighlightRuleComponent.computeWordBounds(bound, lines[lineNumber - 1]);

return new HighlightRuleComponent(lineNumber, true, [bound]);
Expand Down Expand Up @@ -102,26 +102,25 @@ export class HighlightRuleComponent {
* comparing the bounds and the line's range.
*
* If the bound does not specify either the start or the end bound, the computed bound will default
* to the start or end of line. The bound will either include or exclude leading whitespaces depending
* on the user's preference.
* to the start or end of line. The bound can be either absolute or relative to the indentation level.
*
* @param bound The user-defined bound
* @param line The given line
* @param isHighlightSpaces Whether to highlight leading whitespaces
* @param isAbsoluteIndexing Whether the bound is absolute or relative to the indentation level
* @returns {[number, number]} The actual bound computed
*/
static computeCharBounds(bound: [number, number], line: string,
isHighlightSpaces: boolean): [number, number] {
isAbsoluteIndexing: boolean): [number, number] {
const [indents] = splitCodeAndIndentation(line);
let [start, end] = bound;

if (start === UNBOUNDED) {
if (isHighlightSpaces) {
if (isAbsoluteIndexing) {
start = 0;
} else {
start = indents.length;
}
} else if (!isHighlightSpaces) {
} else if (!isAbsoluteIndexing) {
start += indents.length;
// Clamp values
if (start < indents.length) {
Expand All @@ -135,7 +134,7 @@ export class HighlightRuleComponent {

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

// Clamp values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('parseRuleComponent', () => {
});
});

describe('computeCharBounds, no whitespace highlighting', () => {
describe('computeCharBounds, relative to indent-level', () => {
test('computes character bounds correctly', () => {
const bounds = HighlightRuleComponent.computeCharBounds([2, 5], ' some text', false);
expect(bounds).toEqual([4, 7]);
Expand All @@ -66,7 +66,7 @@ describe('computeCharBounds, no whitespace highlighting', () => {
});
});

describe('computeCharBounds, with whitespace highlighting', () => {
describe('computeCharBounds, absolute value bounds', () => {
test('computes character bounds correctly', () => {
const bounds = HighlightRuleComponent.computeCharBounds([2, 5], ' some text', true);
expect(bounds).toEqual([2, 5]);
Expand Down

0 comments on commit 7cb451b

Please sign in to comment.