Default to language that started LSP when querying project symbols

This commit is contained in:
Antonio Scandurra 2022-08-22 12:32:58 +02:00
parent cd62e60eba
commit 13c2021aef

View file

@ -202,6 +202,7 @@ pub enum Event {
pub enum LanguageServerState { pub enum LanguageServerState {
Starting(Task<Option<Arc<LanguageServer>>>), Starting(Task<Option<Arc<LanguageServer>>>),
Running { Running {
language: Arc<Language>,
adapter: Arc<CachedLspAdapter>, adapter: Arc<CachedLspAdapter>,
server: Arc<LanguageServer>, server: Arc<LanguageServer>,
}, },
@ -1969,7 +1970,7 @@ impl Project {
uri: lsp::Url::from_file_path(abs_path).unwrap(), uri: lsp::Url::from_file_path(abs_path).unwrap(),
}; };
for (_, server) in self.language_servers_for_worktree(worktree_id) { for (_, _, server) in self.language_servers_for_worktree(worktree_id) {
server server
.notify::<lsp::notification::DidSaveTextDocument>( .notify::<lsp::notification::DidSaveTextDocument>(
lsp::DidSaveTextDocumentParams { lsp::DidSaveTextDocumentParams {
@ -2004,15 +2005,18 @@ impl Project {
fn language_servers_for_worktree( fn language_servers_for_worktree(
&self, &self,
worktree_id: WorktreeId, worktree_id: WorktreeId,
) -> impl Iterator<Item = (&Arc<CachedLspAdapter>, &Arc<LanguageServer>)> { ) -> impl Iterator<Item = (&Arc<CachedLspAdapter>, &Arc<Language>, &Arc<LanguageServer>)> {
self.language_server_ids self.language_server_ids
.iter() .iter()
.filter_map(move |((language_server_worktree_id, _), id)| { .filter_map(move |((language_server_worktree_id, _), id)| {
if *language_server_worktree_id == worktree_id { if *language_server_worktree_id == worktree_id {
if let Some(LanguageServerState::Running { adapter, server }) = if let Some(LanguageServerState::Running {
self.language_servers.get(id) adapter,
language,
server,
}) = self.language_servers.get(id)
{ {
return Some((adapter, server)); return Some((adapter, language, server));
} }
} }
None None
@ -2282,6 +2286,7 @@ impl Project {
server_id, server_id,
LanguageServerState::Running { LanguageServerState::Running {
adapter: adapter.clone(), adapter: adapter.clone(),
language,
server: language_server.clone(), server: language_server.clone(),
}, },
); );
@ -3314,10 +3319,14 @@ impl Project {
.worktree_for_id(worktree_id, cx) .worktree_for_id(worktree_id, cx)
.and_then(|worktree| worktree.read(cx).as_local()) .and_then(|worktree| worktree.read(cx).as_local())
{ {
if let Some(LanguageServerState::Running { adapter, server }) = if let Some(LanguageServerState::Running {
self.language_servers.get(server_id) adapter,
language,
server,
}) = self.language_servers.get(server_id)
{ {
let adapter = adapter.clone(); let adapter = adapter.clone();
let language = language.clone();
let worktree_abs_path = worktree.abs_path().clone(); let worktree_abs_path = worktree.abs_path().clone();
requests.push( requests.push(
server server
@ -3331,6 +3340,7 @@ impl Project {
.map(move |response| { .map(move |response| {
( (
adapter, adapter,
language,
worktree_id, worktree_id,
worktree_abs_path, worktree_abs_path,
response.unwrap_or_default(), response.unwrap_or_default(),
@ -3350,7 +3360,14 @@ impl Project {
}; };
let symbols = this.read_with(&cx, |this, cx| { let symbols = this.read_with(&cx, |this, cx| {
let mut symbols = Vec::new(); let mut symbols = Vec::new();
for (adapter, source_worktree_id, worktree_abs_path, response) in responses { for (
adapter,
adapter_language,
source_worktree_id,
worktree_abs_path,
response,
) in responses
{
symbols.extend(response.into_iter().flatten().filter_map(|lsp_symbol| { symbols.extend(response.into_iter().flatten().filter_map(|lsp_symbol| {
let abs_path = lsp_symbol.location.uri.to_file_path().ok()?; let abs_path = lsp_symbol.location.uri.to_file_path().ok()?;
let mut worktree_id = source_worktree_id; let mut worktree_id = source_worktree_id;
@ -3369,16 +3386,15 @@ impl Project {
path: path.into(), path: path.into(),
}; };
let signature = this.symbol_signature(&project_path); let signature = this.symbol_signature(&project_path);
let language = this.languages.select_language(&project_path.path); let language = this
.languages
.select_language(&project_path.path)
.unwrap_or(adapter_language.clone());
let language_server_name = adapter.name.clone(); let language_server_name = adapter.name.clone();
Some(async move { Some(async move {
let label = if let Some(language) = language { let label = language
language
.label_for_symbol(&lsp_symbol.name, lsp_symbol.kind) .label_for_symbol(&lsp_symbol.name, lsp_symbol.kind)
.await .await;
} else {
None
};
Symbol { Symbol {
language_server_name, language_server_name,
@ -5940,8 +5956,9 @@ impl Project {
let key = (worktree_id, name); let key = (worktree_id, name);
if let Some(server_id) = self.language_server_ids.get(&key) { if let Some(server_id) = self.language_server_ids.get(&key) {
if let Some(LanguageServerState::Running { adapter, server }) = if let Some(LanguageServerState::Running {
self.language_servers.get(server_id) adapter, server, ..
}) = self.language_servers.get(server_id)
{ {
return Some((adapter, server)); return Some((adapter, server));
} }