-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RemovePatternArgument quick fix (#1142)
- Loading branch information
Showing
4 changed files
with
275 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module FsAutoComplete.CodeFix.RemovePatternArgument | ||
|
||
open FsToolkit.ErrorHandling | ||
open FsAutoComplete.CodeFix.Types | ||
open Ionide.LanguageServerProtocol.Types | ||
open FsAutoComplete | ||
open FsAutoComplete.LspHelpers | ||
open FSharp.Compiler.Syntax | ||
open FSharp.Compiler.Text.Range | ||
open FSharp.Compiler.Text | ||
|
||
let title = "Remove argument" | ||
|
||
let tryFindPattern pos input = | ||
let visitor = | ||
{ new SyntaxVisitorBase<range>() with | ||
|
||
member _.VisitExpr(path, traverseSynExpr, defaultTraverse, expr) = defaultTraverse expr | ||
|
||
member this.VisitPat(path: SyntaxVisitorPath, defaultTraverse: SynPat -> range option, synPat: SynPat) = | ||
match synPat with | ||
| SynPat.LongIdent(longDotId = synLongIdent; argPats = SynArgPats.Pats(pats); range = m) when | ||
rangeContainsPos synPat.Range pos && not pats.IsEmpty | ||
-> | ||
let mRemove = mkRange m.FileName synLongIdent.Range.End m.End | ||
Some mRemove | ||
| SynPat.Paren(pat = innerPat) -> | ||
let nextPath = SyntaxNode.SynPat(synPat) :: path | ||
this.VisitPat(nextPath, defaultTraverse, innerPat) | ||
| _ -> None } | ||
|
||
SyntaxTraversal.Traverse(pos, input, visitor) | ||
|
||
let fix (getParseResultsForFile: GetParseResultsForFile) : CodeFix = | ||
Run.ifDiagnosticByCode (Set.ofList [ "725"; "3191" ]) (fun _ codeActionParams -> | ||
asyncResult { | ||
let filePath = codeActionParams.TextDocument.GetFilePath() |> Utils.normalizePath | ||
let fcsPos = protocolPosToPos codeActionParams.Range.Start | ||
let! parseAndCheck, _, _ = getParseResultsForFile filePath fcsPos | ||
|
||
match tryFindPattern fcsPos parseAndCheck.GetAST with | ||
| None -> return [] | ||
| Some removeRange -> | ||
let e = | ||
{ Range = fcsRangeToLsp removeRange | ||
NewText = "" } | ||
|
||
return | ||
[ { Edits = [| e |] | ||
File = codeActionParams.TextDocument | ||
Title = title | ||
SourceDiagnostic = None | ||
Kind = FixKind.Refactor } ] | ||
}) |
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
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