Make the fields of buffer::Language private

This commit is contained in:
Max Brunsfeld 2021-10-05 11:23:00 -07:00
parent 0282e6f255
commit 3cb7ba0f57
6 changed files with 97 additions and 100 deletions

View file

@ -1,10 +1,11 @@
use crate::{HighlightMap}; use crate::HighlightMap;
use anyhow::Result;
use parking_lot::Mutex; use parking_lot::Mutex;
use serde::Deserialize; use serde::Deserialize;
use std::{path::Path, str, sync::Arc}; use std::{path::Path, str, sync::Arc};
use theme::SyntaxTheme;
use tree_sitter::{Language as Grammar, Query}; use tree_sitter::{Language as Grammar, Query};
pub use tree_sitter::{Parser, Tree}; pub use tree_sitter::{Parser, Tree};
use theme::SyntaxTheme;
#[derive(Default, Deserialize)] #[derive(Default, Deserialize)]
pub struct LanguageConfig { pub struct LanguageConfig {
@ -12,18 +13,12 @@ pub struct LanguageConfig {
pub path_suffixes: Vec<String>, pub path_suffixes: Vec<String>,
} }
#[derive(Deserialize)]
pub struct BracketPair {
pub start: String,
pub end: String,
}
pub struct Language { pub struct Language {
pub config: LanguageConfig, pub(crate) config: LanguageConfig,
pub grammar: Grammar, pub(crate) grammar: Grammar,
pub highlight_query: Query, pub(crate) highlight_query: Query,
pub brackets_query: Query, pub(crate) brackets_query: Query,
pub highlight_map: Mutex<HighlightMap>, pub(crate) highlight_map: Mutex<HighlightMap>,
} }
#[derive(Default)] #[derive(Default)]
@ -62,6 +57,26 @@ impl LanguageRegistry {
} }
impl Language { impl Language {
pub fn new(config: LanguageConfig, grammar: Grammar) -> Self {
Self {
config,
brackets_query: Query::new(grammar, "").unwrap(),
highlight_query: Query::new(grammar, "").unwrap(),
grammar,
highlight_map: Default::default(),
}
}
pub fn with_highlights_query(mut self, highlights_query_source: &str) -> Result<Self> {
self.highlight_query = Query::new(self.grammar, highlights_query_source)?;
Ok(self)
}
pub fn with_brackets_query(mut self, brackets_query_source: &str) -> Result<Self> {
self.brackets_query = Query::new(self.grammar, brackets_query_source)?;
Ok(self)
}
pub fn name(&self) -> &str { pub fn name(&self) -> &str {
self.config.name.as_str() self.config.name.as_str()
} }

View file

@ -4078,19 +4078,16 @@ mod tests {
} }
fn rust_lang() -> Arc<Language> { fn rust_lang() -> Arc<Language> {
let lang = tree_sitter_rust::language(); Arc::new(
let brackets_query = r#" Language::new(
("{" @open "}" @close) LanguageConfig {
"#; name: "Rust".to_string(),
Arc::new(Language { path_suffixes: vec!["rs".to_string()],
config: LanguageConfig { },
name: "Rust".to_string(), tree_sitter_rust::language(),
path_suffixes: vec!["rs".to_string()], )
}, .with_brackets_query(r#" ("{" @open "}" @close) "#)
grammar: tree_sitter_rust::language(), .unwrap(),
highlight_query: tree_sitter::Query::new(lang.clone(), "").unwrap(), )
brackets_query: tree_sitter::Query::new(lang.clone(), brackets_query).unwrap(),
highlight_map: Default::default(),
})
} }
} }

View file

@ -670,7 +670,6 @@ mod tests {
async fn test_highlighted_chunks_at(mut cx: gpui::TestAppContext) { async fn test_highlighted_chunks_at(mut cx: gpui::TestAppContext) {
use unindent::Unindent as _; use unindent::Unindent as _;
let grammar = tree_sitter_rust::language();
let text = r#" let text = r#"
fn outer() {} fn outer() {}
@ -678,28 +677,28 @@ mod tests {
fn inner() {} fn inner() {}
}"# }"#
.unindent(); .unindent();
let highlight_query = tree_sitter::Query::new(
grammar,
r#"
(mod_item name: (identifier) body: _ @mod.body)
(function_item name: (identifier) @fn.name)"#,
)
.unwrap();
let theme = SyntaxTheme::new(vec![ let theme = SyntaxTheme::new(vec![
("mod.body".to_string(), Color::from_u32(0xff0000ff).into()), ("mod.body".to_string(), Color::from_u32(0xff0000ff).into()),
("fn.name".to_string(), Color::from_u32(0x00ff00ff).into()), ("fn.name".to_string(), Color::from_u32(0x00ff00ff).into()),
]); ]);
let lang = Arc::new(Language { let lang = Arc::new(
config: LanguageConfig { Language::new(
name: "Test".to_string(), LanguageConfig {
path_suffixes: vec![".test".to_string()], name: "Test".to_string(),
..Default::default() path_suffixes: vec![".test".to_string()],
}, ..Default::default()
grammar: grammar.clone(), },
highlight_query, tree_sitter_rust::language(),
brackets_query: tree_sitter::Query::new(grammar, "").unwrap(), )
highlight_map: Default::default(), .with_highlights_query(
}); r#"
(mod_item name: (identifier) body: _ @mod.body)
(function_item name: (identifier) @fn.name)
"#,
)
.unwrap(),
);
lang.set_theme(&theme); lang.set_theme(&theme);
let buffer = cx.add_model(|cx| { let buffer = cx.add_model(|cx| {
@ -759,7 +758,6 @@ mod tests {
cx.foreground().set_block_on_ticks(usize::MAX..=usize::MAX); cx.foreground().set_block_on_ticks(usize::MAX..=usize::MAX);
let grammar = tree_sitter_rust::language();
let text = r#" let text = r#"
fn outer() {} fn outer() {}
@ -767,28 +765,28 @@ mod tests {
fn inner() {} fn inner() {}
}"# }"#
.unindent(); .unindent();
let highlight_query = tree_sitter::Query::new(
grammar,
r#"
(mod_item name: (identifier) body: _ @mod.body)
(function_item name: (identifier) @fn.name)"#,
)
.unwrap();
let theme = SyntaxTheme::new(vec![ let theme = SyntaxTheme::new(vec![
("mod.body".to_string(), Color::from_u32(0xff0000ff).into()), ("mod.body".to_string(), Color::from_u32(0xff0000ff).into()),
("fn.name".to_string(), Color::from_u32(0x00ff00ff).into()), ("fn.name".to_string(), Color::from_u32(0x00ff00ff).into()),
]); ]);
let lang = Arc::new(Language { let lang = Arc::new(
config: LanguageConfig { Language::new(
name: "Test".to_string(), LanguageConfig {
path_suffixes: vec![".test".to_string()], name: "Test".to_string(),
..Default::default() path_suffixes: vec![".test".to_string()],
}, ..Default::default()
grammar: grammar.clone(), },
highlight_query, tree_sitter_rust::language(),
brackets_query: tree_sitter::Query::new(grammar, "").unwrap(), )
highlight_map: Default::default(), .with_highlights_query(
}); r#"
(mod_item name: (identifier) body: _ @mod.body)
(function_item name: (identifier) @fn.name)
"#,
)
.unwrap(),
);
lang.set_theme(&theme); lang.set_theme(&theme);
let buffer = cx.add_model(|cx| { let buffer = cx.add_model(|cx| {

View file

@ -4068,15 +4068,10 @@ mod tests {
#[gpui::test] #[gpui::test]
async fn test_select_larger_smaller_syntax_node(mut cx: gpui::TestAppContext) { async fn test_select_larger_smaller_syntax_node(mut cx: gpui::TestAppContext) {
let settings = cx.read(EditorSettings::test); let settings = cx.read(EditorSettings::test);
let language = Arc::new(Language::new(
let grammar = tree_sitter_rust::language(); LanguageConfig::default(),
let language = Arc::new(Language { tree_sitter_rust::language(),
config: LanguageConfig::default(), ));
brackets_query: tree_sitter::Query::new(grammar, "").unwrap(),
highlight_query: tree_sitter::Query::new(grammar, "").unwrap(),
highlight_map: Default::default(),
grammar,
});
let text = r#" let text = r#"
use mod1::mod2::{mod3, mod4}; use mod1::mod2::{mod3, mod4};
@ -4086,6 +4081,7 @@ mod tests {
} }
"# "#
.unindent(); .unindent();
let buffer = cx.add_model(|cx| { let buffer = cx.add_model(|cx| {
let history = History::new(text.into()); let history = History::new(text.into());
Buffer::from_history(0, history, None, Some(language), cx) Buffer::from_history(0, history, None, Some(language), cx)

View file

@ -270,19 +270,14 @@ pub struct WorkspaceParams {
impl WorkspaceParams { impl WorkspaceParams {
#[cfg(any(test, feature = "test-support"))] #[cfg(any(test, feature = "test-support"))]
pub fn test(cx: &mut MutableAppContext) -> Self { pub fn test(cx: &mut MutableAppContext) -> Self {
let grammar = tree_sitter_rust::language(); let mut languages = LanguageRegistry::new();
let language = Arc::new(buffer::Language { languages.add(Arc::new(buffer::Language::new(
config: buffer::LanguageConfig { buffer::LanguageConfig {
name: "Rust".to_string(), name: "Rust".to_string(),
path_suffixes: vec!["rs".to_string()], path_suffixes: vec!["rs".to_string()],
}, },
brackets_query: tree_sitter::Query::new(grammar, "").unwrap(), tree_sitter_rust::language(),
highlight_query: tree_sitter::Query::new(grammar, "").unwrap(), )));
highlight_map: Default::default(),
grammar,
});
let mut languages = LanguageRegistry::new();
languages.add(language);
let client = Client::new(); let client = Client::new();
let http_client = client::test::FakeHttpClient::new(|_| async move { let http_client = client::test::FakeHttpClient::new(|_| async move {

View file

@ -1,8 +1,7 @@
use buffer::{HighlightMap, Language, LanguageRegistry}; use buffer::{Language, LanguageRegistry};
use parking_lot::Mutex;
use rust_embed::RustEmbed; use rust_embed::RustEmbed;
use std::borrow::Cow;
use std::{str, sync::Arc}; use std::{str, sync::Arc};
use tree_sitter::Query;
#[derive(RustEmbed)] #[derive(RustEmbed)]
#[folder = "languages"] #[folder = "languages"]
@ -18,19 +17,16 @@ fn rust() -> Language {
let grammar = tree_sitter_rust::language(); let grammar = tree_sitter_rust::language();
let rust_config = let rust_config =
toml::from_slice(&LanguageDir::get("rust/config.toml").unwrap().data).unwrap(); toml::from_slice(&LanguageDir::get("rust/config.toml").unwrap().data).unwrap();
Language { Language::new(rust_config, grammar)
config: rust_config, .with_highlights_query(load_query("rust/highlights.scm").as_ref())
grammar, .unwrap()
highlight_query: load_query(grammar, "rust/highlights.scm"), .with_brackets_query(load_query("rust/brackets.scm").as_ref())
brackets_query: load_query(grammar, "rust/brackets.scm"), .unwrap()
highlight_map: Mutex::new(HighlightMap::default()),
}
} }
fn load_query(grammar: tree_sitter::Language, path: &str) -> Query { fn load_query(path: &str) -> Cow<'static, str> {
Query::new( match LanguageDir::get(path).unwrap().data {
grammar, Cow::Borrowed(s) => Cow::Borrowed(str::from_utf8(s).unwrap()),
str::from_utf8(&LanguageDir::get(path).unwrap().data).unwrap(), Cow::Owned(s) => Cow::Owned(String::from_utf8(s).unwrap()),
) }
.unwrap()
} }