Skip to content
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

Add info tooltip to inlay hints #972

Merged
merged 9 commits into from
Jul 23, 2022
36 changes: 35 additions & 1 deletion src/FsAutoComplete/FsAutoComplete.Lsp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2571,6 +2571,40 @@ type FSharpLspServer(state: State, lspClient: FSharpLspClient) =
showParameterHints = config.InlayHints.parameterNames
)

let getTooltip (h: InlayHints.Hint) =
let hideTypeAnnotations = "fsharp.inlayHints.hideTypeAnnotations"
let hideParameterNames = "fsharp.inlayHints.hideParameterNames"
let hideAll = "fsharp.inlayHints.hideAll"
let setToToggle = "fsharp.inlayHints.setToToggle"
let disableLongTooltip = "fsharp.inlayHints.disableLongTooltip"

if config.InlayHints.disableLongTooltip then
h.Tooltip |> Option.map (InlayHintTooltip.String)
else
let lines = ResizeArray()

let hideCommand =
match h.Kind with
| InlayHints.HintKind.Type -> hideTypeAnnotations
| InlayHints.HintKind.Parameter -> hideParameterNames

lines.Add $"To hide these hints, [click here](command:{hideCommand})."
lines.Add $"To hide *ALL* hints, [click here](command:{hideAll})."

// We don't have access to generic VSCode config so we don't know what inlay hints mode is used
// if not isSetToToggle && toggleSupported then
lines.Add
$"Hints can also be hidden by default, and shown when Ctrl/Cmd+Alt is pressed. To do this, [click here](command:{setToToggle})."

lines.Add $"Finally, to dismiss this long tooltip forever, [click here](command:{disableLongTooltip})."

h.Tooltip
|> Option.iter (fun t ->
lines.Add ""
lines.Add(t))

String.concat "\n" lines |> markdown |> InlayHintTooltip.Markup |> Some

let hints: InlayHint[] =
hints
|> Array.map (fun h ->
Expand All @@ -2590,7 +2624,7 @@ type FSharpLspServer(state: State, lspClient: FSharpLspClient) =
{ Range = fcsPosToProtocolRange insertion.Pos
NewText = insertion.Text })
|> Some
Tooltip = h.Tooltip |> Option.map (InlayHintTooltip.String)
Tooltip = getTooltip h
PaddingLeft =
match h.Kind with
| InlayHints.HintKind.Type -> Some true
Expand Down
15 changes: 10 additions & 5 deletions src/FsAutoComplete/LspHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,8 @@ type CodeLensConfigDto =

type InlayHintDto =
{ typeAnnotations: bool option
parameterNames: bool option }
parameterNames: bool option
disableLongTooltip: bool option }

type FSharpConfigDto =
{ AutomaticWorkspaceInit: bool option
Expand Down Expand Up @@ -641,11 +642,13 @@ type CodeLensConfig =

type InlayHintsConfig =
{ typeAnnotations: bool
parameterNames: bool }
parameterNames: bool
disableLongTooltip: bool }

static member Default =
{ typeAnnotations = true
parameterNames = true }
parameterNames = true
disableLongTooltip = true }

type FSharpConfig =
{ AutomaticWorkspaceInit: bool
Expand Down Expand Up @@ -774,7 +777,8 @@ type FSharpConfig =
| None -> InlayHintsConfig.Default
| Some ihDto ->
{ typeAnnotations = defaultArg ihDto.typeAnnotations true
parameterNames = defaultArg ihDto.parameterNames true } }
parameterNames = defaultArg ihDto.parameterNames true
disableLongTooltip = defaultArg ihDto.disableLongTooltip true } }


/// called when a configuration change takes effect, so None-valued members here should revert options
Expand Down Expand Up @@ -842,7 +846,8 @@ type FSharpConfig =
| None -> InlayHintsConfig.Default
| Some ihDto ->
{ typeAnnotations = defaultArg ihDto.typeAnnotations x.InlayHints.typeAnnotations
parameterNames = defaultArg ihDto.parameterNames x.InlayHints.parameterNames } }
parameterNames = defaultArg ihDto.parameterNames x.InlayHints.parameterNames
disableLongTooltip = defaultArg ihDto.disableLongTooltip x.InlayHints.disableLongTooltip } }

member x.ScriptTFM =
match x.UseSdkScripts with
Expand Down
3 changes: 2 additions & 1 deletion test/FsAutoComplete.Tests.Lsp/Helpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ let defaultConfigDto: FSharpConfigDto =
InlayHints =
Some
{ typeAnnotations = Some true
parameterNames = Some true } }
parameterNames = Some true
disableLongTooltip = Some true } }

let clientCaps: ClientCapabilities =
let dynCaps: DynamicCapabilities = { DynamicRegistration = Some true }
Expand Down
3 changes: 2 additions & 1 deletion test/FsAutoComplete.Tests.Lsp/InlayHintTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ module private LspInlayHints =
let mkDummyEdits o = o |> Option.bind (fun _ -> Some [||])
let ignoreData (hint: InlayHint) = { hint with Data = None }

// we remove edits and tooltips because they are too hard atm.
let actualWithoutEdits =
{ actual with TextEdits = mkDummyEdits actual.TextEdits }
{ actual with TextEdits = mkDummyEdits actual.TextEdits; }
|> ignoreData

let expectedWithoutExpected =
Expand Down