Skip to content

Commit 87bcaa3

Browse files
cubuspl42facebook-github-bot
authored andcommitted
TextLayoutUtils: Use named arguments (#42593)
Summary: `TextLayoutUtils`: Use named arguments to ensure same-type arguments (like `start`/`end`) are not confused This is a minor readability follow-up to #39630. ## Changelog: <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [INTERNAL] [CHANGED] - Increase the `TextLayoutUtils` readability slightly Pull Request resolved: #42593 Reviewed By: NickGerleman Differential Revision: D53028402 Pulled By: mdvacca fbshipit-source-id: 39e99ba70b93eecfc51bda19d30a5b1977cfe406
1 parent 78967b3 commit 87bcaa3

File tree

1 file changed

+122
-37
lines changed
  • packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text

1 file changed

+122
-37
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutUtils.kt

Lines changed: 122 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import android.view.View
1414
import com.facebook.react.common.ReactConstants
1515
import com.facebook.react.uimanager.PixelUtil
1616
import com.facebook.react.uimanager.ReactAccessibilityDelegate
17+
import com.facebook.react.views.text.fragments.TextFragment
1718
import com.facebook.react.views.text.fragments.TextFragmentList
1819

1920
/** Utility methods for building [Spannable]s */
@@ -27,26 +28,53 @@ internal object TextLayoutUtils {
2728
sb: SpannableStringBuilder,
2829
ops: MutableList<SetSpanOperation>,
2930
) {
30-
3131
for (i in 0 until textFragmentList.count) {
3232
val fragment = textFragmentList.getFragment(i)
33-
val start = sb.length
34-
35-
// ReactRawText
36-
val textAttributes = fragment.textAttributeProps
37-
38-
addText(sb, fragment.string, textAttributes)
3933

40-
val end = sb.length
41-
val reactTag = if (fragment.hasReactTag()) fragment.reactTag else View.NO_ID
42-
if (fragment.hasIsAttachment() && fragment.isAttachment) {
43-
val width = PixelUtil.toPixelFromSP(fragment.width)
44-
val height = PixelUtil.toPixelFromSP(fragment.height)
34+
addApplicableFragmentSpans(
35+
context = context,
36+
fragment = fragment,
37+
sb = sb,
38+
ops = ops,
39+
)
40+
}
41+
}
4542

46-
addInlineViewPlaceholderSpan(ops, sb, reactTag, width, height)
47-
} else if (end >= start) {
48-
addApplicableTextAttributeSpans(ops, textAttributes, reactTag, context, start, end)
49-
}
43+
private fun addApplicableFragmentSpans(
44+
context: Context,
45+
fragment: TextFragment,
46+
sb: SpannableStringBuilder,
47+
ops: MutableList<SetSpanOperation>,
48+
) {
49+
val start = sb.length
50+
51+
// ReactRawText
52+
val textAttributes = fragment.textAttributeProps
53+
54+
addText(sb, fragment.string, textAttributes)
55+
56+
val end = sb.length
57+
val reactTag = if (fragment.hasReactTag()) fragment.reactTag else View.NO_ID
58+
if (fragment.hasIsAttachment() && fragment.isAttachment) {
59+
val width = PixelUtil.toPixelFromSP(fragment.width)
60+
val height = PixelUtil.toPixelFromSP(fragment.height)
61+
62+
addInlineViewPlaceholderSpan(
63+
ops = ops,
64+
sb = sb,
65+
reactTag = reactTag,
66+
width = width,
67+
height = height,
68+
)
69+
} else if (end >= start) {
70+
addApplicableTextAttributeSpans(
71+
ops = ops,
72+
textAttributeProvider = textAttributes,
73+
reactTag = reactTag,
74+
context = context,
75+
start = start,
76+
end = end,
77+
)
5078
}
5179
}
5280

@@ -83,27 +111,84 @@ internal object TextLayoutUtils {
83111
start: Int,
84112
end: Int
85113
) {
86-
addColorSpanIfApplicable(ops, textAttributeProvider, start, end)
87-
88-
addBackgroundColorSpanIfApplicable(ops, textAttributeProvider, start, end)
89-
90-
addLinkSpanIfApplicable(ops, textAttributeProvider, reactTag, start, end)
91-
92-
addLetterSpacingSpanIfApplicable(ops, textAttributeProvider, start, end)
93-
94-
addFontSizeSpanIfApplicable(ops, textAttributeProvider, start, end)
95-
96-
addCustomStyleSpanIfApplicable(ops, textAttributeProvider, context, start, end)
97-
98-
addUnderlineSpanIfApplicable(ops, textAttributeProvider, start, end)
99-
100-
addStrikethroughSpanIfApplicable(ops, textAttributeProvider, start, end)
101-
102-
addShadowStyleSpanIfApplicable(ops, textAttributeProvider, start, end)
103-
104-
addLineHeightSpanIfApplicable(ops, textAttributeProvider, start, end)
105-
106-
addReactTagSpan(ops, start, end, reactTag)
114+
addColorSpanIfApplicable(
115+
ops = ops,
116+
textAttributeProvider = textAttributeProvider,
117+
start = start,
118+
end = end,
119+
)
120+
121+
addBackgroundColorSpanIfApplicable(
122+
ops = ops,
123+
textAttributeProvider = textAttributeProvider,
124+
start = start,
125+
end = end,
126+
)
127+
128+
addLinkSpanIfApplicable(
129+
ops = ops,
130+
textAttributeProvider = textAttributeProvider,
131+
reactTag,
132+
start = start,
133+
end = end,
134+
)
135+
136+
addLetterSpacingSpanIfApplicable(
137+
ops = ops,
138+
textAttributeProvider = textAttributeProvider,
139+
start = start,
140+
end = end,
141+
)
142+
143+
addFontSizeSpanIfApplicable(
144+
ops = ops,
145+
textAttributeProvider = textAttributeProvider,
146+
start = start,
147+
end = end,
148+
)
149+
150+
addCustomStyleSpanIfApplicable(
151+
ops = ops,
152+
textAttributeProvider = textAttributeProvider,
153+
context,
154+
start = start,
155+
end = end,
156+
)
157+
158+
addUnderlineSpanIfApplicable(
159+
ops = ops,
160+
textAttributeProvider = textAttributeProvider,
161+
start = start,
162+
end = end,
163+
)
164+
165+
addStrikethroughSpanIfApplicable(
166+
ops = ops,
167+
textAttributeProvider = textAttributeProvider,
168+
start = start,
169+
end = end,
170+
)
171+
172+
addShadowStyleSpanIfApplicable(
173+
ops = ops,
174+
textAttributeProvider = textAttributeProvider,
175+
start = start,
176+
end = end,
177+
)
178+
179+
addLineHeightSpanIfApplicable(
180+
ops = ops,
181+
textAttributeProvider = textAttributeProvider,
182+
start = start,
183+
end = end,
184+
)
185+
186+
addReactTagSpan(
187+
ops = ops,
188+
start = start,
189+
end = end,
190+
reactTag = reactTag,
191+
)
107192
}
108193

109194
@JvmStatic

0 commit comments

Comments
 (0)