Skip to content

RustAnalyzerServer

Base classes

Name Children Inherits
LSPServerInfo
anyenv.lsp_servers._base
Static configuration for an LSP server.

⋔ Inheritance diagram

graph TD
  1143052114["rust_lsps.RustAnalyzerServer"]
  514706066["_base.LSPServerInfo"]
  2751680898["builtins.object"]
  514706066 --> 1143052114
  2751680898 --> 514706066

🛈 DocStrings

Bases: LSPServerInfo

Rust analyzer with workspace detection.

Source code in src/anyenv/lsp_servers/_servers/rust_lsps.py
@dataclass
class RustAnalyzerServer(LSPServerInfo):
    """Rust analyzer with workspace detection."""

    async def resolve_root(
        self,
        file_path: str,
        project_root: str,
        fs: AsyncFileSystem,
    ) -> str | None:
        """Walk up to find workspace root containing [workspace] in Cargo.toml."""
        crate_root = await super().resolve_root(file_path, project_root, fs)
        if crate_root is None:
            return None

        # Walk up looking for workspace
        current = crate_root.rstrip("/")
        project_root = project_root.rstrip("/")

        while True:
            cargo_toml = posixpath.join(current, "Cargo.toml")
            try:
                if await fs._exists(cargo_toml):  # noqa: SLF001
                    content = (await fs._cat_file(cargo_toml)).decode()  # noqa: SLF001
                    if "[workspace]" in content:
                        return current
            except Exception:  # noqa: BLE001
                pass

            if current == project_root:
                break
            parent = posixpath.dirname(current)
            if current in (parent, fs.root_marker):
                break
            current = parent

        return crate_root

resolve_root async

resolve_root(file_path: str, project_root: str, fs: AsyncFileSystem) -> str | None

Walk up to find workspace root containing [workspace] in Cargo.toml.

Source code in src/anyenv/lsp_servers/_servers/rust_lsps.py
async def resolve_root(
    self,
    file_path: str,
    project_root: str,
    fs: AsyncFileSystem,
) -> str | None:
    """Walk up to find workspace root containing [workspace] in Cargo.toml."""
    crate_root = await super().resolve_root(file_path, project_root, fs)
    if crate_root is None:
        return None

    # Walk up looking for workspace
    current = crate_root.rstrip("/")
    project_root = project_root.rstrip("/")

    while True:
        cargo_toml = posixpath.join(current, "Cargo.toml")
        try:
            if await fs._exists(cargo_toml):  # noqa: SLF001
                content = (await fs._cat_file(cargo_toml)).decode()  # noqa: SLF001
                if "[workspace]" in content:
                    return current
        except Exception:  # noqa: BLE001
            pass

        if current == project_root:
            break
        parent = posixpath.dirname(current)
        if current in (parent, fs.root_marker):
            break
        current = parent

    return crate_root

:fa-brands-github: Show source on GitHub