Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
dc50d93
Slice source text instead of copying it line by line for outlining
xperiandri Sep 3, 2026
282c97f
Track comment lines by number instead of storing their text
xperiandri Sep 3, 2026
314ef9e
Link the outlining release note to its PR
xperiandri Sep 3, 2026
1f05242
Format illib.fsi and add the VisualStudio release note
xperiandri Sep 3, 2026
a9dcfd8
Scope the System.Memory pin to the net472 test build
xperiandri Sep 3, 2026
74b65c1
Address review: XML doc structure and shorthand lambda
xperiandri Sep 3, 2026
6454dee
Drop the span IndexOfOrdinal overloads that take a start index
xperiandri Sep 4, 2026
c80ab0a
Report an absolute index from the span IndexOfOrdinal overloads
xperiandri Sep 4, 2026
5430f55
Wrap the IndexOfOrdinal startIndex overloads' doc text in summary
xperiandri Sep 4, 2026
8322e51
Use a struct tuple for the comment-scan accumulator
xperiandri Sep 4, 2026
d75d5ee
Keep getOutliningRanges taking string[] and add a slice entry beside it
xperiandri Sep 9, 2026
04799c0
Keep only the span StartsWithOrdinal the outlining scan calls, and mo…
xperiandri Sep 29, 2026
f983cc4
Cover the outlining scan's slice entry point with a parity test
xperiandri Oct 11, 2026
dc81e75
Drop the outlining note from the shipped 11.0.100 release notes
xperiandri Oct 11, 2026
38e1298
Remove the comments the outlining slices added
xperiandri Nov 2, 2026
bcfe441
Cover GetLinesAsMemory with a test
xperiandri Nov 2, 2026
aa2a9d8
Slice a pooled copy of the text for outlining instead of one big string
xperiandri Nov 2, 2026
6138028
Compare outlining ranges with collection asserts
xperiandri Nov 5, 2026
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
4 changes: 4 additions & 0 deletions docs/release-notes/.FSharp.Compiler.Service/11.0.200.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
* Transparent compiler: with `enablePartialTypeChecking`, a project that is only referenced by another project is type checked from its signatures. Implementation files that have a signature file are no longer parsed or type checked to produce the referenced assembly. `ParseAndCheckProject` still checks all files. ([PR #20703](https://github.com/dotnet/fsharp/pull/20703))
* With `enablePartialTypeChecking`, a project that is only referenced by another project no longer type checks its implementation files that have a signature file. Producing the referenced assembly only needs the signatures, but finalizing the project also collected the diagnostics of the skipped files, which type checked all of them and kept their results in memory. `ParseAndCheckProject` still reports the diagnostics of these files. ([PR #20699](https://github.com/dotnet/fsharp/pull/20699))

### Improved

* `FSharp.Compiler.EditorServices.Structure.getOutliningRanges` scans the source lines without building a trimmed string per comment line, and no longer stores the text of a comment group next to its line numbers. ([PR #20443](https://github.com/dotnet/fsharp/pull/20443))

### Changed

* `FSharpProjectSnapshot`: source files and referenced projects are arrays instead of lists, removing redundant conversions. ([PR #20875](https://github.com/dotnet/fsharp/pull/20875))
Expand Down
1 change: 1 addition & 0 deletions docs/release-notes/.VisualStudio/18.vNext.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@
* The VS project options reactor keeps single-file and script project options in a named record instead of a five-place tuple, and its text-view subscription plumbing uses `voption` instead of `option`. ([PR #20455](https://github.com/dotnet/fsharp/pull/20455))
* The Debug build no longer exports OpenTelemetry unless `FSHARP_OTEL_EXPORT` asks it to (the variable can also carry the collector endpoint), so the default `VisualFSharpDebug` launch profile no longer stalls or retries against a missing collector; the new `OTEL Export` profile turns it back on. ([PR #20468](https://github.com/dotnet/fsharp/pull/20468))
* `FSharpFindUsagesService.onSymbolFound` looks up a reference's definition item with `voption` instead of `option`. No functional change. ([PR #20534](https://github.com/dotnet/fsharp/pull/20534))
* Reduce editor allocations when computing block structure (code folding) by slicing the source text once per outlining pass instead of copying every line. ([PR #20443](https://github.com/dotnet/fsharp/pull/20443))
79 changes: 39 additions & 40 deletions src/Compiler/Service/ServiceStructure.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FSharp.Compiler.EditorServices

open System
open Internal.Utilities.Library
open FSharp.Compiler.Syntax
open FSharp.Compiler.SyntaxTreeOps
Expand Down Expand Up @@ -186,27 +187,30 @@ module Structure =
}

type LineNumber = int
type LineStr = string

type CommentType =
| SingleLine
| XmlDoc

let commentTypeOf (line: ReadOnlySpan<char>) =
if line.StartsWithOrdinal("///") then ValueSome XmlDoc
elif line.StartsWithOrdinal("//") then ValueSome SingleLine
else ValueNone

[<NoComparison>]
type CommentList =
{
Lines: ResizeArray<LineNumber * LineStr>
Lines: ResizeArray<LineNumber>
Type: CommentType
}

static member New ty lineStr =
static member New ty lineNum =
{
Type = ty
Lines = ResizeArray [ lineStr ]
Lines = ResizeArray [ lineNum ]
}

/// Returns outlining ranges for given parsed input.
let getOutliningRanges (sourceLines: string[]) (parsedInput: ParsedInput) =
let getOutliningRangesFromLineSlices (sourceLines: ReadOnlyMemory<char>[]) (parsedInput: ParsedInput) =
let acc = ResizeArray()

/// Validation function to ensure that ranges yielded for outlining span 2 or more lines
Expand Down Expand Up @@ -661,7 +665,7 @@ module Structure =
| r :: rest, last :: _ when
r.StartLine = last.EndLine + 1
|| sourceLines[last.EndLine .. r.StartLine - 2]
|> Array.forall System.String.IsNullOrWhiteSpace
|> Array.forall _.Span.IsWhiteSpace()
->
loop rest res (r :: currentBulk)
| r :: rest, _ -> loop rest (currentBulk :: res) [ r ]
Expand Down Expand Up @@ -719,7 +723,7 @@ module Structure =

let collectConditionalDirectives directives sourceLines =
// Adds a fold region from prevRange.Start to the line above nextLine
let addSectionFold (prevRange: range) (nextLine: int) (sourceLines: string array) =
let addSectionFold (prevRange: range) (nextLine: int) (sourceLines: ReadOnlyMemory<char>[]) =
let startLineIndex = nextLine - 2

if startLineIndex >= 0 then
Expand Down Expand Up @@ -753,7 +757,7 @@ module Structure =
| ConditionalDirectiveTrivia.Else r -> ValueSome r
| _ -> ValueNone

let rec group directives stack (sourceLines: string array) =
let rec group directives stack (sourceLines: ReadOnlyMemory<char>[]) =
match directives with
| [] -> ()
| ConditionalDirectiveTrivia.If _ as ifDirective :: directives -> group directives (ifDirective :: stack) sourceLines
Expand Down Expand Up @@ -822,50 +826,42 @@ module Structure =
collectOpens decls
List.iter parseDeclaration decls

/// Determine if a line is a single line or xml documentation comment
let (|Comment|_|) (line: string) =
if line.StartsWithOrdinal("///") then Some XmlDoc
elif line.StartsWithOrdinal("//") then Some SingleLine
else None

let getCommentRanges trivia (lines: string[]) =
let rec loop (lastLineNum, currentComment, result as state) (lines: string list) lineNum =
match lines with
| [] -> state
| lineStr :: rest ->
match lineStr.TrimStart(), currentComment with
| Comment commentType, Some comment ->
let getCommentRanges trivia (lines: ReadOnlyMemory<char>[]) =
let rec loop (struct (lastLineNum, currentComment, result) as state) lineNum =
if lineNum = lines.Length then
state
else
match commentTypeOf (lines[lineNum].Span.TrimStart()), currentComment with
| ValueSome commentType, Some comment ->
loop
(if comment.Type = commentType && lineNum = lastLineNum + 1 then
comment.Lines.Add(lineNum, lineStr)
lineNum, currentComment, result
comment.Lines.Add lineNum
struct (lineNum, currentComment, result)
else
let comments = CommentList.New commentType (lineNum, lineStr)
lineNum, Some comments, comment :: result)
rest
let comments = CommentList.New commentType lineNum
struct (lineNum, Some comments, comment :: result))
(lineNum + 1)
| Comment commentType, None ->
let comments = CommentList.New commentType (lineNum, lineStr)
loop (lineNum, Some comments, result) rest (lineNum + 1)
| _, Some comment -> loop (lineNum, None, comment :: result) rest (lineNum + 1)
| _ -> loop (lineNum, None, result) rest (lineNum + 1)
| ValueSome commentType, None ->
let comments = CommentList.New commentType lineNum
loop (struct (lineNum, Some comments, result)) (lineNum + 1)
| ValueNone, Some comment -> loop (struct (lineNum, None, comment :: result)) (lineNum + 1)
| ValueNone, None -> loop (struct (lineNum, None, result)) (lineNum + 1)

let comments =
let _, lastComment, comments = loop (-1, None, []) (List.ofArray lines) 0
let struct (_, lastComment, comments) = loop (struct (-1, None, [])) 0

match lastComment with
| Some comment -> comment :: comments
| _ -> comments
|> List.rev

comments
|> List.filter (fun comment -> comment.Lines.Count > 1)
|> List.map (fun comment ->
let lines = comment.Lines
let startLine, startStr = lines[0]
let endLine, endStr = lines[lines.Count - 1]
let startCol = startStr.IndexOf '/'
let endCol = endStr.TrimEnd().Length
|> Seq.filter (fun comment -> comment.Lines.Count > 1)
|> Seq.map (fun comment ->
let startLine = comment.Lines[0]
let endLine = comment.Lines[comment.Lines.Count - 1]
let startCol = lines[startLine].Span.IndexOf '/'
let endCol = lines[endLine].Span.TrimEnd().Length

let scopeType =
match comment.Type with
Expand Down Expand Up @@ -1104,3 +1100,6 @@ module Structure =
getCommentRanges file.Trivia.CodeComments sourceLines

acc :> seq<_>

let getOutliningRanges (sourceLines: string[]) (parsedInput: ParsedInput) =
getOutliningRangesFromLineSlices (sourceLines |> Array.map _.AsMemory()) parsedInput
5 changes: 5 additions & 0 deletions src/Compiler/Service/ServiceStructure.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FSharp.Compiler.EditorServices

open System
open FSharp.Compiler.Syntax
open FSharp.Compiler.Text

Expand Down Expand Up @@ -80,3 +81,7 @@ module public Structure =

/// Returns outlining ranges for given parsed input.
val getOutliningRanges: sourceLines: string[] -> parsedInput: ParsedInput -> seq<ScopeRange>

/// Like getOutliningRanges, with the source lines given as slices of text the caller already holds.
val internal getOutliningRangesFromLineSlices:
sourceLines: ReadOnlyMemory<char>[] -> parsedInput: ParsedInput -> seq<ScopeRange>
Comment on lines +86 to +87

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 20b6c0e: Line slices of one string give the same ranges as a string per line in StructureTests.fs cuts one string into non-zero-offset slices (mixed \r\n and \n, a whitespace-only line between two opens, indented comment groups with trailing whitespace, a directive section whose last line ends in whitespace), passes them to getOutliningRangesFromLineSlices and compares every range with getOutliningRanges over a string per line. It also pins the ranges that are read off the line text (the open group, both directive sections, the comment and the doc comment group).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The editor entry point has its own test now. WithLinesAsMemory slices the same lines as Lines in RoslynSourceTextTests.fs compares its slices with SourceText.Lines for \n, \r\n and \r line breaks, an empty text and a lone \r\n. (The entry point was GetLinesAsMemory when you wrote this; it is a callback API now.)

StructureTests.fs also has Outlining ranges do not read the line slices once the call has returned, which overwrites the buffer behind the slices after the call, because the editor gives the buffer back to a pool.

The measurement on a real VS snapshot is in the thread on Extensions.fs: #20443 (comment)

6 changes: 6 additions & 0 deletions src/Compiler/Utilities/illib.fs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ module internal PervasiveAutoOpens =
member inline x.IndexOfOrdinal(value: string, startIndex, count) =
x.IndexOf(value, startIndex, count, StringComparison.Ordinal)

[<AbstractClass; Sealed>]
type ReadOnlySpanCharExtensions =
Comment thread
T-Gro marked this conversation as resolved.
Resolved

static member inline StartsWithOrdinal(str: ReadOnlySpan<char>, value: string) =
str.StartsWith(value.AsSpan(), StringComparison.Ordinal)

/// Get an initialization hole
let getHole (r: _ ref) =
match r.Value with
Expand Down
6 changes: 6 additions & 0 deletions src/Compiler/Utilities/illib.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ module internal PervasiveAutoOpens =

member inline IndexOfOrdinal: value: string * startIndex: int * count: int -> int

[<AbstractClass; Sealed; Extension>]
type ReadOnlySpanCharExtensions =

[<Extension>]
static member inline StartsWithOrdinal: str: ReadOnlySpan<char> * value: string -> bool

type Async with

/// Runs the computation synchronously, always starting on the current thread.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,12 @@
<ProjectReference Include="..\service\data\CSharp_Analysis\CSharp_Analysis.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
<!-- FSharp.Compiler.Service's PackageReference to System.Memory (for ReadOnlySpan/ReadOnlyMemory) is only
transitive here, so the SetTargetFramework override above does not carry it in on net472; pin it directly.
Not needed on the .NET Core inner build: there System.Memory ships with the framework and NuGet's
package-pruning check (NU1510) errors on an explicit reference to it. -->
<PackageReference Include="System.Memory" />
</ItemGroup>

</Project>
84 changes: 84 additions & 0 deletions tests/FSharp.Compiler.Service.Tests/StructureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -686,3 +686,87 @@ let x = 4
(6, 0, 7, 9), (6, 0, 7, 9)
(8, 0, 10, 6), (8, 0, 10, 6)
]

let private sliceLinesOfOneBuffer (buffer: char[]) =
[| let mutable start = 0
while start <= buffer.Length do
let lineEnd =
match System.Array.IndexOf(buffer, '\n', start) with
| -1 -> buffer.Length
| lineBreak -> lineBreak
let length =
if lineEnd > start && buffer[lineEnd - 1] = '\r' then lineEnd - start - 1 else lineEnd - start
yield System.ReadOnlyMemory<char>(buffer, start, length)
start <- lineEnd + 1 |]

[<Fact>]
let ``Line slices of one string give the same ranges as a string per line``() =
let source =
seq {
"module M\r\n"
"open System\n"
" \t \r\n"
"open System.IO\n"
"\r\n"
" // an indented comment \r\n"
" // its second line \t \n"
"\n"
"/// Doc, first line\r\n"
"/// second line \n"
"let f x =\r\n"
" x\n"
"\r\n"
"#if DEBUG\n"
"let g = 1 \r\n"
"let h = 2\t \n"
"#else\r\n"
"let g = 2\n"
"let h = 3 \r\n"
"#endif\n"
}
|> String.concat ""

let slices = sliceLinesOfOneBuffer (source.ToCharArray())
let ast = parseSourceCode(fileName, source)

let describe (scopeRanges: seq<ScopeRange>) =
let getRange (r: range) = (r.StartLine, r.StartColumn, r.EndLine, r.EndColumn)
[| for sr in scopeRanges -> sr.Scope, sr.Collapse, getRange sr.Range, getRange sr.CollapseRange |]

let fromSlices = getOutliningRangesFromLineSlices slices ast |> describe
let fromStrings = getOutliningRanges (slices |> Array.map string) ast |> describe

Assert.Equal<(Scope * Collapse * Range * Range)>(fromStrings, fromSlices)

let readOffTheText =
[| for scope, _, range, _ in fromSlices do
match scope with
| Scope.Open | Scope.Comment | Scope.XmlDocComment | Scope.HashDirective -> yield scope, range
| _ -> () |]

Assert.Equal<(Scope * Range)>(
[| Scope.Open, (2, 0, 4, 14)
Scope.HashDirective, (14, 0, 16, 11)
Scope.HashDirective, (17, 0, 20, 6)
Scope.Comment, (6, 4, 7, 22)
Scope.XmlDocComment, (9, 0, 10, 15) |],
readOffTheText)
Comment thread
xperiandri marked this conversation as resolved.
Resolved

[<Fact>]
let ``Outlining ranges do not read the line slices once the call has returned``() =
let source = "module M\nopen A\nopen B\n// one\n// two\n#if X\nlet a = 1\n#else\nlet a = 2\n#endif\n"
let buffer = source.ToCharArray()
let ast = parseSourceCode(fileName, source)

let ranges = getOutliningRangesFromLineSlices (sliceLinesOfOneBuffer buffer) ast
Array.fill buffer 0 buffer.Length 'x'

Assert.Equal<(Scope * int * int)>(
[| Scope.Open, 2, 3
Scope.HashDirective, 6, 7
Scope.HashDirective, 8, 10
Scope.Comment, 4, 5 |],
[| for r in ranges do
match r.Scope with
| Scope.Open | Scope.Comment | Scope.HashDirective -> yield r.Scope, r.Range.StartLine, r.Range.EndLine
| _ -> () |])
17 changes: 17 additions & 0 deletions vsintegration/src/FSharp.Editor/Common/Extensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module internal Microsoft.VisualStudio.FSharp.Editor.Extensions

open System
open System.Buffers
open System.IO
open System.Collections.Immutable
open System.Collections.Generic
Expand Down Expand Up @@ -296,6 +297,22 @@ type SourceText with
member this.ToFSharpSourceText() =
SourceText.weakTable.GetValue(this, Runtime.CompilerServices.ConditionalWeakTable<_, _>.CreateValueCallback(SourceText.create))

/// The slices point into a pooled copy of the text and are valid only until the callback returns.
member this.WithLinesAsMemory(f: ReadOnlyMemory<char>[] -> 'T) =
let buffer = ArrayPool<char>.Shared.Rent this.Length

try
this.CopyTo(0, buffer, 0, this.Length)

let lines =
Array.init this.Lines.Count (fun i ->
let line = this.Lines[i]
ReadOnlyMemory<char>(buffer, line.Start, line.End - line.Start))

f lines
finally
ArrayPool<char>.Shared.Return buffer

type NavigationItem with

member x.RoslynGlyph: FSharpRoslynGlyph =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,8 @@ module internal BlockStructure =
let ellipsis = "..."

let createBlockSpans isBlockStructureEnabled (sourceText: SourceText) (parsedInput: ParsedInput) =
let linetext = sourceText.Lines |> Seq.map (fun x -> x.ToString()) |> Seq.toArray

Structure.getOutliningRanges linetext parsedInput
|> Seq.distinctBy (fun x -> x.Range.StartLine)
sourceText.WithLinesAsMemory(fun lines -> Structure.getOutliningRangesFromLineSlices lines parsedInput)
|> Seq.distinctBy _.Range.StartLine
|> Seq.chooseV (fun scopeRange ->
// the range of text to collapse
let textSpan =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ open Microsoft.CodeAnalysis.Text

module RoslynSourceTextTests =

[<Theory>]
[<InlineData("test\ntest2\r\ntest3\n\ntest4\ntest5\rtest6\n")>]
[<InlineData("")>]
[<InlineData("\r\n")>]
let ``WithLinesAsMemory slices the same lines as Lines`` (text: string) =
let sourceText = SourceText.From text

let slices = sourceText.WithLinesAsMemory(Array.map string)
let lines = sourceText.Lines |> Seq.map string |> Seq.toArray

Assert.Equal<string>(lines, slices)

[<Fact>]
let SourceText () =
let text = "test\ntest2\r\ntest3\n\ntest4\ntest5\rtest6\n"
Expand Down
Loading