Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add locale language support for tutor #5199

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 18 additions & 4 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1524,15 +1524,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 @@ -2360,7 +2374,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 @@ -279,6 +279,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