Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Quick fixes: replace return type (#367)
- Loading branch information
1 parent
224356c
commit c3ca234
Showing
65 changed files
with
448 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
ReSharper.FSharp/src/FSharp.Psi.Intentions/src/QuickFixes/ReplaceReturnTypeFix.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
namespace JetBrains.ReSharper.Plugins.FSharp.Psi.Features.Daemon.QuickFixes | ||
|
||
open JetBrains.ReSharper.Psi.ExtensionsAPI.Tree | ||
open JetBrains.ReSharper.Plugins.FSharp.Psi | ||
open JetBrains.ReSharper.Plugins.FSharp.Psi.Util | ||
open JetBrains.ReSharper.Plugins.FSharp.Psi.Impl | ||
open JetBrains.ReSharper.Plugins.FSharp.Psi.Features.Daemon.Highlightings | ||
open JetBrains.ReSharper.Plugins.FSharp.Psi.Tree | ||
open JetBrains.ReSharper.Psi.ExtensionsAPI | ||
open JetBrains.ReSharper.Resources.Shell | ||
|
||
type ReplaceReturnTypeFix(expr: IFSharpExpression, replacementTypeName: string) = | ||
inherit FSharpQuickFixBase() | ||
|
||
let mostOuterParentExpr = expr.GetOutermostParentExpressionFromItsReturn() | ||
|
||
new (error: TypeConstraintMismatchError) = | ||
// error FS0193: Type constraint mismatch. The type ↔ 'A.B' ↔is not compatible with type↔ 'Thing' | ||
ReplaceReturnTypeFix(error.Expr, error.MismatchedType) | ||
|
||
new (error: TypeDoesNotMatchTypeError) = | ||
// error FS0001: The type 'double' does not match the type 'int' | ||
ReplaceReturnTypeFix(error.Expr, error.ActualType) | ||
|
||
(* | ||
match () with | ||
| _ -> "" | ||
*) | ||
new (error: TypeEquationError) = | ||
// error FS0001: This expression was expected to have type↔ 'int' ↔but here has type↔ 'string' | ||
ReplaceReturnTypeFix(error.Expr, error.ActualType) | ||
|
||
new (error: IfExpressionNeedsTypeToSatisfyTypeRequirementsError) = | ||
// error FS0001: The 'if' expression needs to have type 'string' to satisfy context type requirements. It currently has type 'int'. | ||
ReplaceReturnTypeFix(error.Expr, error.ActualType) | ||
|
||
new (error: TypeMisMatchTuplesHaveDifferingLengthsError) = | ||
// Type mismatch. Expecting a | ||
// 'string * string' | ||
// but given a | ||
// 'string * string * 'a' | ||
// The tuples have differing lengths of 2 and 3 | ||
ReplaceReturnTypeFix(error.Expr, error.ActualType) | ||
|
||
override this.Text = "Replace return type" | ||
override this.IsAvailable _ = | ||
if isNull mostOuterParentExpr then false else | ||
let binding = BindingNavigator.GetByExpression(mostOuterParentExpr) | ||
if isNull binding then false else | ||
|
||
// Some types cannot be replaced properly solely on the FCS error message. | ||
// We will ignore these types for now. | ||
match binding.ReturnTypeInfo.ReturnType.IgnoreParentParens() with | ||
| :? ITupleTypeUsage | ||
| :? IFunctionTypeUsage -> false | ||
| _ -> | ||
// An invalid binary infix application will yield a similar error and could be mistaken for an invalid return type. | ||
// F.ex. 1 + "a", error FS0001: The type 'string' does not match the type 'int' | ||
// We ignore this scenario for now. | ||
match mostOuterParentExpr with | ||
| :? IBinaryAppExpr -> false | ||
| _ -> true | ||
|
||
override this.ExecutePsiTransaction(_solution) = | ||
let binding = BindingNavigator.GetByExpression(mostOuterParentExpr) | ||
|
||
use writeCookie = WriteLockCookie.Create(binding.IsPhysical()) | ||
use disableFormatter = new DisableCodeFormatter() | ||
|
||
let refPat = binding.HeadPattern.As<IReferencePat>() | ||
if isNull refPat then () else | ||
|
||
let symbolUse = refPat.GetFcsSymbolUse() | ||
if isNull symbolUse then () else | ||
|
||
if isNotNull binding.ReturnTypeInfo | ||
&& isNotNull binding.ReturnTypeInfo.ReturnType then | ||
let factory = binding.CreateElementFactory() | ||
let typeUsage = factory.CreateTypeUsage(replacementTypeName) | ||
let returnType = binding.ReturnTypeInfo.ReturnType.IgnoreInnerParens() | ||
|
||
match returnType with | ||
| :? INamedTypeUsage as ntu -> | ||
ModificationUtil.ReplaceChild(ntu, typeUsage) |> ignore | ||
| _ -> () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/Constraint Mismatch.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type Thing() = | ||
class | ||
end | ||
|
||
module A = | ||
type B() = | ||
class | ||
end | ||
|
||
|
||
let mkBee () : Thing = A.B(){caret} |
11 changes: 11 additions & 0 deletions
11
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/Constraint Mismatch.fs.gold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type Thing() = | ||
class | ||
end | ||
|
||
module A = | ||
type B() = | ||
class | ||
end | ||
|
||
|
||
let mkBee () : A.B = A.B(){caret} |
2 changes: 2 additions & 0 deletions
2
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/FunctionType 01.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
let f1 _ : unit -> int = | ||
fun _ -> ""{caret} |
2 changes: 2 additions & 0 deletions
2
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/FunctionType 01.fs.gold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
let f1 _ : unit -> string = | ||
fun _ -> ""{caret} |
2 changes: 2 additions & 0 deletions
2
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/FunctionType 02.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
let f2 _ : unit -> int = | ||
id{caret} |
2 changes: 2 additions & 0 deletions
2
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/FunctionType 02.fs.gold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
let f2 _ : unit -> unit = | ||
id{caret} |
1 change: 1 addition & 0 deletions
1
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/FunctionType 03.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let x: int -> int = 1, 1{caret} |
1 change: 1 addition & 0 deletions
1
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/FunctionType 03.fs.gold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let x: int * int = 1, 1{caret} |
1 change: 1 addition & 0 deletions
1
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/IfThenElse - Else.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let v : string = if true then "" else 1{caret} |
1 change: 1 addition & 0 deletions
1
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/IfThenElse - Else.fs.gold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let v : int = if true then "" else 1{caret} |
1 change: 1 addition & 0 deletions
1
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/IfThenElse - If.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let v : string = if true then 0{caret} else "" |
1 change: 1 addition & 0 deletions
1
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/IfThenElse - If.fs.gold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let v : int = if true then 0{caret} else "" |
1 change: 1 addition & 0 deletions
1
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/Infix application.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let sum (a: int) (b: int) : string = a + b{caret} |
1 change: 1 addition & 0 deletions
1
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/Infix application.fs.gold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let sum (a: int) (b: int) : int = a + b{caret} |
3 changes: 3 additions & 0 deletions
3
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/LetOrUse.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
let x: int = | ||
let _ = 1 | ||
""{caret} |
3 changes: 3 additions & 0 deletions
3
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/LetOrUse.fs.gold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
let x: string = | ||
let _ = 1 | ||
""{caret} |
3 changes: 3 additions & 0 deletions
3
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/MatchClause.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
let f x : int = | ||
match () with | ||
| _ -> ""{caret} |
3 changes: 3 additions & 0 deletions
3
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/MatchClause.fs.gold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
let f x : string = | ||
match () with | ||
| _ -> ""{caret} |
2 changes: 2 additions & 0 deletions
2
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/MatchLambda.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
let v: 'a -> int = function | ||
| _ -> ""{caret} |
2 changes: 2 additions & 0 deletions
2
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/MatchLambda.fs.gold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
let v: 'a -> string = function | ||
| _ -> ""{caret} |
6 changes: 6 additions & 0 deletions
6
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/No highlighting 01.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
let f x : int = | ||
match () with | ||
| _ -> "" | ||
| _ -> 1{caret} | ||
|
||
1 |
6 changes: 6 additions & 0 deletions
6
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/No highlighting 01.gold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
let f x : int = | ||
match () with | ||
| _ -> "" | ||
| _ -> 1{caret} | ||
|
||
1 |
2 changes: 2 additions & 0 deletions
2
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/No highlighting 02.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
let v : string = | ||
$"%s{42{caret}}" |
2 changes: 2 additions & 0 deletions
2
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/No highlighting 02.gold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
let v : string = | ||
$"%s{42{caret}}" |
5 changes: 5 additions & 0 deletions
5
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/No highlighting 03.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
let forever () = true | ||
|
||
let meh : unit = | ||
while forever ""{caret} do | ||
() |
5 changes: 5 additions & 0 deletions
5
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/No highlighting 03.gold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
let forever () = true | ||
|
||
let meh : unit = | ||
while forever ""{caret} do | ||
() |
4 changes: 4 additions & 0 deletions
4
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/Not available 01.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
let f x : int = | ||
"" + 1{caret} | ||
|
||
1 |
4 changes: 4 additions & 0 deletions
4
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/Not available 01.gold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
let f x : int = | ||
"" + 1{caret} | ||
|
||
1 |
1 change: 1 addition & 0 deletions
1
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/Not available 02.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
if (1{caret}: string) = 2 then () else () |
1 change: 1 addition & 0 deletions
1
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/Not available 02.gold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
if (1{caret}: string) = 2 then () else () |
3 changes: 3 additions & 0 deletions
3
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/Not available 03.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
let s : unit = | ||
for i = (0:string{caret}) to 9 do | ||
() |
3 changes: 3 additions & 0 deletions
3
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/Not available 03.fs.gold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
let s : unit = | ||
for i = (0:string{caret}) to 9 do | ||
() |
3 changes: 3 additions & 0 deletions
3
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/Not available 04.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
let v : int = | ||
let x = (1{caret}: string) in | ||
(int) x |
3 changes: 3 additions & 0 deletions
3
ReSharper.FSharp/test/data/features/quickFixes/replaceReturnType/Not available 04.fs.gold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
let v : int = | ||
let x = (1{caret}: string) in | ||
(int) x |
Oops, something went wrong.