Skip to content

Commit 1f5c02e

Browse files
committed
feat: support unsaved changes
1 parent 605fc87 commit 1f5c02e

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

packages/core/src/shared/utilities/workspaceUtils.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ export async function collectFiles(
391391
}
392392

393393
let totalSizeBytes = 0
394-
const storage = []
394+
const storage: (Omit<CollectFilesResultItem, 'fileContent'> | CollectFilesResultItem)[] = []
395395
const excludePatternFilter = excludePatternsAsString(excludePatterns)
396396
for (const rootPath of sourcePaths) {
397397
const allFiles = await vscode.workspace.findFiles(
@@ -432,19 +432,21 @@ export async function collectFiles(
432432
isText: !ZipConstants.knownBinaryFileExts.includes(path.extname(file.fsPath)),
433433
}
434434
if (includeContent) {
435-
const content = await readFile(file)
435+
const hasUnsavedChanges = isFileOpenAndDirty(file)
436+
const content =
437+
result.isText && hasUnsavedChanges ? await getCurrentTextContent(file) : await readFile(file)
436438
if (content === undefined) {
437439
continue
438440
}
439-
totalSizeBytes += fileStat.size
441+
440442
storage.push({
441443
...result,
442444
fileContent: content,
443445
})
444446
} else {
445-
totalSizeBytes += fileStat.size
446447
storage.push(result)
447448
}
449+
totalSizeBytes += fileStat.size
448450
}
449451
}
450452
return storage
@@ -463,6 +465,16 @@ export async function collectFiles(
463465
}
464466
return prefix === '' ? path : `${prefix}/${path}`
465467
}
468+
469+
async function getCurrentTextContent(uri: vscode.Uri) {
470+
const document = await vscode.workspace.openTextDocument(uri)
471+
const content = document.getText()
472+
return content
473+
}
474+
475+
function isFileOpenAndDirty(uri: vscode.Uri) {
476+
return vscode.workspace.textDocuments.some((document) => document.uri.fsPath === uri.fsPath && document.isDirty)
477+
}
466478
}
467479

468480
const readFile = async (file: vscode.Uri) => {

packages/core/src/testInteg/shared/utilities/workspaceUtils.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,22 @@ describe('workspaceUtils', () => {
363363
(e) => e instanceof ToolkitError && e.code === 'ContentLengthError'
364364
)
365365
})
366+
367+
it('pulls updated content from unsaved open files', async function () {
368+
const workspace = await createTestWorkspaceFolder()
369+
const filepath = path.join(workspace.uri.fsPath, 'file1')
370+
await toFile('this is some text', filepath)
371+
372+
const document = await vscode.workspace.openTextDocument(filepath)
373+
await vscode.window.showTextDocument(document)
374+
void vscode.window.activeTextEditor?.edit((editBuilder) => {
375+
editBuilder.insert(new vscode.Position(0, 0), '// a comment\n')
376+
})
377+
378+
const result = await collectFiles([workspace.uri.fsPath], [workspace])
379+
assert.strictEqual(result.length, 1)
380+
assert.strictEqual(result[0].fileContent, '// a comment\nthis is some text')
381+
})
366382
})
367383

368384
describe('getWorkspaceFoldersByPrefixes', function () {

0 commit comments

Comments
 (0)