Skip to content

Prefix-to-infix code fix: don't throw if error range extends to EOF #17448

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 5 commits into from
Jul 27, 2024
Merged
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
2 changes: 2 additions & 0 deletions docs/release-notes/.VisualStudio/17.12.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
### Fixed

* In the prefix-to-infix code fix, don't throw an exception if the error range for FS0003 extends through the end of the source text. ([PR #17448](https://github.com/dotnet/fsharp/pull/17448))

### Added

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ type internal ChangePrefixNegationToInfixSubtractionCodeFixProvider() =
static let title = SR.ChangePrefixNegationToInfixSubtraction()

static let rec findNextNonWhitespacePos (sourceText: SourceText) pos =
if pos < sourceText.Length && Char.IsWhiteSpace sourceText[pos] then
findNextNonWhitespacePos sourceText (pos + 1)
if pos < sourceText.Length - 1 then
if Char.IsWhiteSpace sourceText[pos] then
findNextNonWhitespacePos sourceText (pos + 1)
else
ValueSome pos
else
pos
ValueNone

override _.FixableDiagnosticIds = ImmutableArray.Create "FS0003"

Expand All @@ -34,16 +37,15 @@ type internal ChangePrefixNegationToInfixSubtractionCodeFixProvider() =

// in a line like "... x -1 ...",
// squiggly goes for "x", not for "-", hence we search for "-"
let pos = findNextNonWhitespacePos sourceText (context.Span.End + 1)

if sourceText[pos] <> '-' then
return ValueNone
else
return
ValueSome
{
Name = CodeFix.ChangePrefixNegationToInfixSubtraction
Message = title
Changes = [ TextChange(TextSpan(pos + 1, 0), " ") ]
}
let fix =
findNextNonWhitespacePos sourceText (context.Span.End + 1)
|> ValueOption.filter (fun pos -> sourceText[pos] = '-')
|> ValueOption.map (fun pos ->
{
Name = CodeFix.ChangePrefixNegationToInfixSubtraction
Message = title
Changes = [ TextChange(TextSpan(pos + 1, 0), " ") ]
})

return fix
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,23 @@ let x = 1 (+) 2
let actual = codeFix |> tryFix code Auto

Assert.Equal(expected, actual)

[<Fact>]
let ``Doesn't throw when the error range extends to the end of the source text`` () =
let code =
"""
module Microsoft =
module FSharp =
module Core =
module LanguagePrimitives =
module IntrinsicFunctions =
let GetArray2D _ _ = 99

let a = Array2D.init 10 10 (+)
a[5, 5]"""

let expected = None

let actual = codeFix |> tryFix code Auto

Assert.Equal(expected, actual)
Loading