Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit e28cb32

Browse files
committed
Move SetComposingLength to TextRange::set_length
1 parent df5568d commit e28cb32

File tree

4 files changed

+45
-18
lines changed

4 files changed

+45
-18
lines changed

shell/platform/common/cpp/text_input_model.cc

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void TextInputModel::UpdateComposingText(const std::string& composing_text) {
7878
}
7979
DeleteSelected();
8080
text_.replace(composing_range_.start(), composing_range_.length(), text);
81-
SetComposingLength(text.length());
81+
composing_range_.set_length(text.length());
8282
selection_ = TextRange(composing_range_.end());
8383
}
8484

@@ -110,16 +110,6 @@ bool TextInputModel::DeleteSelected() {
110110
return true;
111111
}
112112

113-
void TextInputModel::SetComposingLength(size_t length) {
114-
if (composing_range_.reversed()) {
115-
size_t extent = composing_range_.extent();
116-
composing_range_ = TextRange(extent + length, extent);
117-
} else {
118-
size_t base = composing_range_.base();
119-
composing_range_ = TextRange(base, base + length);
120-
}
121-
}
122-
123113
void TextInputModel::AddCodePoint(char32_t c) {
124114
if (c <= 0xFFFF) {
125115
AddText(std::u16string({static_cast<char16_t>(c)}));
@@ -140,7 +130,7 @@ void TextInputModel::AddText(const std::u16string& text) {
140130
// Delete the current composing text, set the cursor to composing start.
141131
text_.erase(composing_range_.start(), composing_range_.length());
142132
selection_ = TextRange(composing_range_.start());
143-
SetComposingLength(text.length());
133+
composing_range_.set_length(text.length());
144134
}
145135
size_t position = selection_.position();
146136
text_.insert(position, text);
@@ -164,7 +154,7 @@ bool TextInputModel::Backspace() {
164154
text_.erase(position - count, count);
165155
selection_ = TextRange(position - count);
166156
if (composing_) {
167-
SetComposingLength(composing_range_.length() - count);
157+
composing_range_.set_length(composing_range_.length() - count);
168158
}
169159
return true;
170160
}
@@ -181,7 +171,7 @@ bool TextInputModel::Delete() {
181171
int count = IsLeadingSurrogate(text_.at(position)) ? 2 : 1;
182172
text_.erase(position, count);
183173
if (composing_) {
184-
SetComposingLength(composing_range_.length() - count);
174+
composing_range_.set_length(composing_range_.length() - count);
185175
}
186176
return true;
187177
}
@@ -224,7 +214,7 @@ bool TextInputModel::DeleteSurrounding(int offset_from_cursor, int count) {
224214

225215
// Adjust composing range.
226216
if (composing_) {
227-
SetComposingLength(composing_range_.length() - deleted_length);
217+
composing_range_.set_length(composing_range_.length() - deleted_length);
228218
}
229219
return true;
230220
}

shell/platform/common/cpp/text_input_model.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,6 @@ class TextInputModel {
172172
// reset to the start of the selected range.
173173
bool DeleteSelected();
174174

175-
// Adjusts the composing range to |length|.
176-
void SetComposingLength(size_t length);
177-
178175
// Returns the currently editable text range.
179176
//
180177
// In composing mode, returns the composing range; otherwise, returns a range

shell/platform/common/cpp/text_range.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ class TextRange {
4444
// Returns the length of the range.
4545
size_t length() const { return end() - start(); }
4646

47+
// Adjusts the length of the range relative to its start.
48+
//
49+
// For non-reversed ranges, the extent is adjusted relative to the base. For
50+
// reversed ranges, the base is adjusted relative to the extent.
51+
void set_length(size_t length) {
52+
if (base_ > extent_) {
53+
base_ = extent_ + length;
54+
} else {
55+
extent_ = base_ + length;
56+
}
57+
}
58+
4759
// Returns true if the range is of length 0.
4860
bool collapsed() const { return base_ == extent_; }
4961

shell/platform/common/cpp/text_range_unittests.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,34 @@ TEST(TextRange, TextRangeFromReversedRange) {
5050
EXPECT_FALSE(range.collapsed());
5151
}
5252

53+
TEST(TextRange, SetLengthZero) {
54+
TextRange range(3, 7);
55+
range.set_length(0);
56+
EXPECT_EQ(range.base(), size_t(3));
57+
EXPECT_EQ(range.extent(), size_t(3));
58+
}
59+
60+
TEST(TextRange, SetLengthNonZero) {
61+
TextRange range(3, 7);
62+
range.set_length(2);
63+
EXPECT_EQ(range.base(), size_t(3));
64+
EXPECT_EQ(range.extent(), size_t(5));
65+
}
66+
67+
TEST(TextRange, SetLengthZeroReversed) {
68+
TextRange range(7, 3);
69+
range.set_length(0);
70+
EXPECT_EQ(range.base(), size_t(3));
71+
EXPECT_EQ(range.extent(), size_t(3));
72+
}
73+
74+
TEST(TextRange, SetLengthNonZeroReversed) {
75+
TextRange range(7, 3);
76+
range.set_length(2);
77+
EXPECT_EQ(range.base(), size_t(5));
78+
EXPECT_EQ(range.extent(), size_t(3));
79+
}
80+
5381
TEST(TextRange, ContainsPreStartPosition) {
5482
TextRange range(2, 6);
5583
EXPECT_FALSE(range.Contains(1));

0 commit comments

Comments
 (0)