Skip to content

Commit

Permalink
various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
janhrastnik committed Nov 3, 2020
1 parent c9e9fcf commit 2b44031
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 27 deletions.
34 changes: 14 additions & 20 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl Renderer {

pub fn render_prompt(&mut self, view: &View, prompt: &Prompt) {
// completion
if let Some(completion) = &prompt.completion {
if !prompt.completion.is_empty() {
// TODO: find out better way of clearing individual lines of the screen
for i in (3..7) {
self.surface.set_string(
Expand All @@ -255,10 +255,10 @@ impl Renderer {
);
let mut row = 0;
let mut col = 0;
let max_row: u16 = self.size.0 / BASE_WIDTH;
let max_col: u16 = self.size.0 / BASE_WIDTH;
// TODO: this will crash if there are too many cols added
// TODO: set char limit
for (i, command) in completion.iter().enumerate() {
for (i, command) in prompt.completion.iter().enumerate() {
let color = if prompt.completion_selection_index.is_some()
&& i == prompt.completion_selection_index.unwrap()
{
Expand All @@ -267,18 +267,18 @@ impl Renderer {
self.text_color
};
self.surface.set_stringn(
1 + row * BASE_WIDTH,
self.size.1 - 6 + col as u16,
1 + col * BASE_WIDTH,
self.size.1 - 6 + row as u16,
&command,
BASE_WIDTH as usize - 1,
color,
);
col += 1;
if col > 3 {
col = 0;
row += 1;
row += 1;
if row > 3 {
row = 0;
col += 1;
}
if row > max_row {
if col > max_col {
break;
}
}
Expand Down Expand Up @@ -434,7 +434,6 @@ impl Application {
let prompt = Prompt::new(
":".to_owned(),
|_input: &str| {
let mut matches = vec![];
// TODO: i need this duplicate list right now to avoid borrow checker issues
let command_list = vec![
String::from("q"),
Expand Down Expand Up @@ -469,15 +468,10 @@ impl Application {
String::from("ddd"),
String::from("eee"),
];
for command in command_list {
if command.contains(_input) {
matches.push(command);
}
}
if matches.len() != 0 {
return Some(matches);
}
None
command_list
.into_iter()
.filter(|command| command.contains(_input))
.collect()
}, // completion
|editor: &mut Editor, input: &str| match input {
"q" => editor.should_close = true,
Expand Down
4 changes: 4 additions & 0 deletions helix-view/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ pub fn append_mode(view: &mut View, _count: usize) {

// TODO: I, A, o and O can share a lot of the primitives.

pub fn command_mode(_view: &mut View, _count: usize) {
unimplemented!()
}

// calculate line numbers for each selection range
fn selection_lines(state: &State) -> Vec<usize> {
let mut lines = state
Expand Down
1 change: 1 addition & 0 deletions helix-view/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ pub fn default() -> Keymaps {
vec![key!('p')] => commands::paste,
vec![key!('>')] => commands::indent,
vec![key!('<')] => commands::unindent,
vec![key!(':')] => commands::command_mode,
vec![Key {
code: KeyCode::Esc,
modifiers: Modifiers::NONE
Expand Down
12 changes: 5 additions & 7 deletions helix-view/src/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ pub struct Prompt {
pub prompt: String,
pub line: String,
pub cursor: usize,
pub completion: Option<Vec<String>>,
pub completion: Vec<String>,
pub should_close: bool,
pub completion_selection_index: Option<usize>,
completion_fn: Box<dyn FnMut(&str) -> Option<Vec<String>>>,
completion_fn: Box<dyn FnMut(&str) -> Vec<String>>,
callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
}

impl Prompt {
pub fn new(
prompt: String,
mut completion_fn: impl FnMut(&str) -> Option<Vec<String>> + 'static,
mut completion_fn: impl FnMut(&str) -> Vec<String> + 'static,
callback_fn: impl FnMut(&mut Editor, &str) + 'static,
) -> Prompt {
Prompt {
Expand Down Expand Up @@ -68,11 +68,11 @@ impl Prompt {
}

pub fn change_completion_selection(&mut self) {
if self.completion.is_some() {
if !self.completion.is_empty() {
self.completion_selection_index = self
.completion_selection_index
.map(|i| {
if i == self.completion.as_ref().unwrap().len() - 1 {
if i == self.completion.len() - 1 {
0
} else {
i + 1
Expand All @@ -81,8 +81,6 @@ impl Prompt {
.or(Some(0));
self.line = String::from(
self.completion
.as_ref()
.unwrap()
.get(self.completion_selection_index.unwrap())
.unwrap(),
);
Expand Down

0 comments on commit 2b44031

Please sign in to comment.