Skip to content

Commit

Permalink
support for type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
dawedawe committed Mar 25, 2023
1 parent a5725f4 commit 967a009
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/FsAutoComplete/CodeFixes/AddPrivateAccessModifier.fs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ let private getRangesAndPlacement input pos =
| Some a when a.Range.EndLine = posLine -> a.Range.WithStart a.Range.End
| _ -> moduleKeywordRange.WithStart moduleKeywordRange.End

let longIdentContainsPos (longIdent: LongIdent) (pos: FSharp.Compiler.Text.pos) =
longIdent
|> List.tryFind (fun i -> rangeContainsPos i.idRange pos)
|> Option.isSome

let tryGetDeclContainingRange (path: SyntaxVisitorPath) pos =
path
|> Seq.skip 1
Expand All @@ -80,11 +85,7 @@ let private getRangesAndPlacement input pos =
match d with
| SynModuleDecl.NestedModule(
moduleInfo = SynComponentInfo(attributes = attributes; longId = longId; accessibility = None)
trivia = { ModuleKeyword = Some r }) as m when
longId
|> List.tryFind (fun i -> rangeContainsPos i.idRange pos)
|> Option.isSome
->
trivia = { ModuleKeyword = Some r }) as m when longIdentContainsPos longId pos ->
let editRange = getEditRangeForModule attributes r pos.Line
let path = (SyntaxNode.SynModule m) :: path

Expand All @@ -102,6 +103,15 @@ 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
->
let editRange = r.WithEnd r.Start
let path = SyntaxNode.SynTypeDefn t :: path

match tryGetDeclContainingRange path pos with
| Some r -> Some(editRange, r, Before)
| _ -> None
| SynTypeDefn(typeRepr = SynTypeDefnRepr.ObjectModel(_, members, _)) as d ->
let path = SyntaxNode.SynTypeDefn d :: path

Expand Down Expand Up @@ -163,9 +173,7 @@ let private getRangesAndPlacement input pos =
attribs = attribs
accessibility = None
trivia = { LeadingKeyword = SynModuleOrNamespaceLeadingKeyword.Module r }) as mOrN when
longId
|> List.tryFind (fun i -> rangeContainsPos i.idRange pos)
|> Option.isSome
longIdentContainsPos longId pos
->
let editRange = getEditRangeForModule attribs r pos.Line

Expand Down
46 changes: 46 additions & 0 deletions test/FsAutoComplete.Tests.Lsp/CodeFixTests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,20 @@ let private addPrivateAccessModifierTests state =
Diagnostics.acceptAll
selectCodeFix

testCaseAsync "add private works for class type definition"
<| CodeFix.check
server
"""
type [<System.Obsolete>] MyCla$0ss() =
member _.X = 10
"""
Diagnostics.acceptAll
selectCodeFix
"""
type [<System.Obsolete>] private MyClass() =
member _.X = 10
"""

testCaseAsync "add private is not offered for member with reference outside its declaring class"
<| CodeFix.checkNotApplicable
server
Expand Down Expand Up @@ -814,6 +828,22 @@ let private addPrivateAccessModifierTests state =
Diagnostics.acceptAll
selectCodeFix

testCaseAsync "add private works for DU type definition"
<| CodeFix.check
server
"""
type [<System.Obsolete>] MyDi$0scUnion =
| Case1
| Case2
"""
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 @@ -879,6 +909,22 @@ let private addPrivateAccessModifierTests state =
member private _.Foo x = x
"""

testCaseAsync "add private works for Record definition"
<| CodeFix.check
server
"""
type [<System.Obsolete>] My$0Record =
{ Field1: int
Field2: string }
"""
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
server
Expand Down

0 comments on commit 967a009

Please sign in to comment.