python.rs

   1use anyhow::{Context as _, ensure};
   2use anyhow::{Result, anyhow};
   3use async_trait::async_trait;
   4use collections::HashMap;
   5use futures::{AsyncBufReadExt, StreamExt as _};
   6use gpui::{App, AsyncApp, SharedString, Task};
   7use http_client::github::{AssetKind, GitHubLspBinaryVersion, latest_github_release};
   8use language::language_settings::language_settings;
   9use language::{ContextLocation, LanguageToolchainStore, LspInstaller};
  10use language::{ContextProvider, LspAdapter, LspAdapterDelegate};
  11use language::{LanguageName, ManifestName, ManifestProvider, ManifestQuery};
  12use language::{Toolchain, ToolchainList, ToolchainLister, ToolchainMetadata};
  13use lsp::LanguageServerBinary;
  14use lsp::LanguageServerName;
  15use node_runtime::{NodeRuntime, VersionStrategy};
  16use pet_core::Configuration;
  17use pet_core::os_environment::Environment;
  18use pet_core::python_environment::{PythonEnvironment, PythonEnvironmentKind};
  19use pet_virtualenv::is_virtualenv_dir;
  20use project::Fs;
  21use project::lsp_store::language_server_settings;
  22use serde_json::{Value, json};
  23use smol::lock::OnceCell;
  24use std::cmp::Ordering;
  25use std::env::consts;
  26use util::command::new_smol_command;
  27use util::fs::{make_file_executable, remove_matching};
  28use util::rel_path::RelPath;
  29
  30use http_client::github_download::{GithubBinaryMetadata, download_server_binary};
  31use parking_lot::Mutex;
  32use std::str::FromStr;
  33use std::{
  34    borrow::Cow,
  35    fmt::Write,
  36    path::{Path, PathBuf},
  37    sync::Arc,
  38};
  39use task::{ShellKind, TaskTemplate, TaskTemplates, VariableName};
  40use util::{ResultExt, maybe};
  41
  42pub(crate) struct PyprojectTomlManifestProvider;
  43
  44impl ManifestProvider for PyprojectTomlManifestProvider {
  45    fn name(&self) -> ManifestName {
  46        SharedString::new_static("pyproject.toml").into()
  47    }
  48
  49    fn search(
  50        &self,
  51        ManifestQuery {
  52            path,
  53            depth,
  54            delegate,
  55        }: ManifestQuery,
  56    ) -> Option<Arc<RelPath>> {
  57        for path in path.ancestors().take(depth) {
  58            let p = path.join(RelPath::unix("pyproject.toml").unwrap());
  59            if delegate.exists(&p, Some(false)) {
  60                return Some(path.into());
  61            }
  62        }
  63
  64        None
  65    }
  66}
  67
  68enum TestRunner {
  69    UNITTEST,
  70    PYTEST,
  71}
  72
  73impl FromStr for TestRunner {
  74    type Err = ();
  75
  76    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
  77        match s {
  78            "unittest" => Ok(Self::UNITTEST),
  79            "pytest" => Ok(Self::PYTEST),
  80            _ => Err(()),
  81        }
  82    }
  83}
  84
  85/// Pyright assigns each completion item a `sortText` of the form `XX.YYYY.name`.
  86/// Where `XX` is the sorting category, `YYYY` is based on most recent usage,
  87/// and `name` is the symbol name itself.
  88///
  89/// The problem with it is that Pyright adjusts the sort text based on previous resolutions (items for which we've issued `completion/resolve` call have their sortText adjusted),
  90/// which - long story short - makes completion items list non-stable. Pyright probably relies on VSCode's implementation detail.
  91/// see https://github.com/microsoft/pyright/blob/95ef4e103b9b2f129c9320427e51b73ea7cf78bd/packages/pyright-internal/src/languageService/completionProvider.ts#LL2873
  92fn process_pyright_completions(items: &mut [lsp::CompletionItem]) {
  93    for item in items {
  94        item.sort_text.take();
  95    }
  96}
  97
  98pub struct TyLspAdapter {
  99    fs: Arc<dyn Fs>,
 100}
 101
 102#[cfg(target_os = "macos")]
 103impl TyLspAdapter {
 104    const GITHUB_ASSET_KIND: AssetKind = AssetKind::TarGz;
 105    const ARCH_SERVER_NAME: &str = "apple-darwin";
 106}
 107
 108#[cfg(target_os = "linux")]
 109impl TyLspAdapter {
 110    const GITHUB_ASSET_KIND: AssetKind = AssetKind::TarGz;
 111    const ARCH_SERVER_NAME: &str = "unknown-linux-gnu";
 112}
 113
 114#[cfg(target_os = "freebsd")]
 115impl TyLspAdapter {
 116    const GITHUB_ASSET_KIND: AssetKind = AssetKind::TarGz;
 117    const ARCH_SERVER_NAME: &str = "unknown-freebsd";
 118}
 119
 120#[cfg(target_os = "windows")]
 121impl TyLspAdapter {
 122    const GITHUB_ASSET_KIND: AssetKind = AssetKind::Zip;
 123    const ARCH_SERVER_NAME: &str = "pc-windows-msvc";
 124}
 125
 126impl TyLspAdapter {
 127    const SERVER_NAME: LanguageServerName = LanguageServerName::new_static("ty");
 128
 129    pub fn new(fs: Arc<dyn Fs>) -> TyLspAdapter {
 130        TyLspAdapter { fs }
 131    }
 132
 133    fn build_asset_name() -> Result<(String, String)> {
 134        let arch = match consts::ARCH {
 135            "x86" => "i686",
 136            _ => consts::ARCH,
 137        };
 138        let os = Self::ARCH_SERVER_NAME;
 139        let suffix = match consts::OS {
 140            "windows" => "zip",
 141            _ => "tar.gz",
 142        };
 143        let asset_name = format!("ty-{arch}-{os}.{suffix}");
 144        let asset_stem = format!("ty-{arch}-{os}");
 145        Ok((asset_stem, asset_name))
 146    }
 147}
 148
 149#[async_trait(?Send)]
 150impl LspAdapter for TyLspAdapter {
 151    fn name(&self) -> LanguageServerName {
 152        Self::SERVER_NAME
 153    }
 154
 155    async fn workspace_configuration(
 156        self: Arc<Self>,
 157        delegate: &Arc<dyn LspAdapterDelegate>,
 158        toolchain: Option<Toolchain>,
 159        cx: &mut AsyncApp,
 160    ) -> Result<Value> {
 161        let mut ret = cx
 162            .update(|cx| {
 163                language_server_settings(delegate.as_ref(), &self.name(), cx)
 164                    .and_then(|s| s.settings.clone())
 165            })?
 166            .unwrap_or_else(|| json!({}));
 167        if let Some(toolchain) = toolchain.and_then(|toolchain| {
 168            serde_json::from_value::<PythonEnvironment>(toolchain.as_json).ok()
 169        }) {
 170            _ = maybe!({
 171                let uri = url::Url::from_file_path(toolchain.executable?).ok()?;
 172                let sys_prefix = toolchain.prefix.clone()?;
 173                let environment = json!({
 174                    "executable": {
 175                        "uri": uri,
 176                        "sysPrefix": sys_prefix
 177                    }
 178                });
 179                ret.as_object_mut()?
 180                    .entry("pythonExtension")
 181                    .or_insert_with(|| json!({ "activeEnvironment": environment }));
 182                Some(())
 183            });
 184        }
 185        Ok(json!({"ty": ret}))
 186    }
 187}
 188
 189impl LspInstaller for TyLspAdapter {
 190    type BinaryVersion = GitHubLspBinaryVersion;
 191    async fn fetch_latest_server_version(
 192        &self,
 193        delegate: &dyn LspAdapterDelegate,
 194        _: bool,
 195        _: &mut AsyncApp,
 196    ) -> Result<Self::BinaryVersion> {
 197        let release =
 198            latest_github_release("astral-sh/ty", true, true, delegate.http_client()).await?;
 199        let (_, asset_name) = Self::build_asset_name()?;
 200        let asset = release
 201            .assets
 202            .into_iter()
 203            .find(|asset| asset.name == asset_name)
 204            .with_context(|| format!("no asset found matching `{asset_name:?}`"))?;
 205        Ok(GitHubLspBinaryVersion {
 206            name: release.tag_name,
 207            url: asset.browser_download_url,
 208            digest: asset.digest,
 209        })
 210    }
 211
 212    async fn fetch_server_binary(
 213        &self,
 214        latest_version: Self::BinaryVersion,
 215        container_dir: PathBuf,
 216        delegate: &dyn LspAdapterDelegate,
 217    ) -> Result<LanguageServerBinary> {
 218        let GitHubLspBinaryVersion {
 219            name,
 220            url,
 221            digest: expected_digest,
 222        } = latest_version;
 223        let destination_path = container_dir.join(format!("ty-{name}"));
 224
 225        async_fs::create_dir_all(&destination_path).await?;
 226
 227        let server_path = match Self::GITHUB_ASSET_KIND {
 228            AssetKind::TarGz | AssetKind::Gz => destination_path
 229                .join(Self::build_asset_name()?.0)
 230                .join("ty"),
 231            AssetKind::Zip => destination_path.clone().join("ty.exe"),
 232        };
 233
 234        let binary = LanguageServerBinary {
 235            path: server_path.clone(),
 236            env: None,
 237            arguments: vec!["server".into()],
 238        };
 239
 240        let metadata_path = destination_path.with_extension("metadata");
 241        let metadata = GithubBinaryMetadata::read_from_file(&metadata_path)
 242            .await
 243            .ok();
 244        if let Some(metadata) = metadata {
 245            let validity_check = async || {
 246                delegate
 247                    .try_exec(LanguageServerBinary {
 248                        path: server_path.clone(),
 249                        arguments: vec!["--version".into()],
 250                        env: None,
 251                    })
 252                    .await
 253                    .inspect_err(|err| {
 254                        log::warn!("Unable to run {server_path:?} asset, redownloading: {err}",)
 255                    })
 256            };
 257            if let (Some(actual_digest), Some(expected_digest)) =
 258                (&metadata.digest, &expected_digest)
 259            {
 260                if actual_digest == expected_digest {
 261                    if validity_check().await.is_ok() {
 262                        return Ok(binary);
 263                    }
 264                } else {
 265                    log::info!(
 266                        "SHA-256 mismatch for {destination_path:?} asset, downloading new asset. Expected: {expected_digest}, Got: {actual_digest}"
 267                    );
 268                }
 269            } else if validity_check().await.is_ok() {
 270                return Ok(binary);
 271            }
 272        }
 273
 274        download_server_binary(
 275            &*delegate.http_client(),
 276            &url,
 277            expected_digest.as_deref(),
 278            &destination_path,
 279            Self::GITHUB_ASSET_KIND,
 280        )
 281        .await?;
 282        make_file_executable(&server_path).await?;
 283        remove_matching(&container_dir, |path| path != destination_path).await;
 284        GithubBinaryMetadata::write_to_file(
 285            &GithubBinaryMetadata {
 286                metadata_version: 1,
 287                digest: expected_digest,
 288            },
 289            &metadata_path,
 290        )
 291        .await?;
 292
 293        Ok(LanguageServerBinary {
 294            path: server_path,
 295            env: None,
 296            arguments: vec!["server".into()],
 297        })
 298    }
 299
 300    async fn cached_server_binary(
 301        &self,
 302        container_dir: PathBuf,
 303        _: &dyn LspAdapterDelegate,
 304    ) -> Option<LanguageServerBinary> {
 305        maybe!(async {
 306            let mut last = None;
 307            let mut entries = self.fs.read_dir(&container_dir).await?;
 308            while let Some(entry) = entries.next().await {
 309                let path = entry?;
 310                if path.extension().is_some_and(|ext| ext == "metadata") {
 311                    continue;
 312                }
 313                last = Some(path);
 314            }
 315
 316            let path = last.context("no cached binary")?;
 317            let path = match TyLspAdapter::GITHUB_ASSET_KIND {
 318                AssetKind::TarGz | AssetKind::Gz => {
 319                    path.join(Self::build_asset_name()?.0).join("ty")
 320                }
 321                AssetKind::Zip => path.join("ty.exe"),
 322            };
 323
 324            anyhow::Ok(LanguageServerBinary {
 325                path,
 326                env: None,
 327                arguments: vec!["server".into()],
 328            })
 329        })
 330        .await
 331        .log_err()
 332    }
 333}
 334
 335pub struct PyrightLspAdapter {
 336    node: NodeRuntime,
 337}
 338
 339impl PyrightLspAdapter {
 340    const SERVER_NAME: LanguageServerName = LanguageServerName::new_static("pyright");
 341    const SERVER_PATH: &str = "node_modules/pyright/langserver.index.js";
 342    const NODE_MODULE_RELATIVE_SERVER_PATH: &str = "pyright/langserver.index.js";
 343
 344    pub const fn new(node: NodeRuntime) -> Self {
 345        PyrightLspAdapter { node }
 346    }
 347
 348    async fn get_cached_server_binary(
 349        container_dir: PathBuf,
 350        node: &NodeRuntime,
 351    ) -> Option<LanguageServerBinary> {
 352        let server_path = container_dir.join(Self::SERVER_PATH);
 353        if server_path.exists() {
 354            Some(LanguageServerBinary {
 355                path: node.binary_path().await.log_err()?,
 356                env: None,
 357                arguments: vec![server_path.into(), "--stdio".into()],
 358            })
 359        } else {
 360            log::error!("missing executable in directory {:?}", server_path);
 361            None
 362        }
 363    }
 364}
 365
 366#[async_trait(?Send)]
 367impl LspAdapter for PyrightLspAdapter {
 368    fn name(&self) -> LanguageServerName {
 369        Self::SERVER_NAME
 370    }
 371
 372    async fn initialization_options(
 373        self: Arc<Self>,
 374        _: &Arc<dyn LspAdapterDelegate>,
 375    ) -> Result<Option<Value>> {
 376        // Provide minimal initialization options
 377        // Virtual environment configuration will be handled through workspace configuration
 378        Ok(Some(json!({
 379            "python": {
 380                "analysis": {
 381                    "autoSearchPaths": true,
 382                    "useLibraryCodeForTypes": true,
 383                    "autoImportCompletions": true
 384                }
 385            }
 386        })))
 387    }
 388
 389    async fn process_completions(&self, items: &mut [lsp::CompletionItem]) {
 390        process_pyright_completions(items);
 391    }
 392
 393    async fn label_for_completion(
 394        &self,
 395        item: &lsp::CompletionItem,
 396        language: &Arc<language::Language>,
 397    ) -> Option<language::CodeLabel> {
 398        let label = &item.label;
 399        let grammar = language.grammar()?;
 400        let highlight_id = match item.kind? {
 401            lsp::CompletionItemKind::METHOD => grammar.highlight_id_for_name("function.method"),
 402            lsp::CompletionItemKind::FUNCTION => grammar.highlight_id_for_name("function"),
 403            lsp::CompletionItemKind::CLASS => grammar.highlight_id_for_name("type"),
 404            lsp::CompletionItemKind::CONSTANT => grammar.highlight_id_for_name("constant"),
 405            lsp::CompletionItemKind::VARIABLE => grammar.highlight_id_for_name("variable"),
 406            _ => {
 407                return None;
 408            }
 409        };
 410        let mut text = label.clone();
 411        if let Some(completion_details) = item
 412            .label_details
 413            .as_ref()
 414            .and_then(|details| details.description.as_ref())
 415        {
 416            write!(&mut text, " {}", completion_details).ok();
 417        }
 418        Some(language::CodeLabel::filtered(
 419            text,
 420            item.filter_text.as_deref(),
 421            highlight_id
 422                .map(|id| (0..label.len(), id))
 423                .into_iter()
 424                .collect(),
 425        ))
 426    }
 427
 428    async fn label_for_symbol(
 429        &self,
 430        name: &str,
 431        kind: lsp::SymbolKind,
 432        language: &Arc<language::Language>,
 433    ) -> Option<language::CodeLabel> {
 434        let (text, filter_range, display_range) = match kind {
 435            lsp::SymbolKind::METHOD | lsp::SymbolKind::FUNCTION => {
 436                let text = format!("def {}():\n", name);
 437                let filter_range = 4..4 + name.len();
 438                let display_range = 0..filter_range.end;
 439                (text, filter_range, display_range)
 440            }
 441            lsp::SymbolKind::CLASS => {
 442                let text = format!("class {}:", name);
 443                let filter_range = 6..6 + name.len();
 444                let display_range = 0..filter_range.end;
 445                (text, filter_range, display_range)
 446            }
 447            lsp::SymbolKind::CONSTANT => {
 448                let text = format!("{} = 0", name);
 449                let filter_range = 0..name.len();
 450                let display_range = 0..filter_range.end;
 451                (text, filter_range, display_range)
 452            }
 453            _ => return None,
 454        };
 455
 456        Some(language::CodeLabel::new(
 457            text[display_range.clone()].to_string(),
 458            filter_range,
 459            language.highlight_text(&text.as_str().into(), display_range),
 460        ))
 461    }
 462
 463    async fn workspace_configuration(
 464        self: Arc<Self>,
 465        adapter: &Arc<dyn LspAdapterDelegate>,
 466        toolchain: Option<Toolchain>,
 467        cx: &mut AsyncApp,
 468    ) -> Result<Value> {
 469        cx.update(move |cx| {
 470            let mut user_settings =
 471                language_server_settings(adapter.as_ref(), &Self::SERVER_NAME, cx)
 472                    .and_then(|s| s.settings.clone())
 473                    .unwrap_or_default();
 474
 475            // If we have a detected toolchain, configure Pyright to use it
 476            if let Some(toolchain) = toolchain
 477                && let Ok(env) = serde_json::from_value::<
 478                    pet_core::python_environment::PythonEnvironment,
 479                >(toolchain.as_json.clone())
 480            {
 481                if !user_settings.is_object() {
 482                    user_settings = Value::Object(serde_json::Map::default());
 483                }
 484                let object = user_settings.as_object_mut().unwrap();
 485
 486                let interpreter_path = toolchain.path.to_string();
 487                if let Some(venv_dir) = env.prefix {
 488                    // Set venvPath and venv at the root level
 489                    // This matches the format of a pyrightconfig.json file
 490                    if let Some(parent) = venv_dir.parent() {
 491                        // Use relative path if the venv is inside the workspace
 492                        let venv_path = if parent == adapter.worktree_root_path() {
 493                            ".".to_string()
 494                        } else {
 495                            parent.to_string_lossy().into_owned()
 496                        };
 497                        object.insert("venvPath".to_string(), Value::String(venv_path));
 498                    }
 499
 500                    if let Some(venv_name) = venv_dir.file_name() {
 501                        object.insert(
 502                            "venv".to_owned(),
 503                            Value::String(venv_name.to_string_lossy().into_owned()),
 504                        );
 505                    }
 506                }
 507
 508                // Always set the python interpreter path
 509                // Get or create the python section
 510                let python = object
 511                    .entry("python")
 512                    .and_modify(|v| {
 513                        if !v.is_object() {
 514                            *v = Value::Object(serde_json::Map::default());
 515                        }
 516                    })
 517                    .or_insert(Value::Object(serde_json::Map::default()));
 518                let python = python.as_object_mut().unwrap();
 519
 520                // Set both pythonPath and defaultInterpreterPath for compatibility
 521                python.insert(
 522                    "pythonPath".to_owned(),
 523                    Value::String(interpreter_path.clone()),
 524                );
 525                python.insert(
 526                    "defaultInterpreterPath".to_owned(),
 527                    Value::String(interpreter_path),
 528                );
 529            }
 530
 531            user_settings
 532        })
 533    }
 534}
 535
 536impl LspInstaller for PyrightLspAdapter {
 537    type BinaryVersion = String;
 538
 539    async fn fetch_latest_server_version(
 540        &self,
 541        _: &dyn LspAdapterDelegate,
 542        _: bool,
 543        _: &mut AsyncApp,
 544    ) -> Result<String> {
 545        self.node
 546            .npm_package_latest_version(Self::SERVER_NAME.as_ref())
 547            .await
 548    }
 549
 550    async fn check_if_user_installed(
 551        &self,
 552        delegate: &dyn LspAdapterDelegate,
 553        _: Option<Toolchain>,
 554        _: &AsyncApp,
 555    ) -> Option<LanguageServerBinary> {
 556        if let Some(pyright_bin) = delegate.which("pyright-langserver".as_ref()).await {
 557            let env = delegate.shell_env().await;
 558            Some(LanguageServerBinary {
 559                path: pyright_bin,
 560                env: Some(env),
 561                arguments: vec!["--stdio".into()],
 562            })
 563        } else {
 564            let node = delegate.which("node".as_ref()).await?;
 565            let (node_modules_path, _) = delegate
 566                .npm_package_installed_version(Self::SERVER_NAME.as_ref())
 567                .await
 568                .log_err()??;
 569
 570            let path = node_modules_path.join(Self::NODE_MODULE_RELATIVE_SERVER_PATH);
 571
 572            let env = delegate.shell_env().await;
 573            Some(LanguageServerBinary {
 574                path: node,
 575                env: Some(env),
 576                arguments: vec![path.into(), "--stdio".into()],
 577            })
 578        }
 579    }
 580
 581    async fn fetch_server_binary(
 582        &self,
 583        latest_version: Self::BinaryVersion,
 584        container_dir: PathBuf,
 585        delegate: &dyn LspAdapterDelegate,
 586    ) -> Result<LanguageServerBinary> {
 587        let server_path = container_dir.join(Self::SERVER_PATH);
 588
 589        self.node
 590            .npm_install_packages(
 591                &container_dir,
 592                &[(Self::SERVER_NAME.as_ref(), latest_version.as_str())],
 593            )
 594            .await?;
 595
 596        let env = delegate.shell_env().await;
 597        Ok(LanguageServerBinary {
 598            path: self.node.binary_path().await?,
 599            env: Some(env),
 600            arguments: vec![server_path.into(), "--stdio".into()],
 601        })
 602    }
 603
 604    async fn check_if_version_installed(
 605        &self,
 606        version: &Self::BinaryVersion,
 607        container_dir: &PathBuf,
 608        delegate: &dyn LspAdapterDelegate,
 609    ) -> Option<LanguageServerBinary> {
 610        let server_path = container_dir.join(Self::SERVER_PATH);
 611
 612        let should_install_language_server = self
 613            .node
 614            .should_install_npm_package(
 615                Self::SERVER_NAME.as_ref(),
 616                &server_path,
 617                container_dir,
 618                VersionStrategy::Latest(version),
 619            )
 620            .await;
 621
 622        if should_install_language_server {
 623            None
 624        } else {
 625            let env = delegate.shell_env().await;
 626            Some(LanguageServerBinary {
 627                path: self.node.binary_path().await.ok()?,
 628                env: Some(env),
 629                arguments: vec![server_path.into(), "--stdio".into()],
 630            })
 631        }
 632    }
 633
 634    async fn cached_server_binary(
 635        &self,
 636        container_dir: PathBuf,
 637        delegate: &dyn LspAdapterDelegate,
 638    ) -> Option<LanguageServerBinary> {
 639        let mut binary = Self::get_cached_server_binary(container_dir, &self.node).await?;
 640        binary.env = Some(delegate.shell_env().await);
 641        Some(binary)
 642    }
 643}
 644
 645pub(crate) struct PythonContextProvider;
 646
 647const PYTHON_TEST_TARGET_TASK_VARIABLE: VariableName =
 648    VariableName::Custom(Cow::Borrowed("PYTHON_TEST_TARGET"));
 649
 650const PYTHON_ACTIVE_TOOLCHAIN_PATH: VariableName =
 651    VariableName::Custom(Cow::Borrowed("PYTHON_ACTIVE_ZED_TOOLCHAIN"));
 652
 653const PYTHON_MODULE_NAME_TASK_VARIABLE: VariableName =
 654    VariableName::Custom(Cow::Borrowed("PYTHON_MODULE_NAME"));
 655
 656impl ContextProvider for PythonContextProvider {
 657    fn build_context(
 658        &self,
 659        variables: &task::TaskVariables,
 660        location: ContextLocation<'_>,
 661        _: Option<HashMap<String, String>>,
 662        toolchains: Arc<dyn LanguageToolchainStore>,
 663        cx: &mut gpui::App,
 664    ) -> Task<Result<task::TaskVariables>> {
 665        let test_target =
 666            match selected_test_runner(location.file_location.buffer.read(cx).file(), cx) {
 667                TestRunner::UNITTEST => self.build_unittest_target(variables),
 668                TestRunner::PYTEST => self.build_pytest_target(variables),
 669            };
 670
 671        let module_target = self.build_module_target(variables);
 672        let location_file = location.file_location.buffer.read(cx).file().cloned();
 673        let worktree_id = location_file.as_ref().map(|f| f.worktree_id(cx));
 674
 675        cx.spawn(async move |cx| {
 676            let active_toolchain = if let Some(worktree_id) = worktree_id {
 677                let file_path = location_file
 678                    .as_ref()
 679                    .and_then(|f| f.path().parent())
 680                    .map(Arc::from)
 681                    .unwrap_or_else(|| RelPath::empty().into());
 682
 683                toolchains
 684                    .active_toolchain(worktree_id, file_path, "Python".into(), cx)
 685                    .await
 686                    .map_or_else(
 687                        || String::from("python3"),
 688                        |toolchain| toolchain.path.to_string(),
 689                    )
 690            } else {
 691                String::from("python3")
 692            };
 693
 694            let toolchain = (PYTHON_ACTIVE_TOOLCHAIN_PATH, active_toolchain);
 695
 696            Ok(task::TaskVariables::from_iter(
 697                test_target
 698                    .into_iter()
 699                    .chain(module_target.into_iter())
 700                    .chain([toolchain]),
 701            ))
 702        })
 703    }
 704
 705    fn associated_tasks(
 706        &self,
 707        file: Option<Arc<dyn language::File>>,
 708        cx: &App,
 709    ) -> Task<Option<TaskTemplates>> {
 710        let test_runner = selected_test_runner(file.as_ref(), cx);
 711
 712        let mut tasks = vec![
 713            // Execute a selection
 714            TaskTemplate {
 715                label: "execute selection".to_owned(),
 716                command: PYTHON_ACTIVE_TOOLCHAIN_PATH.template_value(),
 717                args: vec![
 718                    "-c".to_owned(),
 719                    VariableName::SelectedText.template_value_with_whitespace(),
 720                ],
 721                cwd: Some(VariableName::WorktreeRoot.template_value()),
 722                ..TaskTemplate::default()
 723            },
 724            // Execute an entire file
 725            TaskTemplate {
 726                label: format!("run '{}'", VariableName::File.template_value()),
 727                command: PYTHON_ACTIVE_TOOLCHAIN_PATH.template_value(),
 728                args: vec![VariableName::File.template_value_with_whitespace()],
 729                cwd: Some(VariableName::WorktreeRoot.template_value()),
 730                ..TaskTemplate::default()
 731            },
 732            // Execute a file as module
 733            TaskTemplate {
 734                label: format!("run module '{}'", VariableName::File.template_value()),
 735                command: PYTHON_ACTIVE_TOOLCHAIN_PATH.template_value(),
 736                args: vec![
 737                    "-m".to_owned(),
 738                    PYTHON_MODULE_NAME_TASK_VARIABLE.template_value(),
 739                ],
 740                cwd: Some(VariableName::WorktreeRoot.template_value()),
 741                tags: vec!["python-module-main-method".to_owned()],
 742                ..TaskTemplate::default()
 743            },
 744        ];
 745
 746        tasks.extend(match test_runner {
 747            TestRunner::UNITTEST => {
 748                [
 749                    // Run tests for an entire file
 750                    TaskTemplate {
 751                        label: format!("unittest '{}'", VariableName::File.template_value()),
 752                        command: PYTHON_ACTIVE_TOOLCHAIN_PATH.template_value(),
 753                        args: vec![
 754                            "-m".to_owned(),
 755                            "unittest".to_owned(),
 756                            VariableName::File.template_value_with_whitespace(),
 757                        ],
 758                        cwd: Some(VariableName::WorktreeRoot.template_value()),
 759                        ..TaskTemplate::default()
 760                    },
 761                    // Run test(s) for a specific target within a file
 762                    TaskTemplate {
 763                        label: "unittest $ZED_CUSTOM_PYTHON_TEST_TARGET".to_owned(),
 764                        command: PYTHON_ACTIVE_TOOLCHAIN_PATH.template_value(),
 765                        args: vec![
 766                            "-m".to_owned(),
 767                            "unittest".to_owned(),
 768                            PYTHON_TEST_TARGET_TASK_VARIABLE.template_value_with_whitespace(),
 769                        ],
 770                        tags: vec![
 771                            "python-unittest-class".to_owned(),
 772                            "python-unittest-method".to_owned(),
 773                        ],
 774                        cwd: Some(VariableName::WorktreeRoot.template_value()),
 775                        ..TaskTemplate::default()
 776                    },
 777                ]
 778            }
 779            TestRunner::PYTEST => {
 780                [
 781                    // Run tests for an entire file
 782                    TaskTemplate {
 783                        label: format!("pytest '{}'", VariableName::File.template_value()),
 784                        command: PYTHON_ACTIVE_TOOLCHAIN_PATH.template_value(),
 785                        args: vec![
 786                            "-m".to_owned(),
 787                            "pytest".to_owned(),
 788                            VariableName::File.template_value_with_whitespace(),
 789                        ],
 790                        cwd: Some(VariableName::WorktreeRoot.template_value()),
 791                        ..TaskTemplate::default()
 792                    },
 793                    // Run test(s) for a specific target within a file
 794                    TaskTemplate {
 795                        label: "pytest $ZED_CUSTOM_PYTHON_TEST_TARGET".to_owned(),
 796                        command: PYTHON_ACTIVE_TOOLCHAIN_PATH.template_value(),
 797                        args: vec![
 798                            "-m".to_owned(),
 799                            "pytest".to_owned(),
 800                            PYTHON_TEST_TARGET_TASK_VARIABLE.template_value_with_whitespace(),
 801                        ],
 802                        cwd: Some(VariableName::WorktreeRoot.template_value()),
 803                        tags: vec![
 804                            "python-pytest-class".to_owned(),
 805                            "python-pytest-method".to_owned(),
 806                        ],
 807                        ..TaskTemplate::default()
 808                    },
 809                ]
 810            }
 811        });
 812
 813        Task::ready(Some(TaskTemplates(tasks)))
 814    }
 815}
 816
 817fn selected_test_runner(location: Option<&Arc<dyn language::File>>, cx: &App) -> TestRunner {
 818    const TEST_RUNNER_VARIABLE: &str = "TEST_RUNNER";
 819    language_settings(Some(LanguageName::new("Python")), location, cx)
 820        .tasks
 821        .variables
 822        .get(TEST_RUNNER_VARIABLE)
 823        .and_then(|val| TestRunner::from_str(val).ok())
 824        .unwrap_or(TestRunner::PYTEST)
 825}
 826
 827impl PythonContextProvider {
 828    fn build_unittest_target(
 829        &self,
 830        variables: &task::TaskVariables,
 831    ) -> Option<(VariableName, String)> {
 832        let python_module_name =
 833            python_module_name_from_relative_path(variables.get(&VariableName::RelativeFile)?);
 834
 835        let unittest_class_name =
 836            variables.get(&VariableName::Custom(Cow::Borrowed("_unittest_class_name")));
 837
 838        let unittest_method_name = variables.get(&VariableName::Custom(Cow::Borrowed(
 839            "_unittest_method_name",
 840        )));
 841
 842        let unittest_target_str = match (unittest_class_name, unittest_method_name) {
 843            (Some(class_name), Some(method_name)) => {
 844                format!("{python_module_name}.{class_name}.{method_name}")
 845            }
 846            (Some(class_name), None) => format!("{python_module_name}.{class_name}"),
 847            (None, None) => python_module_name,
 848            // should never happen, a TestCase class is the unit of testing
 849            (None, Some(_)) => return None,
 850        };
 851
 852        Some((
 853            PYTHON_TEST_TARGET_TASK_VARIABLE.clone(),
 854            unittest_target_str,
 855        ))
 856    }
 857
 858    fn build_pytest_target(
 859        &self,
 860        variables: &task::TaskVariables,
 861    ) -> Option<(VariableName, String)> {
 862        let file_path = variables.get(&VariableName::RelativeFile)?;
 863
 864        let pytest_class_name =
 865            variables.get(&VariableName::Custom(Cow::Borrowed("_pytest_class_name")));
 866
 867        let pytest_method_name =
 868            variables.get(&VariableName::Custom(Cow::Borrowed("_pytest_method_name")));
 869
 870        let pytest_target_str = match (pytest_class_name, pytest_method_name) {
 871            (Some(class_name), Some(method_name)) => {
 872                format!("{file_path}::{class_name}::{method_name}")
 873            }
 874            (Some(class_name), None) => {
 875                format!("{file_path}::{class_name}")
 876            }
 877            (None, Some(method_name)) => {
 878                format!("{file_path}::{method_name}")
 879            }
 880            (None, None) => file_path.to_string(),
 881        };
 882
 883        Some((PYTHON_TEST_TARGET_TASK_VARIABLE.clone(), pytest_target_str))
 884    }
 885
 886    fn build_module_target(
 887        &self,
 888        variables: &task::TaskVariables,
 889    ) -> Result<(VariableName, String)> {
 890        let python_module_name = python_module_name_from_relative_path(
 891            variables.get(&VariableName::RelativeFile).unwrap_or(""),
 892        );
 893
 894        let module_target = (PYTHON_MODULE_NAME_TASK_VARIABLE.clone(), python_module_name);
 895
 896        Ok(module_target)
 897    }
 898}
 899
 900fn python_module_name_from_relative_path(relative_path: &str) -> String {
 901    let path_with_dots = relative_path.replace('/', ".");
 902    path_with_dots
 903        .strip_suffix(".py")
 904        .unwrap_or(&path_with_dots)
 905        .to_string()
 906}
 907
 908const fn is_python_env_global(k: &PythonEnvironmentKind) -> bool {
 909    matches!(
 910        k,
 911        PythonEnvironmentKind::Homebrew
 912            | PythonEnvironmentKind::Pyenv
 913            | PythonEnvironmentKind::GlobalPaths
 914            | PythonEnvironmentKind::MacPythonOrg
 915            | PythonEnvironmentKind::MacCommandLineTools
 916            | PythonEnvironmentKind::LinuxGlobal
 917            | PythonEnvironmentKind::MacXCode
 918            | PythonEnvironmentKind::WindowsStore
 919            | PythonEnvironmentKind::WindowsRegistry
 920    )
 921}
 922
 923const fn python_env_kind_display(k: &PythonEnvironmentKind) -> &'static str {
 924    match k {
 925        PythonEnvironmentKind::Conda => "Conda",
 926        PythonEnvironmentKind::Pixi => "pixi",
 927        PythonEnvironmentKind::Homebrew => "Homebrew",
 928        PythonEnvironmentKind::Pyenv => "global (Pyenv)",
 929        PythonEnvironmentKind::GlobalPaths => "global",
 930        PythonEnvironmentKind::PyenvVirtualEnv => "Pyenv",
 931        PythonEnvironmentKind::Pipenv => "Pipenv",
 932        PythonEnvironmentKind::Poetry => "Poetry",
 933        PythonEnvironmentKind::MacPythonOrg => "global (Python.org)",
 934        PythonEnvironmentKind::MacCommandLineTools => "global (Command Line Tools for Xcode)",
 935        PythonEnvironmentKind::LinuxGlobal => "global",
 936        PythonEnvironmentKind::MacXCode => "global (Xcode)",
 937        PythonEnvironmentKind::Venv => "venv",
 938        PythonEnvironmentKind::VirtualEnv => "virtualenv",
 939        PythonEnvironmentKind::VirtualEnvWrapper => "virtualenvwrapper",
 940        PythonEnvironmentKind::WindowsStore => "global (Windows Store)",
 941        PythonEnvironmentKind::WindowsRegistry => "global (Windows Registry)",
 942    }
 943}
 944
 945pub(crate) struct PythonToolchainProvider;
 946
 947static ENV_PRIORITY_LIST: &[PythonEnvironmentKind] = &[
 948    // Prioritize non-Conda environments.
 949    PythonEnvironmentKind::Poetry,
 950    PythonEnvironmentKind::Pipenv,
 951    PythonEnvironmentKind::VirtualEnvWrapper,
 952    PythonEnvironmentKind::Venv,
 953    PythonEnvironmentKind::VirtualEnv,
 954    PythonEnvironmentKind::PyenvVirtualEnv,
 955    PythonEnvironmentKind::Pixi,
 956    PythonEnvironmentKind::Conda,
 957    PythonEnvironmentKind::Pyenv,
 958    PythonEnvironmentKind::GlobalPaths,
 959    PythonEnvironmentKind::Homebrew,
 960];
 961
 962fn env_priority(kind: Option<PythonEnvironmentKind>) -> usize {
 963    if let Some(kind) = kind {
 964        ENV_PRIORITY_LIST
 965            .iter()
 966            .position(|blessed_env| blessed_env == &kind)
 967            .unwrap_or(ENV_PRIORITY_LIST.len())
 968    } else {
 969        // Unknown toolchains are less useful than non-blessed ones.
 970        ENV_PRIORITY_LIST.len() + 1
 971    }
 972}
 973
 974/// Return the name of environment declared in <worktree-root/.venv.
 975///
 976/// https://virtualfish.readthedocs.io/en/latest/plugins.html#auto-activation-auto-activation
 977async fn get_worktree_venv_declaration(worktree_root: &Path) -> Option<String> {
 978    let file = async_fs::File::open(worktree_root.join(".venv"))
 979        .await
 980        .ok()?;
 981    let mut venv_name = String::new();
 982    smol::io::BufReader::new(file)
 983        .read_line(&mut venv_name)
 984        .await
 985        .ok()?;
 986    Some(venv_name.trim().to_string())
 987}
 988
 989fn get_venv_parent_dir(env: &PythonEnvironment) -> Option<PathBuf> {
 990    // If global, we aren't a virtual environment
 991    if let Some(kind) = env.kind
 992        && is_python_env_global(&kind)
 993    {
 994        return None;
 995    }
 996
 997    // Check to be sure we are a virtual environment using pet's most generic
 998    // virtual environment type, VirtualEnv
 999    let venv = env
1000        .executable
1001        .as_ref()
1002        .and_then(|p| p.parent())
1003        .and_then(|p| p.parent())
1004        .filter(|p| is_virtualenv_dir(p))?;
1005
1006    venv.parent().map(|parent| parent.to_path_buf())
1007}
1008
1009fn wr_distance(wr: &PathBuf, venv: Option<&PathBuf>) -> usize {
1010    if let Some(venv) = venv
1011        && let Ok(p) = venv.strip_prefix(wr)
1012    {
1013        p.components().count()
1014    } else {
1015        usize::MAX
1016    }
1017}
1018
1019#[async_trait]
1020impl ToolchainLister for PythonToolchainProvider {
1021    async fn list(
1022        &self,
1023        worktree_root: PathBuf,
1024        subroot_relative_path: Arc<RelPath>,
1025        project_env: Option<HashMap<String, String>>,
1026    ) -> ToolchainList {
1027        let env = project_env.unwrap_or_default();
1028        let environment = EnvironmentApi::from_env(&env);
1029        let locators = pet::locators::create_locators(
1030            Arc::new(pet_conda::Conda::from(&environment)),
1031            Arc::new(pet_poetry::Poetry::from(&environment)),
1032            &environment,
1033        );
1034        let mut config = Configuration::default();
1035
1036        // `.ancestors()` will yield at least one path, so in case of empty `subroot_relative_path`, we'll just use
1037        // worktree root as the workspace directory.
1038        config.workspace_directories = Some(
1039            subroot_relative_path
1040                .ancestors()
1041                .map(|ancestor| worktree_root.join(ancestor.as_std_path()))
1042                .collect(),
1043        );
1044        for locator in locators.iter() {
1045            locator.configure(&config);
1046        }
1047
1048        let reporter = pet_reporter::collect::create_reporter();
1049        pet::find::find_and_report_envs(&reporter, config, &locators, &environment, None);
1050
1051        let mut toolchains = reporter
1052            .environments
1053            .lock()
1054            .map_or(Vec::new(), |mut guard| std::mem::take(&mut guard));
1055
1056        let wr = worktree_root;
1057        let wr_venv = get_worktree_venv_declaration(&wr).await;
1058        // Sort detected environments by:
1059        //     environment name matching activation file (<workdir>/.venv)
1060        //     environment project dir matching worktree_root
1061        //     general env priority
1062        //     environment path matching the CONDA_PREFIX env var
1063        //     executable path
1064        toolchains.sort_by(|lhs, rhs| {
1065            // Compare venv names against worktree .venv file
1066            let venv_ordering =
1067                wr_venv
1068                    .as_ref()
1069                    .map_or(Ordering::Equal, |venv| match (&lhs.name, &rhs.name) {
1070                        (Some(l), Some(r)) => (r == venv).cmp(&(l == venv)),
1071                        (Some(l), None) if l == venv => Ordering::Less,
1072                        (None, Some(r)) if r == venv => Ordering::Greater,
1073                        _ => Ordering::Equal,
1074                    });
1075
1076            // Compare project paths against worktree root
1077            let proj_ordering = || {
1078                let lhs_project = lhs.project.clone().or_else(|| get_venv_parent_dir(lhs));
1079                let rhs_project = rhs.project.clone().or_else(|| get_venv_parent_dir(rhs));
1080                wr_distance(&wr, lhs_project.as_ref()).cmp(&wr_distance(&wr, rhs_project.as_ref()))
1081            };
1082
1083            // Compare environment priorities
1084            let priority_ordering = || env_priority(lhs.kind).cmp(&env_priority(rhs.kind));
1085
1086            // Compare conda prefixes
1087            let conda_ordering = || {
1088                if lhs.kind == Some(PythonEnvironmentKind::Conda) {
1089                    environment
1090                        .get_env_var("CONDA_PREFIX".to_string())
1091                        .map(|conda_prefix| {
1092                            let is_match = |exe: &Option<PathBuf>| {
1093                                exe.as_ref().is_some_and(|e| e.starts_with(&conda_prefix))
1094                            };
1095                            match (is_match(&lhs.executable), is_match(&rhs.executable)) {
1096                                (true, false) => Ordering::Less,
1097                                (false, true) => Ordering::Greater,
1098                                _ => Ordering::Equal,
1099                            }
1100                        })
1101                        .unwrap_or(Ordering::Equal)
1102                } else {
1103                    Ordering::Equal
1104                }
1105            };
1106
1107            // Compare Python executables
1108            let exe_ordering = || lhs.executable.cmp(&rhs.executable);
1109
1110            venv_ordering
1111                .then_with(proj_ordering)
1112                .then_with(priority_ordering)
1113                .then_with(conda_ordering)
1114                .then_with(exe_ordering)
1115        });
1116
1117        let mut toolchains: Vec<_> = toolchains
1118            .into_iter()
1119            .filter_map(venv_to_toolchain)
1120            .collect();
1121        toolchains.dedup();
1122        ToolchainList {
1123            toolchains,
1124            default: None,
1125            groups: Default::default(),
1126        }
1127    }
1128    fn meta(&self) -> ToolchainMetadata {
1129        ToolchainMetadata {
1130            term: SharedString::new_static("Virtual Environment"),
1131            new_toolchain_placeholder: SharedString::new_static(
1132                "A path to the python3 executable within a virtual environment, or path to virtual environment itself",
1133            ),
1134            manifest_name: ManifestName::from(SharedString::new_static("pyproject.toml")),
1135        }
1136    }
1137
1138    async fn resolve(
1139        &self,
1140        path: PathBuf,
1141        env: Option<HashMap<String, String>>,
1142    ) -> anyhow::Result<Toolchain> {
1143        let env = env.unwrap_or_default();
1144        let environment = EnvironmentApi::from_env(&env);
1145        let locators = pet::locators::create_locators(
1146            Arc::new(pet_conda::Conda::from(&environment)),
1147            Arc::new(pet_poetry::Poetry::from(&environment)),
1148            &environment,
1149        );
1150        let toolchain = pet::resolve::resolve_environment(&path, &locators, &environment)
1151            .context("Could not find a virtual environment in provided path")?;
1152        let venv = toolchain.resolved.unwrap_or(toolchain.discovered);
1153        venv_to_toolchain(venv).context("Could not convert a venv into a toolchain")
1154    }
1155
1156    async fn activation_script(
1157        &self,
1158        toolchain: &Toolchain,
1159        shell: ShellKind,
1160        fs: &dyn Fs,
1161    ) -> Vec<String> {
1162        let Ok(toolchain) = serde_json::from_value::<pet_core::python_environment::PythonEnvironment>(
1163            toolchain.as_json.clone(),
1164        ) else {
1165            return vec![];
1166        };
1167        let mut activation_script = vec![];
1168
1169        match toolchain.kind {
1170            Some(PythonEnvironmentKind::Conda) => {
1171                if let Some(name) = &toolchain.name {
1172                    activation_script.push(format!("conda activate {name}"));
1173                } else {
1174                    activation_script.push("conda activate".to_string());
1175                }
1176            }
1177            Some(PythonEnvironmentKind::Venv | PythonEnvironmentKind::VirtualEnv) => {
1178                if let Some(prefix) = &toolchain.prefix {
1179                    let activate_keyword = shell.activate_keyword();
1180                    let activate_script_name = match shell {
1181                        ShellKind::Posix | ShellKind::Rc => "activate",
1182                        ShellKind::Csh => "activate.csh",
1183                        ShellKind::Tcsh => "activate.csh",
1184                        ShellKind::Fish => "activate.fish",
1185                        ShellKind::Nushell => "activate.nu",
1186                        ShellKind::PowerShell => "activate.ps1",
1187                        ShellKind::Cmd => "activate.bat",
1188                        ShellKind::Xonsh => "activate.xsh",
1189                    };
1190                    let path = prefix.join(BINARY_DIR).join(activate_script_name);
1191
1192                    if let Some(quoted) = shell.try_quote(&path.to_string_lossy())
1193                        && fs.is_file(&path).await
1194                    {
1195                        activation_script.push(format!("{activate_keyword} {quoted}"));
1196                    }
1197                }
1198            }
1199            Some(PythonEnvironmentKind::Pyenv) => {
1200                let Some(manager) = toolchain.manager else {
1201                    return vec![];
1202                };
1203                let version = toolchain.version.as_deref().unwrap_or("system");
1204                let pyenv = manager.executable;
1205                let pyenv = pyenv.display();
1206                activation_script.extend(match shell {
1207                    ShellKind::Fish => Some(format!("\"{pyenv}\" shell - fish {version}")),
1208                    ShellKind::Posix => Some(format!("\"{pyenv}\" shell - sh {version}")),
1209                    ShellKind::Nushell => Some(format!("\"{pyenv}\" shell - nu {version}")),
1210                    ShellKind::PowerShell => None,
1211                    ShellKind::Csh => None,
1212                    ShellKind::Tcsh => None,
1213                    ShellKind::Cmd => None,
1214                    ShellKind::Rc => None,
1215                    ShellKind::Xonsh => None,
1216                })
1217            }
1218            _ => {}
1219        }
1220        activation_script
1221    }
1222}
1223
1224fn venv_to_toolchain(venv: PythonEnvironment) -> Option<Toolchain> {
1225    let mut name = String::from("Python");
1226    if let Some(ref version) = venv.version {
1227        _ = write!(name, " {version}");
1228    }
1229
1230    let name_and_kind = match (&venv.name, &venv.kind) {
1231        (Some(name), Some(kind)) => Some(format!("({name}; {})", python_env_kind_display(kind))),
1232        (Some(name), None) => Some(format!("({name})")),
1233        (None, Some(kind)) => Some(format!("({})", python_env_kind_display(kind))),
1234        (None, None) => None,
1235    };
1236
1237    if let Some(nk) = name_and_kind {
1238        _ = write!(name, " {nk}");
1239    }
1240
1241    Some(Toolchain {
1242        name: name.into(),
1243        path: venv.executable.as_ref()?.to_str()?.to_owned().into(),
1244        language_name: LanguageName::new("Python"),
1245        as_json: serde_json::to_value(venv).ok()?,
1246    })
1247}
1248
1249pub struct EnvironmentApi<'a> {
1250    global_search_locations: Arc<Mutex<Vec<PathBuf>>>,
1251    project_env: &'a HashMap<String, String>,
1252    pet_env: pet_core::os_environment::EnvironmentApi,
1253}
1254
1255impl<'a> EnvironmentApi<'a> {
1256    pub fn from_env(project_env: &'a HashMap<String, String>) -> Self {
1257        let paths = project_env
1258            .get("PATH")
1259            .map(|p| std::env::split_paths(p).collect())
1260            .unwrap_or_default();
1261
1262        EnvironmentApi {
1263            global_search_locations: Arc::new(Mutex::new(paths)),
1264            project_env,
1265            pet_env: pet_core::os_environment::EnvironmentApi::new(),
1266        }
1267    }
1268
1269    fn user_home(&self) -> Option<PathBuf> {
1270        self.project_env
1271            .get("HOME")
1272            .or_else(|| self.project_env.get("USERPROFILE"))
1273            .map(|home| pet_fs::path::norm_case(PathBuf::from(home)))
1274            .or_else(|| self.pet_env.get_user_home())
1275    }
1276}
1277
1278impl pet_core::os_environment::Environment for EnvironmentApi<'_> {
1279    fn get_user_home(&self) -> Option<PathBuf> {
1280        self.user_home()
1281    }
1282
1283    fn get_root(&self) -> Option<PathBuf> {
1284        None
1285    }
1286
1287    fn get_env_var(&self, key: String) -> Option<String> {
1288        self.project_env
1289            .get(&key)
1290            .cloned()
1291            .or_else(|| self.pet_env.get_env_var(key))
1292    }
1293
1294    fn get_know_global_search_locations(&self) -> Vec<PathBuf> {
1295        if self.global_search_locations.lock().is_empty() {
1296            let mut paths =
1297                std::env::split_paths(&self.get_env_var("PATH".to_string()).unwrap_or_default())
1298                    .collect::<Vec<PathBuf>>();
1299
1300            log::trace!("Env PATH: {:?}", paths);
1301            for p in self.pet_env.get_know_global_search_locations() {
1302                if !paths.contains(&p) {
1303                    paths.push(p);
1304                }
1305            }
1306
1307            let mut paths = paths
1308                .into_iter()
1309                .filter(|p| p.exists())
1310                .collect::<Vec<PathBuf>>();
1311
1312            self.global_search_locations.lock().append(&mut paths);
1313        }
1314        self.global_search_locations.lock().clone()
1315    }
1316}
1317
1318pub(crate) struct PyLspAdapter {
1319    python_venv_base: OnceCell<Result<Arc<Path>, String>>,
1320}
1321impl PyLspAdapter {
1322    const SERVER_NAME: LanguageServerName = LanguageServerName::new_static("pylsp");
1323    pub(crate) const fn new() -> Self {
1324        Self {
1325            python_venv_base: OnceCell::new(),
1326        }
1327    }
1328    async fn ensure_venv(delegate: &dyn LspAdapterDelegate) -> Result<Arc<Path>> {
1329        let python_path = Self::find_base_python(delegate)
1330            .await
1331            .with_context(|| {
1332                let mut message = "Could not find Python installation for PyLSP".to_owned();
1333                if cfg!(windows){
1334                    message.push_str(". Install Python from the Microsoft Store, or manually from https://www.python.org/downloads/windows.")
1335                }
1336                message
1337            })?;
1338        let work_dir = delegate
1339            .language_server_download_dir(&Self::SERVER_NAME)
1340            .await
1341            .context("Could not get working directory for PyLSP")?;
1342        let mut path = PathBuf::from(work_dir.as_ref());
1343        path.push("pylsp-venv");
1344        if !path.exists() {
1345            util::command::new_smol_command(python_path)
1346                .arg("-m")
1347                .arg("venv")
1348                .arg("pylsp-venv")
1349                .current_dir(work_dir)
1350                .spawn()?
1351                .output()
1352                .await?;
1353        }
1354
1355        Ok(path.into())
1356    }
1357    // Find "baseline", user python version from which we'll create our own venv.
1358    async fn find_base_python(delegate: &dyn LspAdapterDelegate) -> Option<PathBuf> {
1359        for path in ["python3", "python"] {
1360            let Some(path) = delegate.which(path.as_ref()).await else {
1361                continue;
1362            };
1363            // Try to detect situations where `python3` exists but is not a real Python interpreter.
1364            // Notably, on fresh Windows installs, `python3` is a shim that opens the Microsoft Store app
1365            // when run with no arguments, and just fails otherwise.
1366            let Some(output) = new_smol_command(&path)
1367                .args(["-c", "print(1 + 2)"])
1368                .output()
1369                .await
1370                .ok()
1371            else {
1372                continue;
1373            };
1374            if output.stdout.trim_ascii() != b"3" {
1375                continue;
1376            }
1377            return Some(path);
1378        }
1379        None
1380    }
1381
1382    async fn base_venv(&self, delegate: &dyn LspAdapterDelegate) -> Result<Arc<Path>, String> {
1383        self.python_venv_base
1384            .get_or_init(move || async move {
1385                Self::ensure_venv(delegate)
1386                    .await
1387                    .map_err(|e| format!("{e}"))
1388            })
1389            .await
1390            .clone()
1391    }
1392}
1393
1394const BINARY_DIR: &str = if cfg!(target_os = "windows") {
1395    "Scripts"
1396} else {
1397    "bin"
1398};
1399
1400#[async_trait(?Send)]
1401impl LspAdapter for PyLspAdapter {
1402    fn name(&self) -> LanguageServerName {
1403        Self::SERVER_NAME
1404    }
1405
1406    async fn process_completions(&self, _items: &mut [lsp::CompletionItem]) {}
1407
1408    async fn label_for_completion(
1409        &self,
1410        item: &lsp::CompletionItem,
1411        language: &Arc<language::Language>,
1412    ) -> Option<language::CodeLabel> {
1413        let label = &item.label;
1414        let grammar = language.grammar()?;
1415        let highlight_id = match item.kind? {
1416            lsp::CompletionItemKind::METHOD => grammar.highlight_id_for_name("function.method")?,
1417            lsp::CompletionItemKind::FUNCTION => grammar.highlight_id_for_name("function")?,
1418            lsp::CompletionItemKind::CLASS => grammar.highlight_id_for_name("type")?,
1419            lsp::CompletionItemKind::CONSTANT => grammar.highlight_id_for_name("constant")?,
1420            _ => return None,
1421        };
1422        Some(language::CodeLabel::filtered(
1423            label.clone(),
1424            item.filter_text.as_deref(),
1425            vec![(0..label.len(), highlight_id)],
1426        ))
1427    }
1428
1429    async fn label_for_symbol(
1430        &self,
1431        name: &str,
1432        kind: lsp::SymbolKind,
1433        language: &Arc<language::Language>,
1434    ) -> Option<language::CodeLabel> {
1435        let (text, filter_range, display_range) = match kind {
1436            lsp::SymbolKind::METHOD | lsp::SymbolKind::FUNCTION => {
1437                let text = format!("def {}():\n", name);
1438                let filter_range = 4..4 + name.len();
1439                let display_range = 0..filter_range.end;
1440                (text, filter_range, display_range)
1441            }
1442            lsp::SymbolKind::CLASS => {
1443                let text = format!("class {}:", name);
1444                let filter_range = 6..6 + name.len();
1445                let display_range = 0..filter_range.end;
1446                (text, filter_range, display_range)
1447            }
1448            lsp::SymbolKind::CONSTANT => {
1449                let text = format!("{} = 0", name);
1450                let filter_range = 0..name.len();
1451                let display_range = 0..filter_range.end;
1452                (text, filter_range, display_range)
1453            }
1454            _ => return None,
1455        };
1456        Some(language::CodeLabel::new(
1457            text[display_range.clone()].to_string(),
1458            filter_range,
1459            language.highlight_text(&text.as_str().into(), display_range),
1460        ))
1461    }
1462
1463    async fn workspace_configuration(
1464        self: Arc<Self>,
1465        adapter: &Arc<dyn LspAdapterDelegate>,
1466        toolchain: Option<Toolchain>,
1467        cx: &mut AsyncApp,
1468    ) -> Result<Value> {
1469        cx.update(move |cx| {
1470            let mut user_settings =
1471                language_server_settings(adapter.as_ref(), &Self::SERVER_NAME, cx)
1472                    .and_then(|s| s.settings.clone())
1473                    .unwrap_or_else(|| {
1474                        json!({
1475                            "plugins": {
1476                                "pycodestyle": {"enabled": false},
1477                                "rope_autoimport": {"enabled": true, "memory": true},
1478                                "pylsp_mypy": {"enabled": false}
1479                            },
1480                            "rope": {
1481                                "ropeFolder": null
1482                            },
1483                        })
1484                    });
1485
1486            // If user did not explicitly modify their python venv, use one from picker.
1487            if let Some(toolchain) = toolchain {
1488                if !user_settings.is_object() {
1489                    user_settings = Value::Object(serde_json::Map::default());
1490                }
1491                let object = user_settings.as_object_mut().unwrap();
1492                if let Some(python) = object
1493                    .entry("plugins")
1494                    .or_insert(Value::Object(serde_json::Map::default()))
1495                    .as_object_mut()
1496                {
1497                    if let Some(jedi) = python
1498                        .entry("jedi")
1499                        .or_insert(Value::Object(serde_json::Map::default()))
1500                        .as_object_mut()
1501                    {
1502                        jedi.entry("environment".to_string())
1503                            .or_insert_with(|| Value::String(toolchain.path.clone().into()));
1504                    }
1505                    if let Some(pylint) = python
1506                        .entry("pylsp_mypy")
1507                        .or_insert(Value::Object(serde_json::Map::default()))
1508                        .as_object_mut()
1509                    {
1510                        pylint.entry("overrides".to_string()).or_insert_with(|| {
1511                            Value::Array(vec![
1512                                Value::String("--python-executable".into()),
1513                                Value::String(toolchain.path.into()),
1514                                Value::String("--cache-dir=/dev/null".into()),
1515                                Value::Bool(true),
1516                            ])
1517                        });
1518                    }
1519                }
1520            }
1521            user_settings = Value::Object(serde_json::Map::from_iter([(
1522                "pylsp".to_string(),
1523                user_settings,
1524            )]));
1525
1526            user_settings
1527        })
1528    }
1529}
1530
1531impl LspInstaller for PyLspAdapter {
1532    type BinaryVersion = ();
1533    async fn check_if_user_installed(
1534        &self,
1535        delegate: &dyn LspAdapterDelegate,
1536        toolchain: Option<Toolchain>,
1537        _: &AsyncApp,
1538    ) -> Option<LanguageServerBinary> {
1539        if let Some(pylsp_bin) = delegate.which(Self::SERVER_NAME.as_ref()).await {
1540            let env = delegate.shell_env().await;
1541            Some(LanguageServerBinary {
1542                path: pylsp_bin,
1543                env: Some(env),
1544                arguments: vec![],
1545            })
1546        } else {
1547            let toolchain = toolchain?;
1548            let pylsp_path = Path::new(toolchain.path.as_ref()).parent()?.join("pylsp");
1549            pylsp_path.exists().then(|| LanguageServerBinary {
1550                path: toolchain.path.to_string().into(),
1551                arguments: vec![pylsp_path.into()],
1552                env: None,
1553            })
1554        }
1555    }
1556
1557    async fn fetch_latest_server_version(
1558        &self,
1559        _: &dyn LspAdapterDelegate,
1560        _: bool,
1561        _: &mut AsyncApp,
1562    ) -> Result<()> {
1563        Ok(())
1564    }
1565
1566    async fn fetch_server_binary(
1567        &self,
1568        _: (),
1569        _: PathBuf,
1570        delegate: &dyn LspAdapterDelegate,
1571    ) -> Result<LanguageServerBinary> {
1572        let venv = self.base_venv(delegate).await.map_err(|e| anyhow!(e))?;
1573        let pip_path = venv.join(BINARY_DIR).join("pip3");
1574        ensure!(
1575            util::command::new_smol_command(pip_path.as_path())
1576                .arg("install")
1577                .arg("python-lsp-server[all]")
1578                .arg("--upgrade")
1579                .output()
1580                .await?
1581                .status
1582                .success(),
1583            "python-lsp-server[all] installation failed"
1584        );
1585        ensure!(
1586            util::command::new_smol_command(pip_path)
1587                .arg("install")
1588                .arg("pylsp-mypy")
1589                .arg("--upgrade")
1590                .output()
1591                .await?
1592                .status
1593                .success(),
1594            "pylsp-mypy installation failed"
1595        );
1596        let pylsp = venv.join(BINARY_DIR).join("pylsp");
1597        ensure!(
1598            delegate.which(pylsp.as_os_str()).await.is_some(),
1599            "pylsp installation was incomplete"
1600        );
1601        Ok(LanguageServerBinary {
1602            path: pylsp,
1603            env: None,
1604            arguments: vec![],
1605        })
1606    }
1607
1608    async fn cached_server_binary(
1609        &self,
1610        _: PathBuf,
1611        delegate: &dyn LspAdapterDelegate,
1612    ) -> Option<LanguageServerBinary> {
1613        let venv = self.base_venv(delegate).await.ok()?;
1614        let pylsp = venv.join(BINARY_DIR).join("pylsp");
1615        delegate.which(pylsp.as_os_str()).await?;
1616        Some(LanguageServerBinary {
1617            path: pylsp,
1618            env: None,
1619            arguments: vec![],
1620        })
1621    }
1622}
1623
1624pub(crate) struct BasedPyrightLspAdapter {
1625    node: NodeRuntime,
1626}
1627
1628impl BasedPyrightLspAdapter {
1629    const SERVER_NAME: LanguageServerName = LanguageServerName::new_static("basedpyright");
1630    const BINARY_NAME: &'static str = "basedpyright-langserver";
1631    const SERVER_PATH: &str = "node_modules/basedpyright/langserver.index.js";
1632    const NODE_MODULE_RELATIVE_SERVER_PATH: &str = "basedpyright/langserver.index.js";
1633
1634    pub(crate) const fn new(node: NodeRuntime) -> Self {
1635        BasedPyrightLspAdapter { node }
1636    }
1637
1638    async fn get_cached_server_binary(
1639        container_dir: PathBuf,
1640        node: &NodeRuntime,
1641    ) -> Option<LanguageServerBinary> {
1642        let server_path = container_dir.join(Self::SERVER_PATH);
1643        if server_path.exists() {
1644            Some(LanguageServerBinary {
1645                path: node.binary_path().await.log_err()?,
1646                env: None,
1647                arguments: vec![server_path.into(), "--stdio".into()],
1648            })
1649        } else {
1650            log::error!("missing executable in directory {:?}", server_path);
1651            None
1652        }
1653    }
1654}
1655
1656#[async_trait(?Send)]
1657impl LspAdapter for BasedPyrightLspAdapter {
1658    fn name(&self) -> LanguageServerName {
1659        Self::SERVER_NAME
1660    }
1661
1662    async fn initialization_options(
1663        self: Arc<Self>,
1664        _: &Arc<dyn LspAdapterDelegate>,
1665    ) -> Result<Option<Value>> {
1666        // Provide minimal initialization options
1667        // Virtual environment configuration will be handled through workspace configuration
1668        Ok(Some(json!({
1669            "python": {
1670                "analysis": {
1671                    "autoSearchPaths": true,
1672                    "useLibraryCodeForTypes": true,
1673                    "autoImportCompletions": true
1674                }
1675            }
1676        })))
1677    }
1678
1679    async fn process_completions(&self, items: &mut [lsp::CompletionItem]) {
1680        process_pyright_completions(items);
1681    }
1682
1683    async fn label_for_completion(
1684        &self,
1685        item: &lsp::CompletionItem,
1686        language: &Arc<language::Language>,
1687    ) -> Option<language::CodeLabel> {
1688        let label = &item.label;
1689        let grammar = language.grammar()?;
1690        let highlight_id = match item.kind? {
1691            lsp::CompletionItemKind::METHOD => grammar.highlight_id_for_name("function.method"),
1692            lsp::CompletionItemKind::FUNCTION => grammar.highlight_id_for_name("function"),
1693            lsp::CompletionItemKind::CLASS => grammar.highlight_id_for_name("type"),
1694            lsp::CompletionItemKind::CONSTANT => grammar.highlight_id_for_name("constant"),
1695            lsp::CompletionItemKind::VARIABLE => grammar.highlight_id_for_name("variable"),
1696            _ => {
1697                return None;
1698            }
1699        };
1700        let mut text = label.clone();
1701        if let Some(completion_details) = item
1702            .label_details
1703            .as_ref()
1704            .and_then(|details| details.description.as_ref())
1705        {
1706            write!(&mut text, " {}", completion_details).ok();
1707        }
1708        Some(language::CodeLabel::filtered(
1709            text,
1710            item.filter_text.as_deref(),
1711            highlight_id
1712                .map(|id| (0..label.len(), id))
1713                .into_iter()
1714                .collect(),
1715        ))
1716    }
1717
1718    async fn label_for_symbol(
1719        &self,
1720        name: &str,
1721        kind: lsp::SymbolKind,
1722        language: &Arc<language::Language>,
1723    ) -> Option<language::CodeLabel> {
1724        let (text, filter_range, display_range) = match kind {
1725            lsp::SymbolKind::METHOD | lsp::SymbolKind::FUNCTION => {
1726                let text = format!("def {}():\n", name);
1727                let filter_range = 4..4 + name.len();
1728                let display_range = 0..filter_range.end;
1729                (text, filter_range, display_range)
1730            }
1731            lsp::SymbolKind::CLASS => {
1732                let text = format!("class {}:", name);
1733                let filter_range = 6..6 + name.len();
1734                let display_range = 0..filter_range.end;
1735                (text, filter_range, display_range)
1736            }
1737            lsp::SymbolKind::CONSTANT => {
1738                let text = format!("{} = 0", name);
1739                let filter_range = 0..name.len();
1740                let display_range = 0..filter_range.end;
1741                (text, filter_range, display_range)
1742            }
1743            _ => return None,
1744        };
1745        Some(language::CodeLabel::new(
1746            text[display_range.clone()].to_string(),
1747            filter_range,
1748            language.highlight_text(&text.as_str().into(), display_range),
1749        ))
1750    }
1751
1752    async fn workspace_configuration(
1753        self: Arc<Self>,
1754        adapter: &Arc<dyn LspAdapterDelegate>,
1755        toolchain: Option<Toolchain>,
1756        cx: &mut AsyncApp,
1757    ) -> Result<Value> {
1758        cx.update(move |cx| {
1759            let mut user_settings =
1760                language_server_settings(adapter.as_ref(), &Self::SERVER_NAME, cx)
1761                    .and_then(|s| s.settings.clone())
1762                    .unwrap_or_default();
1763
1764            // If we have a detected toolchain, configure Pyright to use it
1765            if let Some(toolchain) = toolchain
1766                && let Ok(env) = serde_json::from_value::<
1767                    pet_core::python_environment::PythonEnvironment,
1768                >(toolchain.as_json.clone())
1769            {
1770                if !user_settings.is_object() {
1771                    user_settings = Value::Object(serde_json::Map::default());
1772                }
1773                let object = user_settings.as_object_mut().unwrap();
1774
1775                let interpreter_path = toolchain.path.to_string();
1776                if let Some(venv_dir) = env.prefix {
1777                    // Set venvPath and venv at the root level
1778                    // This matches the format of a pyrightconfig.json file
1779                    if let Some(parent) = venv_dir.parent() {
1780                        // Use relative path if the venv is inside the workspace
1781                        let venv_path = if parent == adapter.worktree_root_path() {
1782                            ".".to_string()
1783                        } else {
1784                            parent.to_string_lossy().into_owned()
1785                        };
1786                        object.insert("venvPath".to_string(), Value::String(venv_path));
1787                    }
1788
1789                    if let Some(venv_name) = venv_dir.file_name() {
1790                        object.insert(
1791                            "venv".to_owned(),
1792                            Value::String(venv_name.to_string_lossy().into_owned()),
1793                        );
1794                    }
1795                }
1796
1797                // Set both pythonPath and defaultInterpreterPath for compatibility
1798                if let Some(python) = object
1799                    .entry("python")
1800                    .or_insert(Value::Object(serde_json::Map::default()))
1801                    .as_object_mut()
1802                {
1803                    python.insert(
1804                        "pythonPath".to_owned(),
1805                        Value::String(interpreter_path.clone()),
1806                    );
1807                    python.insert(
1808                        "defaultInterpreterPath".to_owned(),
1809                        Value::String(interpreter_path),
1810                    );
1811                }
1812                // Basedpyright by default uses `strict` type checking, we tone it down as to not surpris users
1813                maybe!({
1814                    let basedpyright = object
1815                        .entry("basedpyright")
1816                        .or_insert(Value::Object(serde_json::Map::default()));
1817                    let analysis = basedpyright
1818                        .as_object_mut()?
1819                        .entry("analysis")
1820                        .or_insert(Value::Object(serde_json::Map::default()));
1821                    if let serde_json::map::Entry::Vacant(v) =
1822                        analysis.as_object_mut()?.entry("typeCheckingMode")
1823                    {
1824                        v.insert(Value::String("standard".to_owned()));
1825                    }
1826                    Some(())
1827                });
1828            }
1829
1830            user_settings
1831        })
1832    }
1833}
1834
1835impl LspInstaller for BasedPyrightLspAdapter {
1836    type BinaryVersion = String;
1837
1838    async fn fetch_latest_server_version(
1839        &self,
1840        _: &dyn LspAdapterDelegate,
1841        _: bool,
1842        _: &mut AsyncApp,
1843    ) -> Result<String> {
1844        self.node
1845            .npm_package_latest_version(Self::SERVER_NAME.as_ref())
1846            .await
1847    }
1848
1849    async fn check_if_user_installed(
1850        &self,
1851        delegate: &dyn LspAdapterDelegate,
1852        _: Option<Toolchain>,
1853        _: &AsyncApp,
1854    ) -> Option<LanguageServerBinary> {
1855        if let Some(path) = delegate.which(Self::BINARY_NAME.as_ref()).await {
1856            let env = delegate.shell_env().await;
1857            Some(LanguageServerBinary {
1858                path,
1859                env: Some(env),
1860                arguments: vec!["--stdio".into()],
1861            })
1862        } else {
1863            // TODO shouldn't this be self.node.binary_path()?
1864            let node = delegate.which("node".as_ref()).await?;
1865            let (node_modules_path, _) = delegate
1866                .npm_package_installed_version(Self::SERVER_NAME.as_ref())
1867                .await
1868                .log_err()??;
1869
1870            let path = node_modules_path.join(Self::NODE_MODULE_RELATIVE_SERVER_PATH);
1871
1872            let env = delegate.shell_env().await;
1873            Some(LanguageServerBinary {
1874                path: node,
1875                env: Some(env),
1876                arguments: vec![path.into(), "--stdio".into()],
1877            })
1878        }
1879    }
1880
1881    async fn fetch_server_binary(
1882        &self,
1883        latest_version: Self::BinaryVersion,
1884        container_dir: PathBuf,
1885        delegate: &dyn LspAdapterDelegate,
1886    ) -> Result<LanguageServerBinary> {
1887        let server_path = container_dir.join(Self::SERVER_PATH);
1888
1889        self.node
1890            .npm_install_packages(
1891                &container_dir,
1892                &[(Self::SERVER_NAME.as_ref(), latest_version.as_str())],
1893            )
1894            .await?;
1895
1896        let env = delegate.shell_env().await;
1897        Ok(LanguageServerBinary {
1898            path: self.node.binary_path().await?,
1899            env: Some(env),
1900            arguments: vec![server_path.into(), "--stdio".into()],
1901        })
1902    }
1903
1904    async fn check_if_version_installed(
1905        &self,
1906        version: &Self::BinaryVersion,
1907        container_dir: &PathBuf,
1908        delegate: &dyn LspAdapterDelegate,
1909    ) -> Option<LanguageServerBinary> {
1910        let server_path = container_dir.join(Self::SERVER_PATH);
1911
1912        let should_install_language_server = self
1913            .node
1914            .should_install_npm_package(
1915                Self::SERVER_NAME.as_ref(),
1916                &server_path,
1917                container_dir,
1918                VersionStrategy::Latest(version),
1919            )
1920            .await;
1921
1922        if should_install_language_server {
1923            None
1924        } else {
1925            let env = delegate.shell_env().await;
1926            Some(LanguageServerBinary {
1927                path: self.node.binary_path().await.ok()?,
1928                env: Some(env),
1929                arguments: vec![server_path.into(), "--stdio".into()],
1930            })
1931        }
1932    }
1933
1934    async fn cached_server_binary(
1935        &self,
1936        container_dir: PathBuf,
1937        delegate: &dyn LspAdapterDelegate,
1938    ) -> Option<LanguageServerBinary> {
1939        let mut binary = Self::get_cached_server_binary(container_dir, &self.node).await?;
1940        binary.env = Some(delegate.shell_env().await);
1941        Some(binary)
1942    }
1943}
1944
1945pub(crate) struct RuffLspAdapter {
1946    fs: Arc<dyn Fs>,
1947}
1948
1949#[cfg(target_os = "macos")]
1950impl RuffLspAdapter {
1951    const GITHUB_ASSET_KIND: AssetKind = AssetKind::TarGz;
1952    const ARCH_SERVER_NAME: &str = "apple-darwin";
1953}
1954
1955#[cfg(target_os = "linux")]
1956impl RuffLspAdapter {
1957    const GITHUB_ASSET_KIND: AssetKind = AssetKind::TarGz;
1958    const ARCH_SERVER_NAME: &str = "unknown-linux-gnu";
1959}
1960
1961#[cfg(target_os = "freebsd")]
1962impl RuffLspAdapter {
1963    const GITHUB_ASSET_KIND: AssetKind = AssetKind::TarGz;
1964    const ARCH_SERVER_NAME: &str = "unknown-freebsd";
1965}
1966
1967#[cfg(target_os = "windows")]
1968impl RuffLspAdapter {
1969    const GITHUB_ASSET_KIND: AssetKind = AssetKind::Zip;
1970    const ARCH_SERVER_NAME: &str = "pc-windows-msvc";
1971}
1972
1973impl RuffLspAdapter {
1974    const SERVER_NAME: LanguageServerName = LanguageServerName::new_static("ruff");
1975
1976    pub fn new(fs: Arc<dyn Fs>) -> RuffLspAdapter {
1977        RuffLspAdapter { fs }
1978    }
1979
1980    fn build_asset_name() -> Result<(String, String)> {
1981        let arch = match consts::ARCH {
1982            "x86" => "i686",
1983            _ => consts::ARCH,
1984        };
1985        let os = Self::ARCH_SERVER_NAME;
1986        let suffix = match consts::OS {
1987            "windows" => "zip",
1988            _ => "tar.gz",
1989        };
1990        let asset_name = format!("ruff-{arch}-{os}.{suffix}");
1991        let asset_stem = format!("ruff-{arch}-{os}");
1992        Ok((asset_stem, asset_name))
1993    }
1994}
1995
1996#[async_trait(?Send)]
1997impl LspAdapter for RuffLspAdapter {
1998    fn name(&self) -> LanguageServerName {
1999        Self::SERVER_NAME
2000    }
2001}
2002
2003impl LspInstaller for RuffLspAdapter {
2004    type BinaryVersion = GitHubLspBinaryVersion;
2005    async fn check_if_user_installed(
2006        &self,
2007        delegate: &dyn LspAdapterDelegate,
2008        toolchain: Option<Toolchain>,
2009        _: &AsyncApp,
2010    ) -> Option<LanguageServerBinary> {
2011        let ruff_in_venv = if let Some(toolchain) = toolchain
2012            && toolchain.language_name.as_ref() == "Python"
2013        {
2014            Path::new(toolchain.path.as_str())
2015                .parent()
2016                .map(|path| path.join("ruff"))
2017        } else {
2018            None
2019        };
2020
2021        for path in ruff_in_venv.into_iter().chain(["ruff".into()]) {
2022            if let Some(ruff_bin) = delegate.which(path.as_os_str()).await {
2023                let env = delegate.shell_env().await;
2024                return Some(LanguageServerBinary {
2025                    path: ruff_bin,
2026                    env: Some(env),
2027                    arguments: vec!["server".into()],
2028                });
2029            }
2030        }
2031
2032        None
2033    }
2034
2035    async fn fetch_latest_server_version(
2036        &self,
2037        delegate: &dyn LspAdapterDelegate,
2038        _: bool,
2039        _: &mut AsyncApp,
2040    ) -> Result<GitHubLspBinaryVersion> {
2041        let release =
2042            latest_github_release("astral-sh/ruff", true, false, delegate.http_client()).await?;
2043        let (_, asset_name) = Self::build_asset_name()?;
2044        let asset = release
2045            .assets
2046            .into_iter()
2047            .find(|asset| asset.name == asset_name)
2048            .with_context(|| format!("no asset found matching `{asset_name:?}`"))?;
2049        Ok(GitHubLspBinaryVersion {
2050            name: release.tag_name,
2051            url: asset.browser_download_url,
2052            digest: asset.digest,
2053        })
2054    }
2055
2056    async fn fetch_server_binary(
2057        &self,
2058        latest_version: GitHubLspBinaryVersion,
2059        container_dir: PathBuf,
2060        delegate: &dyn LspAdapterDelegate,
2061    ) -> Result<LanguageServerBinary> {
2062        let GitHubLspBinaryVersion {
2063            name,
2064            url,
2065            digest: expected_digest,
2066        } = latest_version;
2067        let destination_path = container_dir.join(format!("ruff-{name}"));
2068        let server_path = match Self::GITHUB_ASSET_KIND {
2069            AssetKind::TarGz | AssetKind::Gz => destination_path
2070                .join(Self::build_asset_name()?.0)
2071                .join("ruff"),
2072            AssetKind::Zip => destination_path.clone().join("ruff.exe"),
2073        };
2074
2075        let binary = LanguageServerBinary {
2076            path: server_path.clone(),
2077            env: None,
2078            arguments: vec!["server".into()],
2079        };
2080
2081        let metadata_path = destination_path.with_extension("metadata");
2082        let metadata = GithubBinaryMetadata::read_from_file(&metadata_path)
2083            .await
2084            .ok();
2085        if let Some(metadata) = metadata {
2086            let validity_check = async || {
2087                delegate
2088                    .try_exec(LanguageServerBinary {
2089                        path: server_path.clone(),
2090                        arguments: vec!["--version".into()],
2091                        env: None,
2092                    })
2093                    .await
2094                    .inspect_err(|err| {
2095                        log::warn!("Unable to run {server_path:?} asset, redownloading: {err}",)
2096                    })
2097            };
2098            if let (Some(actual_digest), Some(expected_digest)) =
2099                (&metadata.digest, &expected_digest)
2100            {
2101                if actual_digest == expected_digest {
2102                    if validity_check().await.is_ok() {
2103                        return Ok(binary);
2104                    }
2105                } else {
2106                    log::info!(
2107                        "SHA-256 mismatch for {destination_path:?} asset, downloading new asset. Expected: {expected_digest}, Got: {actual_digest}"
2108                    );
2109                }
2110            } else if validity_check().await.is_ok() {
2111                return Ok(binary);
2112            }
2113        }
2114
2115        download_server_binary(
2116            &*delegate.http_client(),
2117            &url,
2118            expected_digest.as_deref(),
2119            &destination_path,
2120            Self::GITHUB_ASSET_KIND,
2121        )
2122        .await?;
2123        make_file_executable(&server_path).await?;
2124        remove_matching(&container_dir, |path| path != destination_path).await;
2125        GithubBinaryMetadata::write_to_file(
2126            &GithubBinaryMetadata {
2127                metadata_version: 1,
2128                digest: expected_digest,
2129            },
2130            &metadata_path,
2131        )
2132        .await?;
2133
2134        Ok(LanguageServerBinary {
2135            path: server_path,
2136            env: None,
2137            arguments: vec!["server".into()],
2138        })
2139    }
2140
2141    async fn cached_server_binary(
2142        &self,
2143        container_dir: PathBuf,
2144        _: &dyn LspAdapterDelegate,
2145    ) -> Option<LanguageServerBinary> {
2146        maybe!(async {
2147            let mut last = None;
2148            let mut entries = self.fs.read_dir(&container_dir).await?;
2149            while let Some(entry) = entries.next().await {
2150                let path = entry?;
2151                if path.extension().is_some_and(|ext| ext == "metadata") {
2152                    continue;
2153                }
2154                last = Some(path);
2155            }
2156
2157            let path = last.context("no cached binary")?;
2158            let path = match Self::GITHUB_ASSET_KIND {
2159                AssetKind::TarGz | AssetKind::Gz => {
2160                    path.join(Self::build_asset_name()?.0).join("ruff")
2161                }
2162                AssetKind::Zip => path.join("ruff.exe"),
2163            };
2164
2165            anyhow::Ok(LanguageServerBinary {
2166                path,
2167                env: None,
2168                arguments: vec!["server".into()],
2169            })
2170        })
2171        .await
2172        .log_err()
2173    }
2174}
2175
2176#[cfg(test)]
2177mod tests {
2178    use gpui::{AppContext as _, BorrowAppContext, Context, TestAppContext};
2179    use language::{AutoindentMode, Buffer};
2180    use settings::SettingsStore;
2181    use std::num::NonZeroU32;
2182
2183    #[gpui::test]
2184    async fn test_python_autoindent(cx: &mut TestAppContext) {
2185        cx.executor().set_block_on_ticks(usize::MAX..=usize::MAX);
2186        let language = crate::language("python", tree_sitter_python::LANGUAGE.into());
2187        cx.update(|cx| {
2188            let test_settings = SettingsStore::test(cx);
2189            cx.set_global(test_settings);
2190            language::init(cx);
2191            cx.update_global::<SettingsStore, _>(|store, cx| {
2192                store.update_user_settings(cx, |s| {
2193                    s.project.all_languages.defaults.tab_size = NonZeroU32::new(2);
2194                });
2195            });
2196        });
2197
2198        cx.new(|cx| {
2199            let mut buffer = Buffer::local("", cx).with_language(language, cx);
2200            let append = |buffer: &mut Buffer, text: &str, cx: &mut Context<Buffer>| {
2201                let ix = buffer.len();
2202                buffer.edit([(ix..ix, text)], Some(AutoindentMode::EachLine), cx);
2203            };
2204
2205            // indent after "def():"
2206            append(&mut buffer, "def a():\n", cx);
2207            assert_eq!(buffer.text(), "def a():\n  ");
2208
2209            // preserve indent after blank line
2210            append(&mut buffer, "\n  ", cx);
2211            assert_eq!(buffer.text(), "def a():\n  \n  ");
2212
2213            // indent after "if"
2214            append(&mut buffer, "if a:\n  ", cx);
2215            assert_eq!(buffer.text(), "def a():\n  \n  if a:\n    ");
2216
2217            // preserve indent after statement
2218            append(&mut buffer, "b()\n", cx);
2219            assert_eq!(buffer.text(), "def a():\n  \n  if a:\n    b()\n    ");
2220
2221            // preserve indent after statement
2222            append(&mut buffer, "else", cx);
2223            assert_eq!(buffer.text(), "def a():\n  \n  if a:\n    b()\n    else");
2224
2225            // dedent "else""
2226            append(&mut buffer, ":", cx);
2227            assert_eq!(buffer.text(), "def a():\n  \n  if a:\n    b()\n  else:");
2228
2229            // indent lines after else
2230            append(&mut buffer, "\n", cx);
2231            assert_eq!(
2232                buffer.text(),
2233                "def a():\n  \n  if a:\n    b()\n  else:\n    "
2234            );
2235
2236            // indent after an open paren. the closing paren is not indented
2237            // because there is another token before it on the same line.
2238            append(&mut buffer, "foo(\n1)", cx);
2239            assert_eq!(
2240                buffer.text(),
2241                "def a():\n  \n  if a:\n    b()\n  else:\n    foo(\n      1)"
2242            );
2243
2244            // dedent the closing paren if it is shifted to the beginning of the line
2245            let argument_ix = buffer.text().find('1').unwrap();
2246            buffer.edit(
2247                [(argument_ix..argument_ix + 1, "")],
2248                Some(AutoindentMode::EachLine),
2249                cx,
2250            );
2251            assert_eq!(
2252                buffer.text(),
2253                "def a():\n  \n  if a:\n    b()\n  else:\n    foo(\n    )"
2254            );
2255
2256            // preserve indent after the close paren
2257            append(&mut buffer, "\n", cx);
2258            assert_eq!(
2259                buffer.text(),
2260                "def a():\n  \n  if a:\n    b()\n  else:\n    foo(\n    )\n    "
2261            );
2262
2263            // manually outdent the last line
2264            let end_whitespace_ix = buffer.len() - 4;
2265            buffer.edit(
2266                [(end_whitespace_ix..buffer.len(), "")],
2267                Some(AutoindentMode::EachLine),
2268                cx,
2269            );
2270            assert_eq!(
2271                buffer.text(),
2272                "def a():\n  \n  if a:\n    b()\n  else:\n    foo(\n    )\n"
2273            );
2274
2275            // preserve the newly reduced indentation on the next newline
2276            append(&mut buffer, "\n", cx);
2277            assert_eq!(
2278                buffer.text(),
2279                "def a():\n  \n  if a:\n    b()\n  else:\n    foo(\n    )\n\n"
2280            );
2281
2282            // reset to a for loop statement
2283            let statement = "for i in range(10):\n  print(i)\n";
2284            buffer.edit([(0..buffer.len(), statement)], None, cx);
2285
2286            // insert single line comment after each line
2287            let eol_ixs = statement
2288                .char_indices()
2289                .filter_map(|(ix, c)| if c == '\n' { Some(ix) } else { None })
2290                .collect::<Vec<usize>>();
2291            let editions = eol_ixs
2292                .iter()
2293                .enumerate()
2294                .map(|(i, &eol_ix)| (eol_ix..eol_ix, format!(" # comment {}", i + 1)))
2295                .collect::<Vec<(std::ops::Range<usize>, String)>>();
2296            buffer.edit(editions, Some(AutoindentMode::EachLine), cx);
2297            assert_eq!(
2298                buffer.text(),
2299                "for i in range(10): # comment 1\n  print(i) # comment 2\n"
2300            );
2301
2302            // reset to a simple if statement
2303            buffer.edit([(0..buffer.len(), "if a:\n  b(\n  )")], None, cx);
2304
2305            // dedent "else" on the line after a closing paren
2306            append(&mut buffer, "\n  else:\n", cx);
2307            assert_eq!(buffer.text(), "if a:\n  b(\n  )\nelse:\n  ");
2308
2309            buffer
2310        });
2311    }
2312}