Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional absolute char indexing for highlighting #2584

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update documentation to follow idea of absolute char indexing
AgentHagu committed Jan 27, 2025
commit 7cb451b974b541733e97e03b48017a38f6eb5cfd
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;
@@ -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
@@ -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]);
@@ -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) {
@@ -135,7 +134,7 @@ export class HighlightRuleComponent {

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

// Clamp values
Original file line number Diff line number Diff line change
@@ -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]);
@@ -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]);