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 leading whitespace highlighting capability #2583

Closed
wants to merge 11 commits into from
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ packages/vue-components/src/directives/Float.js
packages/vue-components/src/utils/NodeList.js
packages/vue-components/src/utils/utils.js

# --- packages/vue-components end ---
# --- packages/vue-components end ---
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ packages/core/src/variables/*.js
packages/core/test/unit/**/*.js
packages/core/index.js

# --- packages/core end ---
# --- packages/core end ---
55 changes: 55 additions & 0 deletions dangerfile.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified packages/core-web/dist/fonts/KaTeX_AMS-Regular.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_AMS-Regular.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_AMS-Regular.woff2
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Caligraphic-Bold.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Caligraphic-Bold.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Caligraphic-Bold.woff2
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Caligraphic-Regular.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Caligraphic-Regular.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Caligraphic-Regular.woff2
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Fraktur-Bold.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Fraktur-Bold.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Fraktur-Bold.woff2
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Fraktur-Regular.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Fraktur-Regular.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Fraktur-Regular.woff2
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Main-Bold.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Main-Bold.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Main-Bold.woff2
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Main-BoldItalic.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Main-BoldItalic.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Main-BoldItalic.woff2
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Main-Italic.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Main-Italic.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Main-Italic.woff2
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Main-Regular.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Main-Regular.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Main-Regular.woff2
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Math-BoldItalic.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Math-BoldItalic.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Math-BoldItalic.woff2
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Math-Italic.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Math-Italic.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Math-Italic.woff2
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_SansSerif-Bold.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_SansSerif-Bold.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_SansSerif-Bold.woff2
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_SansSerif-Italic.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_SansSerif-Italic.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_SansSerif-Italic.woff2
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_SansSerif-Regular.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_SansSerif-Regular.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_SansSerif-Regular.woff2
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Script-Regular.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Script-Regular.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Script-Regular.woff2
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Size1-Regular.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Size1-Regular.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Size1-Regular.woff2
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Size2-Regular.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Size2-Regular.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Size2-Regular.woff2
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Size3-Regular.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Size3-Regular.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Size3-Regular.woff2
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Size4-Regular.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Size4-Regular.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Size4-Regular.woff2
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Typewriter-Regular.ttf
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Typewriter-Regular.woff
Binary file not shown.
Binary file modified packages/core-web/dist/fonts/KaTeX_Typewriter-Regular.woff2
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/core-web/dist/js/vueCommonAppFactory.min.js

Large diffs are not rendered by default.

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 @@ -51,14 +51,15 @@ export class HighlightRuleComponent {
if (!lineNumber) return null;

const isUnbounded = groups.every(x => x === '');
const highlightSpaces = groups.pop() === '+';
if (isUnbounded) {
return new HighlightRuleComponent(lineNumber, true, []);
}

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])
? HighlightRuleComponent.computeCharBounds(bound, lines[lineNumber - 1], highlightSpaces)
: HighlightRuleComponent.computeWordBounds(bound, lines[lineNumber - 1]);

return new HighlightRuleComponent(lineNumber, true, [bound]);
Expand Down Expand Up @@ -101,32 +102,42 @@ export class HighlightRuleComponent {
* @param line The given line
* @returns {[number, number]} The actual bound computed
*/
static computeCharBounds(bound: [number, number], line: string): [number, number] {
static computeCharBounds(bound: [number, number], line: string,
highlightSpaces: boolean): [number, number] {
const [indents] = splitCodeAndIndentation(line);
let [start, end] = bound;

if (start === UNBOUNDED) {
start = indents.length;
} else {
if (highlightSpaces) {
start = 0;
} else {
start = indents.length;
}
} else if (!highlightSpaces) {
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;
}

if (end === UNBOUNDED) {
end = line.length;
} else {
} else if (!highlightSpaces) {
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;
}

return [start, end];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,55 @@ describe('parseRuleComponent', () => {
});
});

describe('computeCharBounds', () => {
describe('computeCharBounds, no whitespace highlighting', () => {
test('computes character bounds correctly', () => {
const bounds = HighlightRuleComponent.computeCharBounds([2, 5], ' some text');
const bounds = HighlightRuleComponent.computeCharBounds([2, 5], ' some text', false);
expect(bounds).toEqual([4, 7]);
});

test('handles unbounded start correctly', () => {
const bounds = HighlightRuleComponent.computeCharBounds([-1, 4], ' some text');
const bounds = HighlightRuleComponent.computeCharBounds([-1, 4], ' some text', false);
expect(bounds).toEqual([2, 6]);
});

test('handles unbounded end correctly', () => {
const bounds = HighlightRuleComponent.computeCharBounds([3, -1], ' some text');
const bounds = HighlightRuleComponent.computeCharBounds([3, -1], ' some text', false);
expect(bounds).toEqual([5, ' some text'.length]);
});

test('handles out-of-range bounds correctly', () => {
const bounds = HighlightRuleComponent.computeCharBounds([30, 40], ' some text');
const bounds = HighlightRuleComponent.computeCharBounds([30, 40], ' some text', false);
expect(bounds).toEqual([' some text'.length, ' some text'.length]);
});
});

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

test('handles unbounded start correctly', () => {
const bounds = HighlightRuleComponent.computeCharBounds([-1, 4], ' some text', true);
expect(bounds).toEqual([0, 4]);
});

test('handles unbounded end correctly', () => {
const bounds = HighlightRuleComponent.computeCharBounds([3, -1], ' some text', true);
expect(bounds).toEqual([3, ' some text'.length]);
});

test('handles out-of-range bounds correctly', () => {
const bounds = HighlightRuleComponent.computeCharBounds([30, 40], ' some text', true);
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]);
});
});

describe('computeWordBounds', () => {
test('computes word bounds correctly', () => {
const bounds = HighlightRuleComponent.computeWordBounds([1, 2], ' some text here');
Expand Down