1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::str::FromStr;
use crate::{FileType, HelixLangCfg, IndentStyle};
pub fn languages(input: &str) -> Vec<HelixLangCfg> {
let input = toml_edit::DocumentMut::from_str(input).unwrap();
input["language"]
.as_array_of_tables()
.unwrap()
.iter()
.map(|raw_toml| {
let lang = raw_toml;
let name = lang.get("name").unwrap().as_str().unwrap().to_string();
let file_types = lang.get("file-types").map(|ft| {
ft.as_array()
.unwrap()
.iter()
.map(|file_type| {
if let Some(file_type) = file_type.as_str() {
FileType::Extension(file_type.to_string())
} else {
FileType::Glob(
file_type
.as_inline_table()
.unwrap()
.get("glob")
.unwrap()
.as_str()
.unwrap()
.to_string(),
)
}
})
.collect()
});
let indent = if let Some(indent) = lang.get("indent") {
let size = indent
.get("tab-width")
.unwrap()
.as_integer()
.unwrap()
.try_into()
.unwrap();
let unit = indent.get("unit").unwrap().as_str().unwrap();
let style = if unit.starts_with(' ') {
IndentStyle::Space
} else {
// This is exactly how Helix behaves, everything that's not a
// space is a tab.
IndentStyle::Tab
};
Some((size, style))
} else {
None
};
// This is overly caution, because the configured LSP or formatter
// may not even be installed. In main.rs, ec2hx tries to get more
// precise information from `hx --health`, but this is a good
// default in case that fails.
let has_formatter =
lang.get("language-servers").is_some() || lang.get("formatter").is_some();
HelixLangCfg {
name,
indent,
file_types,
has_formatter,
raw_toml: lang.clone(),
}
})
.collect()
}