Skip to content

NestedRecordUpdate: support multiline records #710

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

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ type RecordExprAnalyzer() =
let copyExpr = getCopyInfoReferenceExpr recordExpr
if isNull copyExpr then produceHighlighting fieldsChainMatch previousFieldBinding consumer else

let fieldBindings = recordExpr.FieldBindingsEnumerable
let fieldBindings = recordExpr.FieldBindings
let singleField = fieldBindings.SingleItem
let hasFieldsMatch =
isNotNull previousFieldBinding &&
compareFieldWithNextCopyExpr previousCopyExpr previousFieldBinding.ReferenceName copyExpr
let searchInDepthWhileMatched = hasFieldsMatch && isNotNull singleField

for currentFieldBinding in fieldBindings do
for i, currentFieldBinding in Seq.indexed fieldBindings do
let isSingleLine = currentFieldBinding.IsSingleLine
let fieldsChainMatch =
if searchInDepthWhileMatched then
match fieldsChainMatch with
Expand All @@ -64,23 +65,34 @@ type RecordExprAnalyzer() =
let currentFieldNameReversed = currentFieldBinding.ReferenceName |> appendFieldNameReversed previousFieldNameReversed
ValueSome(rootFieldBinding, currentFieldNameReversed)
else
produceHighlighting fieldsChainMatch previousFieldBinding consumer
if isSingleLine then produceHighlighting fieldsChainMatch previousFieldBinding consumer
ValueNone

match currentFieldBinding.Expression.IgnoreInnerParens() with
| :? IRecordExpr as recordExpr ->
collectRecordExprsToSimplify recordExpr currentFieldBinding copyExpr fieldsChainMatch consumer
let nextFieldIdx = i + 1
let nextFieldOnTheSameLine =
if nextFieldIdx = fieldBindings.Count then false else
fieldBindings[nextFieldIdx].StartLine = currentFieldBinding.EndLine

if fieldsChainMatch.IsNone && not isSingleLine && nextFieldOnTheSameLine then
analyzeRecordExpr recordExpr consumer
else
collectRecordExprsToSimplify recordExpr currentFieldBinding copyExpr fieldsChainMatch consumer
| _ ->
if not searchInDepthWhileMatched then () else
if not searchInDepthWhileMatched || not isSingleLine then () else
produceHighlighting fieldsChainMatch currentFieldBinding consumer

and analyzeRecordExpr recordExpr consumer =
collectRecordExprsToSimplify recordExpr null null ValueNone consumer

override this.Run(recordExpr, data, consumer) =
if not data.IsFSharp80Supported || not recordExpr.IsSingleLine then () else
if not data.IsFSharp80Supported then () else

let isInnerRecordExpr =
recordExpr.IgnoreParentParens()
|> RecordFieldBindingNavigator.GetByExpression
|> isNotNull

if isInnerRecordExpr then () else
collectRecordExprsToSimplify recordExpr null null ValueNone consumer
analyzeRecordExpr recordExpr consumer
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ ignore { item with Foo = { item.Foo with Zoo = { item.Foo.Zoo with Foo = 3 } } }
ignore { item with Bar = { item.Foo with Zoo = { item.Foo.Zoo with Bar = 3 } } }
ignore { Module.item with Foo = { item.Foo with Zoo = { item.Foo.Zoo with Foo = 3 } } }
ignore { item with Foo = { item.Foo with Foo = 3 }; Bar = { item.Bar with Foo = 3 } }
ignore { item with
Foo = {
item.Foo with
Foo = 3
}
Bar = {
item.Bar with
Foo = 3
}
}
ignore { item with
Foo = {
item.Foo with
Foo = 3
}; Bar = {
item.Bar with
Foo = 3
}
}


// Not available
ignore { item with Foo = { item.Foo with Foo = } }
Expand All @@ -38,3 +58,7 @@ ignore { (id item) with Foo = { (id item).Foo with Foo = 3 } }
ignore { item with Foo = { item2.Foo with Foo = 3 } }
ignore { item with Foo = { item.Foo with Unresolved = 3 } }
ignore { item with Unresolved = { item.Unresolved with Unresolved = 3 } }
ignore { item with Foo = {
item.Foo with Foo = 3
}; Bar.Foo = 3
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ ignore { item with Foo |= { item.Foo with Zoo = { item.Foo.Zoo with|(7) Foo = 3
ignore { item with Bar = { item.Foo with Zoo |= { item.Foo.Zoo with|(8) Bar = 3 } } }
ignore { Module.item with Foo = { item.Foo with Zoo |= { item.Foo.Zoo with|(9) Foo = 3 } } }
ignore { item with Foo |= { item.Foo with|(10) Foo = 3 }; Bar |= { item.Bar with|(11) Foo = 3 } }
ignore { item with
Foo |= {
item.Foo with|(12)
Foo = 3
}
Bar |= {
item.Bar with|(13)
Foo = 3
}
}
ignore { item with
Foo = {
item.Foo with
Foo = 3
}; Bar |= {
item.Bar with|(14)
Foo = 3
}
}


// Not available
ignore { item with Foo = { item.Foo with Foo = } }
Expand All @@ -38,6 +58,10 @@ ignore { (id item) with Foo = { (id item).Foo with Foo = 3 } }
ignore { item with Foo = { item2.Foo with Foo = 3 } }
ignore { item with Foo = { item.Foo with Unresolved = 3 } }
ignore { item with Unresolved = { item.Unresolved with Unresolved = 3 } }
ignore { item with Foo = {
item.Foo with Foo = 3
}; Bar.Foo = 3
}

---------------------------------------------------------
(0): ReSharper Dead Code: Nested record update can be simplified
Expand All @@ -52,3 +76,6 @@ ignore { item with Unresolved = { item.Unresolved with Unresolved = 3 } }
(9): ReSharper Dead Code: Nested record update can be simplified
(10): ReSharper Dead Code: Nested record update can be simplified
(11): ReSharper Dead Code: Nested record update can be simplified
(12): ReSharper Dead Code: Nested record update can be simplified
(13): ReSharper Dead Code: Nested record update can be simplified
(14): ReSharper Dead Code: Nested record update can be simplified
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ ignore { item with Foo = { item.Foo with Zoo = { item.Foo.Zoo with Foo = 3 } } }
ignore { item with Bar = { item.Foo with Zoo = { item.Foo.Zoo with Bar = 3 } } }
ignore { Module.item with Foo = { item.Foo with Zoo = { item.Foo.Zoo with Foo = 3 } } }
ignore { item with Foo = { item.Foo with Foo = 3 }; Bar = { item.Bar with Foo = 3 } }
ignore { item with
Foo = {
item.Foo with
Foo = 3
}
Bar = {
item.Bar with
Foo = 3
}
}
ignore { item with
Foo = {
item.Foo with
Foo = 3
}; Bar = {
item.Bar with
Foo = 3
}
}


// Not available
ignore { item with Foo = { item.Foo with Foo = } }
Expand All @@ -38,3 +58,7 @@ ignore { (id item) with Foo = { (id item).Foo with Foo = 3 } }
ignore { item with Foo = { item2.Foo with Foo = 3 } }
ignore { item with Foo = { item.Foo with Unresolved = 3 } }
ignore { item with Unresolved = { item.Unresolved with Unresolved = 3 } }
ignore { item with Foo = {
item.Foo with Foo = 3
}; Bar.Foo = 3
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ ignore { item with Foo.Zoo.Foo = 3 }
ignore { item with Bar = { item.Foo with Record1.Zoo.Bar = 3 } }
ignore { Module.item with Foo = { item.Foo with Record1.Zoo.Foo = 3 } }
ignore { item with Foo.Foo = 3; Bar.Foo = 3 }
ignore { item with
Foo.Foo = 3
Bar.Foo = 3
}
ignore { item with
Foo = {
item.Foo with
Foo = 3
}; Bar.Foo = 3
}


// Not available
ignore { item with Foo = { item.Foo with Foo = } }
Expand All @@ -38,3 +49,7 @@ ignore { (id item) with Foo = { (id item).Foo with Foo = 3 } }
ignore { item with Foo = { item2.Foo with Foo = 3 } }
ignore { item with Foo = { item.Foo with Unresolved = 3 } }
ignore { item with Unresolved = { item.Unresolved with Unresolved = 3 } }
ignore { item with Foo = {
item.Foo with Foo = 3
}; Bar.Foo = 3
}
Loading