Get it to build with multiple adapters per language! 🎉

Co-Authored-By: Max Brunsfeld <max@zed.dev>
This commit is contained in:
Julia 2023-04-17 16:55:10 -04:00 committed by Max Brunsfeld
parent ba7233f265
commit 6e68ff5a50
5 changed files with 100 additions and 89 deletions

View file

@ -1641,7 +1641,7 @@ impl Project {
.1 .1
.notify::<lsp::notification::DidCloseTextDocument>( .notify::<lsp::notification::DidCloseTextDocument>(
lsp::DidCloseTextDocumentParams { lsp::DidCloseTextDocumentParams {
text_document: lsp::TextDocumentIdentifier::new(uri), text_document: lsp::TextDocumentIdentifier::new(uri.clone()),
}, },
) )
.log_err(); .log_err();
@ -1670,17 +1670,17 @@ impl Project {
let uri = lsp::Url::from_file_path(file.abs_path(cx)).unwrap(); let uri = lsp::Url::from_file_path(file.abs_path(cx)).unwrap();
let initial_snapshot = buffer.text_snapshot(); let initial_snapshot = buffer.text_snapshot();
let language = buffer.language().cloned();
let worktree_id = file.worktree_id(cx);
if let Some(local_worktree) = file.worktree.read(cx).as_local() { if let Some(local_worktree) = file.worktree.read(cx).as_local() {
if let Some(diagnostics) = local_worktree.diagnostics_for_path(file.path()) { for (server_id, diagnostics) in local_worktree.diagnostics_for_path(file.path()) {
self.update_buffer_diagnostics(buffer_handle, diagnostics, None, cx) self.update_buffer_diagnostics(buffer_handle, diagnostics, server_id, None, cx)
.log_err(); .log_err();
} }
} }
if let Some(language) = buffer.language() { if let Some(language) = language {
let worktree_id = file.worktree_id(cx);
for adapter in language.lsp_adapters() { for adapter in language.lsp_adapters() {
let language_id = adapter.language_ids.get(language.name().as_ref()).cloned(); let language_id = adapter.language_ids.get(language.name().as_ref()).cloned();
let server = self let server = self
@ -1703,7 +1703,7 @@ impl Project {
.notify::<lsp::notification::DidOpenTextDocument>( .notify::<lsp::notification::DidOpenTextDocument>(
lsp::DidOpenTextDocumentParams { lsp::DidOpenTextDocumentParams {
text_document: lsp::TextDocumentItem::new( text_document: lsp::TextDocumentItem::new(
uri, uri.clone(),
language_id.unwrap_or_default(), language_id.unwrap_or_default(),
0, 0,
initial_snapshot.text(), initial_snapshot.text(),
@ -1726,7 +1726,7 @@ impl Project {
let snapshot = LspBufferSnapshot { let snapshot = LspBufferSnapshot {
version: 0, version: 0,
snapshot: initial_snapshot, snapshot: initial_snapshot.clone(),
}; };
self.buffer_snapshots self.buffer_snapshots
.entry(buffer_id) .entry(buffer_id)
@ -1746,13 +1746,12 @@ impl Project {
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer.update_diagnostics(Default::default(), cx); buffer.update_diagnostics(Default::default(), cx);
self.buffer_snapshots.remove(&buffer.remote_id()); self.buffer_snapshots.remove(&buffer.remote_id());
let file_url = lsp::Url::from_file_path(old_path).unwrap();
for (_, language_server) in self.language_servers_for_buffer(buffer, cx) { for (_, language_server) in self.language_servers_for_buffer(buffer, cx) {
language_server language_server
.notify::<lsp::notification::DidCloseTextDocument>( .notify::<lsp::notification::DidCloseTextDocument>(
lsp::DidCloseTextDocumentParams { lsp::DidCloseTextDocumentParams {
text_document: lsp::TextDocumentIdentifier::new( text_document: lsp::TextDocumentIdentifier::new(file_url.clone()),
lsp::Url::from_file_path(old_path).unwrap(),
),
}, },
) )
.log_err(); .log_err();
@ -1856,7 +1855,12 @@ impl Project {
let uri = lsp::Url::from_file_path(abs_path).unwrap(); let uri = lsp::Url::from_file_path(abs_path).unwrap();
let next_snapshot = buffer.text_snapshot(); let next_snapshot = buffer.text_snapshot();
for (_, language_server) in self.language_servers_for_buffer(buffer, cx) { let language_servers: Vec<_> = self
.language_servers_iter_for_buffer(buffer, cx)
.map(|i| i.1.clone())
.collect();
for language_server in language_servers {
let language_server = language_server.clone(); let language_server = language_server.clone();
let buffer_snapshots = self let buffer_snapshots = self
@ -1887,14 +1891,14 @@ impl Project {
buffer_snapshots.push(LspBufferSnapshot { buffer_snapshots.push(LspBufferSnapshot {
version: next_version, version: next_version,
snapshot: next_snapshot, snapshot: next_snapshot.clone(),
}); });
language_server language_server
.notify::<lsp::notification::DidChangeTextDocument>( .notify::<lsp::notification::DidChangeTextDocument>(
lsp::DidChangeTextDocumentParams { lsp::DidChangeTextDocumentParams {
text_document: lsp::VersionedTextDocumentIdentifier::new( text_document: lsp::VersionedTextDocumentIdentifier::new(
uri, uri.clone(),
next_version, next_version,
), ),
content_changes, content_changes,
@ -1925,26 +1929,24 @@ impl Project {
let language_server_ids = self.language_server_ids_for_buffer(buffer.read(cx), cx); let language_server_ids = self.language_server_ids_for_buffer(buffer.read(cx), cx);
for language_server_id in language_server_ids { for language_server_id in language_server_ids {
let LanguageServerState::Running { if let Some(LanguageServerState::Running {
adapter, adapter,
simulate_disk_based_diagnostics_completion, simulate_disk_based_diagnostics_completion,
.. ..
} = match self.language_servers.get_mut(&language_server_id) { }) = self.language_servers.get_mut(&language_server_id)
Some(state) => state, {
None => continue, // After saving a buffer using a language server that doesn't provide
}; // a disk-based progress token, kick off a timer that will reset every
// time the buffer is saved. If the timer eventually fires, simulate
// disk-based diagnostics being finished so that other pieces of UI
// (e.g., project diagnostics view, diagnostic status bar) can update.
// We don't emit an event right away because the language server might take
// some time to publish diagnostics.
if adapter.disk_based_diagnostics_progress_token.is_none() {
const DISK_BASED_DIAGNOSTICS_DEBOUNCE: Duration =
Duration::from_secs(1);
// After saving a buffer using a language server that doesn't provide let task = cx.spawn_weak(|this, mut cx| async move {
// a disk-based progress token, kick off a timer that will reset every
// time the buffer is saved. If the timer eventually fires, simulate
// disk-based diagnostics being finished so that other pieces of UI
// (e.g., project diagnostics view, diagnostic status bar) can update.
// We don't emit an event right away because the language server might take
// some time to publish diagnostics.
if adapter.disk_based_diagnostics_progress_token.is_none() {
const DISK_BASED_DIAGNOSTICS_DEBOUNCE: Duration = Duration::from_secs(1);
let task = cx.spawn_weak(|this, mut cx| async move {
cx.background().timer(DISK_BASED_DIAGNOSTICS_DEBOUNCE).await; cx.background().timer(DISK_BASED_DIAGNOSTICS_DEBOUNCE).await;
if let Some(this) = this.upgrade(&cx) { if let Some(this) = this.upgrade(&cx) {
this.update(&mut cx, |this, cx | { this.update(&mut cx, |this, cx | {
@ -1958,7 +1960,8 @@ impl Project {
}); });
} }
}); });
*simulate_disk_based_diagnostics_completion = Some(task); *simulate_disk_based_diagnostics_completion = Some(task);
}
} }
} }
} }
@ -2123,7 +2126,7 @@ impl Project {
let adapters = language.lsp_adapters(); let adapters = language.lsp_adapters();
let language_servers = self.languages.start_language_servers( let language_servers = self.languages.start_language_servers(
language.clone(), language.clone(),
worktree_path, worktree_path.clone(),
self.client.http_client(), self.client.http_client(),
cx, cx,
); );
@ -2143,19 +2146,18 @@ impl Project {
_ => {} _ => {}
} }
self.language_server_ids if !self.language_server_ids.contains_key(&key) {
.entry(key.clone()) let adapter = self.setup_language_adapter(
.or_insert_with(|| { worktree_path.clone(),
self.setup_language_adapter( initialization_options,
worktree_path, pending_server,
initialization_options, adapter.clone(),
pending_server, language.clone(),
adapter, key.clone(),
&language, cx,
key, );
cx, self.language_server_ids.insert(key.clone(), adapter);
) }
});
} }
} }
@ -2164,8 +2166,8 @@ impl Project {
worktree_path: Arc<Path>, worktree_path: Arc<Path>,
initialization_options: Option<serde_json::Value>, initialization_options: Option<serde_json::Value>,
pending_server: PendingLanguageServer, pending_server: PendingLanguageServer,
adapter: &Arc<CachedLspAdapter>, adapter: Arc<CachedLspAdapter>,
language: &Arc<Language>, language: Arc<Language>,
key: (WorktreeId, LanguageServerName), key: (WorktreeId, LanguageServerName),
cx: &mut ModelContext<Project>, cx: &mut ModelContext<Project>,
) -> usize { ) -> usize {
@ -2409,7 +2411,7 @@ impl Project {
let snapshot = versions.last().unwrap(); let snapshot = versions.last().unwrap();
let version = snapshot.version; let version = snapshot.version;
let initial_snapshot = snapshot.snapshot; let initial_snapshot = &snapshot.snapshot;
let uri = lsp::Url::from_file_path(file.abs_path(cx)).unwrap(); let uri = lsp::Url::from_file_path(file.abs_path(cx)).unwrap();
language_server language_server
.notify::<lsp::notification::DidOpenTextDocument>( .notify::<lsp::notification::DidOpenTextDocument>(
@ -2532,6 +2534,7 @@ impl Project {
None None
} }
// TODO This will break in the case where the adapter's root paths and worktrees are not equal
fn restart_language_servers( fn restart_language_servers(
&mut self, &mut self,
worktree_id: WorktreeId, worktree_id: WorktreeId,
@ -2568,7 +2571,7 @@ impl Project {
.map(|path_buf| Arc::from(path_buf.as_path())) .map(|path_buf| Arc::from(path_buf.as_path()))
.unwrap_or(fallback_path); .unwrap_or(fallback_path);
this.start_language_servers(worktree_id, root_path, language, cx); this.start_language_servers(worktree_id, root_path, language.clone(), cx);
// Lookup new server ids and set them for each of the orphaned worktrees // Lookup new server ids and set them for each of the orphaned worktrees
for adapter in language.lsp_adapters() { for adapter in language.lsp_adapters() {
@ -2577,7 +2580,7 @@ impl Project {
.get(&(worktree_id, adapter.name.clone())) .get(&(worktree_id, adapter.name.clone()))
.cloned() .cloned()
{ {
for orphaned_worktree in orphaned_worktrees { for &orphaned_worktree in &orphaned_worktrees {
this.language_server_ids this.language_server_ids
.insert((orphaned_worktree, adapter.name.clone()), new_server_id); .insert((orphaned_worktree, adapter.name.clone()), new_server_id);
} }
@ -2948,7 +2951,7 @@ impl Project {
pub fn update_diagnostic_entries( pub fn update_diagnostic_entries(
&mut self, &mut self,
language_server_id: usize, server_id: usize,
abs_path: PathBuf, abs_path: PathBuf,
version: Option<i32>, version: Option<i32>,
diagnostics: Vec<DiagnosticEntry<Unclipped<PointUtf16>>>, diagnostics: Vec<DiagnosticEntry<Unclipped<PointUtf16>>>,
@ -2964,23 +2967,18 @@ impl Project {
}; };
if let Some(buffer) = self.get_open_buffer(&project_path, cx) { if let Some(buffer) = self.get_open_buffer(&project_path, cx) {
self.update_buffer_diagnostics(&buffer, diagnostics.clone(), version, cx)?; self.update_buffer_diagnostics(&buffer, diagnostics.clone(), server_id, version, cx)?;
} }
let updated = worktree.update(cx, |worktree, cx| { let updated = worktree.update(cx, |worktree, cx| {
worktree worktree
.as_local_mut() .as_local_mut()
.ok_or_else(|| anyhow!("not a local worktree"))? .ok_or_else(|| anyhow!("not a local worktree"))?
.update_diagnostics( .update_diagnostics(server_id, project_path.path.clone(), diagnostics, cx)
language_server_id,
project_path.path.clone(),
diagnostics,
cx,
)
})?; })?;
if updated { if updated {
cx.emit(Event::DiagnosticsUpdated { cx.emit(Event::DiagnosticsUpdated {
language_server_id, language_server_id: server_id,
path: project_path, path: project_path,
}); });
} }
@ -2991,6 +2989,7 @@ impl Project {
&mut self, &mut self,
buffer: &ModelHandle<Buffer>, buffer: &ModelHandle<Buffer>,
mut diagnostics: Vec<DiagnosticEntry<Unclipped<PointUtf16>>>, mut diagnostics: Vec<DiagnosticEntry<Unclipped<PointUtf16>>>,
server_id: usize,
version: Option<i32>, version: Option<i32>,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Result<()> { ) -> Result<()> {
@ -3002,7 +3001,7 @@ impl Project {
.then_with(|| a.message.cmp(&b.message)) .then_with(|| a.message.cmp(&b.message))
} }
let snapshot = self.buffer_snapshot_for_lsp_version(buffer, version, cx)?; let snapshot = self.buffer_snapshot_for_lsp_version(buffer, server_id, version, cx)?;
diagnostics.sort_unstable_by(|a, b| { diagnostics.sort_unstable_by(|a, b| {
Ordering::Equal Ordering::Equal
@ -6261,7 +6260,7 @@ impl Project {
} }
} }
fn running_language_servers_for_buffer( pub fn language_servers_iter_for_buffer(
&self, &self,
buffer: &Buffer, buffer: &Buffer,
cx: &AppContext, cx: &AppContext,
@ -6286,8 +6285,7 @@ impl Project {
buffer: &Buffer, buffer: &Buffer,
cx: &AppContext, cx: &AppContext,
) -> Vec<(&Arc<CachedLspAdapter>, &Arc<LanguageServer>)> { ) -> Vec<(&Arc<CachedLspAdapter>, &Arc<LanguageServer>)> {
self.running_language_servers_for_buffer(buffer, cx) self.language_servers_iter_for_buffer(buffer, cx).collect()
.collect()
} }
fn primary_language_servers_for_buffer( fn primary_language_servers_for_buffer(
@ -6295,7 +6293,7 @@ impl Project {
buffer: &Buffer, buffer: &Buffer,
cx: &AppContext, cx: &AppContext,
) -> Option<(&Arc<CachedLspAdapter>, &Arc<LanguageServer>)> { ) -> Option<(&Arc<CachedLspAdapter>, &Arc<LanguageServer>)> {
self.running_language_servers_for_buffer(buffer, cx).next() self.language_servers_iter_for_buffer(buffer, cx).next()
} }
fn language_server_for_buffer( fn language_server_for_buffer(
@ -6304,7 +6302,7 @@ impl Project {
server_id: usize, server_id: usize,
cx: &AppContext, cx: &AppContext,
) -> Option<(&Arc<CachedLspAdapter>, &Arc<LanguageServer>)> { ) -> Option<(&Arc<CachedLspAdapter>, &Arc<LanguageServer>)> {
self.running_language_servers_for_buffer(buffer, cx) self.language_servers_iter_for_buffer(buffer, cx)
.find(|(_, s)| s.server_id() == server_id) .find(|(_, s)| s.server_id() == server_id)
} }

View file

@ -1420,6 +1420,7 @@ async fn test_empty_diagnostic_ranges(cx: &mut gpui::TestAppContext) {
}, },
}, },
], ],
0,
None, None,
cx, cx,
) )

View file

@ -67,7 +67,7 @@ pub struct LocalWorktree {
is_scanning: (watch::Sender<bool>, watch::Receiver<bool>), is_scanning: (watch::Sender<bool>, watch::Receiver<bool>),
_background_scanner_task: Task<()>, _background_scanner_task: Task<()>,
share: Option<ShareState>, share: Option<ShareState>,
diagnostics: HashMap<Arc<Path>, Vec<DiagnosticEntry<Unclipped<PointUtf16>>>>, diagnostics: HashMap<Arc<Path>, Vec<(usize, Vec<DiagnosticEntry<Unclipped<PointUtf16>>>)>>,
diagnostic_summaries: TreeMap<PathKey, DiagnosticSummary>, diagnostic_summaries: TreeMap<PathKey, DiagnosticSummary>,
client: Arc<Client>, client: Arc<Client>,
fs: Arc<dyn Fs>, fs: Arc<dyn Fs>,
@ -514,13 +514,13 @@ impl LocalWorktree {
pub fn diagnostics_for_path( pub fn diagnostics_for_path(
&self, &self,
path: &Path, path: &Path,
) -> Option<Vec<DiagnosticEntry<Unclipped<PointUtf16>>>> { ) -> Vec<(usize, Vec<DiagnosticEntry<Unclipped<PointUtf16>>>)> {
self.diagnostics.get(path).cloned() self.diagnostics.get(path).cloned().unwrap_or_default()
} }
pub fn update_diagnostics( pub fn update_diagnostics(
&mut self, &mut self,
language_server_id: usize, server_id: usize,
worktree_path: Arc<Path>, worktree_path: Arc<Path>,
diagnostics: Vec<DiagnosticEntry<Unclipped<PointUtf16>>>, diagnostics: Vec<DiagnosticEntry<Unclipped<PointUtf16>>>,
_: &mut ModelContext<Worktree>, _: &mut ModelContext<Worktree>,
@ -530,11 +530,16 @@ impl LocalWorktree {
.diagnostic_summaries .diagnostic_summaries
.remove(&PathKey(worktree_path.clone())) .remove(&PathKey(worktree_path.clone()))
.unwrap_or_default(); .unwrap_or_default();
let new_summary = DiagnosticSummary::new(language_server_id, &diagnostics); let new_summary = DiagnosticSummary::new(server_id, &diagnostics);
if !new_summary.is_empty() { if !new_summary.is_empty() {
self.diagnostic_summaries self.diagnostic_summaries
.insert(PathKey(worktree_path.clone()), new_summary); .insert(PathKey(worktree_path.clone()), new_summary);
self.diagnostics.insert(worktree_path.clone(), diagnostics); let diagnostics_by_server_id =
self.diagnostics.entry(worktree_path.clone()).or_default();
let ix = match diagnostics_by_server_id.binary_search_by_key(&server_id, |e| e.0) {
Ok(ix) | Err(ix) => ix,
};
diagnostics_by_server_id[ix] = (server_id, diagnostics);
} }
let updated = !old_summary.is_empty() || !new_summary.is_empty(); let updated = !old_summary.is_empty() || !new_summary.is_empty();
@ -546,7 +551,7 @@ impl LocalWorktree {
worktree_id: self.id().to_proto(), worktree_id: self.id().to_proto(),
summary: Some(proto::DiagnosticSummary { summary: Some(proto::DiagnosticSummary {
path: worktree_path.to_string_lossy().to_string(), path: worktree_path.to_string_lossy().to_string(),
language_server_id: language_server_id as u64, language_server_id: server_id as u64,
error_count: new_summary.error_count as u32, error_count: new_summary.error_count as u32,
warning_count: new_summary.warning_count as u32, warning_count: new_summary.warning_count as u32,
}), }),

View file

@ -89,23 +89,26 @@ pub fn init(
( (
"tsx", "tsx",
tree_sitter_typescript::language_tsx(), tree_sitter_typescript::language_tsx(),
vec![adapter_arc(typescript::TypeScriptLspAdapter::new( vec![
node_runtime.clone(), adapter_arc(typescript::TypeScriptLspAdapter::new(node_runtime.clone())),
))], adapter_arc(typescript::EsLintLspAdapter::new(node_runtime.clone())),
],
), ),
( (
"typescript", "typescript",
tree_sitter_typescript::language_typescript(), tree_sitter_typescript::language_typescript(),
vec![adapter_arc(typescript::TypeScriptLspAdapter::new( vec![
node_runtime.clone(), adapter_arc(typescript::TypeScriptLspAdapter::new(node_runtime.clone())),
))], adapter_arc(typescript::EsLintLspAdapter::new(node_runtime.clone())),
],
), ),
( (
"javascript", "javascript",
tree_sitter_typescript::language_tsx(), tree_sitter_typescript::language_tsx(),
vec![adapter_arc(typescript::TypeScriptLspAdapter::new( vec![
node_runtime.clone(), adapter_arc(typescript::TypeScriptLspAdapter::new(node_runtime.clone())),
))], adapter_arc(typescript::EsLintLspAdapter::new(node_runtime.clone())),
],
), ),
( (
"html", "html",
@ -149,7 +152,7 @@ pub async fn language(
) -> Arc<Language> { ) -> Arc<Language> {
Arc::new( Arc::new(
Language::new(load_config(name), Some(grammar)) Language::new(load_config(name), Some(grammar))
.with_lsp_adapters(lsp_adapter) .with_lsp_adapters(lsp_adapter.into_iter().collect())
.await .await
.with_queries(load_queries(name)) .with_queries(load_queries(name))
.unwrap(), .unwrap(),

View file

@ -15,7 +15,7 @@ use std::{
use util::http::HttpClient; use util::http::HttpClient;
use util::ResultExt; use util::ResultExt;
fn server_binary_arguments(server_path: &Path) -> Vec<OsString> { fn typescript_server_binary_arguments(server_path: &Path) -> Vec<OsString> {
vec![ vec![
server_path.into(), server_path.into(),
"--stdio".into(), "--stdio".into(),
@ -24,6 +24,10 @@ fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
] ]
} }
fn eslint_server_binary_arguments(server_path: &Path) -> Vec<OsString> {
vec![server_path.into(), "--stdin".into()]
}
pub struct TypeScriptLspAdapter { pub struct TypeScriptLspAdapter {
node: Arc<NodeRuntime>, node: Arc<NodeRuntime>,
} }
@ -89,7 +93,7 @@ impl LspAdapter for TypeScriptLspAdapter {
Ok(LanguageServerBinary { Ok(LanguageServerBinary {
path: self.node.binary_path().await?, path: self.node.binary_path().await?,
arguments: server_binary_arguments(&server_path), arguments: typescript_server_binary_arguments(&server_path),
}) })
} }
@ -101,12 +105,12 @@ impl LspAdapter for TypeScriptLspAdapter {
if new_server_path.exists() { if new_server_path.exists() {
Ok(LanguageServerBinary { Ok(LanguageServerBinary {
path: self.node.binary_path().await?, path: self.node.binary_path().await?,
arguments: server_binary_arguments(&new_server_path), arguments: typescript_server_binary_arguments(&new_server_path),
}) })
} else if old_server_path.exists() { } else if old_server_path.exists() {
Ok(LanguageServerBinary { Ok(LanguageServerBinary {
path: self.node.binary_path().await?, path: self.node.binary_path().await?,
arguments: server_binary_arguments(&old_server_path), arguments: typescript_server_binary_arguments(&old_server_path),
}) })
} else { } else {
Err(anyhow!( Err(anyhow!(
@ -169,7 +173,7 @@ pub struct EsLintLspAdapter {
} }
impl EsLintLspAdapter { impl EsLintLspAdapter {
const SERVER_PATH: &'static str = "node_modules/typescript-language-server/lib/cli.mjs"; const SERVER_PATH: &'static str = "node_modules/eslint/bin/eslint.js";
pub fn new(node: Arc<NodeRuntime>) -> Self { pub fn new(node: Arc<NodeRuntime>) -> Self {
EsLintLspAdapter { node } EsLintLspAdapter { node }
@ -208,7 +212,7 @@ impl LspAdapter for EsLintLspAdapter {
Ok(LanguageServerBinary { Ok(LanguageServerBinary {
path: self.node.binary_path().await?, path: self.node.binary_path().await?,
arguments: server_binary_arguments(&server_path), arguments: eslint_server_binary_arguments(&server_path),
}) })
} }
@ -218,7 +222,7 @@ impl LspAdapter for EsLintLspAdapter {
if server_path.exists() { if server_path.exists() {
Ok(LanguageServerBinary { Ok(LanguageServerBinary {
path: self.node.binary_path().await?, path: self.node.binary_path().await?,
arguments: server_binary_arguments(&server_path), arguments: eslint_server_binary_arguments(&server_path),
}) })
} else { } else {
Err(anyhow!( Err(anyhow!(