From 87747b1d62daa3ed3b26b39b20465b94b1c5d95b Mon Sep 17 00:00:00 2001 From: Brian Rourke Boll Date: Thu, 25 Jul 2024 11:25:28 -0400 Subject: [PATCH 1/3] Don't throw if error range extends to EOF --- .../ChangePrefixNegationToInfixSubtraction.fs | 32 ++++++++++--------- ...gePrefixNegationToInfixSubtractionTests.fs | 20 ++++++++++++ 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/vsintegration/src/FSharp.Editor/CodeFixes/ChangePrefixNegationToInfixSubtraction.fs b/vsintegration/src/FSharp.Editor/CodeFixes/ChangePrefixNegationToInfixSubtraction.fs index 87841d5d57b..ee8b8870538 100644 --- a/vsintegration/src/FSharp.Editor/CodeFixes/ChangePrefixNegationToInfixSubtraction.fs +++ b/vsintegration/src/FSharp.Editor/CodeFixes/ChangePrefixNegationToInfixSubtraction.fs @@ -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" @@ -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 } diff --git a/vsintegration/tests/FSharp.Editor.Tests/CodeFixes/ChangePrefixNegationToInfixSubtractionTests.fs b/vsintegration/tests/FSharp.Editor.Tests/CodeFixes/ChangePrefixNegationToInfixSubtractionTests.fs index 5245d8a3ae0..156bdabbc30 100644 --- a/vsintegration/tests/FSharp.Editor.Tests/CodeFixes/ChangePrefixNegationToInfixSubtractionTests.fs +++ b/vsintegration/tests/FSharp.Editor.Tests/CodeFixes/ChangePrefixNegationToInfixSubtractionTests.fs @@ -48,3 +48,23 @@ let x = 1 (+) 2 let actual = codeFix |> tryFix code Auto Assert.Equal(expected, actual) + +[] +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) From dcd7925438b1978c67cd5e7c1ff7a1cec702af80 Mon Sep 17 00:00:00 2001 From: Brian Rourke Boll Date: Thu, 25 Jul 2024 11:32:04 -0400 Subject: [PATCH 2/3] Add release notes entry --- docs/release-notes/.VisualStudio/17.12.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 docs/release-notes/.VisualStudio/17.12.md diff --git a/docs/release-notes/.VisualStudio/17.12.md b/docs/release-notes/.VisualStudio/17.12.md new file mode 100644 index 00000000000..6df973cf607 --- /dev/null +++ b/docs/release-notes/.VisualStudio/17.12.md @@ -0,0 +1,9 @@ +### 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 + +### Breaking Changes From 4e010cc764e3a718de016ec55949151a0341e71e Mon Sep 17 00:00:00 2001 From: Brian Rourke Boll Date: Thu, 25 Jul 2024 11:41:18 -0400 Subject: [PATCH 3/3] Indent --- .../ChangePrefixNegationToInfixSubtractionTests.fs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/vsintegration/tests/FSharp.Editor.Tests/CodeFixes/ChangePrefixNegationToInfixSubtractionTests.fs b/vsintegration/tests/FSharp.Editor.Tests/CodeFixes/ChangePrefixNegationToInfixSubtractionTests.fs index 156bdabbc30..b93055a3a90 100644 --- a/vsintegration/tests/FSharp.Editor.Tests/CodeFixes/ChangePrefixNegationToInfixSubtractionTests.fs +++ b/vsintegration/tests/FSharp.Editor.Tests/CodeFixes/ChangePrefixNegationToInfixSubtractionTests.fs @@ -54,11 +54,11 @@ 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 + module FSharp = + module Core = + module LanguagePrimitives = + module IntrinsicFunctions = + let GetArray2D _ _ = 99 let a = Array2D.init 10 10 (+) a[5, 5]"""