Start on autoclosing pairs

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2021-10-06 16:34:57 +02:00
parent 3cb7ba0f57
commit 05d7e9c4e7
6 changed files with 174 additions and 3 deletions

View file

@ -11,6 +11,13 @@ pub use tree_sitter::{Parser, Tree};
pub struct LanguageConfig {
pub name: String,
pub path_suffixes: Vec<String>,
pub autoclose_pairs: Vec<AutoclosePair>,
}
#[derive(Clone, Deserialize)]
pub struct AutoclosePair {
pub start: String,
pub end: String,
}
pub struct Language {
@ -81,6 +88,10 @@ impl Language {
self.config.name.as_str()
}
pub fn autoclose_pairs(&self) -> &[AutoclosePair] {
&self.config.autoclose_pairs
}
pub fn highlight_map(&self) -> HighlightMap {
self.highlight_map.lock().clone()
}

View file

@ -14,7 +14,7 @@ use clock::ReplicaId;
use gpui::{AppContext, Entity, ModelContext, MutableAppContext, Task};
pub use highlight_map::{HighlightId, HighlightMap};
use language::Tree;
pub use language::{Language, LanguageConfig, LanguageRegistry};
pub use language::{AutoclosePair, Language, LanguageConfig, LanguageRegistry};
use lazy_static::lazy_static;
use operation_queue::OperationQueue;
use parking_lot::Mutex;
@ -1110,6 +1110,23 @@ impl Buffer {
self.visible_text.chars_at(offset)
}
pub fn bytes_at<T: ToOffset>(&self, position: T) -> impl Iterator<Item = u8> + '_ {
let offset = position.to_offset(self);
self.visible_text.bytes_at(offset)
}
pub fn contains_str_at<T>(&self, position: T, needle: &str) -> bool
where
T: ToOffset,
{
let position = position.to_offset(self);
position == self.clip_offset(position, Bias::Left)
&& self
.bytes_at(position)
.take(needle.len())
.eq(needle.bytes())
}
pub fn edits_since<'a>(&'a self, since: clock::Global) -> impl 'a + Iterator<Item = Edit> {
let since_2 = since.clone();
let cursor = if since == self.version {
@ -4083,6 +4100,7 @@ mod tests {
LanguageConfig {
name: "Rust".to_string(),
path_suffixes: vec!["rs".to_string()],
..Default::default()
},
tree_sitter_rust::language(),
)

View file

@ -115,6 +115,10 @@ impl Rope {
self.chunks_in_range(start..self.len()).flat_map(str::chars)
}
pub fn bytes_at(&self, start: usize) -> impl Iterator<Item = u8> + '_ {
self.chunks_in_range(start..self.len()).flat_map(str::bytes)
}
pub fn chunks<'a>(&'a self) -> Chunks<'a> {
self.chunks_in_range(0..self.len())
}