Skip to content
Draft
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
29 changes: 28 additions & 1 deletion src/ModuleResolverNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,29 @@ export class ModuleResolver implements ModuleResolverInterface {
return;
}

/**
* Determines if a document is an untitled (unsaved) document.
* Untitled documents have a scheme other than "file" (e.g., "untitled").
*/
private isUntitledDocument(doc: TextDocument): boolean {
return doc.uri.scheme !== "file";
}

/**
* Gets the appropriate base path for config file searches.
* For untitled documents, uses the workspace folder path.
* For regular files, uses the file path itself.
*/
private getConfigSearchPath(doc: TextDocument): string {
if (this.isUntitledDocument(doc)) {
const workspaceFolder = workspace.getWorkspaceFolder(doc.uri);
if (workspaceFolder) {
return workspaceFolder.uri.fsPath;
}
}
return doc.fileName;
}

public async getResolvedConfig(
doc: TextDocument,
vscodeConfig: PrettierVSCodeConfig,
Expand All @@ -227,7 +250,11 @@ export class ModuleResolver implements ModuleResolverInterface {
(await this.getPrettierInstance(fileName)) ??
(await getBundledPrettier());

return this.resolveConfig(prettier, fileName, vscodeConfig);
// For untitled documents (unsaved files), use workspace folder as base for config search
// This allows untitled documents to use workspace prettier configs when requireConfig is enabled
const configSearchPath = this.getConfigSearchPath(doc);

return this.resolveConfig(prettier, configSearchPath, vscodeConfig);
}

public async clearModuleCache(filePath: string): Promise<void> {
Expand Down
70 changes: 70 additions & 0 deletions src/test/suite/untitled.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as assert from "assert";
import * as vscode from "vscode";
import { ensureExtensionActivated } from "./testUtils.js";

/**
* Tests for untitled documents (unsaved files)
*/
describe("Test untitled documents", () => {
before(async () => {
await ensureExtensionActivated();
});

it("formats untitled JavaScript document", async () => {
// Create an untitled document with ugly JavaScript
const uglyCode = 'const x = 1 ; console.log( x ) ;';
const doc = await vscode.workspace.openTextDocument({
language: "javascript",
content: uglyCode,
});

const editor = await vscode.window.showTextDocument(doc);

// Format the document
await vscode.commands.executeCommand("editor.action.formatDocument");

// The document should be formatted
const formattedText = editor.document.getText();

assert.notEqual(
formattedText,
uglyCode,
"Untitled document should have been formatted",
);

// Should not have excessive spacing
assert.ok(
!formattedText.includes(" "),
"Formatted code should not have excessive spacing",
);
});

it("formats untitled TypeScript document", async () => {
// Create an untitled document with ugly TypeScript
const uglyCode = 'function test ( a : number ) : number { return a * 2 ; }';
const doc = await vscode.workspace.openTextDocument({
language: "typescript",
content: uglyCode,
});

const editor = await vscode.window.showTextDocument(doc);

// Format the document
await vscode.commands.executeCommand("editor.action.formatDocument");

// The document should be formatted
const formattedText = editor.document.getText();

assert.notEqual(
formattedText,
uglyCode,
"Untitled TypeScript document should have been formatted",
);

// Should not have excessive spacing
assert.ok(
!formattedText.includes(" "),
"Formatted code should not have excessive spacing",
);
});
});
4 changes: 4 additions & 0 deletions test-fixtures/require-config/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"semi": false,
"singleQuote": true
}
4 changes: 4 additions & 0 deletions test-fixtures/require-config/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"prettier.requireConfig": true,
"files.eol": "\n"
}
3 changes: 3 additions & 0 deletions test-fixtures/require-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "require-config-test"
}
3 changes: 3 additions & 0 deletions test-fixtures/test.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
},
{
"path": "monorepo-subfolder"
},
{
"path": "require-config"
}
],
"settings": {
Expand Down