-
Notifications
You must be signed in to change notification settings - Fork 171
Ignore expression code fix #1253
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
24e4427
Add ignore expression code fix.
nojaf 3aee389
Check for multiline diagnostic sooner.
nojaf 029a119
Merge branch 'nightly' into IgnoreExpression
nojaf 207afa8
Check new expression if it needs parentheses or not.
nojaf f9c9344
Construct correct path for SynExpr.shouldBeParenthesizedInContext
nojaf 6b0e712
Update FCS with fix from Brian
nojaf 9488ece
Merge branch 'nightly' into ignoreExpression
nojaf d1d6106
Update AST usage
nojaf c2a00f0
Add test to ignore tuple.
nojaf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.IgnoreExpression | ||
|
|
||
| open FSharp.Compiler.Syntax | ||
| open FSharp.Compiler.SyntaxTrivia | ||
| open FSharp.Compiler.Text | ||
| open FSharp.UMX | ||
| open FsToolkit.ErrorHandling | ||
| open Ionide.LanguageServerProtocol.Types | ||
| open FsAutoComplete.CodeFix.Types | ||
| open FsAutoComplete | ||
| open FsAutoComplete.LspHelpers | ||
|
|
||
| let title = "Ignore expression" | ||
|
|
||
| let fix (getParseResultsForFile: GetParseResultsForFile) : CodeFix = | ||
| Run.ifDiagnosticByCode (set [ "20" ]) (fun diagnostic (codeActionParams: CodeActionParams) -> | ||
| asyncResult { | ||
| let fileName = codeActionParams.TextDocument.GetFilePath() |> Utils.normalizePath | ||
| let fcsPos = protocolPosToPos codeActionParams.Range.Start | ||
| let mDiag = protocolRangeToRange (UMX.untag fileName) diagnostic.Range | ||
|
|
||
| // Only do single line for now | ||
| if mDiag.StartLine <> mDiag.EndLine then | ||
| return [] | ||
| else | ||
|
|
||
| let! (parseAndCheckResults: ParseAndCheckResults, _line: string, sourceText: IFSACSourceText) = | ||
| getParseResultsForFile fileName fcsPos | ||
|
|
||
| let mExprOpt = | ||
| (fcsPos, parseAndCheckResults.GetParseResults.ParseTree) | ||
| ||> ParsedInput.tryPick (fun path node -> | ||
| match node with | ||
| | SyntaxNode.SynExpr(e) when Range.equals mDiag e.Range -> Some(path, e) | ||
| | _ -> None) | ||
|
|
||
| match mExprOpt with | ||
| | None -> return [] | ||
| | Some(path, expr) -> | ||
|
|
||
| let needsParentheses = | ||
| let appPipe, appIgnore = | ||
| let lineNumber = expr.Range.StartLine | ||
|
|
||
| let mkRangeInFile (offset: int) (length: int) : range = | ||
| Range.mkRange | ||
| expr.Range.FileName | ||
| (Position.mkPos lineNumber (expr.Range.EndColumn + offset)) | ||
| (Position.mkPos lineNumber (expr.Range.EndColumn + offset + length)) | ||
|
|
||
| let mPipe = mkRangeInFile 2 2 | ||
| let mIgnore = mkRangeInFile 5 6 | ||
|
|
||
| let appPipe = | ||
| SynExpr.App( | ||
| ExprAtomicFlag.NonAtomic, | ||
| true, | ||
| SynExpr.LongIdent( | ||
| false, | ||
| SynLongIdent( | ||
| [ FSharp.Compiler.Syntax.Ident("op_PipeRight", mPipe) ], | ||
| [], | ||
| [ Some(IdentTrivia.OriginalNotation "|>") ] | ||
| ), | ||
| None, | ||
| mPipe | ||
| ), | ||
| expr, // The expr that will now be piped into ignore. | ||
| Range.unionRanges expr.Range mPipe | ||
| ) | ||
|
|
||
| let appIgnore = | ||
| SynExpr.App( | ||
| ExprAtomicFlag.NonAtomic, | ||
| false, | ||
| appPipe, | ||
| SynExpr.Ident(FSharp.Compiler.Syntax.Ident("ignore", mIgnore)), | ||
| Range.unionRanges expr.Range mIgnore | ||
| ) | ||
|
|
||
| appPipe, appIgnore | ||
|
|
||
| let pathWithPipeIgnore = | ||
| SyntaxNode.SynExpr appPipe :: SyntaxNode.SynExpr appIgnore :: path | ||
|
|
||
| SynExpr.shouldBeParenthesizedInContext sourceText.GetLineString pathWithPipeIgnore expr | ||
|
nojaf marked this conversation as resolved.
|
||
|
|
||
| let newText = | ||
| let currentText = sourceText.GetSubTextFromRange expr.Range | ||
|
|
||
| if not needsParentheses then | ||
| $"%s{currentText} |> ignore" | ||
| else | ||
| $"(%s{currentText}) |> ignore" | ||
|
|
||
| return | ||
| [ { SourceDiagnostic = None | ||
| Title = title | ||
| File = codeActionParams.TextDocument | ||
| Edits = | ||
| [| { Range = fcsRangeToLsp expr.Range | ||
| NewText = newText } |] | ||
| Kind = FixKind.Fix } ] | ||
| }) | ||
This file contains hidden or 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.IgnoreExpression | ||
|
|
||
| open FsAutoComplete.CodeFix.Types | ||
|
|
||
| val title: string | ||
| val fix: getParseResultsForFile: GetParseResultsForFile -> CodeFix |
This file contains hidden or 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
75 changes: 75 additions & 0 deletions
75
test/FsAutoComplete.Tests.Lsp/CodeFixTests/IgnoreExpressionTests.fs
This file contains hidden or 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,75 @@ | ||
| module private FsAutoComplete.Tests.CodeFixTests.IgnoreExpressionTests | ||
|
|
||
| open Expecto | ||
| open Helpers | ||
| open Utils.ServerTests | ||
| open Utils.CursorbasedTests | ||
| open FsAutoComplete.CodeFix | ||
|
|
||
| let tests state = | ||
| serverTestList (nameof IgnoreExpression) state defaultConfigDto None (fun server -> | ||
| [ let selectCodeFix = CodeFix.withTitle IgnoreExpression.title | ||
|
|
||
| testCaseAsync "ignore constant" | ||
| <| CodeFix.check | ||
| server | ||
| " | ||
| let a b = | ||
| 9$0 | ||
| null" | ||
| Diagnostics.acceptAll | ||
| selectCodeFix | ||
| " | ||
| let a b = | ||
| 9 |> ignore | ||
|
nojaf marked this conversation as resolved.
|
||
| null" | ||
|
|
||
| testCaseAsync "ignore infix application" | ||
| <| CodeFix.check | ||
| server | ||
| " | ||
| let a b = | ||
| 0 / 9$0 | ||
| null" | ||
| Diagnostics.acceptAll | ||
| selectCodeFix | ||
| " | ||
| let a b = | ||
| 0 / 9 |> ignore | ||
| null" | ||
|
|
||
| testCaseAsync "ignore member invocation" | ||
| <| CodeFix.check | ||
| server | ||
| " | ||
| open System.Collections.Generic | ||
|
|
||
| let foo () = | ||
| let dict = dict [] | ||
| di$0ct.TryAdd(\"foo\", \"bar\") | ||
| ()" | ||
| Diagnostics.acceptAll | ||
| selectCodeFix | ||
| " | ||
| open System.Collections.Generic | ||
|
|
||
| let foo () = | ||
| let dict = dict [] | ||
| dict.TryAdd(\"foo\", \"bar\") |> ignore | ||
| ()" | ||
|
|
||
| testCaseAsync "ignore tuple" | ||
| <| CodeFix.check | ||
| server | ||
| " | ||
| let _ = | ||
| 1, 2$0 | ||
| null" | ||
| Diagnostics.acceptAll | ||
| selectCodeFix | ||
| " | ||
| let _ = | ||
| (1, 2) |> ignore | ||
| null" | ||
|
|
||
| ]) | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.