@@ -170,6 +170,7 @@ void TextEditor::render(const char* title, const ImVec2& size, bool border) {
170170 ImGui::PushStyleVar (ImGuiStyleVar_ItemSpacing, ImVec2 (0 .0f , 0 .0f ));
171171 ImGui::PushStyleColor (ImGuiCol_ChildBg, ImGui::ColorConvertU32ToFloat4 (palette.get (Color::background)));
172172 ImGui::BeginChild (title, size, border, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_NoNavInputs);
173+ lastRenderOrigin = ImGui::GetCursorScreenPos ();
173174
174175 // handle keyboard and mouse inputs
175176 handleKeyboardInputs ();
@@ -1312,6 +1313,26 @@ std::string TextEditor::getCursorText(size_t cursor) const {
13121313}
13131314
13141315
1316+ //
1317+ // TextEditor::GetWordAtScreenPos
1318+ //
1319+
1320+ std::string TextEditor::GetWordAtScreenPos (const ImVec2& screenPos) const {
1321+ // convert screen position to local coordinates using the origin saved during last Render()
1322+ auto local = screenPos - lastRenderOrigin;
1323+
1324+ // convert to text coordinates
1325+ Coordinate glyphCoordinate;
1326+ Coordinate cursorCoordinate;
1327+ document.normalizeCoordinate (local.y / glyphSize.y , (local.x - textOffset) / glyphSize.x , glyphCoordinate, cursorCoordinate);
1328+
1329+ // Find word boundaries and extract text
1330+ auto start = document.findWordStart (glyphCoordinate);
1331+ auto end = document.findWordEnd (glyphCoordinate);
1332+ return document.getSectionText (start, end);
1333+ }
1334+
1335+
13151336//
13161337// TextEditor::makeCursorVisible
13171338//
@@ -2361,19 +2382,11 @@ TextEditor::Palette TextEditor::defaultPalette = TextEditor::GetDarkPalette();
23612382//
23622383
23632384TextEditor::Coordinate TextEditor::Cursor::adjustCoordinateForInsert (Coordinate coordinate, Coordinate insertStart, Coordinate insertEnd) {
2364- if (insertStart.line == insertEnd.line ) {
2365- if (coordinate.line == insertEnd.line ) {
2366- coordinate.column += insertEnd.column - insertStart.column ;
2367- }
2368-
2369- } else {
2370- if (coordinate.line == insertStart.line ) {
2371- coordinate.column += insertEnd.column - insertStart.column ;
2372- }
2373-
2374- coordinate.line += insertEnd.line - insertStart.line ;
2385+ if (coordinate.line == insertStart.line ) {
2386+ coordinate.column += insertEnd.column - insertStart.column ;
23752387 }
23762388
2389+ coordinate.line += insertEnd.line - insertStart.line ;
23772390 return coordinate;
23782391}
23792392
@@ -6240,19 +6253,24 @@ bool TextEditor::CodePoint::isWhiteSpace(ImWchar codepoint) {
62406253
62416254bool TextEditor::CodePoint::isWord (ImWchar codepoint) {
62426255 if (codepoint < 0x7f ) {
6243- return (static_cast <unsigned >((codepoint | 32 ) - ' a' ) < 26 ) || (static_cast <unsigned >(codepoint - ' 0' ) < 10 );
6256+ return
6257+ (static_cast <unsigned >((codepoint | 32 ) - ' a' ) < 26 ) ||
6258+ (static_cast <unsigned >(codepoint - ' 0' ) < 10 ) ||
6259+ codepoint == ' _' ;
62446260
62456261#if defined(IMGUI_USE_WCHAR32)
62466262 } else if (codepoint >= 0x10000 ) {
62476263 return
62486264 rangeContains (letters32, static_cast <ImWchar32>(codepoint)) ||
6249- rangeContains (numbers32, static_cast <ImWchar32>(codepoint));
6265+ rangeContains (numbers32, static_cast <ImWchar32>(codepoint)) ||
6266+ codepoint == ' _' ;
62506267#endif
62516268
62526269 } else {
62536270 return
62546271 rangeContains (letters16, static_cast <ImWchar16>(codepoint)) ||
6255- rangeContains (numbers16, static_cast <ImWchar16>(codepoint));
6272+ rangeContains (numbers16, static_cast <ImWchar16>(codepoint)) ||
6273+ codepoint == ' _' ;
62566274 }
62576275}
62586276
0 commit comments