aboutsummaryrefslogtreecommitdiff
path: root/helix-view
diff options
context:
space:
mode:
authorBlaž Hrastnik2021-06-04 01:50:03 +0000
committerBlaž Hrastnik2021-06-04 02:03:40 +0000
commit06d8d3f55fbf02bb4d938ecbc479cd60309a0a5d (patch)
treec47d95747397d3b5e00a8b57717b3fc5093646df /helix-view
parent8afd4e1bc21c244a4ed241630a0e845f4ab81f74 (diff)
Try to detect language when document file path is set
Fixes #91
Diffstat (limited to 'helix-view')
-rw-r--r--helix-view/src/document.rs33
-rw-r--r--helix-view/src/editor.rs14
2 files changed, 33 insertions, 14 deletions
diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs
index cf4a6faa..7a7617c0 100644
--- a/helix-view/src/document.rs
+++ b/helix-view/src/document.rs
@@ -131,9 +131,8 @@ impl Document {
}
}
- // TODO: passing scopes here is awkward
// TODO: async fn?
- pub fn load(path: PathBuf, scopes: &[String]) -> Result<Self, Error> {
+ pub fn load(path: PathBuf) -> Result<Self, Error> {
use std::{env, fs::File, io::BufReader};
let _current_dir = env::current_dir()?;
@@ -143,15 +142,8 @@ impl Document {
// TODO: create if not found
let mut doc = Self::new(doc);
-
- let language_config = LOADER
- .get()
- .unwrap()
- .language_config_for_file_name(path.as_path());
- doc.set_language(language_config, scopes);
-
- // canonicalize path to absolute value
- doc.path = Some(std::fs::canonicalize(path)?);
+ // set the path and try detecting the language
+ doc.set_path(&path)?;
Ok(doc)
}
@@ -218,6 +210,15 @@ impl Document {
}
}
+ fn detect_language(&mut self) {
+ if let Some(path) = self.path() {
+ let loader = LOADER.get().unwrap();
+ let language_config = loader.language_config_for_file_name(path);
+ let scopes = loader.scopes();
+ self.set_language(language_config, scopes);
+ }
+ }
+
pub fn set_path(&mut self, path: &Path) -> Result<(), std::io::Error> {
// canonicalize path to absolute value
let current_dir = std::env::current_dir()?;
@@ -229,6 +230,10 @@ impl Document {
self.path = Some(path);
}
}
+
+ // try detecting the language based on filepath
+ self.detect_language();
+
Ok(())
}
@@ -251,8 +256,10 @@ impl Document {
};
}
- pub fn set_language2(&mut self, scope: &str, scopes: &[String]) {
- let language_config = LOADER.get().unwrap().language_config_for_scope(scope);
+ pub fn set_language2(&mut self, scope: &str) {
+ let loader = LOADER.get().unwrap();
+ let language_config = loader.language_config_for_scope(scope);
+ let scopes = loader.scopes();
self.set_language(language_config, scopes);
}
diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs
index e7b25814..d991f250 100644
--- a/helix-view/src/editor.rs
+++ b/helix-view/src/editor.rs
@@ -36,6 +36,18 @@ impl Editor {
.unwrap_or(include_bytes!("../../theme.toml"));
let theme: Theme = toml::from_slice(toml).expect("failed to parse theme.toml");
+ // initialize language registry
+ use helix_core::syntax::{Loader, LOADER};
+
+ // load $HOME/.config/helix/languages.toml, fallback to default config
+ let config = std::fs::read(helix_core::config_dir().join("languages.toml"));
+ let toml = config
+ .as_deref()
+ .unwrap_or(include_bytes!("../../languages.toml"));
+
+ let config = toml::from_slice(toml).expect("Could not parse languages.toml");
+ LOADER.get_or_init(|| Loader::new(config, theme.scopes().to_vec()));
+
let language_servers = helix_lsp::Registry::new();
// HAXX: offset the render area height by 1 to account for prompt/commandline
@@ -135,7 +147,7 @@ impl Editor {
let id = if let Some(id) = id {
id
} else {
- let mut doc = Document::load(path, self.theme.scopes())?;
+ let mut doc = Document::load(path)?;
// try to find a language server based on the language name
let language_server = doc