Compare commits

...

1 Commits

Author SHA1 Message Date
Piotr Osiewicz
bf4e1ffad3 WIP: rainbow brackets 2024-09-12 11:36:36 -04:00
5 changed files with 74 additions and 6 deletions

View File

@@ -439,6 +439,7 @@
// The list of language servers to use (or disable) for all languages.
//
// This is typically customized on a per-language basis.
"rainbow_brackets": true,
"language_servers": ["..."],
// When to automatically save edited buffers. This setting can
// take four values.

View File

@@ -0,0 +1,25 @@
use gpui::{HighlightStyle, Hsla};
use std::sync::Arc;
use theme::{AccentColors, SyntaxTheme};
#[derive(Clone, Debug)]
pub struct BracketMap(Vec<Hsla>);
impl BracketMap {
pub(crate) fn new(colors: AccentColors) -> Self {
// For each capture name in the highlight query, find the longest
// key in the theme's syntax styles that matches all of the
// dot-separated components of the capture name.
BracketMap(colors.0)
}
pub fn get(&self, depth: u32) -> Hsla {
self.0.get(depth % self.0.len()).copied().unwrap()
}
}
impl Default for BracketMap {
fn default() -> Self {
Self(vec![])
}
}

View File

@@ -2574,16 +2574,20 @@ impl BufferSnapshot {
None
}
fn get_highlights(&self, range: Range<usize>) -> (SyntaxMapCaptures, Vec<HighlightMap>) {
fn get_highlights(&self, range: Range<usize>) -> (SyntaxMapCaptures, Vec<HighlightMap>, usize) {
//let settings = self.settings_at(range.start, cx).rainbow_brackets;
let captures = self.syntax.captures(range, &self.text, |grammar| {
grammar.highlights_query.as_ref()
grammar
.highlights_with_brackets_query
.as_ref()
.map(|(query, _)| query)
});
let highlight_maps = captures
.grammars()
.iter()
.map(|grammar| grammar.highlight_map())
.collect();
(captures, highlight_maps)
(captures, highlight_maps, bracket_patterns_count)
}
/// Iterates over chunks of text in the given range of the buffer. Text is chunked
/// in an arbitrary way due to being stored in a [`Rope`](text::Rope). The text is also

View File

@@ -62,7 +62,7 @@ use task::RunnableTag;
pub use task_context::{ContextProvider, RunnableRange};
use theme::SyntaxTheme;
use tree_sitter::{self, wasmtime, Query, QueryCursor, WasmStore};
use util::serde::default_true;
use util::{maybe, serde::default_true};
pub use buffer::Operation;
pub use buffer::*;
@@ -848,6 +848,7 @@ impl GrammarId {
}
}
struct HighlighBracketsQuery(Query, usize);
pub struct Grammar {
id: GrammarId,
pub ts_language: tree_sitter::Language,
@@ -862,6 +863,7 @@ pub struct Grammar {
pub(crate) injection_config: Option<InjectionConfig>,
pub(crate) override_config: Option<OverrideConfig>,
pub(crate) highlight_map: Mutex<HighlightMap>,
pub(crate) highlights_with_brackets_query: Option<(Query, usize)>,
}
struct IndentConfig {
@@ -962,6 +964,7 @@ impl Language {
error_query: Query::new(&ts_language, "(ERROR) @error").unwrap(),
ts_language,
highlight_map: Default::default(),
highlights_with_brackets_query: None,
})
}),
context_provider: None,
@@ -974,12 +977,12 @@ impl Language {
}
pub fn with_queries(mut self, queries: LanguageQueries) -> Result<Self> {
if let Some(query) = queries.highlights {
if let Some(query) = queries.highlights.as_ref() {
self = self
.with_highlights_query(query.as_ref())
.context("Error loading highlights query")?;
}
if let Some(query) = queries.brackets {
if let Some(query) = queries.brackets.as_ref() {
self = self
.with_brackets_query(query.as_ref())
.context("Error loading brackets query")?;
@@ -1019,6 +1022,34 @@ impl Language {
.with_runnable_query(query.as_ref())
.context("Error loading tests query")?;
}
if let Some((brackets, highlights)) = queries.brackets.zip(queries.highlights) {
self = self
.with_highlights_and_brackets_query(&brackets, &highlights)
.context("Error loading rainbow brackets query")?;
}
Ok(self)
}
pub fn with_highlights_and_brackets_query(
mut self,
brackets: &str,
highlights: &str,
) -> Result<Self> {
let grammar = self
.grammar_mut()
.ok_or_else(|| anyhow!("cannot mutate grammar"))?;
let merged_query = format!("{brackets}\n{highlights}");
let brackets_count = grammar
.brackets_config
.as_ref()
.expect("Expected bracket query to be initialized")
.query
.pattern_count();
grammar.highlights_with_brackets_query = Some((
Query::new(&grammar.ts_language, &merged_query)?,
brackets_count,
));
Ok(self)
}

View File

@@ -121,6 +121,9 @@ pub struct LanguageSettings {
pub linked_edits: bool,
/// Task configuration for this language.
pub tasks: LanguageTaskConfig,
/// Rainbow brackets config
pub rainbow_brackets: bool,
}
impl LanguageSettings {
@@ -345,6 +348,10 @@ pub struct LanguageSettingsContent {
///
/// Default: {}
pub tasks: Option<LanguageTaskConfig>,
/// Task configuration for this language.
///
/// Default: {}
pub rainbow_brackets: Option<bool>,
}
/// The contents of the inline completion settings.