fix(read_file): empty files should not report hasMoreBefore: true in indentation mode

When totalLinesInFile is 0, there are no lines before the anchor point,
so hasMoreBefore must be false regardless of the offset/anchorLine value.
This commit is contained in:
Hannes Rudolph 2025-12-21 19:22:57 -07:00
parent c903656abc
commit e072ffc91f
2 changed files with 27 additions and 2 deletions

View file

@ -214,7 +214,7 @@ describe("read-file-content", () => {
const codeWithComment = `# This is a comment header
# describing the function
def my_function():
return 42`
return 42`
await withTempFile("indent-header-test.py", codeWithComment, async (filepath) => {
const result = await readIndentationBlock(filepath, 3, 100, {
anchorLine: 3,
@ -224,6 +224,31 @@ def my_function():
expect(result.content).toContain("# This is a comment header")
})
})
it("should report hasMoreBefore as false for empty files", async () => {
await withTempFile("indent-empty-test.txt", "", async (filepath) => {
const result = await readIndentationBlock(filepath, 1, 100)
expect(result.content).toBe("")
expect(result.lineCount).toBe(0)
expect(result.totalLines).toBe(0)
expect(result.metadata.totalLinesInFile).toBe(0)
expect(result.metadata.hasMoreBefore).toBe(false)
expect(result.metadata.hasMoreAfter).toBe(false)
})
})
it("should report hasMoreBefore as false for empty files even with offset > 1", async () => {
await withTempFile("indent-empty-offset-test.txt", "", async (filepath) => {
const result = await readIndentationBlock(filepath, 5, 100)
expect(result.content).toBe("")
expect(result.lineCount).toBe(0)
expect(result.totalLines).toBe(0)
expect(result.metadata.totalLinesInFile).toBe(0)
// Even though offset > 1, there are no lines in an empty file
expect(result.metadata.hasMoreBefore).toBe(false)
expect(result.metadata.hasMoreAfter).toBe(false)
})
})
})
describe("readFileContent", () => {

View file

@ -382,7 +382,7 @@ export async function readIndentationBlock(
linesReturned: 0,
startLine: anchorLine,
endLine: anchorLine,
hasMoreBefore: anchorLine > 1,
hasMoreBefore: false, // Empty file has no lines before
hasMoreAfter: false,
linesBeforeStart: 0,
linesAfterEnd: 0,