| 2315 | } |
| 2316 | |
| 2317 | void CodeEdit::confirm_code_completion(bool p_replace) { |
| 2318 | if (!is_editable() || !code_completion_active) { |
| 2319 | return; |
| 2320 | } |
| 2321 | |
| 2322 | if (GDVIRTUAL_CALL(_confirm_code_completion, p_replace)) { |
| 2323 | return; |
| 2324 | } |
| 2325 | |
| 2326 | char32_t caret_last_completion_char = 0; |
| 2327 | begin_complex_operation(); |
| 2328 | begin_multicaret_edit(); |
| 2329 | |
| 2330 | for (int i = 0; i < get_caret_count(); i++) { |
| 2331 | if (multicaret_edit_ignore_caret(i)) { |
| 2332 | continue; |
| 2333 | } |
| 2334 | int caret_line = get_caret_line(i); |
| 2335 | |
| 2336 | const String &insert_text = code_completion_options[code_completion_current_selected].insert_text; |
| 2337 | const String &display_text = code_completion_options[code_completion_current_selected].display; |
| 2338 | |
| 2339 | if (p_replace) { |
| 2340 | // Find end of current section. |
| 2341 | const String line = get_line(caret_line); |
| 2342 | int caret_col = get_caret_column(i); |
| 2343 | int caret_remove_line = caret_line; |
| 2344 | |
| 2345 | bool merge_text = true; |
| 2346 | int in_string = is_in_string(caret_line, caret_col); |
| 2347 | if (in_string != -1) { |
| 2348 | Point2 string_end = get_delimiter_end_position(caret_line, caret_col); |
| 2349 | if (string_end.x != -1) { |
| 2350 | merge_text = false; |
| 2351 | caret_remove_line = string_end.y; |
| 2352 | caret_col = string_end.x - 1; |
| 2353 | } |
| 2354 | } |
| 2355 | |
| 2356 | if (merge_text) { |
| 2357 | for (; caret_col < line.length(); caret_col++) { |
| 2358 | if (is_symbol(line[caret_col])) { |
| 2359 | break; |
| 2360 | } |
| 2361 | } |
| 2362 | } |
| 2363 | |
| 2364 | // Replace. |
| 2365 | remove_text(caret_line, get_caret_column(i) - code_completion_base.length(), caret_remove_line, caret_col); |
| 2366 | insert_text_at_caret(insert_text, i); |
| 2367 | } else { |
| 2368 | // Get first non-matching char. |
| 2369 | const String line = get_line(caret_line); |
| 2370 | int caret_col = get_caret_column(i); |
| 2371 | int matching_chars = code_completion_base.length(); |
| 2372 | for (; matching_chars <= insert_text.length(); matching_chars++) { |
| 2373 | if (caret_col >= line.length() || line[caret_col] != insert_text[matching_chars]) { |
| 2374 | break; |
no test coverage detected