Skip to content

Commit 60f10a7

Browse files
committed
Update changes to new CreateTypeUsage API.
1 parent 65a9c09 commit 60f10a7

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

ReSharper.FSharp/src/FSharp.Psi.Features/src/LanguageService/FSharpElementFactory.fs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ type FSharpElementFactory(languageService: IFSharpLanguageService, sourceFile: I
9494
let exprStatement = getExpressionStatement source
9595
exprStatement.AttributeLists[0]
9696

97+
let createBindingSignature () =
98+
let source = "module V\nval a: obj"
99+
let moduleMember = getModuleMember source
100+
moduleMember :?> IBindingSignature
101+
97102
interface IFSharpElementFactory with
98103
member x.CreateOpenStatement(ns) =
99104
// todo: mangle ns
@@ -342,6 +347,12 @@ type FSharpElementFactory(languageService: IFSharpLanguageService, sourceFile: I
342347

343348
| _ -> System.ArgumentOutOfRangeException() |> raise
344349

350+
member x.CreateParenType() : IParenTypeUsage =
351+
let expr = getExpression "do () : (unit)"
352+
let doExpr = expr :?> IDoExpr
353+
let typedExpr = doExpr.Expression :?> ITypedExpr
354+
typedExpr.TypeUsage :?> IParenTypeUsage
355+
345356
member x.CreateSetExpr(left: IFSharpExpression, right: IFSharpExpression) =
346357
let source = "() <- ()"
347358
let expr = getExpression source
@@ -385,3 +396,10 @@ type FSharpElementFactory(languageService: IFSharpLanguageService, sourceFile: I
385396
moduleMember.As<ITypeDeclarationGroup>().TypeDeclarations[0] :?> IFSharpTypeDeclaration
386397

387398
typeDeclaration.TypeParameterDeclarationList
399+
400+
member x.CreateBindingSignature(bindingName: IFSharpPattern, returnType: ITypeUsage) =
401+
assert sourceFile.IsFSharpSignatureFile
402+
let signature = createBindingSignature ()
403+
signature.SetHeadPattern(bindingName) |> ignore
404+
replace signature.ReturnTypeInfo.ReturnType returnType
405+
signature

ReSharper.FSharp/src/FSharp.Psi.Intentions/src/Intentions/AddFunctionToSignatureFileAction.fs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ type AddFunctionToSignatureFileAction(dataProvider: FSharpContextActionDataProvi
116116
use writeCookie = WriteLockCookie.Create(binding.IsPhysical())
117117
use disableFormatter = new DisableCodeFormatter()
118118

119-
let factory = signatureModuleOrNamespaceDecl.CreateElementFactory()
120-
let typeInfo = factory.CreateTypeUsageForSignature(text)
119+
let factory = binding.CreateElementFactory()
120+
let typeInfo = factory.CreateTypeUsage(text, TypeUsageContext.Return)
121121

122122
// Enrich the type info with the found parameters from binding.
123123
let rec visit (index:int) (t: ITypeUsage) =
@@ -127,7 +127,9 @@ type AddFunctionToSignatureFileAction(dataProvider: FSharpContextActionDataProvi
127127
// If the return type is a function itself, the safest thing to do is to wrap it in parentheses.
128128
// Example: `let g _ = (*) 3`
129129
// `val g: 'a -> int -> int` is not valid, `val g: 'a -> (int -> int)` is.
130-
replace t (factory.WrapParenAroundTypeUsageForSignature(t))
130+
let parenType = factory.CreateParenType()
131+
replace parenType.InnerTypeUsage t
132+
replace t parenType
131133
| _ -> ()
132134
else
133135
let parameterAtIndex = tryFindParameterName true (binding.ParameterPatterns.Item(index))
@@ -139,8 +141,7 @@ type AddFunctionToSignatureFileAction(dataProvider: FSharpContextActionDataProvi
139141
| :? IFunctionTypeUsage as ft, ParameterNameFromPattern.SingleName (name, attributes) ->
140142
match ft.ArgumentTypeUsage with
141143
| :? IParameterSignatureTypeUsage as pstu ->
142-
factory.CreateParameterSignatureTypeUsage(attributes, name, pstu.TypeUsage)
143-
|> replace ft.ArgumentTypeUsage
144+
pstu.SetIdentifier(name) |> ignore
144145
| _ -> ()
145146

146147
visit (index + 1) ft.ReturnTypeUsage
@@ -153,8 +154,7 @@ type AddFunctionToSignatureFileAction(dataProvider: FSharpContextActionDataProvi
153154
|> Seq.iter (fun (p,t) ->
154155
match t, p with
155156
| :? IParameterSignatureTypeUsage as pstu, ParameterNameFromPattern.SingleName (name, attributes) ->
156-
factory.CreateParameterSignatureTypeUsage(attributes, name, pstu.TypeUsage)
157-
|> replace t
157+
pstu.SetIdentifier(name) |> ignore
158158
| _ -> ()
159159
)
160160
| _ -> visit (index + 1) ft.ReturnTypeUsage
@@ -164,7 +164,10 @@ type AddFunctionToSignatureFileAction(dataProvider: FSharpContextActionDataProvi
164164
if not binding.ParameterPatterns.IsEmpty then
165165
visit 0 typeInfo
166166

167-
let valSig = factory.CreateBindingSignature(refPat, typeInfo)
167+
let valSig =
168+
let signatureFactory = signatureModuleOrNamespaceDecl.CreateElementFactory()
169+
signatureFactory.CreateBindingSignature(refPat, typeInfo)
170+
168171
let newlineNode = NewLine(signatureModuleOrNamespaceDecl.GetLineEnding()) :> ITreeNode
169172
addNodesAfter signatureModuleOrNamespaceDecl.LastChild [| newlineNode; valSig |] |> ignore
170173

ReSharper.FSharp/src/FSharp.Psi/src/IFSharpElementFactory.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public interface IFSharpElementFactory
4141
ITypedPat CreateTypedPat(IFSharpPattern pattern, ITypeUsage typeUsage);
4242

4343
ITypeUsage CreateTypeUsage(string typeUsage, TypeUsageContext context);
44+
IParenTypeUsage CreateParenType();
4445

4546
IReturnTypeInfo CreateReturnTypeInfo(ITypeUsage typeSignature);
4647

@@ -62,5 +63,7 @@ public interface IFSharpElementFactory
6263
IMemberDeclaration CreatePropertyWithAccessor(string propertyName, string accessorName, FSharpList<IParametersPatternDeclaration> args);
6364

6465
ITypeParameterDeclarationList CreateTypeParameterOfTypeList(FSharpList<string> names);
66+
67+
IBindingSignature CreateBindingSignature(IFSharpPattern bindingName, ITypeUsage returnType);
6568
}
6669
}

0 commit comments

Comments
 (0)