python: Re-land usage of source file path in toolchain picker (#31893)
This reverts commit 1e55e88c18
.
Closes #ISSUE
Release Notes:
- Python toolchain selector now uses path to the closest pyproject.toml
as a basis for picking a toolchain. All files under the same
pyproject.toml (in filesystem hierarchy) will share a single virtual
environment. It is possible to have multiple Python virtual environments
selected for disjoint parts of the same project.
This commit is contained in:
parent
2ebe16a52f
commit
9dd18e5ee1
14 changed files with 195 additions and 92 deletions
|
@ -10,7 +10,8 @@ use crate::{
|
|||
lsp_command::{self, *},
|
||||
lsp_store,
|
||||
manifest_tree::{
|
||||
AdapterQuery, LanguageServerTree, LanguageServerTreeNode, LaunchDisposition, ManifestTree,
|
||||
AdapterQuery, LanguageServerTree, LanguageServerTreeNode, LaunchDisposition,
|
||||
ManifestQueryDelegate, ManifestTree,
|
||||
},
|
||||
prettier_store::{self, PrettierStore, PrettierStoreEvent},
|
||||
project_settings::{LspSettings, ProjectSettings},
|
||||
|
@ -1036,7 +1037,7 @@ impl LocalLspStore {
|
|||
else {
|
||||
return Vec::new();
|
||||
};
|
||||
let delegate = LocalLspAdapterDelegate::from_local_lsp(self, &worktree, cx);
|
||||
let delegate = Arc::new(ManifestQueryDelegate::new(worktree.read(cx).snapshot()));
|
||||
let root = self.lsp_tree.update(cx, |this, cx| {
|
||||
this.get(
|
||||
project_path,
|
||||
|
@ -2290,7 +2291,8 @@ impl LocalLspStore {
|
|||
})
|
||||
.map(|(delegate, servers)| (true, delegate, servers))
|
||||
.unwrap_or_else(|| {
|
||||
let delegate = LocalLspAdapterDelegate::from_local_lsp(self, &worktree, cx);
|
||||
let lsp_delegate = LocalLspAdapterDelegate::from_local_lsp(self, &worktree, cx);
|
||||
let delegate = Arc::new(ManifestQueryDelegate::new(worktree.read(cx).snapshot()));
|
||||
let servers = self
|
||||
.lsp_tree
|
||||
.clone()
|
||||
|
@ -2304,7 +2306,7 @@ impl LocalLspStore {
|
|||
)
|
||||
.collect::<Vec<_>>()
|
||||
});
|
||||
(false, delegate, servers)
|
||||
(false, lsp_delegate, servers)
|
||||
});
|
||||
let servers = servers
|
||||
.into_iter()
|
||||
|
@ -3585,6 +3587,7 @@ impl LspStore {
|
|||
prettier_store: Entity<PrettierStore>,
|
||||
toolchain_store: Entity<ToolchainStore>,
|
||||
environment: Entity<ProjectEnvironment>,
|
||||
manifest_tree: Entity<ManifestTree>,
|
||||
languages: Arc<LanguageRegistry>,
|
||||
http_client: Arc<dyn HttpClient>,
|
||||
fs: Arc<dyn Fs>,
|
||||
|
@ -3618,7 +3621,7 @@ impl LspStore {
|
|||
sender,
|
||||
)
|
||||
};
|
||||
let manifest_tree = ManifestTree::new(worktree_store.clone(), cx);
|
||||
|
||||
Self {
|
||||
mode: LspStoreMode::Local(LocalLspStore {
|
||||
weak: cx.weak_entity(),
|
||||
|
@ -4465,10 +4468,13 @@ impl LspStore {
|
|||
)
|
||||
.map(|(delegate, servers)| (true, delegate, servers))
|
||||
.or_else(|| {
|
||||
let delegate = adapters
|
||||
let lsp_delegate = adapters
|
||||
.entry(worktree_id)
|
||||
.or_insert_with(|| get_adapter(worktree_id, cx))
|
||||
.clone()?;
|
||||
let delegate = Arc::new(ManifestQueryDelegate::new(
|
||||
worktree.read(cx).snapshot(),
|
||||
));
|
||||
let path = file
|
||||
.path()
|
||||
.parent()
|
||||
|
@ -4483,7 +4489,7 @@ impl LspStore {
|
|||
cx,
|
||||
);
|
||||
|
||||
Some((false, delegate, nodes.collect()))
|
||||
Some((false, lsp_delegate, nodes.collect()))
|
||||
})
|
||||
else {
|
||||
continue;
|
||||
|
@ -6476,7 +6482,7 @@ impl LspStore {
|
|||
worktree_id,
|
||||
path: Arc::from("".as_ref()),
|
||||
};
|
||||
let delegate = LocalLspAdapterDelegate::from_local_lsp(local, &worktree, cx);
|
||||
let delegate = Arc::new(ManifestQueryDelegate::new(worktree.read(cx).snapshot()));
|
||||
local.lsp_tree.update(cx, |language_server_tree, cx| {
|
||||
for node in language_server_tree.get(
|
||||
path,
|
||||
|
@ -10204,14 +10210,6 @@ impl LspAdapterDelegate for LocalLspAdapterDelegate {
|
|||
self.worktree.id()
|
||||
}
|
||||
|
||||
fn exists(&self, path: &Path, is_dir: Option<bool>) -> bool {
|
||||
self.worktree.entry_for_path(path).map_or(false, |entry| {
|
||||
is_dir.map_or(true, |is_required_to_be_dir| {
|
||||
is_required_to_be_dir == entry.is_dir()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn worktree_root_path(&self) -> &Path {
|
||||
self.worktree.abs_path().as_ref()
|
||||
}
|
||||
|
|
|
@ -11,16 +11,17 @@ use std::{
|
|||
borrow::Borrow,
|
||||
collections::{BTreeMap, hash_map::Entry},
|
||||
ops::ControlFlow,
|
||||
path::Path,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use collections::HashMap;
|
||||
use gpui::{App, AppContext as _, Context, Entity, EventEmitter, Subscription};
|
||||
use language::{LspAdapterDelegate, ManifestName, ManifestQuery};
|
||||
use language::{ManifestDelegate, ManifestName, ManifestQuery};
|
||||
pub use manifest_store::ManifestProviders;
|
||||
use path_trie::{LabelPresence, RootPathTrie, TriePath};
|
||||
use settings::{SettingsStore, WorktreeId};
|
||||
use worktree::{Event as WorktreeEvent, Worktree};
|
||||
use worktree::{Event as WorktreeEvent, Snapshot, Worktree};
|
||||
|
||||
use crate::{
|
||||
ProjectPath,
|
||||
|
@ -89,7 +90,7 @@ pub(crate) enum ManifestTreeEvent {
|
|||
impl EventEmitter<ManifestTreeEvent> for ManifestTree {}
|
||||
|
||||
impl ManifestTree {
|
||||
pub(crate) fn new(worktree_store: Entity<WorktreeStore>, cx: &mut App) -> Entity<Self> {
|
||||
pub fn new(worktree_store: Entity<WorktreeStore>, cx: &mut App) -> Entity<Self> {
|
||||
cx.new(|cx| Self {
|
||||
root_points: Default::default(),
|
||||
_subscriptions: [
|
||||
|
@ -106,11 +107,11 @@ impl ManifestTree {
|
|||
worktree_store,
|
||||
})
|
||||
}
|
||||
fn root_for_path(
|
||||
pub(crate) fn root_for_path(
|
||||
&mut self,
|
||||
ProjectPath { worktree_id, path }: ProjectPath,
|
||||
manifests: &mut dyn Iterator<Item = ManifestName>,
|
||||
delegate: Arc<dyn LspAdapterDelegate>,
|
||||
delegate: Arc<dyn ManifestDelegate>,
|
||||
cx: &mut App,
|
||||
) -> BTreeMap<ManifestName, ProjectPath> {
|
||||
debug_assert_eq!(delegate.worktree_id(), worktree_id);
|
||||
|
@ -218,3 +219,26 @@ impl ManifestTree {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct ManifestQueryDelegate {
|
||||
worktree: Snapshot,
|
||||
}
|
||||
impl ManifestQueryDelegate {
|
||||
pub fn new(worktree: Snapshot) -> Self {
|
||||
Self { worktree }
|
||||
}
|
||||
}
|
||||
|
||||
impl ManifestDelegate for ManifestQueryDelegate {
|
||||
fn exists(&self, path: &Path, is_dir: Option<bool>) -> bool {
|
||||
self.worktree.entry_for_path(path).map_or(false, |entry| {
|
||||
is_dir.map_or(true, |is_required_to_be_dir| {
|
||||
is_required_to_be_dir == entry.is_dir()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn worktree_id(&self) -> WorktreeId {
|
||||
self.worktree.id()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ use std::{
|
|||
use collections::{HashMap, IndexMap};
|
||||
use gpui::{App, AppContext as _, Entity, Subscription};
|
||||
use language::{
|
||||
Attach, CachedLspAdapter, LanguageName, LanguageRegistry, LspAdapterDelegate,
|
||||
Attach, CachedLspAdapter, LanguageName, LanguageRegistry, ManifestDelegate,
|
||||
language_settings::AllLanguageSettings,
|
||||
};
|
||||
use lsp::LanguageServerName;
|
||||
|
@ -151,7 +151,7 @@ impl LanguageServerTree {
|
|||
&'a mut self,
|
||||
path: ProjectPath,
|
||||
query: AdapterQuery<'_>,
|
||||
delegate: Arc<dyn LspAdapterDelegate>,
|
||||
delegate: Arc<dyn ManifestDelegate>,
|
||||
cx: &mut App,
|
||||
) -> impl Iterator<Item = LanguageServerTreeNode> + 'a {
|
||||
let settings_location = SettingsLocation {
|
||||
|
@ -181,7 +181,7 @@ impl LanguageServerTree {
|
|||
LanguageServerName,
|
||||
(LspSettings, BTreeSet<LanguageName>, Arc<CachedLspAdapter>),
|
||||
>,
|
||||
delegate: Arc<dyn LspAdapterDelegate>,
|
||||
delegate: Arc<dyn ManifestDelegate>,
|
||||
cx: &mut App,
|
||||
) -> impl Iterator<Item = LanguageServerTreeNode> + 'a {
|
||||
let worktree_id = path.worktree_id;
|
||||
|
@ -401,7 +401,7 @@ impl<'tree> ServerTreeRebase<'tree> {
|
|||
&'a mut self,
|
||||
path: ProjectPath,
|
||||
query: AdapterQuery<'_>,
|
||||
delegate: Arc<dyn LspAdapterDelegate>,
|
||||
delegate: Arc<dyn ManifestDelegate>,
|
||||
cx: &mut App,
|
||||
) -> impl Iterator<Item = LanguageServerTreeNode> + 'a {
|
||||
let settings_location = SettingsLocation {
|
||||
|
|
|
@ -35,6 +35,7 @@ pub use git_store::{
|
|||
ConflictRegion, ConflictSet, ConflictSetSnapshot, ConflictSetUpdate,
|
||||
git_traversal::{ChildEntriesGitIter, GitEntry, GitEntryRef, GitTraversal},
|
||||
};
|
||||
pub use manifest_tree::ManifestTree;
|
||||
|
||||
use anyhow::{Context as _, Result, anyhow};
|
||||
use buffer_store::{BufferStore, BufferStoreEvent};
|
||||
|
@ -874,11 +875,13 @@ impl Project {
|
|||
cx.new(|cx| ContextServerStore::new(worktree_store.clone(), cx));
|
||||
|
||||
let environment = cx.new(|_| ProjectEnvironment::new(env));
|
||||
let manifest_tree = ManifestTree::new(worktree_store.clone(), cx);
|
||||
let toolchain_store = cx.new(|cx| {
|
||||
ToolchainStore::local(
|
||||
languages.clone(),
|
||||
worktree_store.clone(),
|
||||
environment.clone(),
|
||||
manifest_tree.clone(),
|
||||
cx,
|
||||
)
|
||||
});
|
||||
|
@ -946,6 +949,7 @@ impl Project {
|
|||
prettier_store.clone(),
|
||||
toolchain_store.clone(),
|
||||
environment.clone(),
|
||||
manifest_tree,
|
||||
languages.clone(),
|
||||
client.http_client(),
|
||||
fs.clone(),
|
||||
|
@ -3084,16 +3088,13 @@ impl Project {
|
|||
path: ProjectPath,
|
||||
language_name: LanguageName,
|
||||
cx: &App,
|
||||
) -> Task<Option<ToolchainList>> {
|
||||
if let Some(toolchain_store) = self.toolchain_store.clone() {
|
||||
) -> Task<Option<(ToolchainList, Arc<Path>)>> {
|
||||
if let Some(toolchain_store) = self.toolchain_store.as_ref().map(Entity::downgrade) {
|
||||
cx.spawn(async move |cx| {
|
||||
cx.update(|cx| {
|
||||
toolchain_store
|
||||
.read(cx)
|
||||
.list_toolchains(path, language_name, cx)
|
||||
})
|
||||
.ok()?
|
||||
.await
|
||||
toolchain_store
|
||||
.update(cx, |this, cx| this.list_toolchains(path, language_name, cx))
|
||||
.ok()?
|
||||
.await
|
||||
})
|
||||
} else {
|
||||
Task::ready(None)
|
||||
|
|
|
@ -19,7 +19,11 @@ use rpc::{
|
|||
use settings::WorktreeId;
|
||||
use util::ResultExt as _;
|
||||
|
||||
use crate::{ProjectEnvironment, ProjectPath, worktree_store::WorktreeStore};
|
||||
use crate::{
|
||||
ProjectEnvironment, ProjectPath,
|
||||
manifest_tree::{ManifestQueryDelegate, ManifestTree},
|
||||
worktree_store::WorktreeStore,
|
||||
};
|
||||
|
||||
pub struct ToolchainStore(ToolchainStoreInner);
|
||||
enum ToolchainStoreInner {
|
||||
|
@ -42,6 +46,7 @@ impl ToolchainStore {
|
|||
languages: Arc<LanguageRegistry>,
|
||||
worktree_store: Entity<WorktreeStore>,
|
||||
project_environment: Entity<ProjectEnvironment>,
|
||||
manifest_tree: Entity<ManifestTree>,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Self {
|
||||
let entity = cx.new(|_| LocalToolchainStore {
|
||||
|
@ -49,6 +54,7 @@ impl ToolchainStore {
|
|||
worktree_store,
|
||||
project_environment,
|
||||
active_toolchains: Default::default(),
|
||||
manifest_tree,
|
||||
});
|
||||
let subscription = cx.subscribe(&entity, |_, _, e: &ToolchainStoreEvent, cx| {
|
||||
cx.emit(e.clone())
|
||||
|
@ -80,11 +86,11 @@ impl ToolchainStore {
|
|||
&self,
|
||||
path: ProjectPath,
|
||||
language_name: LanguageName,
|
||||
cx: &App,
|
||||
) -> Task<Option<ToolchainList>> {
|
||||
cx: &mut Context<Self>,
|
||||
) -> Task<Option<(ToolchainList, Arc<Path>)>> {
|
||||
match &self.0 {
|
||||
ToolchainStoreInner::Local(local, _) => {
|
||||
local.read(cx).list_toolchains(path, language_name, cx)
|
||||
local.update(cx, |this, cx| this.list_toolchains(path, language_name, cx))
|
||||
}
|
||||
ToolchainStoreInner::Remote(remote) => {
|
||||
remote.read(cx).list_toolchains(path, language_name, cx)
|
||||
|
@ -181,7 +187,7 @@ impl ToolchainStore {
|
|||
})?
|
||||
.await;
|
||||
let has_values = toolchains.is_some();
|
||||
let groups = if let Some(toolchains) = &toolchains {
|
||||
let groups = if let Some((toolchains, _)) = &toolchains {
|
||||
toolchains
|
||||
.groups
|
||||
.iter()
|
||||
|
@ -195,8 +201,8 @@ impl ToolchainStore {
|
|||
} else {
|
||||
vec![]
|
||||
};
|
||||
let toolchains = if let Some(toolchains) = toolchains {
|
||||
toolchains
|
||||
let (toolchains, relative_path) = if let Some((toolchains, relative_path)) = toolchains {
|
||||
let toolchains = toolchains
|
||||
.toolchains
|
||||
.into_iter()
|
||||
.map(|toolchain| {
|
||||
|
@ -207,15 +213,17 @@ impl ToolchainStore {
|
|||
raw_json: toolchain.as_json.to_string(),
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.collect::<Vec<_>>();
|
||||
(toolchains, relative_path)
|
||||
} else {
|
||||
vec![]
|
||||
(vec![], Arc::from(Path::new("")))
|
||||
};
|
||||
|
||||
Ok(proto::ListToolchainsResponse {
|
||||
has_values,
|
||||
toolchains,
|
||||
groups,
|
||||
relative_worktree_path: Some(relative_path.to_string_lossy().into_owned()),
|
||||
})
|
||||
}
|
||||
pub fn as_language_toolchain_store(&self) -> Arc<dyn LanguageToolchainStore> {
|
||||
|
@ -231,6 +239,7 @@ struct LocalToolchainStore {
|
|||
worktree_store: Entity<WorktreeStore>,
|
||||
project_environment: Entity<ProjectEnvironment>,
|
||||
active_toolchains: BTreeMap<(WorktreeId, LanguageName), BTreeMap<Arc<Path>, Toolchain>>,
|
||||
manifest_tree: Entity<ManifestTree>,
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
|
@ -312,36 +321,73 @@ impl LocalToolchainStore {
|
|||
})
|
||||
}
|
||||
pub(crate) fn list_toolchains(
|
||||
&self,
|
||||
&mut self,
|
||||
path: ProjectPath,
|
||||
language_name: LanguageName,
|
||||
cx: &App,
|
||||
) -> Task<Option<ToolchainList>> {
|
||||
cx: &mut Context<Self>,
|
||||
) -> Task<Option<(ToolchainList, Arc<Path>)>> {
|
||||
let registry = self.languages.clone();
|
||||
let Some(abs_path) = self
|
||||
.worktree_store
|
||||
.read(cx)
|
||||
.worktree_for_id(path.worktree_id, cx)
|
||||
.map(|worktree| worktree.read(cx).abs_path())
|
||||
else {
|
||||
return Task::ready(None);
|
||||
};
|
||||
|
||||
let manifest_tree = self.manifest_tree.downgrade();
|
||||
|
||||
let environment = self.project_environment.clone();
|
||||
cx.spawn(async move |cx| {
|
||||
cx.spawn(async move |this, cx| {
|
||||
let language = cx
|
||||
.background_spawn(registry.language_for_name(language_name.as_ref()))
|
||||
.await
|
||||
.ok()?;
|
||||
let toolchains = language.toolchain_lister()?;
|
||||
let manifest_name = toolchains.manifest_name();
|
||||
let (snapshot, worktree) = this
|
||||
.update(cx, |this, cx| {
|
||||
this.worktree_store
|
||||
.read(cx)
|
||||
.worktree_for_id(path.worktree_id, cx)
|
||||
.map(|worktree| (worktree.read(cx).snapshot(), worktree))
|
||||
})
|
||||
.ok()
|
||||
.flatten()?;
|
||||
let worktree_id = snapshot.id();
|
||||
let worktree_root = snapshot.abs_path().to_path_buf();
|
||||
let relative_path = manifest_tree
|
||||
.update(cx, |this, cx| {
|
||||
this.root_for_path(
|
||||
path,
|
||||
&mut std::iter::once(manifest_name.clone()),
|
||||
Arc::new(ManifestQueryDelegate::new(snapshot)),
|
||||
cx,
|
||||
)
|
||||
})
|
||||
.ok()?
|
||||
.remove(&manifest_name)
|
||||
.unwrap_or_else(|| ProjectPath {
|
||||
path: Arc::from(Path::new("")),
|
||||
worktree_id,
|
||||
});
|
||||
let abs_path = worktree
|
||||
.update(cx, |this, _| this.absolutize(&relative_path.path).ok())
|
||||
.ok()
|
||||
.flatten()?;
|
||||
|
||||
let project_env = environment
|
||||
.update(cx, |environment, cx| {
|
||||
environment.get_directory_environment(abs_path.clone(), cx)
|
||||
environment.get_directory_environment(abs_path.as_path().into(), cx)
|
||||
})
|
||||
.ok()?
|
||||
.await;
|
||||
|
||||
cx.background_spawn(async move {
|
||||
let language = registry
|
||||
.language_for_name(language_name.as_ref())
|
||||
.await
|
||||
.ok()?;
|
||||
let toolchains = language.toolchain_lister()?;
|
||||
Some(toolchains.list(abs_path.to_path_buf(), project_env).await)
|
||||
Some((
|
||||
toolchains
|
||||
.list(
|
||||
worktree_root,
|
||||
Some(relative_path.path.clone())
|
||||
.filter(|_| *relative_path.path != *Path::new("")),
|
||||
project_env,
|
||||
)
|
||||
.await,
|
||||
relative_path.path,
|
||||
))
|
||||
})
|
||||
.await
|
||||
})
|
||||
|
@ -404,7 +450,7 @@ impl RemoteToolchainStore {
|
|||
path: ProjectPath,
|
||||
language_name: LanguageName,
|
||||
cx: &App,
|
||||
) -> Task<Option<ToolchainList>> {
|
||||
) -> Task<Option<(ToolchainList, Arc<Path>)>> {
|
||||
let project_id = self.project_id;
|
||||
let client = self.client.clone();
|
||||
cx.background_spawn(async move {
|
||||
|
@ -444,11 +490,20 @@ impl RemoteToolchainStore {
|
|||
Some((usize::try_from(group.start_index).ok()?, group.name.into()))
|
||||
})
|
||||
.collect();
|
||||
Some(ToolchainList {
|
||||
toolchains,
|
||||
default: None,
|
||||
groups,
|
||||
})
|
||||
let relative_path = Arc::from(Path::new(
|
||||
response
|
||||
.relative_worktree_path
|
||||
.as_deref()
|
||||
.unwrap_or_default(),
|
||||
));
|
||||
Some((
|
||||
ToolchainList {
|
||||
toolchains,
|
||||
default: None,
|
||||
groups,
|
||||
},
|
||||
relative_path,
|
||||
))
|
||||
})
|
||||
}
|
||||
pub(crate) fn active_toolchain(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue