Tweak prompt auto show completion

* Auto show completion even before the first key is pressed
* Auto show next completions when validating a single choice completion
This commit is contained in:
Maxime Coste 2013-11-12 18:56:48 +00:00
parent 6877ae151f
commit 67452c3441

View File

@ -317,6 +317,8 @@ public:
m_completer(completer), m_callback(callback) m_completer(completer), m_callback(callback)
{ {
m_history_it = ms_history[m_prompt].end(); m_history_it = ms_history[m_prompt].end();
if (context().options()["autoshowcompl"].get<bool>())
refresh_completions();
display(); display();
} }
@ -324,6 +326,7 @@ public:
{ {
std::vector<String>& history = ms_history[m_prompt]; std::vector<String>& history = ms_history[m_prompt];
const String& line = m_line_editor.line(); const String& line = m_line_editor.line();
bool showcompl = false;
if (m_mode == Mode::InsertReg) if (m_mode == Mode::InsertReg)
{ {
@ -403,16 +406,11 @@ public:
// first try, we need to ask our completer for completions // first try, we need to ask our completer for completions
if (candidates.empty()) if (candidates.empty())
{ {
m_completions = m_completer(context(), CompletionFlags::None, line, refresh_completions();
line.byte_count_to(m_line_editor.cursor_pos()));
if (candidates.empty()) if (candidates.empty())
return; return;
context().ui().menu_hide();
DisplayCoord menu_pos{ context().ui().dimensions().line, 0_char };
context().ui().menu_show(candidates, menu_pos, get_color("MenuForeground"),
get_color("MenuBackground"), MenuStyle::Prompt);
bool use_common_prefix = context().options()["complete_prefix"].get<bool>(); bool use_common_prefix = context().options()["complete_prefix"].get<bool>();
String prefix = use_common_prefix ? common_prefix(candidates) : String(); String prefix = use_common_prefix ? common_prefix(candidates) : String();
if (m_completions.end - m_completions.start > prefix.length()) if (m_completions.end - m_completions.start > prefix.length())
@ -442,27 +440,23 @@ public:
// when we have only one completion candidate, make next tab complete // when we have only one completion candidate, make next tab complete
// from the new content. // from the new content.
if (candidates.size() == 1) if (candidates.size() == 1)
{
m_current_completion = -1;
candidates.clear(); candidates.clear();
showcompl = true;
}
} }
else else
{ {
m_line_editor.handle_key(key); m_line_editor.handle_key(key);
m_current_completion = -1; m_current_completion = -1;
context().ui().menu_hide(); context().ui().menu_hide();
showcompl = true;
if (context().options()["autoshowcompl"].get<bool>()) try
{
m_completions = m_completer(context(), CompletionFlags::Fast, line,
line.byte_count_to(m_line_editor.cursor_pos()));
CandidateList& candidates = m_completions.candidates;
if (not candidates.empty())
{
DisplayCoord menu_pos{ context().ui().dimensions().line, 0_char };
context().ui().menu_show(candidates, menu_pos, get_color("MenuForeground"),
get_color("MenuBackground"), MenuStyle::Prompt);
}
} catch (runtime_error&) {}
} }
if (showcompl and context().options()["autoshowcompl"].get<bool>())
refresh_completions();
display(); display();
m_callback(line, PromptEvent::Change, context()); m_callback(line, PromptEvent::Change, context());
} }
@ -484,6 +478,23 @@ public:
KeymapMode keymap_mode() const override { return KeymapMode::Prompt; } KeymapMode keymap_mode() const override { return KeymapMode::Prompt; }
private: private:
void refresh_completions()
{
try
{
const String& line = m_line_editor.line();
m_completions = m_completer(context(), CompletionFlags::Fast, line,
line.byte_count_to(m_line_editor.cursor_pos()));
CandidateList& candidates = m_completions.candidates;
if (not candidates.empty())
{
DisplayCoord menu_pos{ context().ui().dimensions().line, 0_char };
context().ui().menu_show(candidates, menu_pos, get_color("MenuForeground"),
get_color("MenuBackground"), MenuStyle::Prompt);
}
} catch (runtime_error&) {}
}
void display() const void display() const
{ {
auto display_line = m_line_editor.build_display_line(); auto display_line = m_line_editor.build_display_line();