-
Notifications
You must be signed in to change notification settings - Fork 156
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 unnecessary parentheses analyzer & code fix #1235
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
93deeef
Add unnecessary parens analyzer & codefix
brianrourkeboll 856fafb
Comments
brianrourkeboll 77dff44
Use nicer APIs
brianrourkeboll 9cac017
Fix
brianrourkeboll 9620231
Better there
brianrourkeboll cee0e21
Fmt
brianrourkeboll b1511ea
Disambiguate AsSpan overload
brianrourkeboll d5bd96f
Remove debug fail
brianrourkeboll 7ed9f2e
Remove redundant diag filter
brianrourkeboll ea8e756
Merge branch 'main' of https://github.com/fsharp/FsAutoComplete into …
brianrourkeboll 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 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
166 changes: 166 additions & 0 deletions
166
src/FsAutoComplete/CodeFixes/RemoveUnnecessaryParentheses.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,166 @@ | ||
module FsAutoComplete.CodeFix.RemoveUnnecessaryParentheses | ||
|
||
open System | ||
open Ionide.LanguageServerProtocol.Types | ||
open FsAutoComplete.CodeFix | ||
open FsAutoComplete.CodeFix.Types | ||
open FsToolkit.ErrorHandling | ||
open FsAutoComplete | ||
open FsAutoComplete.LspHelpers | ||
|
||
let title = "Remove unnecessary parentheses" | ||
|
||
[<AutoOpen>] | ||
module private Patterns = | ||
let inline toPat f x = if f x then ValueSome() else ValueNone | ||
|
||
[<AutoOpen>] | ||
module Char = | ||
[<return: Struct>] | ||
let inline (|LetterOrDigit|_|) c = toPat Char.IsLetterOrDigit c | ||
|
||
[<return: Struct>] | ||
let inline (|Punctuation|_|) c = toPat Char.IsPunctuation c | ||
|
||
[<return: Struct>] | ||
let inline (|Symbol|_|) c = toPat Char.IsSymbol c | ||
|
||
[<AutoOpen>] | ||
module SourceText = | ||
/// E.g., something like: | ||
/// | ||
/// let … = ( | ||
/// … | ||
/// ) | ||
[<return: Struct>] | ||
let (|TrailingOpen|_|) (range: FcsRange) (sourceText: IFSACSourceText) = | ||
match sourceText.GetLine range.Start with | ||
| Some line -> | ||
if | ||
line.AsSpan(0, range.Start.Column).LastIndexOfAnyExcept(' ', '(') >= 0 | ||
&& line.AsSpan(range.Start.Column).IndexOfAnyExcept('(', ' ') < 0 | ||
then | ||
ValueSome TrailingOpen | ||
else | ||
ValueNone | ||
|
||
| None -> ValueNone | ||
|
||
/// Trim only spaces from the start if there is something else | ||
/// before the open paren on the same line (or else we could move | ||
/// the whole inner expression up a line); otherwise trim all whitespace | ||
/// from start and end. | ||
let (|Trim|) (range: FcsRange) (sourceText: IFSACSourceText) = | ||
match sourceText.GetLine range.Start with | ||
| Some line -> | ||
if line.AsSpan(0, range.Start.Column).LastIndexOfAnyExcept(' ', '(') >= 0 then | ||
fun (s: string) -> s.TrimEnd().TrimStart ' ' | ||
else | ||
fun (s: string) -> s.Trim() | ||
|
||
| None -> id | ||
|
||
/// Returns the offsides diff if the given span contains an expression | ||
/// whose indentation would be made invalid if the open paren | ||
/// were removed (because the offside line would be shifted). | ||
[<return: Struct>] | ||
let (|OffsidesDiff|_|) (range: FcsRange) (sourceText: IFSACSourceText) = | ||
let startLineNo = range.StartLine | ||
let endLineNo = range.EndLine | ||
|
||
if startLineNo = endLineNo then | ||
ValueNone | ||
else | ||
let rec loop innerOffsides (pos: FcsPos) (startCol: int) = | ||
if pos.Line <= endLineNo then | ||
match sourceText.GetLine pos with | ||
| None -> ValueNone | ||
| Some line -> | ||
match line.AsSpan(startCol).IndexOfAnyExcept(' ', ')') with | ||
| -1 -> loop innerOffsides (pos.IncLine()) 0 | ||
| i -> loop (i + startCol) (pos.IncLine()) 0 | ||
else | ||
ValueSome(range.StartColumn - innerOffsides) | ||
|
||
loop range.StartColumn range.Start (range.StartColumn + 1) | ||
|
||
let (|ShiftLeft|NoShift|ShiftRight|) n = | ||
if n < 0 then ShiftLeft -n | ||
elif n = 0 then NoShift | ||
else ShiftRight n | ||
|
||
/// A codefix that removes unnecessary parentheses from the source. | ||
let fix (getFileLines: GetFileLines) : CodeFix = | ||
Run.ifDiagnosticByCode (Set.singleton "FSAC0004") (fun d codeActionParams -> | ||
asyncResult { | ||
let fileName = codeActionParams.TextDocument.GetFilePath() |> normalizePath | ||
let range = protocolRangeToRange (string fileName) d.Range | ||
|
||
let! sourceText = getFileLines fileName | ||
let! txt = sourceText.GetText range | ||
|
||
let firstChar = txt[0] | ||
let lastChar = txt[txt.Length - 1] | ||
|
||
match firstChar, lastChar with | ||
| '(', ')' -> | ||
let adjusted = | ||
match sourceText with | ||
| TrailingOpen range -> txt[1 .. txt.Length - 2].TrimEnd() | ||
|
||
| Trim range trim & OffsidesDiff range spaces -> | ||
match spaces with | ||
| NoShift -> trim txt[1 .. txt.Length - 2] | ||
| ShiftLeft spaces -> trim (txt[1 .. txt.Length - 2].Replace("\n" + String(' ', spaces), "\n")) | ||
| ShiftRight spaces -> trim (txt[1 .. txt.Length - 2].Replace("\n", "\n" + String(' ', spaces))) | ||
|
||
| _ -> txt[1 .. txt.Length - 2].Trim() | ||
|
||
let newText = | ||
let (|ShouldPutSpaceBefore|_|) (s: string) = | ||
// ……(……) | ||
// ↑↑ ↑ | ||
(sourceText.TryGetChar(range.Start.IncColumn -1), sourceText.TryGetChar range.Start) | ||
||> Option.map2 (fun twoBefore oneBefore -> | ||
match twoBefore, oneBefore, s[0] with | ||
| _, _, ('\n' | '\r') -> None | ||
| '[', '|', (Punctuation | LetterOrDigit) -> None | ||
| _, '[', '<' -> Some ShouldPutSpaceBefore | ||
| _, ('(' | '[' | '{'), _ -> None | ||
| _, '>', _ -> Some ShouldPutSpaceBefore | ||
| ' ', '=', _ -> Some ShouldPutSpaceBefore | ||
| _, '=', ('(' | '[' | '{') -> None | ||
| _, '=', (Punctuation | Symbol) -> Some ShouldPutSpaceBefore | ||
| _, LetterOrDigit, '(' -> None | ||
| _, (LetterOrDigit | '`'), _ -> Some ShouldPutSpaceBefore | ||
| _, (Punctuation | Symbol), (Punctuation | Symbol) -> Some ShouldPutSpaceBefore | ||
| _ -> None) | ||
|> Option.flatten | ||
|
||
let (|ShouldPutSpaceAfter|_|) (s: string) = | ||
// (……)… | ||
// ↑ ↑ | ||
sourceText.TryGetChar(range.End.IncColumn 1) | ||
|> Option.bind (fun endChar -> | ||
match s[s.Length - 1], endChar with | ||
| '>', ('|' | ']') -> Some ShouldPutSpaceAfter | ||
| _, (')' | ']' | '[' | '}' | '.' | ';' | ',' | '|') -> None | ||
| (Punctuation | Symbol), (Punctuation | Symbol | LetterOrDigit) -> Some ShouldPutSpaceAfter | ||
| LetterOrDigit, LetterOrDigit -> Some ShouldPutSpaceAfter | ||
| _ -> None) | ||
|
||
match adjusted with | ||
| ShouldPutSpaceBefore & ShouldPutSpaceAfter -> " " + adjusted + " " | ||
| ShouldPutSpaceBefore -> " " + adjusted | ||
| ShouldPutSpaceAfter -> adjusted + " " | ||
| adjusted -> adjusted | ||
|
||
return | ||
[ { Edits = [| { Range = d.Range; NewText = newText } |] | ||
File = codeActionParams.TextDocument | ||
Title = title | ||
SourceDiagnostic = Some d | ||
Kind = FixKind.Fix } ] | ||
|
||
| _notParens -> return [] | ||
}) |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Check notice
Code scanning / Ionide.Analyzers.Cli
Detect if generic type should be in the postfix position. Note