Skip to content

Commit

Permalink
deal with ref finding shortages
Browse files Browse the repository at this point in the history
  • Loading branch information
dawedawe committed Mar 25, 2023
1 parent 967a009 commit 25e92c5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
21 changes: 10 additions & 11 deletions src/FsAutoComplete/CodeFixes/AddPrivateAccessModifier.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
53 changes: 38 additions & 15 deletions test/FsAutoComplete.Tests.Lsp/CodeFixTests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,29 @@ let private addPrivateAccessModifierTests state =
type [<System.Obsolete>] 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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 [<System.Obsolete>] MyDi$0scUnion =
Expand All @@ -838,12 +871,7 @@ let private addPrivateAccessModifierTests state =
"""
Diagnostics.acceptAll
selectCodeFix
"""
type [<System.Obsolete>] private MyDiscUnion =
| Case1
| Case2
"""


testCaseAsync "add private is not offered for member with reference outside its declaring DU"
<| CodeFix.checkNotApplicable
server
Expand Down Expand Up @@ -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 [<System.Obsolete>] My$0Record =
Expand All @@ -919,11 +947,6 @@ let private addPrivateAccessModifierTests state =
"""
Diagnostics.acceptAll
selectCodeFix
"""
type [<System.Obsolete>] private MyRecord =
{ Field1: int
Field2: string }
"""

testCaseAsync "add private is not offered for member with reference outside its declaring Record"
<| CodeFix.checkNotApplicable
Expand Down

0 comments on commit 25e92c5

Please sign in to comment.