Skip to content

Commit

Permalink
Fix bug in Space.format()
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 25, 2024
1 parent 8d15b5c commit f31efab
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
24 changes: 12 additions & 12 deletions openrewrite/src/java/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ export class Space {
return new Space(comments, whitespace);
}

static format(formatting: string, beginIndex: number, toIndex: number): Space {
if (beginIndex == toIndex) {
static format(formatting: string, beginIndex: number = 0, toIndex: number = formatting.length): Space {
if (beginIndex === toIndex) {
return Space.EMPTY;
} else if (toIndex == beginIndex + 1 && formatting[beginIndex] === ' ') {
} else if (toIndex === beginIndex + 1 && formatting[beginIndex] === ' ') {
return Space.SINGLE_SPACE;
}

Expand All @@ -131,7 +131,7 @@ export class Space {
while (i < toIndex && (formatting[i] === ' ' || formatting[i] === '\t' || formatting[i] === '\n' || formatting[i] === '\r')) {
i++;
}
let whitespaceEnd = i; // Capture end of leading whitespace
let whitespaceEnd = i; // Capture end of leading whitespace

// Step 2: Parse comments
while (i < toIndex) {
Expand All @@ -142,12 +142,12 @@ export class Space {
commentStart = i + 2; // Skip the "//"
i += 2;
while (i < toIndex && formatting[i] !== '\n' && formatting[i] !== '\r') {
i++; // Continue until end of line or end of input
i++; // Continue until end of line or end of input
}
commentEnd = i;
suffixStart = i; // Capture newline as suffix
while (i < toIndex && (formatting[i] === '\n' || formatting[i] === '\r')) {
i++;
suffixStart = i; // Start of suffix
while (i < toIndex && (formatting[i] === ' ' || formatting[i] === '\t' || formatting[i] === '\n' || formatting[i] === '\r')) {
i++; // Capture any trailing whitespace after comment
}
const commentText = formatting.slice(commentStart, commentEnd);
const suffix = formatting.slice(suffixStart, i);
Expand All @@ -158,20 +158,20 @@ export class Space {
commentStart = i + 2; // Skip the "/*"
i += 2;
while (i + 1 < toIndex && !(formatting[i] === '*' && formatting[i + 1] === '/')) {
i++; // Continue until "*/" or end of input
i++; // Continue until "*/" or end of input
}
commentEnd = i; // Position before */
i += 2; // Skip the closing "*/"
i += 2; // Skip the closing "*/"

suffixStart = i;
while (i < toIndex && (formatting[i] === ' ' || formatting[i] === '\t' || formatting[i] === '\n' || formatting[i] === '\r')) {
i++; // Capture any trailing whitespace after comment
i++; // Capture any trailing whitespace after comment
}
const commentText = formatting.slice(commentStart, commentEnd);
const suffix = formatting.slice(suffixStart, i);
comments.push(new TextComment(true, commentText, suffix, Markers.EMPTY));
} else {
i++; // Skip non-comment characters
i++; // Skip non-comment characters
}
}

Expand Down
6 changes: 6 additions & 0 deletions openrewrite/test/.editorconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space

[*.ts]
indent_size = 4
ij_continuation_indent_size = 2
34 changes: 23 additions & 11 deletions openrewrite/test/java/tree.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import {Space} from "../../src/java/tree";
import {Space, TextComment} from "../../src/java/tree";

describe('Space parsing', () => {
test('parse space', () => {
let str = ' /* c1*/ /*c2 */ ';
console.log(Space.format(str, 0, str.length));
test('multi-line comments', () => {
let space = Space.format(' /* c1*/ /*c2 */ ');
expect(space.whitespace).toBe(' ');
expect(space.comments).toHaveLength(2);
expect((space.comments[0] as TextComment).text).toBe(' c1');
expect(space.comments[0].suffix).toBe(' ');
expect((space.comments[1] as TextComment).text).toBe('c2 ');
expect(space.comments[1].suffix).toBe(' ');
});

test('parse space 2', () => {
let str = ' // c1 \n // c2\n//c3';
console.log(Space.format(str, 0, str.length));
test('single-line comments', () => {
let space = Space.format(' // c1 \n // c2\n//c3');
expect(space.whitespace).toBe(' ');
expect(space.comments).toHaveLength(3);
expect((space.comments[0] as TextComment).text).toBe(' c1 ');
expect(space.comments[0].suffix).toBe('\n ');
expect((space.comments[1] as TextComment).text).toBe(' c2');
expect(space.comments[1].suffix).toBe('\n');
expect((space.comments[2] as TextComment).text).toBe('c3');
expect(space.comments[2].suffix).toBe('');
});

test('parse empty space', () => {
let str = '';
console.log(Space.format(str, 0, str.length));
let space = Space.format('');
expect(space).toBe(Space.EMPTY);
});

test('parse single space', () => {
let str = ' ';
console.log(Space.format(str, 0, str.length));
let space = Space.format(' ');
expect(space).toBe(Space.SINGLE_SPACE);
});
});

0 comments on commit f31efab

Please sign in to comment.