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 code fix for wildcard _ in pattern match for fs43 on |-> #1157

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
56 changes: 56 additions & 0 deletions src/FsAutoComplete/CodeFixes/AddMissingWildcardOperator.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module FsAutoComplete.CodeFix.AddMissingWildcardOperator

open FsToolkit.ErrorHandling
open FsAutoComplete.CodeFix.Navigation
open FsAutoComplete.CodeFix.Types
open Ionide.LanguageServerProtocol.Types
open FsAutoComplete
open FsAutoComplete.LspHelpers
open FSharp.Compiler.Syntax
open FSharp.Compiler.Text
open FSharp.Compiler.SyntaxTrivia

let title = "Add missing wildcard operator"


let tryFindPattern pos input =

let visitor =
{ new SyntaxVisitorBase<range>() with

member _.VisitExpr(path, traverseSynExpr, defaultTraverse, expr) =
match expr with
| SynExpr.LongIdent(
longDotId = SynLongIdent.SynLongIdent(trivia = [ Some(IdentTrivia.OriginalNotation("|->")) ])
range = range) when FSharp.Compiler.Text.Range.rangeContainsPos range pos ->

Some(range)

| _ -> defaultTraverse expr }

SyntaxTraversal.Traverse(pos, input, visitor)



/// a codefix that adds a missing 'fun' keyword to a lambda
jkone27 marked this conversation as resolved.
Show resolved Hide resolved
let fix (getParseResultsForFile: GetParseResultsForFile) : CodeFix =
Run.ifDiagnosticByCode (Set.ofList [ "43" ]) (fun diagnostic codeActionParams ->
asyncResult {

let filePath = codeActionParams.TextDocument.GetFilePath() |> Utils.normalizePath
let fcsPos = protocolPosToPos codeActionParams.Range.Start
let! (parseAndCheck, lineStr, _sourceText) = getParseResultsForFile filePath fcsPos

match tryFindPattern fcsPos parseAndCheck.GetAST with
| None -> return []
| Some operatorRange ->

let lspRange = fcsRangeToLsp operatorRange

return
[ { Title = title
File = codeActionParams.TextDocument
SourceDiagnostic = Some diagnostic
Edits = [| { Range = lspRange; NewText = "| _ ->" } |]
Kind = FixKind.Fix } ]
})
12 changes: 12 additions & 0 deletions src/FsAutoComplete/CodeFixes/AddMissingWildcardOperator.fsi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module FsAutoComplete.CodeFix.AddMissingWildcardOperator

open FsToolkit.ErrorHandling
open FsAutoComplete.CodeFix.Navigation
open FsAutoComplete.CodeFix.Types
open Ionide.LanguageServerProtocol.Types
open FsAutoComplete
open FsAutoComplete.LspHelpers

val title: string
/// a codefix that adds a missing wildcard pattern to a match case
val fix: getParseResultsForFile: GetParseResultsForFile -> CodeFix
1 change: 1 addition & 0 deletions src/FsAutoComplete/LspServers/AdaptiveFSharpLspServer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1806,6 +1806,7 @@ type AdaptiveFSharpLspServer
RemoveUnnecessaryReturnOrYield.fix tryGetParseResultsForFile getLineText
ConvertCSharpLambdaToFSharpLambda.fix tryGetParseResultsForFile getLineText
AddMissingFunKeyword.fix forceFindSourceText getLineText
AddMissingWildcardOperator.fix tryGetParseResultsForFile
MakeOuterBindingRecursive.fix tryGetParseResultsForFile getLineText
AddMissingRecKeyword.fix forceFindSourceText getLineText
ConvertBangEqualsToInequality.fix getRangeText
Expand Down
1 change: 1 addition & 0 deletions src/FsAutoComplete/LspServers/FsAutoComplete.Lsp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,7 @@ type FSharpLspServer(state: State, lspClient: FSharpLspClient, sourceTextFactory
RemoveUnnecessaryReturnOrYield.fix tryGetParseResultsForFile getLineText
ConvertCSharpLambdaToFSharpLambda.fix tryGetParseResultsForFile getLineText
AddMissingFunKeyword.fix getFileLines getLineText
AddMissingWildcardOperator.fix tryGetParseResultsForFile
MakeOuterBindingRecursive.fix tryGetParseResultsForFile getLineText
AddMissingRecKeyword.fix getFileLines getLineText
ConvertBangEqualsToInequality.fix getRangeText
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module private FsAutoComplete.Tests.CodeFixTests.AddMissingWildcardOperatorTests

open Expecto
open Helpers
open Utils.ServerTests
open Utils.CursorbasedTests
open FsAutoComplete.CodeFix

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

ftestCaseAsync "can suggest wildcard pattern for missing match case"
<| CodeFix.check
server
"""
type SomeUnion =
| First
| Second

let testMatch su =
match su with
| First -> "hey"
$0|-> "hello"
"""
(Diagnostics.expectCode "43")
selectCodeFix
"""
type SomeUnion =
| First
| Second

let testMatch su =
match su with
| First -> "hey"
| _ -> "hello"
""" ])
3 changes: 2 additions & 1 deletion test/FsAutoComplete.Tests.Lsp/CodeFixTests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3351,4 +3351,5 @@ let tests state =
useTripleQuotedInterpolationTests state
wrapExpressionInParenthesesTests state
removeRedundantAttributeSuffixTests state
removePatternArgumentTests state ]
removePatternArgumentTests state
AddMissingWildcardOperatorTests.tests state ]