Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/completion/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ pub trait Completer: Send {
pub struct Suggestion {
/// String replacement that will be introduced to the the buffer
pub value: String,
/// If given, overrides `value` as text displayed to user
pub display_override: Option<String>,
/// Optional description for the replacement
pub description: Option<String>,
/// Optional style for the replacement
Expand All @@ -94,3 +96,10 @@ pub struct Suggestion {
/// Useful if using fuzzy matching.
pub match_indices: Option<Vec<usize>>,
}

impl Suggestion {
/// Get value to display to user for this suggestion
pub fn display_value(&self) -> &str {
self.display_override.as_ref().unwrap_or(&self.value)
}
}
19 changes: 12 additions & 7 deletions src/menu/columnar_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
core_editor::Editor,
menu_functions::{
can_partially_complete, completer_input, floor_char_boundary, get_match_indices,
replace_in_buffer, style_suggestion, truncate_no_ansi,
replace_in_buffer, style_suggestion, truncate_with_ansi,
},
painting::Painter,
Completer, Suggestion,
Expand Down Expand Up @@ -384,6 +384,7 @@ impl ColumnarMenu {
use_ansi_coloring: bool,
) -> String {
let selected = index == self.index();
let display_value = suggestion.display_value();
let empty_space = self.get_width().saturating_sub(self.display_widths[index]);

if use_ansi_coloring {
Expand All @@ -395,7 +396,7 @@ impl ColumnarMenu {
.unwrap_or(shortest_base);

let match_indices =
get_match_indices(&suggestion.value, &suggestion.match_indices, shortest_base);
get_match_indices(display_value, &suggestion.match_indices, shortest_base);

let left_text_size = self
.get_width()
Expand All @@ -409,7 +410,7 @@ impl ColumnarMenu {
} else {
&self.settings.color.match_style
};
let value_trunc = truncate_no_ansi(&suggestion.value, left_text_size);
let value_trunc = truncate_with_ansi(display_value, left_text_size);
let styled_value = style_suggestion(
&value_trunc,
&match_indices,
Expand All @@ -421,7 +422,7 @@ impl ColumnarMenu {
match &suggestion.description {
Some(desc) if description_size > 3 => {
let desc = desc.replace('\n', "");
let desc_trunc = truncate_no_ansi(desc.as_str(), description_size);
let desc_trunc = truncate_with_ansi(desc.as_str(), description_size);
if selected {
format!(
"{}{}{}{}{}{}{}",
Expand Down Expand Up @@ -462,7 +463,7 @@ impl ColumnarMenu {
format!(
"{}{:max$}{}",
marker,
&suggestion.value,
display_value,
description
.chars()
.take(empty_space)
Expand All @@ -478,7 +479,7 @@ impl ColumnarMenu {
format!(
"{}{}{:>empty$}",
marker,
&suggestion.value,
display_value,
"",
empty = empty_space.saturating_sub(marker.width()),
)
Expand Down Expand Up @@ -564,7 +565,11 @@ impl Menu for ColumnarMenu {
let (values, base_ranges) = completer.complete_with_base_ranges(&input, pos);

self.values = values;
self.display_widths = self.values.iter().map(|sugg| sugg.value.width()).collect();
self.display_widths = self
.values
.iter()
.map(|sugg| sugg.display_value().width())
.collect();
self.working_details.shortest_base_string = base_ranges
.iter()
.map(|range| {
Expand Down
27 changes: 15 additions & 12 deletions src/menu/description_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,13 @@ impl DescriptionMenu {
empty_space: usize,
use_ansi_coloring: bool,
) -> String {
let display_value = suggestion.display_value();
if use_ansi_coloring {
if index == self.index() {
format!(
"{}{}{}{:>empty$}{}",
self.settings.color.selected_text_style.prefix(),
&suggestion.value,
display_value,
RESET,
"",
self.end_of_line(column, index),
Expand All @@ -299,7 +300,7 @@ impl DescriptionMenu {
format!(
"{}{}{}{:>empty$}{}",
self.settings.color.text_style.prefix(),
&suggestion.value,
display_value,
RESET,
"",
self.end_of_line(column, index),
Expand All @@ -318,7 +319,7 @@ impl DescriptionMenu {
let line = format!(
"{}{}{:>empty$}{}",
marker,
&suggestion.value,
display_value,
"",
self.end_of_line(column, index),
empty = empty_space,
Expand Down Expand Up @@ -532,14 +533,14 @@ impl Menu for DescriptionMenu {
MenuEvent::PreviousPage | MenuEvent::NextPage => {}
}

let max_width = self.get_values().iter().fold(0, |acc, suggestion| {
let str_len = suggestion.value.len() + self.default_details.col_padding;
if str_len > acc {
str_len
} else {
acc
}
});
let max_width = self
.get_values()
.iter()
.map(|suggestion| {
suggestion.display_value().len() + self.default_details.col_padding
})
.max()
.unwrap_or(0);

// If no default width is found, then the total screen width is used to estimate
// the column width based on the default number of columns
Expand Down Expand Up @@ -646,7 +647,9 @@ impl Menu for DescriptionMenu {
// Correcting the enumerate index based on the number of skipped values
let index = index + skip_values;
let column = index as u16 % self.get_cols();
let empty_space = self.get_width().saturating_sub(suggestion.value.len());
let empty_space = self
.get_width()
.saturating_sub(suggestion.display_value().len());

self.create_entry_string(
suggestion,
Expand Down
12 changes: 7 additions & 5 deletions src/menu/ide_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
core_editor::Editor,
menu_functions::{
can_partially_complete, completer_input, floor_char_boundary, get_match_indices,
replace_in_buffer, style_suggestion, truncate_no_ansi,
replace_in_buffer, style_suggestion, truncate_with_ansi,
},
painting::Painter,
Completer, Suggestion,
Expand Down Expand Up @@ -495,13 +495,15 @@ impl IdeMenu {
.map(|border| border.vertical)
.unwrap_or_default();

let display_value = suggestion.display_value();

let padding_right = (self.working_details.completion_width as usize)
.saturating_sub(suggestion.value.width() + border_width + padding);
.saturating_sub(display_value.width() + border_width + padding);

let max_string_width =
(self.working_details.completion_width as usize).saturating_sub(border_width + padding);

let string = truncate_no_ansi(&suggestion.value, max_string_width);
let string = truncate_with_ansi(display_value, max_string_width);

if use_ansi_coloring {
// TODO(ysthakur): let the user strip quotes, rather than doing it here
Expand All @@ -512,7 +514,7 @@ impl IdeMenu {
.unwrap_or(shortest_base);

let match_indices =
get_match_indices(&suggestion.value, &suggestion.match_indices, shortest_base);
get_match_indices(display_value, &suggestion.match_indices, shortest_base);

let suggestion_style = suggestion.style.unwrap_or(self.settings.color.text_style);

Expand Down Expand Up @@ -677,7 +679,7 @@ impl Menu for IdeMenu {
self.longest_suggestion = self
.get_values()
.iter()
.map(|s| s.value.width())
.map(|s| s.display_value().width())
.max()
.unwrap_or_default();

Expand Down
6 changes: 3 additions & 3 deletions src/menu/list_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl ListMenu {
Some(total_lines) => {
let new_total_lines = total_lines
+ self.number_of_lines(
&suggestion.value,
suggestion.display_value(),
// to account for the index and the indicator e.g. 0: XXXX
painter.screen_width().saturating_sub(
self.indicator().width() as u16 + count_digits(lines),
Expand Down Expand Up @@ -517,7 +517,7 @@ impl Menu for ListMenu {
// to account for the the index and the indicator e.g. 0: XXXX
let ret = total_lines
+ self.number_of_lines(
&suggestion.value,
suggestion.display_value(),
terminal_columns.saturating_sub(
self.indicator().width() as u16 + count_digits(entry_index),
),
Expand All @@ -539,7 +539,7 @@ impl Menu for ListMenu {
.enumerate()
.map(|(index, suggestion)| {
// Final string with colors
let line = &suggestion.value;
let line = suggestion.display_value();
let line = if line.lines().count() > self.max_lines as usize {
let lines = line.lines().take(self.max_lines as usize).fold(
String::new(),
Expand Down
Loading