Skip to content

Add an extension setting to control inlay hints #1526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@
{
"title": "Swift",
"properties": {
"swift.inlayHints.enabled": {
"type": "boolean",
"default": false,
"markdownDescription": "Display inlay hints. Inlay hints are variable annotations indicating their inferred type."
},
"swift.path": {
"type": "string",
"default": "",
Expand Down Expand Up @@ -683,9 +688,9 @@
},
"sourcekit-lsp.inlayHints.enabled": {
"type": "boolean",
"default": true,
"default": false,
"markdownDescription": "Display Inlay Hints. Inlay Hints are variable annotations indicating their inferred type. They are only available if you are using Swift 5.6 or later.",
"markdownDeprecationMessage": "**Deprecated**: Please use `#editor.inlayHints.enabled#` instead."
"markdownDeprecationMessage": "**Deprecated**: Please use `#swift.inlayHints.enabled#` instead."
},
"sourcekit-lsp.support-c-cpp": {
"type": "string",
Expand Down
2 changes: 2 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { runTask } from "./commands/runTask";
import { TestKind } from "./TestExplorer/TestKind";
import { pickProcess } from "./commands/pickProcess";
import { openDocumentation } from "./commands/openDocumentation";
import { toggleInlayHints } from "./commands/toggleInlayHints";

/**
* References:
Expand Down Expand Up @@ -217,6 +218,7 @@ export function register(ctx: WorkspaceContext): vscode.Disposable[] {
vscode.commands.executeCommand("vscode.open", vscode.Uri.file(packagePath));
}),
vscode.commands.registerCommand("swift.openDocumentation", () => openDocumentation()),
vscode.commands.registerCommand("swift.toggleInlayHints", () => toggleInlayHints()),
];
}

Expand Down
35 changes: 35 additions & 0 deletions src/commands/toggleInlayHints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2025 the VS Code Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import * as vscode from "vscode";
import configuration from "../configuration";

/**
* Configures editor.inlayHints.enabled settings based on swift.inlayHints.enabled settings
*/
export async function toggleInlayHints() {
let settingValue = undefined;

if (!configuration.inlayHintsEnabled) {
settingValue = "off";
}

const config = vscode.workspace.getConfiguration("", { languageId: "swift" });
await config.update(
Copy link
Contributor

@plemarquand plemarquand Apr 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to automatically add a .vscode/settings.json file for every package opened in VS Code and add the following to it:

    "[swift]": {
        "editor.inlayHints.enabled": "off"
    }

When I update .vscode/settings.json to set this value to "on", save, and reload the window it gets re-written to "off".

Automatically resetting the setting aside, I don't think we should be generating a settings.json for the user at all as these are meant to be for the user to control, not an extension.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, yes agreed that touching settings.json automatically is probably not welcome (although we do touch the settings.json for other things like swift environment variables, for example, but the user would have to set them manually I think). Any other suggestions on how the inlay hints could be disabled by default so that new users who don't know how to turn this setting off are not annoyed?

Could send a setting to sourcekit-lsp separately to turn off inlay hints possibly, and then if the user says:

  "[swift]": {
        "editor.inlayHints.enabled": "on"
    }

or sets swift.inlayHints.enabled to true then it would turn on inlay hints?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do other extensions handle this? Do popular language extensions they swim against the VS Code defaults?

My gut feeling is we should tell sourcekit-lsp to disable them unless there is a setting set explicitly that turns them on, but because the default for editor.inlayHints.enabled is "on" I dont know if we can tell if this is explicitly set by the user or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good ideas thanks, it seems like most do it with passing in flags to lsp (Go does it with gopls and rust with rust-analyser), although it looks like C/C++ use vscode api's to override inlayHint behaviour directly.

You are correct that it's not possible to tell whether editor.inlayHints.enabled is explicitly set to on. So it's looking like I'll just have to add functionality like the sourcekit-lsp.inlayHints.enabled setting had at one point.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the inspect() method to find out if the user has explicitly configured a setting. Have a look at https://github.com/swiftlang/vscode-swift/blob/main/src/configuration.ts#L245 for an example in the Swift extension's configuration. It will even tell you if it has been configured for a particular language.

"editor.inlayHints.enabled",
settingValue,
vscode.ConfigurationTarget.Workspace,
true
);
}
7 changes: 7 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as os from "os";
import * as path from "path";
import { showReloadExtensionNotification } from "./ui/ReloadExtension";
import { WorkspaceContext } from "./WorkspaceContext";
import { toggleInlayHints } from "./commands/toggleInlayHints";

export type DebugAdapters = "auto" | "lldb-dap" | "CodeLLDB";
export type SetupCodeLLDBOptions =
Expand Down Expand Up @@ -410,6 +411,10 @@ const configuration = {
get diagnostics(): boolean {
return vscode.workspace.getConfiguration("swift").get<boolean>("diagnostics", false);
},
/** enable inlay hints from SourceKit LSP by setting editor.inlayHints.enabled */
get inlayHintsEnabled(): boolean {
return vscode.workspace.getConfiguration("swift.inlayHints").get<boolean>("enabled", false);
},
/**
* Test coverage settings
*/
Expand Down Expand Up @@ -535,6 +540,8 @@ export function handleConfigurationChangeEvent(
showReloadExtensionNotification(
"Changing environment variables requires the project be reloaded."
);
} else if (event.affectsConfiguration("swift.inlayHints.enabled")) {
toggleInlayHints();
}
};
}
Expand Down
4 changes: 4 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { checkAndWarnAboutWindowsSymlinks } from "./ui/win32";
import { SwiftEnvironmentVariablesManager, SwiftTerminalProfileProvider } from "./terminal";
import { resolveFolderDependencies } from "./commands/dependencies/resolve";
import { SelectedXcodeWatcher } from "./toolchain/SelectedXcodeWatcher";
import { toggleInlayHints } from "./commands/toggleInlayHints";
import configuration, { handleConfigurationChangeEvent } from "./configuration";
import contextKeys from "./contextKeys";

Expand Down Expand Up @@ -140,6 +141,9 @@ export async function activate(context: vscode.ExtensionContext): Promise<Api> {
// Mark the extension as activated.
contextKeys.isActivated = true;

// toggle inlay hints based on swift.inlayHints.enabled settings
toggleInlayHints();

return {
workspaceContext,
outputChannel,
Expand Down
Loading