Skip to content

Commit a8a66d2

Browse files
Copilotntotten
andcommitted
Fix untitled document formatting with requireConfig enabled
- Modified getResolvedConfig to use workspace folder path for config search when document is untitled - Added test fixture for requireConfig scenario - Added integration tests for untitled document formatting Co-authored-by: ntotten <[email protected]>
1 parent db80123 commit a8a66d2

File tree

6 files changed

+95
-1
lines changed

6 files changed

+95
-1
lines changed

src/ModuleResolverNode.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,17 @@ export class ModuleResolver implements ModuleResolverInterface {
227227
(await this.getPrettierInstance(fileName)) ??
228228
(await getBundledPrettier());
229229

230-
return this.resolveConfig(prettier, fileName, vscodeConfig);
230+
// For untitled documents (unsaved files), use workspace folder as base for config search
231+
// This allows untitled documents to use workspace prettier configs when requireConfig is enabled
232+
let configSearchPath = fileName;
233+
if (doc.uri.scheme !== "file") {
234+
const workspaceFolder = workspace.getWorkspaceFolder(doc.uri);
235+
if (workspaceFolder) {
236+
configSearchPath = workspaceFolder.uri.fsPath;
237+
}
238+
}
239+
240+
return this.resolveConfig(prettier, configSearchPath, vscodeConfig);
231241
}
232242

233243
public async clearModuleCache(filePath: string): Promise<void> {

src/test/suite/untitled.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import * as assert from "assert";
2+
import * as vscode from "vscode";
3+
import { ensureExtensionActivated } from "./testUtils.js";
4+
5+
/**
6+
* Tests for untitled documents (unsaved files)
7+
*/
8+
describe("Test untitled documents", () => {
9+
before(async () => {
10+
await ensureExtensionActivated();
11+
});
12+
13+
it("formats untitled JavaScript document", async () => {
14+
// Create an untitled document with ugly JavaScript
15+
const uglyCode = 'const x = 1 ; console.log( x ) ;';
16+
const doc = await vscode.workspace.openTextDocument({
17+
language: "javascript",
18+
content: uglyCode,
19+
});
20+
21+
const editor = await vscode.window.showTextDocument(doc);
22+
23+
// Format the document
24+
await vscode.commands.executeCommand("editor.action.formatDocument");
25+
26+
// The document should be formatted
27+
const formattedText = editor.document.getText();
28+
29+
assert.notEqual(
30+
formattedText,
31+
uglyCode,
32+
"Untitled document should have been formatted",
33+
);
34+
35+
// Should not have excessive spacing
36+
assert.ok(
37+
!formattedText.includes(" "),
38+
"Formatted code should not have excessive spacing",
39+
);
40+
});
41+
42+
it("formats untitled TypeScript document", async () => {
43+
// Create an untitled document with ugly TypeScript
44+
const uglyCode = 'function test ( a : number ) : number { return a * 2 ; }';
45+
const doc = await vscode.workspace.openTextDocument({
46+
language: "typescript",
47+
content: uglyCode,
48+
});
49+
50+
const editor = await vscode.window.showTextDocument(doc);
51+
52+
// Format the document
53+
await vscode.commands.executeCommand("editor.action.formatDocument");
54+
55+
// The document should be formatted
56+
const formattedText = editor.document.getText();
57+
58+
assert.notEqual(
59+
formattedText,
60+
uglyCode,
61+
"Untitled TypeScript document should have been formatted",
62+
);
63+
64+
// Should not have excessive spacing
65+
assert.ok(
66+
!formattedText.includes(" "),
67+
"Formatted code should not have excessive spacing",
68+
);
69+
});
70+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"semi": false,
3+
"singleQuote": true
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"prettier.requireConfig": true,
3+
"files.eol": "\n"
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "require-config-test"
3+
}

test-fixtures/test.code-workspace

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
},
6666
{
6767
"path": "monorepo-subfolder"
68+
},
69+
{
70+
"path": "require-config"
6871
}
6972
],
7073
"settings": {

0 commit comments

Comments
 (0)