-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ReplaceLambdaWithDotLambda code fix
- Loading branch information
Showing
5 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
src/FsAutoComplete/CodeFixes/ReplaceLambdaWithDotLambda.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,104 @@ | ||
module FsAutoComplete.CodeFix.ReplaceLambdaWithDotLambda | ||
|
||
open FSharp.Compiler.Syntax | ||
open FsAutoComplete.FCSPatches | ||
open FsToolkit.ErrorHandling | ||
open Ionide.LanguageServerProtocol.Types | ||
open FsAutoComplete.CodeFix.Types | ||
open FsAutoComplete | ||
open FsAutoComplete.LspHelpers | ||
open FSharp.Compiler.Text.Range | ||
open FSharp.Compiler.Text | ||
|
||
[<return: Struct>] | ||
let (|LongIdentExprIdentifier|_|) = | ||
function | ||
| SynExpr.LongIdent(longDotId = SynLongIdent(id = identifierIdent :: _)) -> ValueSome identifierIdent | ||
| _ -> ValueNone | ||
|
||
[<RequireQualifiedAccess>] | ||
type ReplaceInfo = | ||
| RawLambda of range | ||
| ParenLambda of lpr: range * lambdaRange: range * rpr: range | ||
|
||
let tryFindLambda (cursor: pos) (tree: ParsedInput) = | ||
let visitor = | ||
{ new SyntaxVisitorBase<ReplaceInfo>() with | ||
member _.VisitExpr(path, traverseSynExpr, defaultTraverse, synExpr) = | ||
match synExpr with | ||
| SynExpr.Lambda(parsedData = Some([ SynPat.Named(ident = SynIdent(patIdent, _)) ], bodyExpr)) when | ||
rangeContainsPos synExpr.Range cursor | ||
-> | ||
match bodyExpr with | ||
// x.Foo | ||
| LongIdentExprIdentifier identifierIdent | ||
// x.Foo().Bar | ||
| SynExpr.DotGet( | ||
expr = SynExpr.App(ExprAtomicFlag.Atomic, | ||
false, | ||
LongIdentExprIdentifier identifierIdent, | ||
SynExpr.Paren _, | ||
_)) | ||
// x.Foo() | ||
| SynExpr.App(ExprAtomicFlag.Atomic, | ||
false, | ||
LongIdentExprIdentifier identifierIdent, | ||
(SynExpr.Const(constant = SynConst.Unit) | SynExpr.Paren _), | ||
_) -> | ||
if identifierIdent.idText <> patIdent.idText then | ||
None | ||
else | ||
let mLambda = | ||
mkRange synExpr.Range.FileName synExpr.Range.Start identifierIdent.idRange.End | ||
|
||
match List.tryHead path with | ||
| Some(SyntaxNode.SynExpr(SynExpr.Paren(leftParenRange = lpr; rightParenRange = Some rpr))) -> | ||
Some(ReplaceInfo.ParenLambda(lpr, mLambda, rpr)) | ||
| _ -> Some(ReplaceInfo.RawLambda mLambda) | ||
| _ -> None | ||
| e -> defaultTraverse e } | ||
|
||
SyntaxTraversal.Traverse(cursor, tree, visitor) | ||
|
||
let title = "Replace lambda with _." | ||
|
||
let languageFeature = lazy LanguageFeatureShim("AccessorFunctionShorthand") | ||
|
||
let fix (getLanguageVersion: GetLanguageVersion) (getParseResultsForFile: GetParseResultsForFile) : CodeFix = | ||
fun (codeActionParams: CodeActionParams) -> | ||
asyncResult { | ||
let fileName = codeActionParams.TextDocument.GetFilePath() |> Utils.normalizePath | ||
let! languageVersion = getLanguageVersion fileName | ||
|
||
if not (languageVersion.SupportsFeature languageFeature.Value) then | ||
return [] | ||
else | ||
|
||
let fcsPos = protocolPosToPos codeActionParams.Range.Start | ||
|
||
let! (parseAndCheckResults: ParseAndCheckResults, _line: string, _sourceText: IFSACSourceText) = | ||
getParseResultsForFile fileName fcsPos | ||
|
||
match tryFindLambda fcsPos parseAndCheckResults.GetParseResults.ParseTree with | ||
| None -> return [] | ||
| Some replaceInfo -> | ||
let edits = | ||
match replaceInfo with | ||
| ReplaceInfo.RawLambda m -> | ||
[| { Range = fcsRangeToLsp m | ||
NewText = "_" } |] | ||
| ReplaceInfo.ParenLambda(lpr, mLambda, rpr) -> | ||
[| { Range = fcsRangeToLsp lpr | ||
NewText = "" } | ||
{ Range = fcsRangeToLsp mLambda | ||
NewText = " _" } | ||
{ Range = fcsRangeToLsp rpr | ||
NewText = "" } |] | ||
|
||
return | ||
[ { SourceDiagnostic = None | ||
Title = title | ||
File = codeActionParams.TextDocument | ||
Edits = edits | ||
Kind = FixKind.Fix } ] | ||
} |
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 @@ | ||
module FsAutoComplete.CodeFix.ReplaceLambdaWithDotLambda | ||
|
||
open FsAutoComplete.CodeFix.Types | ||
|
||
val title: string | ||
val fix: getLanguageVersion: GetLanguageVersion -> getParseResultsForFile: GetParseResultsForFile -> CodeFix |
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
36 changes: 36 additions & 0 deletions
36
test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.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,36 @@ | ||
module private FsAutoComplete.Tests.CodeFixTests.ReplaceLambdaWithDotLambdaTests | ||
|
||
open Expecto | ||
open Helpers | ||
open Utils.ServerTests | ||
open Utils.CursorbasedTests | ||
open FsAutoComplete.CodeFix | ||
|
||
let tests state = | ||
serverTestList (nameof ReplaceLambdaWithDotLambda) state defaultConfigDto None (fun server -> | ||
[ let selectCodeFix = CodeFix.withTitle ReplaceLambdaWithDotLambda.title | ||
|
||
testCaseAsync "Simple property" | ||
<| CodeFix.check | ||
server | ||
"let x = \"\" |> fun y -> $0y.Length" | ||
Diagnostics.acceptAll | ||
selectCodeFix | ||
"let x = \"\" |> _.Length" | ||
|
||
|
||
testCaseAsync "Property of application" | ||
<| CodeFix.check | ||
server | ||
"let a5 : {| Foo : int -> {| X : string |} |} -> string = fun x -> x.$0Foo(5).X" | ||
Diagnostics.acceptAll | ||
selectCodeFix | ||
"let a5 : {| Foo : int -> {| X : string |} |} -> string = _.Foo(5).X" | ||
|
||
testCaseAsync "Application" | ||
<| CodeFix.check | ||
server | ||
"let a6 = [1] |> List.map(fun x -> x$0.ToString())" | ||
Diagnostics.acceptAll | ||
selectCodeFix | ||
"let a6 = [1] |> List.map _.ToString()" ]) |
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