Skip to content

Commit

Permalink
Add locale language support for tutor
Browse files Browse the repository at this point in the history
- Added Chinese Simplified tutor file
  • Loading branch information
erasin committed Dec 28, 2022
1 parent df1830e commit dc89837
Show file tree
Hide file tree
Showing 3 changed files with 1,167 additions and 4 deletions.
22 changes: 18 additions & 4 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1398,15 +1398,29 @@ fn debug_remote(

fn tutor(
cx: &mut compositor::Context,
_args: &[Cow<str>],
args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

let path = helix_loader::runtime_dir().join("tutor");
cx.editor.open(&path, Action::Replace)?;
let default_tutor = helix_loader::runtime_dir().join("tutor");

let path = if args.is_empty() {
None
} else {
let name = args[0].to_lowercase();
if name.eq("default") {
None
} else {
let p = helix_loader::runtime_dir().join("tutors").join(&name);
(p.exists()).then(|| p)
}
};

cx.editor
.open(&path.unwrap_or(default_tutor), Action::Replace)?;
// Unset path to prevent accidentally saving to the original tutor file.
doc_mut!(cx.editor).set_path(None)?;
Ok(())
Expand Down Expand Up @@ -2225,7 +2239,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
aliases: &[],
doc: "Open the tutorial.",
fun: tutor,
completer: None,
completer: Some(completers::tutor),
},
TypableCommand {
name: "goto",
Expand Down
44 changes: 44 additions & 0 deletions helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,50 @@ pub mod completers {
names
}

pub fn tutor(_editor: &Editor, input: &str) -> Vec<Completion> {
let mut names: Vec<String> = std::fs::read_dir(&helix_loader::runtime_dir().join("tutors"))
.map(|entries| {
entries
.filter_map(|entry| {
entry.ok().map(|entry| {
entry
.path()
.file_stem()
.unwrap()
.to_string_lossy()
.into_owned()
})
})
.collect()
})
.unwrap_or_default();

names.push("default".into());
names.sort();
names.dedup();

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(|(name1, score1), (name2, score2)| {
(Reverse(*score1), name1).cmp(&(Reverse(*score2), name2))
});
names = matches.into_iter().map(|(name, _)| ((0..), name)).collect();

names
}

pub fn theme(_editor: &Editor, input: &str) -> Vec<Completion> {
let mut names = theme::Loader::read_names(&helix_loader::runtime_dir().join("themes"));
names.extend(theme::Loader::read_names(
Expand Down
Loading

0 comments on commit dc89837

Please sign in to comment.