| 1084 | } |
| 1085 | |
| 1086 | TextPosition TextBox::insert(TextPosition pos, std::string_view textToInsert, const std::optional<TextStyleModifier::ModifierMap>& inputModMap) { |
| 1087 | TextPosition oldPos = pos = move(Movement::NOWHERE, pos); |
| 1088 | |
| 1089 | for(char c : textToInsert) { |
| 1090 | if(c == '\r') {} |
| 1091 | else if(c == '\n') { |
| 1092 | if(newlinesAllowed) { |
| 1093 | for(auto& [p, tStylesInPos] : tStyleMods) { |
| 1094 | if(p.fParagraphIndex > pos.fParagraphIndex) |
| 1095 | p.fParagraphIndex++; |
| 1096 | else if(p.fParagraphIndex == pos.fParagraphIndex && p.fTextByteIndex >= pos.fTextByteIndex && !(p.fTextByteIndex == pos.fTextByteIndex && p.fTextByteIndex == 0)) { |
| 1097 | p.fTextByteIndex = p.fTextByteIndex - pos.fTextByteIndex; |
| 1098 | p.fParagraphIndex++; |
| 1099 | } |
| 1100 | } |
| 1101 | paragraphs.insert(paragraphs.begin() + pos.fParagraphIndex + 1, ParagraphData{}); |
| 1102 | paragraphs[pos.fParagraphIndex + 1].pStyleData = paragraphs[pos.fParagraphIndex].pStyleData; |
| 1103 | paragraphs[pos.fParagraphIndex + 1].text = paragraphs[pos.fParagraphIndex].text.substr(pos.fTextByteIndex, paragraphs[pos.fParagraphIndex].text.size() - pos.fTextByteIndex); |
| 1104 | paragraphs[pos.fParagraphIndex].text.erase(pos.fTextByteIndex, paragraphs[pos.fParagraphIndex].text.size() - pos.fTextByteIndex); |
| 1105 | pos.fParagraphIndex++; |
| 1106 | pos.fTextByteIndex = 0; |
| 1107 | } |
| 1108 | } |
| 1109 | else if(c == '\t' && !newlinesAllowed) { |
| 1110 | for(auto& [p, tStylesInPos] : tStyleMods) { |
| 1111 | if(p.fParagraphIndex == pos.fParagraphIndex && p.fTextByteIndex >= pos.fTextByteIndex && !(p.fTextByteIndex == pos.fTextByteIndex && p.fTextByteIndex == 0)) |
| 1112 | p.fTextByteIndex += tabWidth; |
| 1113 | } |
| 1114 | paragraphs[pos.fParagraphIndex].text.insert(pos.fTextByteIndex, std::string(tabWidth, ' ')); |
| 1115 | pos.fTextByteIndex += tabWidth; |
| 1116 | } |
| 1117 | else { |
| 1118 | for(auto& [p, tStylesInPos] : tStyleMods) { |
| 1119 | if(p.fParagraphIndex == pos.fParagraphIndex && p.fTextByteIndex >= pos.fTextByteIndex && !(p.fTextByteIndex == pos.fTextByteIndex && p.fTextByteIndex == 0)) |
| 1120 | p.fTextByteIndex++; |
| 1121 | } |
| 1122 | paragraphs[pos.fParagraphIndex].text.insert(paragraphs[pos.fParagraphIndex].text.begin() + pos.fTextByteIndex, c); |
| 1123 | pos.fTextByteIndex++; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | if(!textToInsert.empty()) { |
| 1128 | remove_duplicate_text_style_mods(); |
| 1129 | if(inputModMap.has_value()) { |
| 1130 | for(const auto& [modType, modifier] : inputModMap.value()) |
| 1131 | set_text_style_modifier_between(oldPos, pos, modifier); |
| 1132 | } |
| 1133 | needsRebuild = true; |
| 1134 | } |
| 1135 | |
| 1136 | return pos; |
| 1137 | } |
| 1138 | |
| 1139 | TextPosition TextBox::remove(TextPosition p1, TextPosition p2) { |
| 1140 | auto [start, end] = get_start_end_text_pos(p1, p2); |
no test coverage detected