Skip to content

Commit df2236e

Browse files
authored
fix(highlighting): fixing an issue where overlay was not applied correctly (#7)
- Reducing reach of while statement to capture ranges correctly - Applying in cases where overall length was greater than applied range
1 parent 84ec801 commit df2236e

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

__tests__/codeMirror.test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { mount, shallow } from 'enzyme';
22
import path from 'path';
33
import glob from 'glob';
44
import { promises as fs } from 'fs';
5+
import Variable from '@readme/variable';
56
import syntaxHighlighter, { uppercase, canonical } from '../src';
67

78
const fixtures = glob.sync(path.join(__dirname, '/__fixtures__/*'));
@@ -157,7 +158,15 @@ describe('highlight mode', () => {
157158
expect(node.find('p').first().hasClass('cm-lineNumber')).toBe(true);
158159
});
159160

161+
it('should convert variable regex matches to a component instance', () => {
162+
expect(node.find(Variable)).toHaveLength(1);
163+
});
164+
160165
it('should highlight based on range input', () => {
161-
expect(node.find('.cm-linerow.cm-highlight')).toHaveLength(4);
166+
expect(node.find('.cm-linerow.cm-highlight')).toHaveLength(2);
167+
});
168+
169+
it('should add an overlay to non-highlighted in lines when ranges are applied', () => {
170+
expect(node.find('.cm-linerow.cm-overlay')).toHaveLength(6);
162171
});
163172
});

public/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ ReactDOM.render(
2222
{ ch: 0, line: 1 },
2323
],
2424
[
25+
{ ch: 0, line: 2 },
2526
{ ch: 0, line: 3 },
26-
{ ch: 0, line: 5 },
2727
],
2828
],
2929
}

src/codeMirror/index.jsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,19 @@ StructuredOutput.propTypes = {
4747
* @arg {[][]{line: Int}} ranges
4848
* @return {[String]} Consumable classNames
4949
*/
50-
const highlightedLines = ranges => {
50+
const highlightedLines = (ranges, totalLength) => {
5151
const highlights = [];
5252

5353
ranges.forEach(([anchor, head]) => {
5454
const end = head.line;
5555
let position = anchor.line;
56-
57-
while (position <= end) {
56+
while (position < end) {
5857
highlights[position] = 'cm-highlight';
5958
position += 1;
6059
}
6160
});
6261

63-
for (let i = 0; i < highlights.length; i += 1) {
62+
for (let i = 0; i < totalLength; i += 1) {
6463
if (!highlights[i]) highlights[i] = 'cm-overlay';
6564
}
6665

@@ -88,7 +87,7 @@ const StyledSyntaxHighlighter = ({ output, ranges }) => {
8887
}
8988
});
9089

91-
const highlights = ranges && ranges.length ? highlightedLines(ranges) : [];
90+
const highlights = ranges && ranges.length ? highlightedLines(ranges, gutteredOutput.length) : [];
9291
return (
9392
<div className="CodeMirror cm-s-material-palenight">
9493
<StructuredOutput gutteredInput={gutteredOutput} highlights={highlights} />

0 commit comments

Comments
 (0)