Skip to content

Commit

Permalink
update formatting and align test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
baronfel committed Feb 2, 2025
1 parent 88e4b3a commit de0b1ec
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/FsAutoComplete.Core/AbstractClassStubGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ let tryFindAbstractClassExprInBufferAtPos

let allMembers = reprMembers @ members

let! inheritType, inheritMemberRange = // this must exist for abstract types
let! inheritType, inheritMemberRange =
allMembers
|> List.tryPick (function
| SynMemberDefn.ImplicitInherit(inheritType, _, _, range) -> Some(inheritType, range)
Expand Down
4 changes: 2 additions & 2 deletions src/FsAutoComplete.Core/DocumentationFormatter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ module DocumentationFormatter =
if String.IsNullOrWhiteSpace formattedParam then
formattedParam
else
"(requires " + formattedParam + " )"
"(requires " + formattedParam + ")"
else
""

Expand Down Expand Up @@ -379,7 +379,7 @@ module DocumentationFormatter =
if String.IsNullOrWhiteSpace formattedParam then
formattedParam
else
"(requires " + formattedParam + " )"
"(requires " + formattedParam + ")"

if paramConstraint = retTypeConstraint then
paddedParam
Expand Down
23 changes: 15 additions & 8 deletions src/FsAutoComplete.Core/SignatureFormatter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ module SignatureFormatter =
let rec formatFSharpType (context: FSharpDisplayContext) (typ: FSharpType) : string =
let context = context.WithPrefixGenericParameters()

let nullabilityClause =
if typ.HasNullAnnotation || typ.IsNullAmbivalent then
" | null"
else
""

try
if typ.IsTupleType || typ.IsStructTupleType then
let refTupleStr =
Expand All @@ -59,7 +65,7 @@ module SignatureFormatter =
else
refTupleStr
elif typ.IsGenericParameter then // no longer need to differentiate between SRTP and normal generic parameter types
"'" + typ.GenericParameter.Name
"'" + typ.GenericParameter.Name + nullabilityClause
elif typ.HasTypeDefinition && typ.GenericArguments.Count > 0 then
let typeDef = typ.TypeDefinition

Expand All @@ -68,19 +74,20 @@ module SignatureFormatter =

if entityIsArray typeDef then
if typ.GenericArguments.Count = 1 && typ.GenericArguments.[0].IsTupleType then
sprintf "(%s) array" genericArgs
$"(%s{genericArgs}) array%s{nullabilityClause}"
else
sprintf "%s array" genericArgs
$"%s{genericArgs} array%s{nullabilityClause}"
elif isMeasureType typeDef then
typ.Format context
typ.Format context + nullabilityClause
else
sprintf "%s<%s>" (FSharpKeywords.NormalizeIdentifierBackticks typeDef.DisplayName) genericArgs
$"%s{FSharpKeywords.NormalizeIdentifierBackticks typeDef.DisplayName}<%s{genericArgs}>%s{nullabilityClause}"
else if typ.HasTypeDefinition then
FSharpKeywords.NormalizeIdentifierBackticks typ.TypeDefinition.DisplayName
+ nullabilityClause
else
typ.Format context
typ.Format context + nullabilityClause
with _ ->
typ.Format context
typ.Format context + nullabilityClause

let formatGenericParameter includeMemberConstraintTypes displayContext (param: FSharpGenericParameter) =

Expand Down Expand Up @@ -357,7 +364,7 @@ module SignatureFormatter =
if String.IsNullOrWhiteSpace formattedParam then
formattedParam
else
"(requires " + formattedParam + " )"
"(requires " + formattedParam + ")"

if paramConstraint = retTypeConstraint then
paramFormat
Expand Down
40 changes: 14 additions & 26 deletions test/FsAutoComplete.Tests.Lsp/CoreTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,8 @@ let tooltipTests state =
13u
(concatLines
[ "val inline add:"
" x: 'a (requires static member ( + ) ) ->"
" y: 'b (requires static member ( + ) )"
" x: 'a (requires static member ( + )) ->"
" y: 'b (requires static member ( + ))"
" -> 'c" ])
//verify rendering of solved generic constraints in tooltips for members where they are solved
verifyDescription "solved generic parameters are called out in tooltip"
Expand All @@ -548,15 +548,9 @@ let tooltipTests state =
60u
7u
(concatLines
#if NET8_0
[ "active pattern Value: "
" input: Expr"
" -> option<obj * System.Type>" ])
#else
[ "active pattern Value: "
" input: Expr"
" -> option<objnull * System.Type>" ])
#endif
verifySignature "generic constraint rendering for IWSAM"
77u
5u
Expand All @@ -570,7 +564,7 @@ let tooltipTests state =
25u
(concatLines [
"static member GetAwaiter:"
" awaitable: 'Awaitable (requires member GetAwaiter )"
" awaitable: 'Awaitable (requires member GetAwaiter)"
" -> Awaiter<^Awaiter,'TResult> (requires :> ICriticalNotifyCompletion and member IsCompleted and member GetResult)"
])
verifySignature "basic active pattern"
Expand All @@ -584,47 +578,41 @@ let tooltipTests state =
70u
7u
(concatLines
#if NET8_0
[ "active pattern ValueWithName: "
" input: Expr"
" -> option<obj * System.Type * string>" ])
#else
[ "active pattern ValueWithName: "
" input: Expr"
" -> option<objnull * System.Type * string>" ])
#endif
verifySignature "interface with members with and without parameter names"
96u
7u
(concatLines
[ "interface IWithAndWithoutParamNames"
" abstract member WithParamNames: arg1: int * arg2: float -> string"
" abstract member WithoutParamNames: int * string -> int" ])
verifySignature "function with unsolved nullable parameter" 100u 7u (concatLines [
verifySignature "function with unsolved nullable parameter" 102u 7u (concatLines [
"val usesNullable:"
" x: 't | null"
" -> 't (requires reference )"
" x: 't | null"
" -> 't (requires reference)"
])

verifySignature "function with concrete nullable parameter" 101u 7u (concatLines [
verifySignature "function with concrete nullable parameter" 103u 7u (concatLines [
"val usesConcreteNullable:"
" x: string | null"
" -> String (requires reference )"
" -> String"
])
verifySignature "function with generic nullable return" 102u 7u (concatLines [
verifySignature "function with generic nullable return" 104u 7u (concatLines [
"val makesNullable:"
" x: 'x (requires reference)"
" -> 'x | null"
" x: 'x"
" -> 'x | null (requires reference)"
])
verifySignature "function with concrete nullable return" 103u 7u (concatLines [
verifySignature "function with concrete nullable return" 105u 7u (concatLines [
"val makesConcreteNullable:"
" x: string"
" -> string | null"
])
verifySignature "function with nullable return from BCL call" 104u 7u (concatLines [
verifySignature "function with nullable return from BCL call" 106u 7u (concatLines [
"val usesBCLNullable:"
" key: string"
" -> string | null"
" -> string | null"
])] ]

let closeTests state =
Expand Down
2 changes: 2 additions & 0 deletions test/FsAutoComplete.Tests.Lsp/TestCases/Tooltips/Script.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ type IWithAndWithoutParamNames =
abstract member WithParamNames : arg1: int * arg2: float -> string
abstract member WithoutParamNames : int * string -> int

#nullable enable

let usesNullable (x: 't | null) = nonNull x
let usesConcreteNullable (x: string | null) = nonNull x
let makesNullable (x: 'x): 'x | null = null
Expand Down

0 comments on commit de0b1ec

Please sign in to comment.