Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: getBodyText() doesn't work as would be expected #1560

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions packages/common/src/tests/utils/stringUtilsTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,14 @@ describe("StringUtils", () => {
str += "\n other";
doTest(str, "this is a `\n test`\nother", { isInStringAtPos: index => index >= pos && index < end });
});

it("should handle lines that only have indentation", () => {
doTest(" test\n \n test", "test\n\ntest", { indentSizeInSpaces: 2 });
});

it("should ignore empty lines", () => {
doTest(" test\n\n test", "test\n\ntest", { indentSizeInSpaces: 2 });
});
});

describe(nameof(StringUtils, "indent"), () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/common/src/utils/StringUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,15 @@ export class StringUtils {

// indentation for spaces rounds up to the nearest tab size multiple
const indentWidth = Math.ceil(spacesCount / indentSizeInSpaces) * indentSizeInSpaces + tabsCount * indentSizeInSpaces;
if (minIndentWidth == null || indentWidth < minIndentWidth)
if (str.charCodeAt(i) !== CharCodes.NEWLINE && (minIndentWidth == null || indentWidth < minIndentWidth))
minIndentWidth = indentWidth;

endPositions.push(i);
isAtStartOfLine = false;

// this check is needed for lines that are empty or consist purely of spaces/tabs
if (str.charCodeAt(i) === CharCodes.NEWLINE)
i--;
}
}

Expand Down