Skip to content

Commit

Permalink
simplify check for let in object model
Browse files Browse the repository at this point in the history
  • Loading branch information
dawedawe committed Mar 25, 2023
1 parent fde67bd commit 2a2c771
Showing 1 changed file with 32 additions and 51 deletions.
83 changes: 32 additions & 51 deletions src/FsAutoComplete/CodeFixes/AddPrivateAccessModifier.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,6 @@ type private Placement =
| Before
| After

let private isLetInsideObjectModel input pos =
SyntaxTraversal.Traverse(
pos,
input,
{ new SyntaxVisitorBase<_>() with
member _.VisitModuleOrNamespace(_, synModuleOrNamespace) =

let rec tryFind (decls: SynModuleDecl list) =
decls
|> List.tryPick (fun d ->
match d with
| SynModuleDecl.Let(range = range) when rangeContainsPos range pos -> None
| SynModuleDecl.Types(typeDefns = typeDefns) ->
typeDefns
|> List.tryPick (fun td ->
match td with
| SynTypeDefn(typeRepr = SynTypeDefnRepr.ObjectModel(_, members, _)) ->
members
|> List.tryPick (fun m ->
match m with
| SynMemberDefn.LetBindings(range = range) when rangeContainsPos range pos -> Some()
| _ -> None)
| _ -> None)
| SynModuleDecl.NestedModule(decls = nestedDecls) as m -> tryFind nestedDecls
| _ -> None)

match synModuleOrNamespace with
| SynModuleOrNamespace(decls = decls) as s -> tryFind decls }
)
|> Option.isSome

let private getRangesAndPlacement input pos =

let getEditRangeForModule (attributes: SynAttributes) (moduleKeywordRange: FSharp.Compiler.Text.Range) posLine =
Expand All @@ -69,6 +38,17 @@ let private getRangesAndPlacement input pos =
|> List.tryFind (fun i -> rangeContainsPos i.idRange pos)
|> Option.isSome

let isLetInsideObjectModel (path: SyntaxVisitorPath) pos =
path
|> List.exists (function
| SyntaxNode.SynTypeDefn(SynTypeDefn(typeRepr = SynTypeDefnRepr.ObjectModel(_, members, _))) ->
members
|> List.exists (fun m ->
match m with
| SynMemberDefn.LetBindings(range = range) when rangeContainsPos range pos -> true
| _ -> false)
| _ -> false)

let tryGetDeclContainingRange (path: SyntaxVisitorPath) pos =
let skip =
match path with
Expand Down Expand Up @@ -159,31 +139,35 @@ let private getRangesAndPlacement input pos =
match synBinding with
// explicit Ctor
| SynBinding(valData = SynValData(memberFlags = Some({ MemberKind = SynMemberKind.Constructor }))) -> None
// let bindings, members
| SynBinding(headPat = headPat; kind = SynBindingKind.Normal) as s when
rangeContainsPos s.RangeOfHeadPattern pos
->
match headPat with
| 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 not posValidInSynLongIdent then
None
else
if isLetInsideObjectModel path pos then
None
else
match headPat with
| 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 not posValidInSynLongIdent then
None
else
let editRange = s.RangeOfHeadPattern.WithEnd s.RangeOfHeadPattern.Start

match tryGetDeclContainingRange path pos with
| Some r -> Some(editRange, r, Before)
| _ -> None
| SynPat.Named(accessibility = None; isThisVal = false) ->
let editRange = s.RangeOfHeadPattern.WithEnd s.RangeOfHeadPattern.Start

match tryGetDeclContainingRange path pos with
| Some r -> Some(editRange, r, Before)
| _ -> None
| SynPat.Named(accessibility = None; isThisVal = false) ->
let editRange = s.RangeOfHeadPattern.WithEnd s.RangeOfHeadPattern.Start

match tryGetDeclContainingRange path pos with
| Some r -> Some(editRange, r, Before)
| _ -> None
| _ -> None
| _ -> None

member _.VisitModuleOrNamespace(path, synModuleOrNamespace) =
Expand All @@ -207,10 +191,7 @@ let private getRangesAndPlacement input pos =
let path = SyntaxNode.SynModuleOrNamespace mOrN :: path
findNested path decls }

if isLetInsideObjectModel input pos then
None
else
SyntaxTraversal.Traverse(pos, input, visitor)
SyntaxTraversal.Traverse(pos, input, visitor)

let fix (getParseResultsForFile: GetParseResultsForFile) (symbolUseWorkspace: SymbolUseWorkspace) : CodeFix =
fun codeActionParams ->
Expand Down

0 comments on commit 2a2c771

Please sign in to comment.