aboutsummaryrefslogtreecommitdiff
path: root/helix-view/src/document.rs
diff options
context:
space:
mode:
Diffstat (limited to 'helix-view/src/document.rs')
-rw-r--r--helix-view/src/document.rs33
1 files changed, 20 insertions, 13 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);
}