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 23, 2022
1 parent df1830e commit d9d5b2f
Show file tree
Hide file tree
Showing 3 changed files with 1,166 additions and 4 deletions.
25 changes: 21 additions & 4 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1398,15 +1398,32 @@ 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 mut name = args[0].to_lowercase();
name.push_str(".txt");

let p = helix_loader::runtime_dir().join("tutors").join(&name);

if p.exists() {
Some(p)
} else {
None
}
};

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 +2242,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
40 changes: 40 additions & 0 deletions helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,46 @@ 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| {
let entry = entry.ok()?;
let path = entry.path();
(path.extension()? == "txt")
.then(|| 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 d9d5b2f

Please sign in to comment.