From 25e92c52a3b05128bde97aef531ef623922ed57b Mon Sep 17 00:00:00 2001 From: dawe Date: Sat, 25 Mar 2023 19:11:36 +0100 Subject: [PATCH] deal with ref finding shortages --- .../CodeFixes/AddPrivateAccessModifier.fs | 21 ++++---- .../CodeFixTests/Tests.fs | 53 +++++++++++++------ 2 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/FsAutoComplete/CodeFixes/AddPrivateAccessModifier.fs b/src/FsAutoComplete/CodeFixes/AddPrivateAccessModifier.fs index a461feded..e49fa099d 100644 --- a/src/FsAutoComplete/CodeFixes/AddPrivateAccessModifier.fs +++ b/src/FsAutoComplete/CodeFixes/AddPrivateAccessModifier.fs @@ -103,9 +103,9 @@ let private getRangesAndPlacement input pos = typeDefns |> List.tryPick (fun td -> match td with - | SynTypeDefn(typeInfo = SynComponentInfo(longId = longId; accessibility = None; range = r)) as t when - longIdentContainsPos longId pos - -> + | SynTypeDefn( + typeInfo = SynComponentInfo(longId = longId; accessibility = None; range = r) + typeRepr = SynTypeDefnRepr.ObjectModel _) as t when longIdentContainsPos longId pos -> let editRange = r.WithEnd r.Start let path = SyntaxNode.SynTypeDefn t :: path @@ -137,19 +137,18 @@ let private getRangesAndPlacement input pos = { new SyntaxVisitorBase<_>() with member _.VisitBinding(path, _, synBinding) = match synBinding with + | SynBinding(valData = SynValData(memberFlags = Some({ MemberKind = SynMemberKind.Constructor }))) -> None | SynBinding(headPat = headPat; kind = SynBindingKind.Normal) as s when rangeContainsPos s.RangeOfHeadPattern pos -> match headPat with - | SynPat.LongIdent(longDotId = longDotId; accessibility = None; argPats = synArgPats) -> - let posInArgs = - synArgPats.Patterns |> List.exists (fun p -> rangeContainsPos p.Range pos) - - let posInFirstIdent = - longDotId.LongIdent.Length > 1 - && rangeContainsPos longDotId.LongIdent[0].idRange pos + | SynPat.LongIdent(longDotId = longDotId; accessibility = None) -> + let posValidInSynLongIdent = + longDotId.LongIdent + |> List.skip (if longDotId.LongIdent.Length > 1 then 1 else 0) + |> List.exists (fun i -> rangeContainsPos i.idRange pos) - if posInArgs || posInFirstIdent then + if not posValidInSynLongIdent then None else let editRange = s.RangeOfHeadPattern.WithEnd s.RangeOfHeadPattern.Start diff --git a/test/FsAutoComplete.Tests.Lsp/CodeFixTests/Tests.fs b/test/FsAutoComplete.Tests.Lsp/CodeFixTests/Tests.fs index e815cb55e..c5dc4f0b8 100644 --- a/test/FsAutoComplete.Tests.Lsp/CodeFixTests/Tests.fs +++ b/test/FsAutoComplete.Tests.Lsp/CodeFixTests/Tests.fs @@ -740,6 +740,29 @@ let private addPrivateAccessModifierTests state = type [] private MyClass() = member _.X = 10 """ + + testCaseAsync "add private is not offered for class type definition with reference" + <| CodeFix.checkNotApplicable + server + """ + type MyCla$0ss() = + member _.X = 10 + + let _ = MyClass() + """ + Diagnostics.acceptAll + selectCodeFix + + testCaseAsync "add private is not offered for explicit ctor" // ref finding might not show us usages + <| CodeFix.checkNotApplicable + server + """ + type MyC(x: int) = + ne$0w() = + MyC(23) + """ + Diagnostics.acceptAll + selectCodeFix testCaseAsync "add private is not offered for member with reference outside its declaring class" <| CodeFix.checkNotApplicable @@ -767,6 +790,16 @@ let private addPrivateAccessModifierTests state = Diagnostics.acceptAll selectCodeFix + testCaseAsync "add private is not offered for member when caret is in SynTypArDecl" + <| CodeFix.checkNotApplicable + server + """ + type MyC() = + member _.X<'T$0> a = a + """ + Diagnostics.acceptAll + selectCodeFix + testCaseAsync "add private is not offered for class member when caret is on parameter" <| CodeFix.checkNotApplicable server @@ -828,8 +861,8 @@ let private addPrivateAccessModifierTests state = Diagnostics.acceptAll selectCodeFix - testCaseAsync "add private works for DU type definition" - <| CodeFix.check + testCaseAsync "add private is not offered for DU type definition" // ref finding might not show us type inferred usages + <| CodeFix.checkNotApplicable server """ type [] MyDi$0scUnion = @@ -838,12 +871,7 @@ let private addPrivateAccessModifierTests state = """ Diagnostics.acceptAll selectCodeFix - """ - type [] private MyDiscUnion = - | Case1 - | Case2 - """ - + testCaseAsync "add private is not offered for member with reference outside its declaring DU" <| CodeFix.checkNotApplicable server @@ -909,8 +937,8 @@ let private addPrivateAccessModifierTests state = member private _.Foo x = x """ - testCaseAsync "add private works for Record definition" - <| CodeFix.check + testCaseAsync "add private is not offered for Record type definition" // ref finding might not show us type inferred usages + <| CodeFix.checkNotApplicable server """ type [] My$0Record = @@ -919,11 +947,6 @@ let private addPrivateAccessModifierTests state = """ Diagnostics.acceptAll selectCodeFix - """ - type [] private MyRecord = - { Field1: int - Field2: string } - """ testCaseAsync "add private is not offered for member with reference outside its declaring Record" <| CodeFix.checkNotApplicable