Skip to content

Commit 932731c

Browse files
CopilotJoseVSeb
andcommitted
Fix: Register DocumentFormattingEditProvider for VS Code 1.93.1 compatibility
Co-authored-by: JoseVSeb <20752081+JoseVSeb@users.noreply.github.com>
1 parent 53c6bc2 commit 932731c

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

src/GoogleJavaFormatEditService.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
DocumentFormattingEditProvider,
23
DocumentRangeFormattingEditProvider,
34
DocumentSelector,
45
ExtensionContext,
@@ -10,7 +11,8 @@ export default class GoogleJavaFormatEditService {
1011
private readonly selector: DocumentSelector = { language: "java" };
1112

1213
constructor(
13-
private editProvider: DocumentRangeFormattingEditProvider,
14+
private editProvider: DocumentRangeFormattingEditProvider &
15+
DocumentFormattingEditProvider,
1416
private context: ExtensionContext,
1517
private log: LogOutputChannel,
1618
) {}
@@ -22,6 +24,12 @@ export default class GoogleJavaFormatEditService {
2224
this.editProvider,
2325
),
2426
);
27+
this.context.subscriptions.push(
28+
languages.registerDocumentFormattingEditProvider(
29+
this.selector,
30+
this.editProvider,
31+
),
32+
);
2533
this.log.debug("Enabled Google Java Formatter globally", this.selector);
2634
};
2735
}

src/test/suite/editService.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import * as assert from "assert";
2+
import * as vscode from "vscode";
3+
import GoogleJavaFormatEditProvider from "../../GoogleJavaFormatEditProvider";
4+
import GoogleJavaFormatEditService from "../../GoogleJavaFormatEditService";
5+
import { IGoogleJavaFormatter } from "../../IGoogleJavaFormatter";
6+
7+
// Mock formatter for testing
8+
class MockFormatter implements IGoogleJavaFormatter {
9+
async format(
10+
text: string,
11+
lines: [number, number],
12+
signal: AbortSignal,
13+
): Promise<string> {
14+
return text; // Return text as-is for testing
15+
}
16+
}
17+
18+
suite("GoogleJavaFormatEditService Test Suite", () => {
19+
let context: vscode.ExtensionContext;
20+
let log: vscode.LogOutputChannel;
21+
let editProvider: GoogleJavaFormatEditProvider;
22+
let editService: GoogleJavaFormatEditService;
23+
24+
suiteSetup(() => {
25+
// Create mock context and log channel
26+
context = {
27+
subscriptions: [],
28+
} as any;
29+
30+
log = {
31+
debug: () => {},
32+
info: () => {},
33+
error: () => {},
34+
} as any;
35+
36+
const mockFormatter = new MockFormatter();
37+
editProvider = new GoogleJavaFormatEditProvider(mockFormatter, log);
38+
editService = new GoogleJavaFormatEditService(
39+
editProvider,
40+
context,
41+
log,
42+
);
43+
});
44+
45+
test("Should register both DocumentFormattingEditProvider and DocumentRangeFormattingEditProvider", () => {
46+
// Clear any existing subscriptions
47+
context.subscriptions.length = 0;
48+
49+
// Subscribe the edit service
50+
editService.subscribe();
51+
52+
// Should have registered two providers
53+
assert.strictEqual(
54+
context.subscriptions.length,
55+
2,
56+
"Should register exactly 2 formatting providers",
57+
);
58+
});
59+
60+
test("EditProvider should implement both required interfaces", () => {
61+
// Verify that the edit provider implements both required methods
62+
assert.ok(
63+
typeof editProvider.provideDocumentFormattingEdits === "function",
64+
"Should implement provideDocumentFormattingEdits method",
65+
);
66+
assert.ok(
67+
typeof editProvider.provideDocumentRangeFormattingEdits ===
68+
"function",
69+
"Should implement provideDocumentRangeFormattingEdits method",
70+
);
71+
});
72+
});

0 commit comments

Comments
 (0)