fix: dont panic if cant find language

This commit is contained in:
Jozef Steinhübl 2024-08-04 22:49:23 +02:00
parent 458f93971f
commit 0810b2c79a
No known key found for this signature in database
GPG key ID: E6BC90C91973B08F
4 changed files with 8 additions and 8 deletions

2
Cargo.lock generated
View file

@ -124,7 +124,7 @@ dependencies = [
[[package]] [[package]]
name = "discord-presence-lsp" name = "discord-presence-lsp"
version = "0.4.0" version = "0.4.1"
dependencies = [ dependencies = [
"discord-rich-presence", "discord-rich-presence",
"git2", "git2",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "discord-presence-lsp" name = "discord-presence-lsp"
version = "0.4.0" version = "0.4.1"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View file

@ -14,13 +14,13 @@ lazy_static! {
}; };
} }
pub fn get_language(document: &Document) -> Option<String> { pub fn get_language(document: &Document) -> String {
let map = LANGUAGE_MAP.lock().unwrap(); let map = LANGUAGE_MAP.lock().unwrap();
let filename = document.get_filename().to_string(); let filename = document.get_filename().to_string();
let extension = format!(".{}", document.get_extension()); let extension = format!(".{}", document.get_extension());
if let Some(s) = map.get(&filename) { if let Some(s) = map.get(&filename) {
return Some(s.to_string()); return s.to_string();
} }
for (pattern, language) in map.iter() { for (pattern, language) in map.iter() {
@ -31,14 +31,14 @@ pub fn get_language(document: &Document) -> Option<String> {
if let Ok(re) = Regex::new(pattern.unwrap()) { if let Ok(re) = Regex::new(pattern.unwrap()) {
if re.is_match(&filename) || re.is_match(&extension) { if re.is_match(&filename) || re.is_match(&extension) {
return Some(language.to_string()); return language.to_string();
} }
} }
} }
if let Some(s) = map.get(&extension) { if let Some(s) = map.get(&extension) {
return Some(s.to_string()); return s.to_string();
} }
map.get("text").map(|s| s.to_string()) String::from("text")
} }

View file

@ -24,7 +24,7 @@ impl<'a> Placeholders<'a> {
Self { Self {
filename: doc.get_filename(), filename: doc.get_filename(),
workspace, workspace,
language: get_language(doc).unwrap(), language: get_language(doc),
base_icons_url: &config.base_icons_url, base_icons_url: &config.base_icons_url,
} }
} }