Skip to content
Draft
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Compiler.Service/11.0.200.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Runtime-async sequences allow awaits inside `try/with` bodies, filters and handlers, and reject overlapping `MoveNextAsync` calls. ([PR #20655](https://github.com/dotnet/fsharp/pull/20655))
* F# Interactive gains a JSON-RPC server mode, `--fsi-server-jsonrpc:<pipe name>`, in which a host submits interactions over a named pipe and receives structured results — diagnostics with positions, escaping exceptions, the values each interaction bound, and the session's own process id — instead of recovering them by looking for a `SERVER-PROMPT>` marker in the output text. Program output continues to flow through the redirected console streams. The pipe admits only the user running the session; `--fsi-server-client-pid:<pid>` names the host process whose exit ends the session. `FsiEvaluationSession` exposes both options as `JsonRpcServerPipeName` and `JsonRpcClientProcessId`. The mode is part of the .NET fsi only. ([PR #20396](https://github.com/dotnet/fsharp/pull/20396))
* Include individual active-pattern cases from unopened modules in pattern completion. ([PR #20719](https://github.com/dotnet/fsharp/pull/20719))
* `NavigableItem` carries `ParameterCount` and `TypeParameterCount`, the counts C# and VB give Navigate To to order matches that are otherwise equal. ([PR #20531](https://github.com/dotnet/fsharp/pull/20531))

### Fixed

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 @@ -13,6 +13,7 @@
* Peek Definition on an F# symbol whose definition lives in metadata no longer deadlocks Visual Studio. Peek holds the main thread in `JoinableTaskFactory.Run` without pumping messages while it asks the language service for the definition, and generating the metadata document needs that same thread; Peek now stops at definitions that already have a document, and Go To Definition, which owns the wait it makes, still opens the generated one. ([PR #20503](https://github.com/dotnet/fsharp/pull/20503))
* Improve Find All References performance by throttling parallel typechecks. ([PR #20128](https://github.com/dotnet/fsharp/pull/20128), [PR #20695](https://github.com/dotnet/fsharp/pull/20695))
* Fix record-type Rename corrupting copy-and-update expressions, including from abstract signatures, while retaining Find All References results. ([Issue #20791](https://github.com/dotnet/fsharp/issues/20791), [PR #20797](https://github.com/dotnet/fsharp/pull/20797))
* Navigate To (Ctrl+T) orders F# matches of the same kind as it orders C# and Visual Basic ones: a match in the file being edited first, then by how far its folder is from that file, then by parameter and type parameter count and name; the part of the name that matched is highlighted, and a match in the name's own case ranks first. ([PR #20532](https://github.com/dotnet/fsharp/pull/20532))
* Fixed Rename incorrectly renaming `get` and `set` keywords for properties with explicit accessors. ([Issue #18270](https://github.com/dotnet/fsharp/issues/18270), [PR #19252](https://github.com/dotnet/fsharp/pull/19252))
* Fixed Find All References crash when F# project contains non-F# files like `.cshtml`. ([Issue #16394](https://github.com/dotnet/fsharp/issues/16394), [PR #19252](https://github.com/dotnet/fsharp/pull/19252))
* Avoid using `cancellableTask` in `DocumentCache`; the editor cache now uses direct `CancellationToken`-aware `task` wrappers, avoiding the background `Task.Run` offload and a larger wrapper closure from the `cancellableTask` builder. ([Issue #20268](https://github.com/dotnet/fsharp/issues/20268))
Expand Down
65 changes: 56 additions & 9 deletions src/Compiler/Service/ServiceNavigation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -764,10 +764,39 @@ type NavigableItem =
IsSignature: bool
Kind: NavigableItemKind
Container: NavigableContainer
ParameterCount: int
TypeParameterCount: int
}

[<RequireQualifiedAccess>]
module NavigateTo =
let private typeParameterCountOf (typars: SynTyparDecls option) =
match typars with
| Some typars -> typars.TyparDecls.Length
| None -> 0

let rec private isUnitType synType =
match synType with
| SynType.LongIdent(SynLongIdent([ id ], _, _)) -> id.idText = "unit"
| SynType.Paren(innerType, _)
| SynType.WithGlobalConstraints(innerType, _, _)
// `val f: u: unit -> int` names the argument the compiler drops, so the name must not hide it.
| SynType.SignatureParameter(usedType = innerType) -> isUnitType innerType
| _ -> false

/// The parameters of the compiled method: the parser leaves a solitary unit argument of a signature in its arity,
/// where a binding has already dropped it.
let private parameterCountOfSignature (SynValInfo(curriedArgInfos, _)) (synType: SynType) =
match curriedArgInfos, synType with
| [ [ _ ] ], SynType.Fun(argType = argType) when isUnitType argType -> 0
| [ [ _ ] ], SynType.WithGlobalConstraints(SynType.Fun(argType = argType), _, _) when isUnitType argType -> 0
| _ -> List.sumBy List.length curriedArgInfos

let private parameterCountOfBinding (SynValData(memberFlags = memberFlags; valInfo = SynValInfo(curriedArgInfos, _))) =
match memberFlags, curriedArgInfos with
| Some memberFlags, _self :: argInfos when memberFlags.IsInstance -> List.sumBy List.length argInfos
| _ -> List.sumBy List.length curriedArgInfos

let GetNavigableItems (parsedInput: ParsedInput) : NavigableItem[] =

let convertToDisplayName name =
Expand All @@ -778,7 +807,7 @@ module NavigateTo =

let result = ResizeArray()

let addLongIdent kind (lid: LongIdent) (isSignature: bool) (container: NavigableContainer) =
let addLongIdent kind (lid: LongIdent) (isSignature: bool) (container: NavigableContainer) typeParameterCount =
if not lid.IsEmpty then
let name = textOfLid lid

Expand All @@ -789,10 +818,12 @@ module NavigateTo =
IsSignature = isSignature
Kind = kind
Container = container
ParameterCount = 0
TypeParameterCount = typeParameterCount
}
|> result.Add

let addIdent kind (id: Ident) (isSignature: bool) (container: NavigableContainer) =
let addIdentWithArity kind (id: Ident) (isSignature: bool) (container: NavigableContainer) parameterCount typeParameterCount =
if not (String.IsNullOrEmpty id.idText) then
let name = convertToDisplayName id.idText

Expand All @@ -803,11 +834,16 @@ module NavigateTo =
IsSignature = isSignature
Kind = kind
Container = container
ParameterCount = parameterCount
TypeParameterCount = typeParameterCount
}
|> result.Add

let addIdent kind id isSignature container =
addIdentWithArity kind id isSignature container 0 0

let addModule lid isSig container =
addLongIdent NavigableItemKind.Module lid isSig container
addLongIdent NavigableItemKind.Module lid isSig container 0

let addModuleAbbreviation (id: Ident) isSig container =
addIdent NavigableItemKind.ModuleAbbreviation id isSig container
Expand All @@ -818,14 +854,17 @@ module NavigateTo =
NavigableContainer.Container(NavigableContainerType.Exception, [ id.idText ], container)

let addComponentInfo containerType kind (info: SynComponentInfo) isSig container =
let (SynComponentInfo(typeParams = typeParams)) = info
let lid = info.LongIdent
addLongIdent kind lid isSig container
addLongIdent kind lid isSig container (typeParameterCountOf typeParams)

NavigableContainer.Container(containerType, pathOfLid lid, container)

let addValSig kind synValSig isSig container =
let (SynValSig(ident = SynIdent(id, _))) = synValSig
addIdent kind id isSig container
let (SynValSig(ident = SynIdent(id, _); explicitTypeParams = SynValTyparDecls(typars, _); synType = synType; arity = arity)) =
synValSig

addIdentWithArity kind id isSig container (parameterCountOfSignature arity synType) (typeParameterCountOf typars)

let addField synField isSig container =
let (SynField(idOpt = id)) = synField
Expand Down Expand Up @@ -863,17 +902,25 @@ module NavigateTo =
| Some mf -> mapMemberKind mf.MemberKind
| _ -> NavigableItemKind.ModuleValue

let typeParameterCount =
match headPat with
| SynPat.LongIdent(typarDecls = Some(SynValTyparDecls(typars, _))) -> typeParameterCountOf typars
| _ -> 0

let addBindingIdent id =
addIdentWithArity kind id false container (parameterCountOfBinding valData) typeParameterCount

match headPat with
| SynPat.LongIdent(longDotId = SynLongIdent([ _; id ], _, _)) ->
// instance members
addIdent kind id false container
addBindingIdent id
| SynPat.LongIdent(longDotId = SynLongIdent([ id ], _, _)) ->
// functions
addIdent kind id false container
addBindingIdent id
| SynPat.Named(SynIdent(id, _), _, _, _)
| SynPat.As(_, SynPat.Named(SynIdent(id, _), _, _, _), _) ->
// values
addIdent kind id false container
addBindingIdent id
| _ -> ()

let addMember valSig (memberFlags: SynMemberFlags) isSig container =
Expand Down
20 changes: 14 additions & 6 deletions src/Compiler/Service/ServiceNavigation.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,20 @@ type NavigableContainer =
member Name: string

type NavigableItem =
{ Name: string
NeedsBackticks: bool
Range: range
IsSignature: bool
Kind: NavigableItemKind
Container: NavigableContainer }
{
Name: string
NeedsBackticks: bool
Range: range
IsSignature: bool
Kind: NavigableItemKind
Container: NavigableContainer
/// The number of parameters of the method the declaration compiles to, as C# and VB count them to order
/// equally good Navigate To matches: every curried and tupled argument, without the instance and without a
/// solitary unit argument.
ParameterCount: int
/// The number of explicitly declared type parameters.
TypeParameterCount: int
}

[<RequireQualifiedAccess>]
module public NavigateTo =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3954,10 +3954,14 @@ FSharp.Compiler.EditorServices.NavigableItem: FSharp.Compiler.Text.Range Range
FSharp.Compiler.EditorServices.NavigableItem: FSharp.Compiler.Text.Range get_Range()
FSharp.Compiler.EditorServices.NavigableItem: Int32 GetHashCode()
FSharp.Compiler.EditorServices.NavigableItem: Int32 GetHashCode(System.Collections.IEqualityComparer)
FSharp.Compiler.EditorServices.NavigableItem: Int32 ParameterCount
FSharp.Compiler.EditorServices.NavigableItem: Int32 TypeParameterCount
FSharp.Compiler.EditorServices.NavigableItem: Int32 get_ParameterCount()
FSharp.Compiler.EditorServices.NavigableItem: Int32 get_TypeParameterCount()
FSharp.Compiler.EditorServices.NavigableItem: System.String Name
FSharp.Compiler.EditorServices.NavigableItem: System.String ToString()
FSharp.Compiler.EditorServices.NavigableItem: System.String get_Name()
FSharp.Compiler.EditorServices.NavigableItem: Void .ctor(System.String, Boolean, FSharp.Compiler.Text.Range, Boolean, FSharp.Compiler.EditorServices.NavigableItemKind, FSharp.Compiler.EditorServices.NavigableContainer)
FSharp.Compiler.EditorServices.NavigableItem: Void .ctor(System.String, Boolean, FSharp.Compiler.Text.Range, Boolean, FSharp.Compiler.EditorServices.NavigableItemKind, FSharp.Compiler.EditorServices.NavigableContainer, Int32, Int32)
FSharp.Compiler.EditorServices.NavigableItemKind+Tags: Int32 Constructor
FSharp.Compiler.EditorServices.NavigableItemKind+Tags: Int32 EnumCase
FSharp.Compiler.EditorServices.NavigableItemKind+Tags: Int32 Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<Compile Include="ExprTests.fs" />
<Compile Include="CSharpProjectAnalysis.fs" />
<Compile Include="ServiceUntypedParseTests.fs" />
<Compile Include="NavigateToTests.fs" />
<Compile Include="PatternMatchCompilationTests.fs" />
<Compile Include="CompletionTests.fs" />
<Compile Include="ScriptOptionsTests.fs" />
Expand Down
65 changes: 65 additions & 0 deletions tests/FSharp.Compiler.Service.Tests/NavigateToTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module FSharp.Compiler.Service.Tests.NavigateToTests

open FSharp.Compiler.EditorServices
open FSharp.Compiler.Service.Tests.Common
open FSharp.Compiler.Syntax
open Xunit

let private implementation =
"""
let value = 1
let curried a b = a + b
let tupledAndCurried (a, b) c = a + b + c
let takesUnit () = 1
let generic<'T> (x: 'T) = x

type C<'T, 'U>() =
member _.Method(a: int, b: int) = a + b
member _.Property = 1
static member Static x y = x + y
"""

let private signature =
"""
module M

val curried: int -> int -> int
val takesUnit: unit -> int
val takesNamedUnit: u: unit -> int
val generic<'T> : 'T * 'T -> 'T

type C<'T> =
member Method: a: int * b: int -> int
abstract Abstract: unit -> unit
"""

let private arityOf (parseTree: ParsedInput) name =
let item =
NavigateTo.GetNavigableItems parseTree
|> Array.find (fun item -> item.Name = name)

item.ParameterCount, item.TypeParameterCount

[<Theory>]
[<InlineData("value", 0, 0)>]
[<InlineData("curried", 2, 0)>]
[<InlineData("tupledAndCurried", 3, 0)>]
[<InlineData("takesUnit", 0, 0)>]
[<InlineData("generic", 1, 1)>]
[<InlineData("C", 0, 2)>]
[<InlineData("Method", 2, 0)>]
[<InlineData("Property", 0, 0)>]
[<InlineData("Static", 2, 0)>]
let ``A declaration in an implementation file counts the parameters it compiles to`` (name: string, parameterCount: int, typeParameterCount: int) =
Assert.Equal((parameterCount, typeParameterCount), arityOf (getParseResults implementation) name)

[<Theory>]
[<InlineData("curried", 2, 0)>]
[<InlineData("takesUnit", 0, 0)>]
[<InlineData("takesNamedUnit", 0, 0)>]
[<InlineData("generic", 2, 1)>]
[<InlineData("C", 0, 1)>]
[<InlineData("Method", 2, 0)>]
[<InlineData("Abstract", 0, 0)>]
let ``A declaration in a signature file counts the parameters it compiles to`` (name: string, parameterCount: int, typeParameterCount: int) =
Assert.Equal((parameterCount, typeParameterCount), arityOf (getParseResultsOfSignatureFile signature) name)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ open System.Globalization
open Microsoft.CodeAnalysis
open Microsoft.CodeAnalysis.ExternalAccess.FSharp.Navigation
open Microsoft.CodeAnalysis.ExternalAccess.FSharp.NavigateTo
open Microsoft.CodeAnalysis.Text
open Microsoft.VisualStudio.LanguageServices
open Microsoft.VisualStudio.Text.PatternMatching

Expand Down Expand Up @@ -59,27 +60,46 @@ type internal FSharpNavigableItemsCache
searchPattern,
PatternMatcherCreationOptions(
cultureInfo = CultureInfo.CurrentUICulture,
flags = PatternMatcherCreationFlags.AllowFuzzyMatching,
flags =
(PatternMatcherCreationFlags.AllowFuzzyMatching
||| PatternMatcherCreationFlags.IncludeMatchedSpans),
containerSplitCharacters = [ '.' ]
)
)

// The results window highlights spans of the name, but the matcher reports them in the candidate it was
// given, which carries the container before the name: keep the ones inside the name, at its own offsets.
let spansOfName (offset: int) (m: PatternMatch) =
[|
for span in m.MatchedSpans do
if span.Start >= offset then
Microsoft.VisualStudio.Text.Span(span.Start - offset, span.Length)
|]
|> ImmutableArray.CreateRange
|> m.WithMatchedSpans

fun (item: NavigableItem) ->
// PatternMatcher will not match operators and some backtick escaped identifiers.
// To handle them, we fall back to simple substring match.
let name = item.Name

if item.NeedsBackticks then
let matchedSpan start =
ImmutableArray.Create(Microsoft.VisualStudio.Text.Span(start, searchPattern.Length))

match name.IndexOf(searchPattern, StringComparison.CurrentCultureIgnoreCase) with
| i when i > 0 -> ValueSome(PatternMatch(PatternMatchKind.Substring, false, false))
| 0 when name.Length = searchPattern.Length -> ValueSome(PatternMatch(PatternMatchKind.Exact, false, false))
| 0 -> ValueSome(PatternMatch(PatternMatchKind.Prefix, false, false))
| i when i > 0 -> ValueSome(PatternMatch(PatternMatchKind.Substring, false, false, matchedSpan i))
| 0 when name.Length = searchPattern.Length -> ValueSome(PatternMatch(PatternMatchKind.Exact, false, false, matchedSpan 0))
| 0 -> ValueSome(PatternMatch(PatternMatchKind.Prefix, false, false, matchedSpan 0))
| _ -> ValueNone
else
// full name with dots allows for path matching, e.g.
// "f.c.so.elseif" will match "Fantomas.Core.SyntaxOak.ElseIfNode"
patternMatcher.TryMatch $"{item.Container.FullName}.{name}"
let container = item.Container.FullName

patternMatcher.TryMatch $"{container}.{name}"
|> ValueOption.ofNullable
|> ValueOption.map (spansOfName (container.Length + 1))

[<Export(typeof<IFSharpNavigateToSearchService>); Shared>]
type internal FSharpNavigateToSearchService [<ImportingConstructor>] (itemsCache: FSharpNavigableItemsCache) =
Expand Down Expand Up @@ -154,6 +174,10 @@ type internal FSharpNavigateToSearchService [<ImportingConstructor>] (itemsCache
| PatternMatchKind.Fuzzy -> FSharpNavigateToMatchKind.Fuzzy
| _ -> FSharpNavigateToMatchKind.None

/// Where in the name the search pattern matched, for the results window to highlight.
let nameMatchSpans (m: PatternMatch) =
ImmutableArray.CreateRange [| for span in m.MatchedSpans -> TextSpan(span.Start, span.Length) |]

let createMatcherFor (searchPattern: string) =
itemsCache.CreateMatcherFor searchPattern

Expand Down Expand Up @@ -187,13 +211,17 @@ type internal FSharpNavigateToSearchService [<ImportingConstructor>] (itemsCache
additionalInfo,
kind,
patternMatchKindToNavigateToMatchKind m.Kind,
m.IsCaseSensitive,
item.Name,
nameMatchSpans m,
FSharpNavigableItem(
glyph,
ImmutableArray.Create(TaggedText(TextTags.Text, item.Name)),
document,
sourceSpan
)
),
item.ParameterCount,
item.TypeParameterCount
)
| _ -> ()
|]
Expand Down
Loading
Loading