Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add codefix "replace dotLambda to lambda" #1264

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add replace dotLambda to lambda
  • Loading branch information
ijklam committed Apr 14, 2024
commit 6860466025283a11e157d1d6ca12b54520476584
54 changes: 49 additions & 5 deletions src/FsAutoComplete/CodeFixes/ReplaceLambdaWithDotLambda.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ type ReplaceInfo =
| RawLambda of range
| ParenLambda of lpr: range * lambdaRange: range * rpr: range

[<RequireQualifiedAccess; NoComparison; NoEquality; Struct>]
type DotLambdaReplaceInfo =
| NotNeedParen of range
| NeedParen of underscoreRange: range * endRange: range

[<RequireQualifiedAccess; NoComparison; NoEquality; Struct>]
type FixType =
| ReplaceToDotLambda of replaceInfo: ReplaceInfo
| ReplaceToLambda of dotLambdaReplaceInfo: DotLambdaReplaceInfo

let tryFindLambda (cursor: pos) (tree: ParsedInput) =
(cursor, tree)
||> ParsedInput.tryPick (fun path node ->
Expand All @@ -46,12 +56,27 @@ let tryFindLambda (cursor: pos) (tree: ParsedInput) =

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)
ReplaceInfo.ParenLambda(lpr, mLambda, rpr) |> FixType.ReplaceToDotLambda |> Some
| _ -> ReplaceInfo.RawLambda mLambda |> FixType.ReplaceToDotLambda |> Some
| _ -> None

| SyntaxNode.SynExpr(SynExpr.DotLambda(
range = mLambda; trivia = { FSharp.Compiler.SyntaxTrivia.SynExprDotLambdaTrivia.UnderscoreRange = m })) ->

match List.tryHead path with
| Some(SyntaxNode.SynExpr(SynExpr.Paren _)) ->
DotLambdaReplaceInfo.NotNeedParen m |> FixType.ReplaceToLambda |> Some
| _ ->
let (Pos(line, column)) = mLambda.End
let posEnd = Position.mkPos line (column + 1)

DotLambdaReplaceInfo.NeedParen(m, mkRange m.FileName posEnd posEnd)
|> FixType.ReplaceToLambda
|> Some
| _ -> None)

let title = "Replace lambda with _."
let titleReplaceToDotLambda = "Replace lambda with _."
let titleReplaceToLambda = "Replace _. with lambda"

let languageFeature = lazy LanguageFeatureShim("AccessorFunctionShorthand")

Expand All @@ -72,7 +97,7 @@ let fix (getLanguageVersion: GetLanguageVersion) (getParseResultsForFile: GetPar

match tryFindLambda fcsPos parseAndCheckResults.GetParseResults.ParseTree with
| None -> return []
| Some replaceInfo ->
| Some(FixType.ReplaceToDotLambda replaceInfo) ->
let edits =
match replaceInfo with
| ReplaceInfo.RawLambda m ->
Expand All @@ -88,7 +113,26 @@ let fix (getLanguageVersion: GetLanguageVersion) (getParseResultsForFile: GetPar

return
[ { SourceDiagnostic = None
Title = title
Title = titleReplaceToDotLambda
File = codeActionParams.TextDocument
Edits = edits
Kind = FixKind.Fix } ]
| Some(FixType.ReplaceToLambda replaceInfo) ->
let edits =
match replaceInfo with
| DotLambdaReplaceInfo.NeedParen(m, mEnd) ->
[| { Range = fcsRangeToLsp m
NewText = "(fun _i -> _i" }
{ Range = fcsRangeToLsp mEnd
NewText = ")" } |]

| DotLambdaReplaceInfo.NotNeedParen m ->
[| { Range = fcsRangeToLsp m
NewText = "fun _i -> _i" } |]

return
[ { SourceDiagnostic = None
Title = titleReplaceToLambda
File = codeActionParams.TextDocument
Edits = edits
Kind = FixKind.Fix } ]
Expand Down
3 changes: 2 additions & 1 deletion src/FsAutoComplete/CodeFixes/ReplaceLambdaWithDotLambda.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ module FsAutoComplete.CodeFix.ReplaceLambdaWithDotLambda

open FsAutoComplete.CodeFix.Types

val title: string
val titleReplaceToDotLambda: string
val titleReplaceToLambda: string
val fix: getLanguageVersion: GetLanguageVersion -> getParseResultsForFile: GetParseResultsForFile -> CodeFix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

let tests state =
serverTestList (nameof ReplaceLambdaWithDotLambda) state defaultConfigDto None (fun server ->
[ let selectCodeFix = CodeFix.withTitle ReplaceLambdaWithDotLambda.title

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on ubuntu-latest for repo global.json

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on ubuntu-latest for repo global.json

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on ubuntu-latest for 6.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on ubuntu-latest for 6.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on ubuntu-latest for 7.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on ubuntu-latest for 7.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on ubuntu-latest for 7.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on ubuntu-latest for 7.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on macos-13 for repo global.json

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on macos-13 for repo global.json

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on ubuntu-latest for 8.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on ubuntu-latest for 8.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on ubuntu-latest for 8.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on ubuntu-latest for 8.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on ubuntu-latest for 8.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on ubuntu-latest for 8.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on macos-13 for 6.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on macos-13 for 6.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on macos-13 for 8.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on macos-13 for 8.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on macos-13 for 8.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on macos-13 for 8.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on macos-13 for 8.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on macos-13 for 8.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on windows-latest for 6.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on windows-latest for 6.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on windows-latest for repo global.json

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on windows-latest for repo global.json

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on macos-13 for 7.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on macos-13 for 7.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on macos-13 for 7.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on macos-13 for 7.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on windows-latest for 7.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on windows-latest for 7.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on windows-latest for 7.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on windows-latest for 7.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on windows-latest for 8.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on windows-latest for 8.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on windows-latest for 8.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on windows-latest for 8.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on windows-latest for 8.0

The value, constructor, namespace or type 'title' is not defined.

Check failure on line 11 in test/FsAutoComplete.Tests.Lsp/CodeFixTests/ReplaceLambdaWithDotLambdaTests.fs

View workflow job for this annotation

GitHub Actions / Build on windows-latest for 8.0

The value, constructor, namespace or type 'title' is not defined.

testCaseAsync "Simple property"
<| CodeFix.check
Expand Down Expand Up @@ -42,4 +42,35 @@
selectCodeFix
"let a6 = _.ToString()"

testCaseAsync "Simple property (from dot lambda to lambda)"
<| CodeFix.check
server
"let x = \"\" |> $0_.Length"
Diagnostics.acceptAll
selectCodeFix
"let x = \"\" |> (fun _i -> _i.Length)"

testCaseAsync "Property of application (from dot lambda to lambda)"
<| CodeFix.check
server
"let a5 : {| Foo : int -> {| X : string |} |} -> string = _.$0Foo(5).X"
Diagnostics.acceptAll
selectCodeFix
"let a5 : {| Foo : int -> {| X : string |} |} -> string = (fun _i -> _i.Foo(5).X)"

testCaseAsync "Application (from dot lambda to lambda)"
<| CodeFix.check
server
"let a6 = [1] |> List.map _$0.ToString()"
Diagnostics.acceptAll
selectCodeFix
"let a6 = [1] |> List.map (fun _i -> _i.ToString())"

testCaseAsync "fun x -> x.ToString() (from dot lambda to lambda)"
<| CodeFix.check
server
"let a6 = _.ToStri$0ng()"
Diagnostics.acceptAll
selectCodeFix
"let a6 = (fun _i -> _i.ToString())"
])
Loading