Skip to content

Commit

Permalink
Add a completer for :theme, remove :list-themes
Browse files Browse the repository at this point in the history
  • Loading branch information
vv9k committed Jun 19, 2021
1 parent 1e78618 commit cc4728f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
16 changes: 1 addition & 15 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1280,13 +1280,6 @@ mod cmd {
editor.set_theme_from_name(theme);
}

fn list_themes(editor: &mut Editor, args: &[&str], event: PromptEvent) {
let mut themes = editor.theme_loader.as_ref().names();
themes.push("default".into());
let status = format!("Available themes: {}", themes.join(", "));
editor.set_status(status);
}

pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
TypableCommand {
name: "quit",
Expand Down Expand Up @@ -1405,14 +1398,7 @@ mod cmd {
alias: None,
doc: "Change the theme of current view. Requires theme name as argument (:theme <name>)",
fun: theme,
completer: None,
},
TypableCommand {
name: "list-themes",
alias: None,
doc: "List available themes.",
fun: list_themes,
completer: None,
completer: Some(completers::theme),
},

];
Expand Down
39 changes: 34 additions & 5 deletions helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,43 @@ pub fn file_picker(root: PathBuf) -> Picker<PathBuf> {

pub mod completers {
use crate::ui::prompt::Completion;
use std::borrow::Cow;
use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
use fuzzy_matcher::FuzzyMatcher;
use helix_view::theme;
use std::cmp::Reverse;
use std::{borrow::Cow, sync::Arc};

pub type Completer = fn(&str) -> Vec<Completion>;

pub fn theme(input: &str) -> Vec<Completion> {
let mut names = theme::Loader::read_names(&helix_core::runtime_dir().join("themes"));
names.extend(theme::Loader::read_names(
&helix_core::config_dir().join("themes"),
));
names.push("default".into());

let mut names: Vec<_> = names
.into_iter()
.map(|name| ((0..), Cow::from(name)))
.collect();

let matcher = Matcher::default();

let mut matches: Vec<_> = names
.into_iter()
.filter_map(|(range, name)| {
matcher
.fuzzy_match(&name, &input)
.map(|score| (name, score))
})
.collect();

matches.sort_unstable_by_key(|(_file, score)| Reverse(*score));
names = matches.into_iter().map(|(name, _)| ((0..), name)).collect();

names
}

// TODO: we could return an iter/lazy thing so it can fetch as many as it needs.
pub fn filename(input: &str) -> Vec<Completion> {
// Rust's filename handling is really annoying.
Expand Down Expand Up @@ -178,10 +211,6 @@ pub mod completers {

// if empty, return a list of dirs and files in current dir
if let Some(file_name) = file_name {
use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
use fuzzy_matcher::FuzzyMatcher;
use std::cmp::Reverse;

let matcher = Matcher::default();

// inefficient, but we need to calculate the scores, filter out None, then sort.
Expand Down
6 changes: 3 additions & 3 deletions helix-view/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl Loader {
toml::from_slice(data.as_slice()).context("Failed to deserialize theme")
}

fn _names(&self, path: &Path) -> Vec<String> {
pub fn read_names(path: &Path) -> Vec<String> {
std::fs::read_dir(path)
.map(|entries| {
entries
Expand Down Expand Up @@ -157,8 +157,8 @@ impl Loader {

/// Lists all theme names available in default and user directory
pub fn names(&self) -> Vec<String> {
let mut names = self._names(&self.user_dir);
names.extend(self._names(&self.default_dir));
let mut names = Self::read_names(&self.user_dir);
names.extend(Self::read_names(&self.default_dir));
names
}

Expand Down

0 comments on commit cc4728f

Please sign in to comment.