Skip to content

Commit 8aa4659

Browse files
committed
Reregister language configuration when the dart.automaticCommentSlashes setting changes
Fixes #5890
1 parent 22e6f39 commit 8aa4659

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

‎src/extension/extension.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ export async function activate(context: vs.ExtensionContext, isRestart = false)
460460
context.subscriptions.push(vs.languages.registerCompletionItemProvider(DART_MODE, new SnippetCompletionItemProvider(dartCapabilities, "snippets/dart.json", () => true)));
461461
context.subscriptions.push(vs.languages.registerCompletionItemProvider(DART_MODE, new SnippetCompletionItemProvider(dartCapabilities, "snippets/flutter.json", (uri) => util.isInsideFlutterProject(uri))));
462462

463-
context.subscriptions.push(vs.languages.setLanguageConfiguration(DART_LANGUAGE, new DartLanguageConfiguration()));
463+
context.subscriptions.push(DartLanguageConfiguration.register(DART_LANGUAGE));
464464

465465
context.subscriptions.push(new FileChangeWarnings());
466466
context.subscriptions.push(new DiagnosticReport(logger, workspaceContext, rebuildLogHeaders));

‎src/extension/providers/dart_language_configuration.ts‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
1-
import { IndentAction, LanguageConfiguration, OnEnterRule } from "vscode";
1+
import { Disposable, IndentAction, LanguageConfiguration, languages, OnEnterRule, workspace } from "vscode";
22
import { config } from "../config";
33

44
export class DartLanguageConfiguration implements LanguageConfiguration {
5+
public static register(language: string): Disposable {
6+
// Track the current active subscription for the language config.
7+
let subscription: Disposable | undefined;
8+
9+
// Helper to register the current language config, unregistering
10+
// any existing config first.
11+
const register = () => {
12+
subscription?.dispose();
13+
subscription = languages.setLanguageConfiguration(language, new DartLanguageConfiguration());
14+
};
15+
16+
// Watch the config and re-register any time the automaticCommentSlashes setting changes.
17+
const configWatcher = workspace.onDidChangeConfiguration((e) => {
18+
if (e.affectsConfiguration("dart.automaticCommentSlashes"))
19+
register();
20+
});
21+
22+
// Perform initial registration.
23+
register();
24+
25+
// Return a wrapper that disposes both the config watcher and the current active
26+
// config.
27+
return {
28+
dispose: () => {
29+
configWatcher.dispose();
30+
subscription?.dispose();
31+
},
32+
};
33+
}
34+
535
private readonly doubleSlashRules: OnEnterRule[] = [
636
{
737
// Double-slash with space.

0 commit comments

Comments
 (0)