2021-11-30 19:46:39 +00:00
|
|
|
mod buffer;
|
2021-12-09 15:38:46 +00:00
|
|
|
mod diagnostic_set;
|
2021-11-30 19:46:39 +00:00
|
|
|
mod highlight_map;
|
2022-01-13 02:17:19 +00:00
|
|
|
mod outline;
|
2021-11-30 19:46:39 +00:00
|
|
|
pub mod proto;
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2021-11-29 16:38:59 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
2021-11-30 19:46:39 +00:00
|
|
|
pub use buffer::Operation;
|
|
|
|
pub use buffer::*;
|
2022-01-04 15:11:29 +00:00
|
|
|
use collections::HashSet;
|
2021-12-09 16:53:08 +00:00
|
|
|
pub use diagnostic_set::DiagnosticEntry;
|
2021-12-21 22:07:50 +00:00
|
|
|
use gpui::AppContext;
|
2021-11-30 19:46:39 +00:00
|
|
|
use highlight_map::HighlightMap;
|
2021-11-29 17:01:51 +00:00
|
|
|
use lazy_static::lazy_static;
|
2022-01-13 11:01:11 +00:00
|
|
|
pub use outline::{Outline, OutlineItem};
|
2021-10-04 14:50:12 +00:00
|
|
|
use parking_lot::Mutex;
|
|
|
|
use serde::Deserialize;
|
2022-02-03 01:01:48 +00:00
|
|
|
use std::{cell::RefCell, ops::Range, path::Path, str, sync::Arc};
|
2021-10-05 18:23:00 +00:00
|
|
|
use theme::SyntaxTheme;
|
2021-11-29 16:38:59 +00:00
|
|
|
use tree_sitter::{self, Query};
|
2021-10-04 14:50:12 +00:00
|
|
|
pub use tree_sitter::{Parser, Tree};
|
|
|
|
|
2022-02-03 01:01:48 +00:00
|
|
|
thread_local! {
|
|
|
|
static PARSER: RefCell<Parser> = RefCell::new(Parser::new());
|
|
|
|
}
|
|
|
|
|
2021-11-29 17:01:51 +00:00
|
|
|
lazy_static! {
|
|
|
|
pub static ref PLAIN_TEXT: Arc<Language> = Arc::new(Language::new(
|
|
|
|
LanguageConfig {
|
|
|
|
name: "Plain Text".to_string(),
|
|
|
|
path_suffixes: Default::default(),
|
|
|
|
brackets: Default::default(),
|
|
|
|
line_comment: None,
|
|
|
|
language_server: None,
|
|
|
|
},
|
|
|
|
None,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2022-01-12 17:01:20 +00:00
|
|
|
pub trait ToPointUtf16 {
|
|
|
|
fn to_point_utf16(self) -> PointUtf16;
|
|
|
|
}
|
|
|
|
|
2022-01-31 18:11:13 +00:00
|
|
|
pub trait ToLspPosition {
|
|
|
|
fn to_lsp_position(self) -> lsp::Position;
|
|
|
|
}
|
|
|
|
|
2022-02-02 17:43:55 +00:00
|
|
|
pub trait LspPostProcessor: 'static + Send + Sync {
|
2022-01-25 16:49:50 +00:00
|
|
|
fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams);
|
2022-02-02 17:43:55 +00:00
|
|
|
fn label_for_completion(&self, _completion: &lsp::CompletionItem) -> Option<String> {
|
|
|
|
None
|
|
|
|
}
|
2022-01-25 16:49:50 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 14:50:12 +00:00
|
|
|
#[derive(Default, Deserialize)]
|
|
|
|
pub struct LanguageConfig {
|
|
|
|
pub name: String,
|
|
|
|
pub path_suffixes: Vec<String>,
|
2021-10-19 11:17:16 +00:00
|
|
|
pub brackets: Vec<BracketPair>,
|
2021-11-23 22:13:28 +00:00
|
|
|
pub line_comment: Option<String>,
|
2021-10-26 19:17:51 +00:00
|
|
|
pub language_server: Option<LanguageServerConfig>,
|
|
|
|
}
|
|
|
|
|
2021-11-03 00:41:01 +00:00
|
|
|
#[derive(Default, Deserialize)]
|
2021-10-26 19:17:51 +00:00
|
|
|
pub struct LanguageServerConfig {
|
|
|
|
pub binary: String,
|
2021-10-28 00:58:07 +00:00
|
|
|
pub disk_based_diagnostic_sources: HashSet<String>,
|
2022-01-04 16:38:45 +00:00
|
|
|
pub disk_based_diagnostics_progress_token: Option<String>,
|
2021-11-03 00:41:01 +00:00
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
|
|
#[serde(skip)]
|
2021-12-21 22:07:50 +00:00
|
|
|
pub fake_server: Option<(Arc<lsp::LanguageServer>, Arc<std::sync::atomic::AtomicBool>)>,
|
2021-10-06 14:34:57 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 11:17:16 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
pub struct BracketPair {
|
2021-10-06 14:34:57 +00:00
|
|
|
pub start: String,
|
|
|
|
pub end: String,
|
2021-10-19 11:17:16 +00:00
|
|
|
pub close: bool,
|
|
|
|
pub newline: bool,
|
2021-10-04 14:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Language {
|
2021-10-05 18:23:00 +00:00
|
|
|
pub(crate) config: LanguageConfig,
|
2021-11-29 16:38:59 +00:00
|
|
|
pub(crate) grammar: Option<Arc<Grammar>>,
|
2022-02-02 17:43:55 +00:00
|
|
|
pub(crate) lsp_post_processor: Option<Box<dyn LspPostProcessor>>,
|
2021-11-29 16:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Grammar {
|
|
|
|
pub(crate) ts_language: tree_sitter::Language,
|
2021-10-06 23:09:30 +00:00
|
|
|
pub(crate) highlights_query: Query,
|
2021-10-05 18:23:00 +00:00
|
|
|
pub(crate) brackets_query: Query,
|
2021-10-06 23:09:30 +00:00
|
|
|
pub(crate) indents_query: Query,
|
2022-01-13 02:17:19 +00:00
|
|
|
pub(crate) outline_query: Query,
|
2021-10-05 18:23:00 +00:00
|
|
|
pub(crate) highlight_map: Mutex<HighlightMap>,
|
2021-10-04 14:50:12 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 17:59:03 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct LanguageRegistry {
|
|
|
|
languages: Vec<Arc<Language>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LanguageRegistry {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add(&mut self, language: Arc<Language>) {
|
|
|
|
self.languages.push(language);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_theme(&self, theme: &SyntaxTheme) {
|
|
|
|
for language in &self.languages {
|
|
|
|
language.set_theme(theme);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-26 19:17:51 +00:00
|
|
|
pub fn get_language(&self, name: &str) -> Option<&Arc<Language>> {
|
|
|
|
self.languages
|
|
|
|
.iter()
|
|
|
|
.find(|language| language.name() == name)
|
|
|
|
}
|
|
|
|
|
2021-10-04 17:59:03 +00:00
|
|
|
pub fn select_language(&self, path: impl AsRef<Path>) -> Option<&Arc<Language>> {
|
|
|
|
let path = path.as_ref();
|
|
|
|
let filename = path.file_name().and_then(|name| name.to_str());
|
|
|
|
let extension = path.extension().and_then(|name| name.to_str());
|
|
|
|
let path_suffixes = [extension, filename];
|
|
|
|
self.languages.iter().find(|language| {
|
|
|
|
language
|
|
|
|
.config
|
|
|
|
.path_suffixes
|
|
|
|
.iter()
|
|
|
|
.any(|suffix| path_suffixes.contains(&Some(suffix.as_str())))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 14:50:12 +00:00
|
|
|
impl Language {
|
2021-11-29 16:38:59 +00:00
|
|
|
pub fn new(config: LanguageConfig, ts_language: Option<tree_sitter::Language>) -> Self {
|
2021-10-05 18:23:00 +00:00
|
|
|
Self {
|
|
|
|
config,
|
2021-11-29 16:38:59 +00:00
|
|
|
grammar: ts_language.map(|ts_language| {
|
|
|
|
Arc::new(Grammar {
|
|
|
|
brackets_query: Query::new(ts_language, "").unwrap(),
|
|
|
|
highlights_query: Query::new(ts_language, "").unwrap(),
|
|
|
|
indents_query: Query::new(ts_language, "").unwrap(),
|
2022-01-13 02:17:19 +00:00
|
|
|
outline_query: Query::new(ts_language, "").unwrap(),
|
2021-11-29 16:38:59 +00:00
|
|
|
ts_language,
|
|
|
|
highlight_map: Default::default(),
|
|
|
|
})
|
|
|
|
}),
|
2022-02-02 17:43:55 +00:00
|
|
|
lsp_post_processor: None,
|
2021-10-05 18:23:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 23:09:30 +00:00
|
|
|
pub fn with_highlights_query(mut self, source: &str) -> Result<Self> {
|
2021-11-29 16:38:59 +00:00
|
|
|
let grammar = self
|
|
|
|
.grammar
|
|
|
|
.as_mut()
|
|
|
|
.and_then(Arc::get_mut)
|
|
|
|
.ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
|
|
|
|
grammar.highlights_query = Query::new(grammar.ts_language, source)?;
|
2021-10-05 18:23:00 +00:00
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
2021-10-06 23:09:30 +00:00
|
|
|
pub fn with_brackets_query(mut self, source: &str) -> Result<Self> {
|
2021-11-29 16:38:59 +00:00
|
|
|
let grammar = self
|
|
|
|
.grammar
|
|
|
|
.as_mut()
|
|
|
|
.and_then(Arc::get_mut)
|
|
|
|
.ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
|
|
|
|
grammar.brackets_query = Query::new(grammar.ts_language, source)?;
|
2021-10-06 23:09:30 +00:00
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_indents_query(mut self, source: &str) -> Result<Self> {
|
2021-11-29 16:38:59 +00:00
|
|
|
let grammar = self
|
|
|
|
.grammar
|
|
|
|
.as_mut()
|
|
|
|
.and_then(Arc::get_mut)
|
|
|
|
.ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
|
|
|
|
grammar.indents_query = Query::new(grammar.ts_language, source)?;
|
2021-10-05 18:23:00 +00:00
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:17:19 +00:00
|
|
|
pub fn with_outline_query(mut self, source: &str) -> Result<Self> {
|
|
|
|
let grammar = self
|
|
|
|
.grammar
|
|
|
|
.as_mut()
|
|
|
|
.and_then(Arc::get_mut)
|
|
|
|
.ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
|
|
|
|
grammar.outline_query = Query::new(grammar.ts_language, source)?;
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
2022-02-02 17:43:55 +00:00
|
|
|
pub fn with_lsp_post_processor(mut self, processor: impl LspPostProcessor) -> Self {
|
|
|
|
self.lsp_post_processor = Some(Box::new(processor));
|
2022-01-25 16:49:50 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-10-04 14:50:12 +00:00
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
self.config.name.as_str()
|
|
|
|
}
|
2021-10-26 19:17:51 +00:00
|
|
|
|
2021-11-23 22:13:28 +00:00
|
|
|
pub fn line_comment_prefix(&self) -> Option<&str> {
|
|
|
|
self.config.line_comment.as_deref()
|
|
|
|
}
|
|
|
|
|
2021-10-26 19:17:51 +00:00
|
|
|
pub fn start_server(
|
|
|
|
&self,
|
|
|
|
root_path: &Path,
|
|
|
|
cx: &AppContext,
|
|
|
|
) -> Result<Option<Arc<lsp::LanguageServer>>> {
|
|
|
|
if let Some(config) = &self.config.language_server {
|
2021-11-03 00:41:01 +00:00
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
|
|
if let Some((server, started)) = &config.fake_server {
|
|
|
|
started.store(true, std::sync::atomic::Ordering::SeqCst);
|
|
|
|
return Ok(Some(server.clone()));
|
|
|
|
}
|
|
|
|
|
2021-10-26 19:17:51 +00:00
|
|
|
const ZED_BUNDLE: Option<&'static str> = option_env!("ZED_BUNDLE");
|
|
|
|
let binary_path = if ZED_BUNDLE.map_or(Ok(false), |b| b.parse())? {
|
|
|
|
cx.platform()
|
|
|
|
.path_for_resource(Some(&config.binary), None)?
|
|
|
|
} else {
|
|
|
|
Path::new(&config.binary).to_path_buf()
|
|
|
|
};
|
2021-11-01 18:57:21 +00:00
|
|
|
lsp::LanguageServer::new(&binary_path, root_path, cx.background().clone()).map(Some)
|
2021-10-26 19:17:51 +00:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 00:58:07 +00:00
|
|
|
pub fn disk_based_diagnostic_sources(&self) -> Option<&HashSet<String>> {
|
2021-10-26 19:17:51 +00:00
|
|
|
self.config
|
|
|
|
.language_server
|
|
|
|
.as_ref()
|
2021-10-28 00:58:07 +00:00
|
|
|
.map(|config| &config.disk_based_diagnostic_sources)
|
2021-10-26 19:17:51 +00:00
|
|
|
}
|
2021-10-04 14:50:12 +00:00
|
|
|
|
2022-01-04 16:38:45 +00:00
|
|
|
pub fn disk_based_diagnostics_progress_token(&self) -> Option<&String> {
|
|
|
|
self.config
|
|
|
|
.language_server
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|config| config.disk_based_diagnostics_progress_token.as_ref())
|
|
|
|
}
|
|
|
|
|
2022-01-25 16:49:50 +00:00
|
|
|
pub fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams) {
|
2022-02-02 17:43:55 +00:00
|
|
|
if let Some(processor) = self.lsp_post_processor.as_ref() {
|
2022-01-25 16:49:50 +00:00
|
|
|
processor.process_diagnostics(diagnostics);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-02 17:43:55 +00:00
|
|
|
pub fn label_for_completion(&self, completion: &lsp::CompletionItem) -> Option<String> {
|
|
|
|
self.lsp_post_processor
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|p| p.label_for_completion(completion))
|
|
|
|
}
|
|
|
|
|
2022-02-03 01:01:48 +00:00
|
|
|
pub fn highlight_text<'a>(&'a self, text: &'a Rope) -> Vec<(Range<usize>, HighlightId)> {
|
|
|
|
let mut result = Vec::new();
|
|
|
|
if let Some(grammar) = &self.grammar {
|
|
|
|
let tree = grammar.parse_text(text, None);
|
|
|
|
let mut offset = 0;
|
|
|
|
for chunk in BufferChunks::new(
|
|
|
|
text,
|
|
|
|
0..text.len(),
|
|
|
|
Some(&tree),
|
|
|
|
self.grammar.as_ref(),
|
|
|
|
vec![],
|
|
|
|
) {
|
|
|
|
let end_offset = offset + chunk.text.len();
|
|
|
|
if let Some(highlight_id) = chunk.highlight_id {
|
|
|
|
result.push((offset..end_offset, highlight_id));
|
|
|
|
}
|
|
|
|
offset = end_offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2021-10-19 11:17:16 +00:00
|
|
|
pub fn brackets(&self) -> &[BracketPair] {
|
|
|
|
&self.config.brackets
|
2021-10-06 14:34:57 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 16:38:59 +00:00
|
|
|
pub fn set_theme(&self, theme: &SyntaxTheme) {
|
|
|
|
if let Some(grammar) = self.grammar.as_ref() {
|
|
|
|
*grammar.highlight_map.lock() =
|
|
|
|
HighlightMap::new(grammar.highlights_query.capture_names(), theme);
|
|
|
|
}
|
2021-10-04 14:50:12 +00:00
|
|
|
}
|
2021-11-29 16:38:59 +00:00
|
|
|
}
|
2021-10-04 14:50:12 +00:00
|
|
|
|
2021-11-29 16:38:59 +00:00
|
|
|
impl Grammar {
|
2022-02-03 01:01:48 +00:00
|
|
|
fn parse_text(&self, text: &Rope, old_tree: Option<Tree>) -> Tree {
|
|
|
|
PARSER.with(|parser| {
|
|
|
|
let mut parser = parser.borrow_mut();
|
|
|
|
parser
|
|
|
|
.set_language(self.ts_language)
|
|
|
|
.expect("incompatible grammar");
|
|
|
|
let mut chunks = text.chunks_in_range(0..text.len());
|
|
|
|
parser
|
|
|
|
.parse_with(
|
|
|
|
&mut move |offset, _| {
|
|
|
|
chunks.seek(offset);
|
|
|
|
chunks.next().unwrap_or("").as_bytes()
|
|
|
|
},
|
|
|
|
old_tree.as_ref(),
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-29 16:38:59 +00:00
|
|
|
pub fn highlight_map(&self) -> HighlightMap {
|
|
|
|
self.highlight_map.lock().clone()
|
2021-10-04 14:50:12 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-04 17:59:03 +00:00
|
|
|
|
2021-11-03 00:41:01 +00:00
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
|
|
impl LanguageServerConfig {
|
2021-12-21 22:07:50 +00:00
|
|
|
pub async fn fake(
|
|
|
|
executor: Arc<gpui::executor::Background>,
|
|
|
|
) -> (Self, lsp::FakeLanguageServer) {
|
2021-11-03 00:41:01 +00:00
|
|
|
let (server, fake) = lsp::LanguageServer::fake(executor).await;
|
|
|
|
fake.started
|
|
|
|
.store(false, std::sync::atomic::Ordering::SeqCst);
|
|
|
|
let started = fake.started.clone();
|
|
|
|
(
|
|
|
|
Self {
|
|
|
|
fake_server: Some((server, started)),
|
2022-01-06 16:32:08 +00:00
|
|
|
disk_based_diagnostics_progress_token: Some("fakeServer/check".to_string()),
|
2021-11-03 00:41:01 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
fake,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2022-01-12 17:01:20 +00:00
|
|
|
|
|
|
|
impl ToPointUtf16 for lsp::Position {
|
|
|
|
fn to_point_utf16(self) -> PointUtf16 {
|
|
|
|
PointUtf16::new(self.line, self.character)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 18:11:13 +00:00
|
|
|
impl ToLspPosition for PointUtf16 {
|
|
|
|
fn to_lsp_position(self) -> lsp::Position {
|
|
|
|
lsp::Position::new(self.row, self.column)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-12 17:01:20 +00:00
|
|
|
pub fn range_from_lsp(range: lsp::Range) -> Range<PointUtf16> {
|
|
|
|
let start = PointUtf16::new(range.start.line, range.start.character);
|
|
|
|
let end = PointUtf16::new(range.end.line, range.end.character);
|
|
|
|
start..end
|
|
|
|
}
|