Skip to content

Commit 34c331e

Browse files
xperiandriclaude
andcommitted
Take the line break from the line above when the last line has none
The open goes in front of a line, and the last line of a file often ends without a line break. The file's own line ending is then on the line above, so a file with LF endings no longer gets CRLF from the host. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 777c4bd commit 34c331e

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎vsintegration/src/FSharp.Editor/Common/RoslynHelpers.fs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,14 @@ module internal OpenDeclarationHelper =
247247
let pos = ParsedInput.AdjustInsertionPoint getLineStr ctx
248248
let line = sourceText.Lines[min (Line.toZ pos.Line) (sourceText.Lines.Count - 1)]
249249

250-
// Follow the line endings the file itself uses rather than assuming the host's.
250+
let lineBreakOf (line: TextLine) =
251+
sourceText.ToString(TextSpan.FromBounds(line.End, line.EndIncludingLineBreak))
252+
253+
// Follow the line endings the file itself uses rather than assuming the host's. Only the last
254+
// line can lack a line break, and every line above it has one.
251255
let lineBreak =
252-
match line.Text.ToString(TextSpan(line.End, line.EndIncludingLineBreak - line.End)) with
256+
match lineBreakOf line with
257+
| "" when line.LineNumber > 0 -> lineBreakOf sourceText.Lines[line.LineNumber - 1]
253258
| "" -> Environment.NewLine
254259
| breakChars -> breakChars
255260

‎vsintegration/tests/FSharp.Editor.Tests/CodeFixes/AddOpenOnTopOnTests.fs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,21 @@ Console.WriteLine 42
200200

201201
Assert.Equal(expected, actual)
202202

203+
[<Fact>]
204+
let ``Fixes FS0039 for missing opens - the open goes before a last line with no line break`` () =
205+
let code = "module Module1\nConsole.WriteLine 42"
206+
207+
let expected =
208+
Some
209+
{
210+
Message = "open System"
211+
FixedCode = "module Module1\n\nopen System\n\nConsole.WriteLine 42"
212+
}
213+
214+
let actual = codeFix |> tryFix code Auto
215+
216+
Assert.Equal(expected, actual)
217+
203218
[<Fact>]
204219
let ``Fixes FS0039 for missing opens - namespace without a blank line`` () =
205220
let code =

0 commit comments

Comments
 (0)