-
Notifications
You must be signed in to change notification settings - Fork 52
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
Line counter should track all line ends, not just at end of segment #703
Comments
martypdx
changed the title
Line counter should track line ends not at end of segment
Line counter should track all line ends, not just at end of segment
Feb 19, 2024
Here's a possible algorithm: function testTrack(code, lineEnd) {
const state = { line: 1, column: 0 };
// these lines replace those in writeAndMap
if(code.length > 0) {
const segments = code.split(lineEnd);
state.line += (segments.length - 1) * lineEnd.length;
state.column += segments.at(-1).length;
}
return state;
}
test('track new line', ({ expect }) => {
expect(testTrack(` `, `\n`)).toEqual({ column: 4, line: 1, });
expect(testTrack(`\n `, `\n`)).toEqual({ column: 4, line: 2, });
expect(testTrack(` \n`, `\n`)).toEqual({ column: 0, line: 2, });
expect(testTrack(` \n `, `\n`)).toEqual({ column: 4, line: 2, });
expect(testTrack(`\n\n`, `\n`)).toEqual({ column: 0, line: 3, });
expect(testTrack(`\n\n `, `\n`)).toEqual({ column: 4, line: 3, });
expect(testTrack(` \n\n `, `\n`)).toEqual({ column: 4, line: 3, });
expect(testTrack(` \n\n`, `\n`)).toEqual({ column: 0, line: 3, });
expect(testTrack(`\n \n`, `\n`)).toEqual({ column: 0, line: 3, });
});
test('track \r\n', ({ expect }) => {
expect(testTrack(` `, `\r\n`)).toEqual({ column: 4, line: 1, });
expect(testTrack(`\r\n `, `\r\n`)).toEqual({ column: 4, line: 3, });
expect(testTrack(` \r\n`, `\r\n`)).toEqual({ column: 0, line: 3, });
expect(testTrack(` \r\n `, `\r\n`)).toEqual({ column: 4, line: 3, });
expect(testTrack(`\r\n\r\n`, `\r\n`)).toEqual({ column: 0, line: 5, });
expect(testTrack(`\r\n\r\n `, `\r\n`)).toEqual({ column: 4, line: 5, });
expect(testTrack(` \r\n\r\n `, `\r\n`)).toEqual({ column: 4, line: 5, });
expect(testTrack(` \r\n\r\n`, `\r\n`)).toEqual({ column: 0, line: 5, });
expect(testTrack(`\r\n \r\n`, `\r\n`)).toEqual({ column: 0, line: 5, });
}); |
Ugh, but it doesn't count in a string literal, right? Like |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Motivation
Want to use
lineEnd
anywhere code chunk with source maps:Expected behavior
state.line
increments for each occurance oflineEnd
incode
Actual behavior
state.line
only increments once when newLine is at end of code chunkThe text was updated successfully, but these errors were encountered: