@@ -43,6 +43,46 @@ module private OptionalParameterConversion =
4343
4444 let private isSame ( expr : SynExpr ) ( other : SynExpr ) = obj.ReferenceEquals( expr, other)
4545
46+ /// Whether the long identifier resolves to a value or union case of the FSharp.Core entity with the compiled full name.
47+ let isFSharpCoreSymbol
48+ ( checkResults : FSharpCheckFileResults )
49+ ( sourceText : SourceText )
50+ ( entityFullName : string )
51+ ( longIdent : Ident list )
52+ =
53+ match List.tryLast longIdent with
54+ | None -> false
55+ | Some ident ->
56+ let line = sourceText.Lines[ Line.toZ ident.idRange.EndLine]. ToString()
57+ let names = longIdent |> List.map _. idText
58+
59+ match checkResults.GetSymbolUseAtLocation( ident.idRange.EndLine, ident.idRange.EndColumn, line, names) with
60+ | None -> false
61+ | Some symbolUse ->
62+ let declaringEntityName =
63+ match symbolUse.Symbol with
64+ | :? FSharpUnionCase as case -> case.DeclaringEntity.TryFullName
65+ | :? FSharpMemberOrFunctionOrValue as value -> value.DeclaringEntity |> Option.bind _. TryFullName
66+ | _ -> None
67+
68+ match declaringEntityName with
69+ | Some name ->
70+ String.Equals( name, entityFullName, StringComparison.Ordinal)
71+ && String.Equals( symbolUse.Symbol.Assembly.SimpleName, " FSharp.Core" , StringComparison.Ordinal)
72+ | None -> false
73+
74+ let private optionType ( isStruct : bool ) =
75+ if isStruct then
76+ " Microsoft.FSharp.Core.FSharpValueOption`1"
77+ else
78+ " Microsoft.FSharp.Core.FSharpOption`1"
79+
80+ let private optionModule ( isStruct : bool ) =
81+ if isStruct then
82+ " Microsoft.FSharp.Core.ValueOption"
83+ else
84+ " Microsoft.FSharp.Core.OptionModule"
85+
4686 let private isOperator ( name : string ) ( expr : SynExpr ) =
4787 match expr with
4888 | SynExpr.LongIdent( longDotId = SynLongIdent( id = [ operator ])) -> hasText name operator
@@ -140,11 +180,12 @@ module private OptionalParameterConversion =
140180 | SynExpr.App( isInfix = false ; funcExpr = funcExpr) -> functionOf funcExpr
141181 | _ -> expr
142182
143- let private tryModuleQualifier ( isStruct : bool ) ( func : SynExpr ) =
183+ let private tryModuleQualifier ( isFSharpCore : string -> Ident list -> bool ) ( isStruct : bool ) ( func : SynExpr ) =
144184 match functionOf func with
145185 | SynExpr.LongIdent( longDotId = SynLongIdent( id = [ qualifier; name ])) when
146186 hasText ( if isStruct then " ValueOption" else " Option" ) qualifier
147187 && moduleFunctions.Contains name.idText
188+ && isFSharpCore ( optionModule isStruct) [ qualifier; name ]
148189 ->
149190 ValueSome [ qualifier ]
150191 | _ -> ValueNone
@@ -155,19 +196,22 @@ module private OptionalParameterConversion =
155196 else
156197 hasText " Some" ident || hasText " None" ident
157198
158- let private tryClauseHeads ( isStruct : bool ) ( clauses : SynMatchClause list ) =
199+ let private tryClauseHeads ( isFSharpCore : string -> Ident list -> bool ) ( isStruct : bool ) ( clauses : SynMatchClause list ) =
159200 ( ValueSome [], clauses)
160201 ||> List.fold ( fun heads ( SynMatchClause ( pat = pat )) ->
161202 match heads, pat with
162203 | ValueSome heads, ( SynPat.LongIdent( longDotId = SynLongIdent( id = [ head ])) | SynPat.Named( ident = SynIdent( head, _))) when
163204 isCase isStruct head
164205 ->
165- ValueSome( head :: heads)
206+ if isFSharpCore ( optionType isStruct) [ head ] then
207+ ValueSome( head :: heads)
208+ else
209+ ValueNone
166210 | ValueSome heads, ( SynPat.Wild _ | SynPat.Named _) -> ValueSome heads
167211 | _ -> ValueNone)
168212
169213 /// The identifiers to rename for one use of the parameter in the member body, when the use keeps its type.
170- let private tryUseRenames ( isStruct : bool ) ( node : SynExpr ) ( path : SyntaxVisitorPath ) =
214+ let private tryUseRenames ( isFSharpCore : string -> Ident list -> bool ) ( isStruct : bool ) ( node : SynExpr ) ( path : SyntaxVisitorPath ) =
171215 match node, path with
172216 | SynExpr.LongIdent( longDotId = SynLongIdent( id = _ :: property :: _)), _ when
173217 hasText " IsSome" property
@@ -178,16 +222,17 @@ module private OptionalParameterConversion =
178222 | _, SyntaxNode.SynExpr( SynExpr.App( isInfix = false ; funcExpr = SingleIdent func; argExpr = arg)) :: _ when
179223 isSame arg node
180224 && hasText ( if isStruct then " defaultValueArg" else " defaultArg" ) func
225+ && isFSharpCore " Microsoft.FSharp.Core.Operators" [ func ]
181226 ->
182227 ValueSome [ func ]
183228 | _, SyntaxNode.SynExpr( SynExpr.App( isInfix = false ; funcExpr = func; argExpr = arg)) :: _ when isSame arg node ->
184- tryModuleQualifier isStruct func
229+ tryModuleQualifier isFSharpCore isStruct func
185230 | _,
186231 SyntaxNode.SynExpr( SynExpr.App( isInfix = true ; funcExpr = pipe; argExpr = arg)) :: SyntaxNode.SynExpr( SynExpr.App(
187232 isInfix = false ; argExpr = func)) :: _ when isSame arg node && isOperator " op_PipeRight" pipe ->
188- tryModuleQualifier isStruct func
233+ tryModuleQualifier isFSharpCore isStruct func
189234 | _, SyntaxNode.SynExpr( SynExpr.Match( expr = scrutinee; clauses = clauses)) :: _ when isSame scrutinee node ->
190- tryClauseHeads isStruct clauses
235+ tryClauseHeads isFSharpCore isStruct clauses
191236 | _ -> ValueNone
192237
193238 let private tryUseNode ( parseTree : ParsedInput ) ( useRange : range ) =
@@ -203,12 +248,12 @@ module private OptionalParameterConversion =
203248 | _ -> None)
204249
205250 /// The identifiers to rename in the member body, when every use of the parameter keeps its type.
206- let tryBodyRenames ( parseTree : ParsedInput ) ( isStruct : bool ) ( uses : range seq ) =
251+ let tryBodyRenames ( isFSharpCore : string -> Ident list -> bool ) ( parseTree : ParsedInput ) ( isStruct : bool ) ( uses : range seq ) =
207252 ( ValueSome [], uses)
208253 ||> Seq.fold ( fun renames useRange ->
209254 match renames, tryUseNode parseTree useRange with
210255 | ValueSome renames, Some( node, path) ->
211- tryUseRenames isStruct node path
256+ tryUseRenames isFSharpCore isStruct node path
212257 |> ValueOption.map ( fun more -> [ yield ! more; yield ! renames ])
213258 | _ -> ValueNone)
214259
@@ -279,15 +324,18 @@ module private OptionalParameterConversion =
279324 argExpr = value) when isOperator " op_Equality" equals && hasText name argumentName -> Some value
280325 | _ -> None)
281326
282- let private valueChanges ( sourceText : SourceText ) ( isStruct : bool ) ( value : SynExpr ) =
327+ let private valueChanges ( isFSharpCore : string -> Ident list -> bool ) ( sourceText : SourceText ) ( isStruct : bool ) ( value : SynExpr ) =
283328 let valueSpan = spanOf sourceText value.Range
284329 let conversion = if isStruct then " toOption" else " ofOption"
285330 let inverse = if isStruct then " ofOption" else " toOption"
286331
287332 match functionOf value, value with
288- | SingleIdent head, _ when isCase isStruct head -> [ renamed sourceText isStruct head ]
333+ | SingleIdent head, _ when isCase isStruct head && isFSharpCore ( optionType isStruct) [ head ] ->
334+ [ renamed sourceText isStruct head ]
289335 | SynExpr.LongIdent( longDotId = SynLongIdent( id = [ qualifier; name ])), SynExpr.App( isInfix = false ; argExpr = inner) when
290- hasText " ValueOption" qualifier && hasText inverse name
336+ hasText " ValueOption" qualifier
337+ && hasText inverse name
338+ && isFSharpCore ( optionModule true ) [ qualifier; name ]
291339 ->
292340 [ TextChange( valueSpan, sourceText.ToString( spanOf sourceText inner.Range)) ]
293341 | _ when isAtomic value -> [ TextChange( TextSpan( valueSpan.Start, 0 ), $" ValueOption.{conversion} " ) ]
@@ -298,7 +346,14 @@ module private OptionalParameterConversion =
298346 ]
299347
300348 /// Changes to the ` ?name = value ` arguments of the call whose function ends at the use of the member.
301- let callSiteChanges ( sourceText : SourceText ) ( parseTree : ParsedInput ) ( isStruct : bool ) ( name : string ) ( useRange : range ) =
349+ let callSiteChanges
350+ ( isFSharpCore : string -> Ident list -> bool )
351+ ( sourceText : SourceText )
352+ ( parseTree : ParsedInput )
353+ ( isStruct : bool )
354+ ( name : string )
355+ ( useRange : range )
356+ =
302357 let arguments =
303358 ( useRange.Start, parseTree)
304359 ||> ParsedInput.tryPickLast ( fun _ node ->
@@ -312,7 +367,7 @@ module private OptionalParameterConversion =
312367 match arguments with
313368 | Some arguments ->
314369 optionalArgumentValues name arguments
315- |> List.collect ( valueChanges sourceText isStruct)
370+ |> List.collect ( valueChanges isFSharpCore sourceText isStruct)
316371 | None -> []
317372
318373[<ExportCodeRefactoringProvider( FSharpConstants.FSharpLanguageName, Name = " ConvertOptionalParameterStruct" ); Shared>]
@@ -379,7 +434,13 @@ type internal FSharpConvertOptionalParameterStructRefactoring [<ImportingConstru
379434 && Position.posGeq parameter.MemberRange.End symbolUse.Range.End)
380435 |> Seq.map _. Range
381436
382- match OptionalParameterConversion.tryBodyRenames parseResults.ParseTree parameter.IsStruct uses with
437+ match
438+ OptionalParameterConversion.tryBodyRenames
439+ ( OptionalParameterConversion.isFSharpCoreSymbol checkResults sourceText)
440+ parseResults.ParseTree
441+ parameter.IsStruct
442+ uses
443+ with
383444 | ValueSome renames ->
384445 let title =
385446 if parameter.IsStruct then
@@ -406,8 +467,8 @@ type internal FSharpConvertOptionalParameterStructRefactoring [<ImportingConstru
406467 let useDocument = solution.GetDocument documentId
407468 let! text = useDocument.GetTextAsync cancellationToken
408469
409- let! useParseResults =
410- useDocument.GetFSharpParseResultsAsync (
470+ let! useParseResults , useCheckResults =
471+ useDocument.GetFSharpParseAndCheckResultsAsync (
411472 nameof FSharpConvertOptionalParameterStructRefactoring
412473 )
413474
@@ -419,6 +480,7 @@ type internal FSharpConvertOptionalParameterStructRefactoring [<ImportingConstru
419480 for _, useRange in documentUses do
420481 yield !
421482 OptionalParameterConversion.callSiteChanges
483+ ( OptionalParameterConversion.isFSharpCoreSymbol useCheckResults text)
422484 text
423485 useParseResults.ParseTree
424486 parameter.IsStruct
0 commit comments