Make Buffer::language_at fall back to Buffer::language

Max Brunsfeld created

For languages with no grammar (plain text), there
will be no layers.

Change summary

crates/language/src/buffer.rs     | 2 ++
crates/language/src/language.rs   | 9 +++++++++
crates/language/src/syntax_map.rs | 1 +
3 files changed, 12 insertions(+)

Detailed changes

crates/language/src/buffer.rs 🔗

@@ -648,6 +648,7 @@ impl Buffer {
             .layers_for_range(offset..offset, &self.text)
             .last()
             .map(|info| info.language.clone())
+            .or_else(|| self.language.clone())
     }
 
     pub fn parse_count(&self) -> usize {
@@ -1841,6 +1842,7 @@ impl BufferSnapshot {
             .layers_for_range(offset..offset, &self.text)
             .last()
             .map(|info| info.language)
+            .or(self.language.as_ref())
     }
 
     pub fn surrounding_word<T: ToOffset>(&self, start: T) -> (Range<usize>, Option<CharKind>) {

crates/language/src/language.rs 🔗

@@ -26,6 +26,7 @@ use serde_json::Value;
 use std::{
     any::Any,
     cell::RefCell,
+    fmt::Debug,
     mem,
     ops::Range,
     path::{Path, PathBuf},
@@ -866,6 +867,14 @@ impl Language {
     }
 }
 
+impl Debug for Language {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        f.debug_struct("Language")
+            .field("name", &self.config.name)
+            .finish()
+    }
+}
+
 impl Grammar {
     pub fn id(&self) -> usize {
         self.id

crates/language/src/syntax_map.rs 🔗

@@ -92,6 +92,7 @@ struct SyntaxLayer {
     language: Arc<Language>,
 }
 
+#[derive(Debug)]
 pub struct SyntaxLayerInfo<'a> {
     pub depth: usize,
     pub node: Node<'a>,