Skip to content

Commit

Permalink
fix: getBodyText() doesn't work as would be expected (#1560)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmyrick02 authored Oct 5, 2024
1 parent 0d38985 commit 81dc87d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
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

0 comments on commit 81dc87d

Please sign in to comment.