Skip to content

Commit 1a336bd

Browse files
committed
Add locale language support for tutor
- Added Chinese Simplified tutor file
1 parent df1830e commit 1a336bd

File tree

3 files changed

+1169
-4
lines changed

3 files changed

+1169
-4
lines changed

helix-term/src/commands/typed.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,15 +1398,29 @@ fn debug_remote(
13981398

13991399
fn tutor(
14001400
cx: &mut compositor::Context,
1401-
_args: &[Cow<str>],
1401+
args: &[Cow<str>],
14021402
event: PromptEvent,
14031403
) -> anyhow::Result<()> {
14041404
if event != PromptEvent::Validate {
14051405
return Ok(());
14061406
}
14071407

1408-
let path = helix_loader::runtime_dir().join("tutor");
1409-
cx.editor.open(&path, Action::Replace)?;
1408+
let default_tutor = helix_loader::runtime_dir().join("tutor");
1409+
1410+
let path = if args.is_empty() {
1411+
None
1412+
} else {
1413+
let name = args[0].to_lowercase();
1414+
if name.eq("default") {
1415+
None
1416+
} else {
1417+
let p = helix_loader::runtime_dir().join("tutors").join(&name);
1418+
(p.exists()).then(|| p)
1419+
}
1420+
};
1421+
1422+
cx.editor
1423+
.open(&path.unwrap_or(default_tutor), Action::Replace)?;
14101424
// Unset path to prevent accidentally saving to the original tutor file.
14111425
doc_mut!(cx.editor).set_path(None)?;
14121426
Ok(())
@@ -2225,7 +2239,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
22252239
aliases: &[],
22262240
doc: "Open the tutorial.",
22272241
fun: tutor,
2228-
completer: None,
2242+
completer: Some(completers::tutor),
22292243
},
22302244
TypableCommand {
22312245
name: "goto",

helix-term/src/ui/mod.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,52 @@ pub mod completers {
280280
names
281281
}
282282

283+
pub fn tutor(_editor: &Editor, input: &str) -> Vec<Completion> {
284+
let mut names: Vec<String> = std::fs::read_dir(&helix_loader::runtime_dir().join("tutors"))
285+
.map(|entries| {
286+
entries
287+
.filter_map(|entry| {
288+
entry.ok().and_then(|entry| {
289+
Some(
290+
entry
291+
.path()
292+
.file_stem()
293+
.unwrap()
294+
.to_string_lossy()
295+
.into_owned(),
296+
)
297+
})
298+
})
299+
.collect()
300+
})
301+
.unwrap_or_default();
302+
303+
names.push("default".into());
304+
names.sort();
305+
names.dedup();
306+
307+
let mut names: Vec<_> = names
308+
.into_iter()
309+
.map(|name| ((0..), Cow::from(name)))
310+
.collect();
311+
312+
let matcher = Matcher::default();
313+
314+
let mut matches: Vec<_> = names
315+
.into_iter()
316+
.filter_map(|(_range, name)| {
317+
matcher.fuzzy_match(&name, input).map(|score| (name, score))
318+
})
319+
.collect();
320+
321+
matches.sort_unstable_by(|(name1, score1), (name2, score2)| {
322+
(Reverse(*score1), name1).cmp(&(Reverse(*score2), name2))
323+
});
324+
names = matches.into_iter().map(|(name, _)| ((0..), name)).collect();
325+
326+
names
327+
}
328+
283329
pub fn theme(_editor: &Editor, input: &str) -> Vec<Completion> {
284330
let mut names = theme::Loader::read_names(&helix_loader::runtime_dir().join("themes"));
285331
names.extend(theme::Loader::read_names(

0 commit comments

Comments
 (0)