-
Notifications
You must be signed in to change notification settings - Fork 57
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
Inlay hints/PatternTypeHintsStage: support match clause #756
Open
DedSec256
wants to merge
1
commit into
main
Choose a base branch
from
ber.a/matchClausePatterns
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ open JetBrains.ReSharper.Plugins.FSharp.Psi.Features.Daemon.Highlightings | |
open JetBrains.ReSharper.Plugins.FSharp.Psi.Features.Daemon.Stages | ||
open JetBrains.ReSharper.Plugins.FSharp.Psi.Impl | ||
open JetBrains.ReSharper.Plugins.FSharp.Psi.Tree | ||
open JetBrains.ReSharper.Plugins.FSharp.Psi.Util | ||
open JetBrains.ReSharper.Plugins.FSharp.Settings | ||
open JetBrains.ReSharper.Plugins.FSharp.Util | ||
open JetBrains.ReSharper.Psi.Tree | ||
|
@@ -20,28 +21,39 @@ open JetBrains.ReSharper.Plugins.FSharp.Psi.Services.Util.TypeAnnotationsUtil | |
open JetBrains.TextControl.DocumentMarkup.Adornments.IntraTextAdornments | ||
|
||
type private NodesRequiringHints = | ||
{ TopLevelNodes: VisibilityConsumer<ITreeNode>; LocalNodes: VisibilityConsumer<ITreeNode> } with | ||
{ TopLevelNodes: VisibilityConsumer<ITreeNode> | ||
LocalNodes: VisibilityConsumer<ITreeNode> | ||
MatchNodes: VisibilityConsumer<ITreeNode> } with | ||
|
||
member x.HasVisibleItems = | ||
x.TopLevelNodes.HasVisibleItems || | ||
x.LocalNodes.HasVisibleItems | ||
x.LocalNodes.HasVisibleItems || | ||
x.MatchNodes.HasVisibleItems | ||
|
||
type private FSharpTypeHintSettings = | ||
{ TopLevelMembers: PushToHintMode; LocalBindings: PushToHintMode } with | ||
{ TopLevelMembers: PushToHintMode | ||
LocalBindings: PushToHintMode | ||
MatchPatterns: PushToHintMode } with | ||
|
||
static member Create(settingsStore: IContextBoundSettingsStore) = | ||
{ TopLevelMembers = settingsStore.GetValue(fun (key: FSharpTypeHintOptions) -> key.ShowTypeHintsForTopLevelMembers) | ||
.EnsureInlayHintsDefault(settingsStore) | ||
LocalBindings = settingsStore.GetValue(fun (key: FSharpTypeHintOptions) -> key.ShowTypeHintsForLocalBindings) | ||
.EnsureInlayHintsDefault(settingsStore) } | ||
.EnsureInlayHintsDefault(settingsStore) | ||
MatchPatterns = settingsStore.GetValue(fun (key: FSharpTypeHintOptions) -> key.ShowTypeHintsForMatchPatterns) | ||
.EnsureInlayHintsDefault(settingsStore)} | ||
|
||
member x.IsDisabled = | ||
x.TopLevelMembers = PushToHintMode.Never && | ||
x.LocalBindings = PushToHintMode.Never | ||
x.LocalBindings = PushToHintMode.Never && | ||
x.MatchPatterns = PushToHintMode.Never | ||
|
||
|
||
type private MembersVisitor(settings) = | ||
inherit TreeNodeVisitor<NodesRequiringHints>() | ||
let disabledForTopBindings = settings.TopLevelMembers = PushToHintMode.Never | ||
let disabledForLocalBindings = settings.LocalBindings = PushToHintMode.Never | ||
let disabledForMatchPatterns = settings.MatchPatterns = PushToHintMode.Never | ||
|
||
let isTopLevelMember (node: ITreeNode) = | ||
match node with | ||
|
@@ -50,12 +62,19 @@ type private MembersVisitor(settings) = | |
| :? IConstructorDeclaration -> true | ||
| _ -> false | ||
|
||
let isLocalBinding (node: ITreeNode) = | ||
match node with | ||
| :? ILocalBinding | ||
| :? ILambdaExpr | ||
| :? IForEachExpr -> true | ||
| _ -> false | ||
|
||
override x.VisitNode(node, context) = | ||
if settings.LocalBindings = PushToHintMode.Never && isTopLevelMember node then () else | ||
if disabledForLocalBindings && disabledForMatchPatterns && isTopLevelMember node then () else | ||
|
||
for child in node.Children() do | ||
if settings.TopLevelMembers = PushToHintMode.Never && | ||
isTopLevelMember child then x.VisitNode(child, context) else | ||
if disabledForTopBindings && isTopLevelMember child || disabledForLocalBindings && isLocalBinding child | ||
then x.VisitNode(child, context) else | ||
|
||
match child with | ||
| :? IFSharpTreeNode as treeNode -> treeNode.Accept(x, context) | ||
|
@@ -103,6 +122,14 @@ type private MembersVisitor(settings) = | |
|
||
x.VisitNode(forEachExpr, context) | ||
|
||
override x.VisitMatchClause(matchClause, context) = | ||
if not disabledForMatchPatterns then | ||
let result = collectTypeHintAnchorsForMatchClause matchClause | ||
context.MatchNodes.AddRange(result) | ||
|
||
x.VisitNode(matchClause, context) | ||
|
||
|
||
type private PatternsHighlightingProcess(fsFile, settingsStore: IContextBoundSettingsStore, daemonProcess: IDaemonProcess, settings) = | ||
inherit FSharpDaemonStageProcessBase(fsFile, daemonProcess) | ||
static let defaultDisplayContext = FSharpDisplayContext.Empty.WithShortTypeNames(true) | ||
|
@@ -153,18 +180,6 @@ type private PatternsHighlightingProcess(fsFile, settingsStore: IContextBoundSet | |
|
||
let rec getHintForPattern (pattern: IFSharpPattern) pushToHintMode actionsProvider = | ||
match pattern with | ||
| :? IParametersOwnerPat as pattern -> | ||
let asPat = AsPatNavigator.GetByLeftPattern(pattern.IgnoreParentParens()) | ||
if isNull asPat then ValueNone else | ||
|
||
let reference = pattern.Reference | ||
if isNull reference then ValueNone else | ||
|
||
match reference.GetFcsSymbol() with | ||
| :? FSharpActivePatternCase -> | ||
getHintForPattern (asPat.RightPattern.IgnoreInnerParens()) pushToHintMode actionsProvider | ||
| _ -> ValueNone | ||
|
||
| :? IReferencePat as refPat -> | ||
let symbolUse = refPat.GetFcsSymbolUse() | ||
if isNull symbolUse then ValueNone else | ||
|
@@ -181,7 +196,25 @@ type private PatternsHighlightingProcess(fsFile, settingsStore: IContextBoundSet | |
createTypeHintHighlighting fcsType defaultDisplayContext range pushToHintMode actionsProvider false | ||
|> ValueSome | ||
|
||
| _ -> ValueNone | ||
| :? IParametersOwnerPat as pattern -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason for this change? |
||
let asPat = AsPatNavigator.GetByLeftPattern(pattern.IgnoreParentParens()) | ||
if isNull asPat then ValueNone else | ||
|
||
let reference = pattern.Reference | ||
if isNull reference then ValueNone else | ||
|
||
match reference.GetFcsSymbol() with | ||
| :? FSharpActivePatternCase -> | ||
getHintForPattern (asPat.RightPattern.IgnoreInnerParens()) pushToHintMode actionsProvider | ||
| _ -> ValueNone | ||
|
||
| pattern -> | ||
let fcsType = pattern.TryGetFcsType() | ||
if isNull fcsType then ValueNone else | ||
|
||
let range = pattern.GetDocumentRange().EndOffsetRange() | ||
createTypeHintHighlighting fcsType defaultDisplayContext range pushToHintMode actionsProvider false | ||
|> ValueSome | ||
|
||
let rec getHighlighting (node: ITreeNode) pushToHintMode actionsProvider = | ||
match node with | ||
|
@@ -193,7 +226,10 @@ type private PatternsHighlightingProcess(fsFile, settingsStore: IContextBoundSet | |
|
||
| _ -> ValueNone | ||
|
||
let adornNodes (topLevelNodes : ITreeNode ICollection) (localNodes : ITreeNode ICollection) = | ||
let adornNodes | ||
(topLevelNodes: ITreeNode ICollection) | ||
(localNodes: ITreeNode ICollection) | ||
(matchClauses: ITreeNode ICollection) = | ||
let highlightingConsumer = FilteringHighlightingConsumer(daemonProcess.SourceFile, fsFile, settingsStore) | ||
|
||
let inline adornNodes nodes pushToHintMode actionsProvider = | ||
|
@@ -206,6 +242,7 @@ type private PatternsHighlightingProcess(fsFile, settingsStore: IContextBoundSet | |
|
||
adornNodes topLevelNodes settings.TopLevelMembers FSharpTopLevelMembersTypeHintBulbActionsProvider.Instance | ||
adornNodes localNodes settings.LocalBindings FSharpLocalBindingTypeHintBulbActionsProvider.Instance | ||
adornNodes matchClauses settings.MatchPatterns FSharpMatchClauseTypeHintBulbActionsProvider.Instance | ||
|
||
highlightingConsumer.CollectHighlightings() | ||
|
||
|
@@ -214,22 +251,26 @@ type private PatternsHighlightingProcess(fsFile, settingsStore: IContextBoundSet | |
// Intersect them to ensure commit doesn't throw | ||
let documentRange = daemonProcess.Document.GetDocumentRange() | ||
let visibleRange = daemonProcess.VisibleRange.Intersect(&documentRange) | ||
let consumer = { TopLevelNodes = VisibilityConsumer(visibleRange, _.GetNavigationRange()) | ||
LocalNodes = VisibilityConsumer(visibleRange, _.GetNavigationRange()) } | ||
let consumer = { | ||
TopLevelNodes = VisibilityConsumer(visibleRange, _.GetNavigationRange()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please reformat. |
||
LocalNodes = VisibilityConsumer(visibleRange, _.GetNavigationRange()) | ||
MatchNodes = VisibilityConsumer(visibleRange, _.GetNavigationRange()) } | ||
fsFile.Accept(MembersVisitor(settings), consumer) | ||
|
||
let topLevelNodes = consumer.TopLevelNodes | ||
let localNodes = consumer.LocalNodes | ||
let matchNodes = consumer.MatchNodes | ||
|
||
// Partition the expressions to adorn by whether they're visible in the viewport or not | ||
let remainingHighlightings = | ||
if consumer.HasVisibleItems then | ||
// Adorn visible expressions first | ||
let visibleHighlightings = adornNodes topLevelNodes.VisibleItems localNodes.VisibleItems | ||
let visibleHighlightings = | ||
adornNodes topLevelNodes.VisibleItems localNodes.VisibleItems matchNodes.VisibleItems | ||
committer.Invoke(DaemonStageResult(visibleHighlightings, visibleRange)) | ||
|
||
// Finally adorn expressions that aren't visible in the viewport | ||
adornNodes topLevelNodes.NonVisibleItems localNodes.NonVisibleItems | ||
adornNodes topLevelNodes.NonVisibleItems localNodes.NonVisibleItems matchNodes.NonVisibleItems | ||
|
||
committer.Invoke(DaemonStageResult remainingHighlightings) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
ReSharper.FSharp/test/data/features/daemon/typeHints/Patterns 02 - Match clauses.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Test | ||
|
||
let _ = function x -> () | ||
|
||
match [Some 5] with | ||
| [] -> () | ||
| [x] -> () | ||
| [x: int option] -> () | ||
| [x; y] -> () | ||
| [x; y: int option] -> () | ||
| [_; x] -> () | ||
| [_; _] -> () | ||
| [x; _; _] -> () | ||
| [x; _; Some 5] -> () | ||
| x :: tail -> () | ||
| _ :: (tail: int option list) -> () | ||
| x: int option :: tail -> () | ||
| x: int option :: _ -> () | ||
| x :: (tail: int option list) -> () | ||
| ((x :: tail): int option list) -> () | ||
| _ :: _ -> () | ||
| x :: _ -> () | ||
| _ :: x :: _ -> () | ||
| x :: _ :: _ -> () | ||
| x :: Some y :: _ -> () | ||
| x :: (y :: _) -> () | ||
| x :: y :: [] -> () | ||
| x :: [y] -> () | ||
| Some(x) :: tail -> () | ||
| [Some(x)] | ||
| [_; Some(x)] when let x = 5 in true -> | ||
let y = 5 in () | ||
|
||
match [[5]] with | ||
| [[]] -> () | ||
| [[]; x] -> () | ||
|
||
match [|5|] with | ||
| [||] -> () | ||
| [|x|] -> () | ||
| [|x; y|] -> () | ||
|
||
exception MyException of string | ||
try () with | MyException(x) -> () |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the idea behind having separate settings for patterns in local bindings and match clauses?