Make python run local worktree LSPs (#18353)

Release Notes:

- Python: made it possible to use locally installed `pyright` if
available

---------

Co-authored-by: conrad <conrad@zed.dev>
This commit is contained in:
Mikayla Maki 2024-09-25 12:45:41 -07:00 committed by GitHub
parent dc7c49bd0b
commit ae6a3d15af
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 73 additions and 1 deletions

View file

@ -48,6 +48,7 @@ use lsp::{
LspRequestFuture, MessageActionItem, MessageType, OneOf, ServerHealthStatus, ServerStatus,
SymbolKind, TextEdit, Url, WorkDoneProgressCancelParams, WorkspaceFolder,
};
use node_runtime::read_package_installed_version;
use parking_lot::{Mutex, RwLock};
use postage::watch;
use rand::prelude::*;
@ -7801,6 +7802,44 @@ impl LspAdapterDelegate for LocalLspAdapterDelegate {
task.await.unwrap_or_default()
}
async fn npm_package_installed_version(
&self,
package_name: &str,
) -> Result<Option<(PathBuf, String)>> {
let local_package_directory = self.worktree_root_path();
let node_modules_directory = local_package_directory.join("node_modules");
if let Some(version) =
read_package_installed_version(node_modules_directory.clone(), package_name).await?
{
return Ok(Some((node_modules_directory, version)));
}
let Some(npm) = self.which("npm".as_ref()).await else {
log::warn!(
"Failed to find npm executable for {:?}",
local_package_directory
);
return Ok(None);
};
let env = self.shell_env().await;
let output = smol::process::Command::new(&npm)
.args(["root", "-g"])
.envs(env)
.current_dir(local_package_directory)
.output()
.await?;
let global_node_modules =
PathBuf::from(String::from_utf8_lossy(&output.stdout).to_string());
if let Some(version) =
read_package_installed_version(global_node_modules.clone(), package_name).await?
{
return Ok(Some((global_node_modules, version)));
}
return Ok(None);
}
#[cfg(not(target_os = "windows"))]
async fn which(&self, command: &OsStr) -> Option<PathBuf> {
let worktree_abs_path = self.worktree.abs_path();
@ -7883,6 +7922,13 @@ impl LspAdapterDelegate for SshLspAdapterDelegate {
.ok();
}
async fn npm_package_installed_version(
&self,
_package_name: &str,
) -> Result<Option<(PathBuf, String)>> {
Ok(None)
}
fn http_client(&self) -> Arc<dyn HttpClient> {
Arc::new(BlockedHttpClient)
}