insert_completer.cc (22799B)
1 #include "insert_completer.hh" 2 3 #include "buffer_manager.hh" 4 #include "buffer_utils.hh" 5 #include "debug.hh" 6 #include "command_manager.hh" 7 #include "changes.hh" 8 #include "context.hh" 9 #include "client.hh" 10 #include "display_buffer.hh" 11 #include "face_registry.hh" 12 #include "file.hh" 13 #include "hook_manager.hh" 14 #include "option_manager.hh" 15 #include "regex.hh" 16 #include "window.hh" 17 #include "word_db.hh" 18 #include "word_splitter.hh" 19 #include "option_types.hh" 20 #include "utf8_iterator.hh" 21 #include "user_interface.hh" 22 23 #include <utility> 24 25 namespace Kakoune 26 { 27 28 using StringList = Vector<String, MemoryDomain::Options>; 29 30 String option_to_string(const InsertCompleterDesc& opt) 31 { 32 switch (opt.mode) 33 { 34 case InsertCompleterDesc::Word: 35 return "word=" + (opt.param ? *opt.param : ""); 36 case InsertCompleterDesc::Filename: 37 return "filename"; 38 case InsertCompleterDesc::Option: 39 return "option=" + (opt.param ? *opt.param : ""); 40 case InsertCompleterDesc::Line: 41 return "line=" + (opt.param ? *opt.param : ""); 42 } 43 kak_assert(false); 44 return ""; 45 } 46 47 InsertCompleterDesc option_from_string(Meta::Type<InsertCompleterDesc>, StringView str) 48 { 49 if (str.substr(0_byte, 7_byte) == "option=") 50 return {InsertCompleterDesc::Option, str.substr(7_byte).str()}; 51 else if (str.substr(0_byte, 5_byte) == "word=") 52 { 53 auto param = str.substr(5_byte); 54 if (param == "all" or param == "buffer") 55 return {InsertCompleterDesc::Word, param.str()}; 56 } 57 else if (str == "filename") 58 return {InsertCompleterDesc::Filename, {}}; 59 else if (str.substr(0_byte, 5_byte) == "line=") 60 { 61 auto param = str.substr(5_byte); 62 if (param == "all" or param == "buffer") 63 return {InsertCompleterDesc::Line, param.str()}; 64 } 65 throw runtime_error(format("invalid completer description: '{}'", str)); 66 } 67 68 namespace 69 { 70 71 template<bool other_buffers> 72 InsertCompletion complete_word(const SelectionList& sels, 73 const OptionManager& options, 74 const FaceRegistry& faces) 75 { 76 ConstArrayView<Codepoint> extra_word_chars = options["extra_word_chars"].get<Vector<Codepoint, MemoryDomain::Options>>(); 77 auto is_word_pred = [extra_word_chars](Codepoint c) { return is_word(c, extra_word_chars); }; 78 79 const Buffer& buffer = sels.buffer(); 80 BufferCoord cursor_pos = sels.main().cursor(); 81 82 using Utf8It = utf8::iterator<BufferIterator>; 83 Utf8It pos{buffer.iterator_at(cursor_pos), buffer}; 84 if (pos == buffer.begin() or not is_word_pred(*(pos-1))) 85 return {}; 86 87 BufferCoord word_begin; 88 StringView prefix; 89 HashMap<StringView, int> sel_word_counts; 90 for (int i = 0; i < sels.size(); ++i) 91 { 92 int len = 0; 93 auto is_short_enough_word = [&] (Codepoint c) { return len++ < WordSplitter::max_word_len and is_word_pred(c); }; 94 95 Utf8It end{buffer.iterator_at(sels[i].cursor()), buffer}; 96 Utf8It begin = end-1; 97 if (not skip_while_reverse(begin, buffer.begin(), is_short_enough_word) and 98 begin < end) // (begin might == end if end == buffer.begin()) 99 ++begin; 100 101 if (i == sels.main_index()) 102 { 103 word_begin = begin.base().coord(); 104 prefix = buffer.substr(word_begin, end.base().coord()); 105 } 106 107 skip_while(end, buffer.end(), is_short_enough_word); 108 109 if (len <= WordSplitter::max_word_len) 110 { 111 StringView word = buffer.substr(begin.base().coord(), end.base().coord()); 112 ++sel_word_counts[word]; 113 } 114 } 115 116 struct RankedMatchAndBuffer : RankedMatch 117 { 118 RankedMatchAndBuffer(RankedMatch m, const Buffer* b) 119 : RankedMatch{std::move(m)}, buffer{b} {} 120 121 using RankedMatch::operator==; 122 using RankedMatch::operator<; 123 bool operator==(StringView other) const { return candidate() == other; } 124 125 const Buffer* buffer; 126 }; 127 128 auto& word_db = get_word_db(buffer); 129 auto matches = word_db.find_matching(prefix) 130 | transform([&](auto& m) { return RankedMatchAndBuffer{m, &buffer}; }) 131 | gather<Vector<RankedMatchAndBuffer>>(); 132 // Remove words that are being edited 133 for (auto& word_count : sel_word_counts) 134 { 135 if (word_db.get_word_occurences(word_count.key) <= word_count.value) 136 unordered_erase(matches, word_count.key); 137 } 138 139 if (other_buffers) 140 { 141 for (const auto& buf : BufferManager::instance()) 142 { 143 if (buf.get() == &buffer or buf->flags() & Buffer::Flags::Debug) 144 continue; 145 for (auto& m : get_word_db(*buf).find_matching(prefix) | 146 // filter out words that are not considered words for the current buffer 147 filter([&](auto& rm) { 148 auto&& c = rm.candidate(); 149 return std::all_of(utf8::iterator{c.begin(), c}, 150 utf8::iterator{c.end(), c}, 151 is_word_pred); })) 152 matches.push_back({ m, buf.get() }); 153 } 154 } 155 156 using StaticWords = Vector<String, MemoryDomain::Options>; 157 for (auto& word : options["static_words"].get<StaticWords>()) 158 if (RankedMatch match{word, prefix}) 159 matches.emplace_back(match, nullptr); 160 161 unordered_erase(matches, prefix); 162 const auto longest = accumulate(matches, 0_char, 163 [](const CharCount& lhs, const RankedMatchAndBuffer& rhs) 164 { return std::max(lhs, rhs.candidate().char_length()); }); 165 166 auto limit = [](StringView s, ColumnCount l) { return s.column_length() <= l ? s.str() : "…" + s.substr(s.column_length() - (l + 1)); }; 167 constexpr size_t max_count = 100; 168 // Gather best max_count matches 169 InsertCompletion::CandidateList candidates; 170 candidates.reserve(std::min(matches.size(), max_count) + 1); 171 172 for_n_best(matches, max_count, [](auto& lhs, auto& rhs) { return rhs < lhs; }, 173 [&](RankedMatchAndBuffer& m) { 174 if (not candidates.empty() and candidates.back().completion == m.candidate()) 175 return false; 176 DisplayLine menu_entry; 177 if (other_buffers and m.buffer) 178 { 179 const auto pad_len = longest + 1 - m.candidate().char_length(); 180 menu_entry.push_back({ m.candidate().str(), {} }); 181 menu_entry.push_back({ String{' ', pad_len}, {} }); 182 menu_entry.push_back({ limit(m.buffer->display_name(), 20), faces["MenuInfo"] }); 183 } 184 else 185 menu_entry.push_back({ m.candidate().str(), {} }); 186 187 candidates.push_back({m.candidate().str(), "", std::move(menu_entry)}); 188 return true; 189 }); 190 191 return { std::move(candidates), word_begin, cursor_pos, buffer.timestamp() }; 192 } 193 194 template<bool require_slash> 195 InsertCompletion complete_filename(const SelectionList& sels, 196 const OptionManager& options, 197 const FaceRegistry&) 198 { 199 const Buffer& buffer = sels.buffer(); 200 auto pos = buffer.iterator_at(sels.main().cursor()); 201 auto begin = pos; 202 203 auto is_filename = [](char c) 204 { 205 return isalnum(c) or c == '/' or c == '.' or c == '_' or c == '-'; 206 }; 207 while (begin != buffer.begin() and is_filename(*(begin-1))) 208 --begin; 209 210 if (begin != buffer.begin() and *begin == '/' and *(begin-1) == '~') 211 --begin; 212 213 StringView prefix = buffer.substr(begin.coord(), pos.coord()); 214 if (require_slash and not contains(prefix, '/')) 215 return {}; 216 217 // Do not try to complete in that case as its unlikely to be a filename, 218 // and triggers network host search of cygwin. 219 if (prefix.substr(0_byte, 2_byte) == "//") 220 return {}; 221 222 InsertCompletion::CandidateList candidates; 223 if (prefix.front() == '/' or prefix.front() == '~') 224 { 225 for (auto& filename : Kakoune::complete_filename(prefix, 226 options["ignored_files"].get<Regex>())) 227 candidates.push_back({ filename, "", {filename, {}} }); 228 } 229 else 230 { 231 Vector<String> visited_dirs; 232 for (auto dir : options["path"].get<StringList>()) 233 { 234 dir = real_path(parse_filename(dir, (buffer.flags() & Buffer::Flags::File) ? 235 split_path(buffer.filename()).first : StringView{})); 236 237 if (not dir.empty() and dir.back() != '/') 238 dir += '/'; 239 240 if (contains(visited_dirs, dir)) 241 continue; 242 243 for (auto& filename : Kakoune::complete_filename(dir + prefix, 244 options["ignored_files"].get<Regex>())) 245 { 246 StringView candidate = filename.substr(dir.length()); 247 candidates.push_back({ candidate.str(), "", {candidate.str(), {}} }); 248 } 249 250 visited_dirs.push_back(std::move(dir)); 251 } 252 } 253 if (candidates.empty()) 254 return {}; 255 return { std::move(candidates), begin.coord(), pos.coord(), buffer.timestamp() }; 256 } 257 258 InsertCompletion complete_option(const SelectionList& sels, 259 const OptionManager& options, 260 const FaceRegistry& faces, 261 StringView option_name) 262 { 263 const Buffer& buffer = sels.buffer(); 264 BufferCoord cursor_pos = sels.main().cursor(); 265 266 const CompletionList& opt = options[option_name].get<CompletionList>(); 267 if (opt.list.empty()) 268 return {}; 269 270 auto& desc = opt.prefix; 271 static const Regex re(R"((\d+)\.(\d+)(?:\+(\d+))?@(\d+))"); 272 MatchResults<String::const_iterator> match; 273 if (not regex_match(desc.begin(), desc.end(), match, re)) 274 return {}; 275 276 BufferCoord coord{str_to_int({match[1].first, match[1].second}) - 1, 277 str_to_int({match[2].first, match[2].second}) - 1}; 278 if (not buffer.is_valid(coord)) 279 return {}; 280 size_t timestamp = (size_t)str_to_int({match[4].first, match[4].second}); 281 auto changes = buffer.changes_since(timestamp); 282 if (any_of(changes, [&](auto&& change) { return change.begin < coord; })) 283 return {}; 284 285 if (cursor_pos.line != coord.line or cursor_pos.column < coord.column) 286 return {}; 287 288 const ColumnCount tabstop = options["tabstop"].get<int>(); 289 const ColumnCount column = get_column(buffer, tabstop, cursor_pos); 290 291 struct RankedMatchAndInfo : RankedMatch 292 { 293 using RankedMatch::RankedMatch; 294 using RankedMatch::operator==; 295 using RankedMatch::operator<; 296 297 StringView on_select; 298 DisplayLine menu_entry; 299 }; 300 301 StringView query = buffer.substr(coord, cursor_pos); 302 Vector<RankedMatchAndInfo> matches; 303 304 for (auto&& [i, candidate] : opt.list | enumerate()) 305 { 306 if (RankedMatchAndInfo match{std::get<0>(candidate), query}) 307 { 308 match.set_input_sequence_number(i); 309 match.on_select = std::get<1>(candidate); 310 auto& menu = std::get<2>(candidate); 311 match.menu_entry = not menu.empty() ? 312 parse_display_line(expand_tabs(menu, tabstop, column), faces) 313 : DisplayLine{String{}, {}}; 314 315 matches.push_back(std::move(match)); 316 } 317 } 318 319 constexpr size_t max_count = 100; 320 // Gather best max_count matches 321 InsertCompletion::CandidateList candidates; 322 candidates.reserve(std::min(matches.size(), max_count) + 1); 323 324 for_n_best(matches, max_count, [](auto& lhs, auto& rhs) { return rhs < lhs; }, 325 [&](RankedMatchAndInfo& m) { 326 if (not candidates.empty() 327 and candidates.back().completion == m.candidate() 328 and candidates.back().on_select == m.on_select) 329 return false; 330 candidates.push_back({ m.candidate().str(), m.on_select.str(), 331 std::move(m.menu_entry) }); 332 return true; 333 }); 334 335 auto end = cursor_pos; 336 if (match[3].matched) 337 { 338 ByteCount len = str_to_int({match[3].first, match[3].second}); 339 end = buffer.advance(coord, len); 340 } 341 return { std::move(candidates), coord, end, timestamp }; 342 } 343 344 template<bool other_buffers> 345 InsertCompletion complete_line(const SelectionList& sels, 346 const OptionManager& options, 347 const FaceRegistry&) 348 { 349 const Buffer& buffer = sels.buffer(); 350 BufferCoord cursor_pos = sels.main().cursor(); 351 352 const ColumnCount tabstop = options["tabstop"].get<int>(); 353 const ColumnCount column = get_column(buffer, tabstop, cursor_pos); 354 355 auto trim_leading_whitespaces = [](StringView s) { 356 utf8::iterator it{s.begin(), s}; 357 while (it != s.end() and is_horizontal_blank(*it)) 358 ++it; 359 return StringView{it.base(), s.end()}; 360 }; 361 362 StringView prefix = trim_leading_whitespaces(buffer[cursor_pos.line].substr(0_byte, cursor_pos.column)); 363 BufferCoord replace_begin = buffer.advance(cursor_pos, -prefix.length()); 364 InsertCompletion::CandidateList candidates; 365 366 auto add_candidates = [&](const Buffer& buf) { 367 for (LineCount l = 0_line; l < buf.line_count(); ++l) 368 { 369 if (&buf == &buffer and l == cursor_pos.line) 370 continue; 371 372 StringView line = buf[l]; 373 StringView candidate = trim_leading_whitespaces(line.substr(0_byte, line.length()-1)); 374 375 if (candidate.length() == 0) 376 continue; 377 378 if (prefix == candidate.substr(0_byte, prefix.length())) 379 { 380 candidates.push_back({candidate.str(), "", {expand_tabs(candidate, tabstop, column), {}} }); 381 // perf: it's unlikely the user intends to search among >10 candidates anyway 382 if (candidates.size() == 100) 383 break; 384 } 385 } 386 }; 387 388 add_candidates(buffer); 389 390 if (other_buffers) 391 { 392 for (const auto& buf : BufferManager::instance()) 393 { 394 if (buf.get() != &buffer and not (buf->flags() & Buffer::Flags::Debug)) 395 add_candidates(*buf); 396 } 397 } 398 399 if (candidates.empty()) 400 return {}; 401 std::sort(candidates.begin(), candidates.end()); 402 candidates.erase(std::unique(candidates.begin(), candidates.end()), candidates.end()); 403 return { std::move(candidates), replace_begin, cursor_pos, buffer.timestamp() }; 404 } 405 406 } 407 408 InsertCompleter::InsertCompleter(Context& context) 409 : m_context(context), 410 // local scopes might go away before completion ends, make sure to register on a long lived one 411 m_options(context.scope(false).options()), 412 m_faces(context.scope(false).faces()) 413 { 414 m_options.register_watcher(*this); 415 } 416 417 InsertCompleter::~InsertCompleter() 418 { 419 m_options.unregister_watcher(*this); 420 } 421 422 void InsertCompleter::select(int index, bool relative, FunctionRef<void (Key)> record_key) 423 { 424 m_enabled = true; 425 if (not setup_ifn()) 426 return; 427 428 auto& buffer = m_context.buffer(); 429 m_current_candidate = (relative ? m_current_candidate + index : index) % (int)m_completions.candidates.size(); 430 if (m_current_candidate < 0) 431 m_current_candidate += m_completions.candidates.size(); 432 const InsertCompletion::Candidate& candidate = m_completions.candidates[m_current_candidate]; 433 auto& selections = m_context.selections(); 434 const auto& cursor_pos = selections.main().cursor(); 435 const auto prefix_len = buffer.distance(m_completions.begin, cursor_pos); 436 const auto suffix_len = std::max(0_byte, buffer.distance(cursor_pos, m_completions.end)); 437 438 auto ref = buffer.string(m_completions.begin, m_completions.end); 439 Vector<BufferRange> ranges; 440 for (auto& sel : selections) 441 { 442 auto pos = buffer.iterator_at(sel.cursor()); 443 if (pos.coord().column >= prefix_len and (pos + suffix_len) != buffer.end() and std::equal(ref.begin(), ref.end(), pos - prefix_len)) 444 ranges.push_back({(pos - prefix_len).coord(), (pos + suffix_len).coord()}); 445 } 446 replace(buffer, ranges, candidate.completion); 447 448 selections.update(); 449 m_completions.end = cursor_pos; 450 m_completions.begin = buffer.advance(cursor_pos, -candidate.completion.length()); 451 m_completions.timestamp = buffer.timestamp(); 452 m_inserted_ranges = std::move(ranges); 453 454 if (m_context.has_client()) 455 { 456 m_context.client().menu_select(m_current_candidate); 457 } 458 459 { 460 for (auto i = 0_byte; i < prefix_len; ++i) 461 record_key(Key::Backspace); 462 for (auto i = 0_byte; i < suffix_len; ++i) 463 record_key(Key::Delete); 464 for (auto& c : candidate.completion) 465 record_key(c); 466 } 467 468 if (not candidate.on_select.empty()) 469 CommandManager::instance().execute(candidate.on_select, m_context); 470 } 471 472 void InsertCompleter::update(bool allow_implicit) 473 { 474 m_enabled = allow_implicit; 475 if (m_explicit_completer and try_complete(m_explicit_completer)) 476 return; 477 478 reset(); 479 setup_ifn(); 480 } 481 482 auto& get_first(BufferRange& range) { return range.begin; } 483 auto& get_last(BufferRange& range) { return range.end; } 484 485 bool InsertCompleter::has_candidate_selected() const 486 { 487 return m_completions.is_valid() and m_current_candidate >= 0 and m_current_candidate < m_completions.candidates.size() - 1; 488 } 489 490 void InsertCompleter::try_accept() 491 { 492 if (has_candidate_selected()) 493 reset(); 494 } 495 496 void InsertCompleter::reset() 497 { 498 if (not m_explicit_completer and not m_completions.is_valid()) 499 return; 500 501 String hook_param; 502 if (m_context.has_client() and has_candidate_selected()) 503 { 504 auto& buffer = m_context.buffer(); 505 update_ranges(buffer, m_completions.timestamp, m_inserted_ranges); 506 m_completions.timestamp = buffer.timestamp(); 507 508 hook_param = join(m_inserted_ranges | filter([](auto&& r) { return not r.empty(); }) | transform([&](auto&& r) { 509 return selection_to_string(ColumnType::Byte, buffer, {r.begin, buffer.char_prev(r.end)}); 510 }), ' '); 511 } 512 513 m_explicit_completer = nullptr; 514 m_completions = InsertCompletion{}; 515 m_inserted_ranges.clear(); 516 if (m_context.has_client()) 517 { 518 m_context.client().menu_hide(); 519 m_context.client().info_hide(); 520 m_context.hooks().run_hook(Hook::InsertCompletionHide, hook_param, m_context); 521 } 522 } 523 524 bool InsertCompleter::setup_ifn() 525 { 526 if (not m_enabled) 527 return false; 528 if (not m_completions.is_valid()) 529 { 530 auto& completers = m_options["completers"].get<InsertCompleterDescList>(); 531 for (auto& completer : completers) 532 { 533 if (completer.mode == InsertCompleterDesc::Filename and 534 try_complete(complete_filename<true>)) 535 return true; 536 if (completer.mode == InsertCompleterDesc::Option and 537 try_complete([&](const SelectionList& sels, 538 const OptionManager& options, 539 const FaceRegistry& faces) { 540 return complete_option(sels, options, faces, *completer.param); 541 })) 542 return true; 543 if (completer.mode == InsertCompleterDesc::Word and 544 *completer.param == "buffer" and 545 try_complete(complete_word<false>)) 546 return true; 547 if (completer.mode == InsertCompleterDesc::Word and 548 *completer.param == "all" and 549 try_complete(complete_word<true>)) 550 return true; 551 if (completer.mode == InsertCompleterDesc::Line and 552 *completer.param == "buffer" and 553 try_complete(complete_line<false>)) 554 return true; 555 if (completer.mode == InsertCompleterDesc::Line and 556 *completer.param == "all" and 557 try_complete(complete_line<true>)) 558 return true; 559 } 560 return false; 561 } 562 return true; 563 } 564 565 void InsertCompleter::menu_show() 566 { 567 if (not m_context.has_client()) 568 return; 569 570 Vector<DisplayLine> menu_entries; 571 for (auto& candidate : m_completions.candidates) 572 menu_entries.push_back(candidate.menu_entry); 573 574 m_context.client().menu_show(std::move(menu_entries), m_completions.begin, 575 MenuStyle::Inline); 576 m_context.client().menu_select(m_current_candidate); 577 m_context.hooks().run_hook(Hook::InsertCompletionShow, "", m_context); 578 } 579 580 void InsertCompleter::on_option_changed(const Option& opt) 581 { 582 // Do not reset the menu if the user has selected an entry 583 if (not m_completions.candidates.empty() and 584 m_current_candidate != m_completions.candidates.size() - 1) 585 return; 586 587 const auto& completers = m_options["completers"].get<InsertCompleterDescList>(); 588 for (auto& completer : completers) 589 { 590 if (completer.mode == InsertCompleterDesc::Option and 591 *completer.param == opt.name()) 592 { 593 reset(); 594 setup_ifn(); 595 break; 596 } 597 } 598 } 599 600 template<typename Func> 601 bool InsertCompleter::try_complete(Func complete_func) 602 { 603 auto& sels = m_context.selections(); 604 try 605 { 606 reset(); 607 m_completions = complete_func(sels, m_options, m_faces); 608 } 609 catch (runtime_error& e) 610 { 611 write_to_debug_buffer(format("error while trying to run completer: {}", e.what())); 612 return false; 613 } 614 if (not m_completions.is_valid()) 615 return false; 616 617 kak_assert(m_completions.begin <= sels.main().cursor()); 618 m_current_candidate = m_completions.candidates.size(); 619 menu_show(); 620 m_completions.candidates.push_back({sels.buffer().string(m_completions.begin, m_completions.end), "", {}}); 621 return true; 622 } 623 624 void InsertCompleter::explicit_file_complete() 625 { 626 try_complete(complete_filename<false>); 627 m_explicit_completer = complete_filename<false>; 628 } 629 630 void InsertCompleter::explicit_word_buffer_complete() 631 { 632 try_complete(complete_word<false>); 633 m_explicit_completer = complete_word<false>; 634 } 635 636 void InsertCompleter::explicit_word_all_complete() 637 { 638 try_complete(complete_word<true>); 639 m_explicit_completer = complete_word<true>; 640 } 641 642 void InsertCompleter::explicit_line_buffer_complete() 643 { 644 try_complete(complete_line<false>); 645 m_explicit_completer = complete_line<false>; 646 } 647 648 void InsertCompleter::explicit_line_all_complete() 649 { 650 try_complete(complete_line<true>); 651 m_explicit_completer = complete_line<true>; 652 } 653 654 }