-
Notifications
You must be signed in to change notification settings - Fork 830
Fixing code fixes, part 4 #15551
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
Fixing code fixes, part 4 #15551
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,40 +2,45 @@ | |
|
|
||
| namespace Microsoft.VisualStudio.FSharp.Editor | ||
|
|
||
| open System | ||
| open System.Composition | ||
| open System.Threading.Tasks | ||
| open System.Collections.Immutable | ||
| open System.Text.RegularExpressions | ||
|
|
||
| open Microsoft.CodeAnalysis.Text | ||
| open Microsoft.CodeAnalysis.CodeFixes | ||
|
|
||
| open CancellableTasks | ||
|
|
||
| [<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name = CodeFix.ChangePrefixNegationToInfixSubtraction); Shared>] | ||
| type internal ChangePrefixNegationToInfixSubtractionodeFixProvider() = | ||
| type internal ChangePrefixNegationToInfixSubtractionCodeFixProvider() = | ||
| inherit CodeFixProvider() | ||
|
|
||
| static let title = SR.ChangePrefixNegationToInfixSubtraction() | ||
|
|
||
| override _.FixableDiagnosticIds = ImmutableArray.Create("FS0003") | ||
|
|
||
| override _.RegisterCodeFixesAsync context : Task = | ||
| asyncMaybe { | ||
| let! sourceText = context.Document.GetTextAsync(context.CancellationToken) | ||
|
|
||
| let mutable pos = context.Span.End + 1 | ||
|
|
||
| // This won't ever actually happen, but eh why not | ||
| do! Option.guard (pos < sourceText.Length) | ||
|
|
||
| let mutable ch = sourceText.[pos] | ||
|
|
||
| while pos < sourceText.Length && Char.IsWhiteSpace(ch) do | ||
| pos <- pos + 1 | ||
| ch <- sourceText.[pos] | ||
|
|
||
| // Bail if this isn't a negation | ||
| do! Option.guard (ch = '-') | ||
| do context.RegisterFsharpFix(CodeFix.ChangePrefixNegationToInfixSubtraction, title, [| TextChange(TextSpan(pos, 1), "- ") |]) | ||
| } | ||
| |> Async.Ignore | ||
| |> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken) | ||
| override _.FixableDiagnosticIds = ImmutableArray.Create "FS0003" | ||
|
|
||
| override this.RegisterCodeFixesAsync context = context.RegisterFsharpFix this | ||
|
|
||
| interface IFSharpCodeFixProvider with | ||
| member _.GetCodeFixIfAppliesAsync context = | ||
| cancellableTask { | ||
| let! sourceText = context.GetSourceTextAsync() | ||
|
|
||
| // in a line like "... x -1 ...", | ||
| // squiggly goes for "x", not for "-", hence we search for "-" | ||
| let remainingText = $"{sourceText.GetSubText(context.Span.End)}" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. $"{}" should never be a replacement for .ToString() in such case.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correction, even worse I guess - the previous code was carefully reading one character at a time from the Roslyn's SourceText.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Readability is debatable, performance - I guess not. I will fix this in the followup soon! |
||
| let pattern = @"^\s+(-)" | ||
|
|
||
| match Regex.Match(remainingText, pattern) with | ||
| | m when m.Success -> | ||
| let spacePlace = context.Span.End + m.Groups[1].Index + 1 | ||
|
|
||
| return | ||
| Some | ||
| { | ||
| Name = CodeFix.ChangePrefixNegationToInfixSubtraction | ||
| Message = title | ||
| Changes = [ TextChange(TextSpan(spacePlace, 0), " ") ] | ||
| } | ||
| | _ -> return None | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.