Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ path = "src/helm.rs"
crate-type = ["cdylib"]

[dependencies]
zed_extension_api = "0.0.4"
zed_extension_api = "0.1.0"
1 change: 1 addition & 0 deletions languages/helm/config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name = "Helm"
grammar = "helm"
path_suffixes = ["gotmpl", "helmfile.yaml", "tpl"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this still work for yaml files in the templates directory?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't look like it

line_comments = ["# "]
block_comment = ["{{/* ", " */}}"]
brackets = [
Expand Down
119 changes: 111 additions & 8 deletions src/helm.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,126 @@
use std::fs;
use zed::LanguageServerId;
use zed_extension_api::{self as zed, Result};

struct HelmExtension;
struct HelmExtension {
cached_binary_path: Option<String>,
}

impl HelmExtension {
fn language_server_binary_path(
&mut self,
language_server_id: &LanguageServerId,
worktree: &zed::Worktree,
) -> Result<String> {
if let Some(path) = &self.cached_binary_path {
if fs::metadata(path).map_or(false, |stat| stat.is_file()) {
return Ok(path.clone());
}
}

if let Some(path) = worktree.which("helm_ls") {
self.cached_binary_path = Some(path.clone());
return Ok(path);
}

zed::set_language_server_installation_status(
language_server_id,
&zed::LanguageServerInstallationStatus::CheckingForUpdate,
);
let release = zed::latest_github_release(
"mrjosh/helm-ls",
zed::GithubReleaseOptions {
require_assets: true,
pre_release: true,
},
)?;

let (platform, arch) = zed::current_platform();
let asset_name = format!(
"helm_ls_{os}_{arch}{extension}",
os = match platform {
zed::Os::Mac => "darwin",
zed::Os::Linux => "linux",
zed::Os::Windows => "windows",
},
arch = match arch {
zed::Architecture::Aarch64 => "arm64",
zed::Architecture::X86 => "x86",
zed::Architecture::X8664 => "x86_64",
},
extension = match platform {
zed::Os::Mac | zed::Os::Linux => "",
zed::Os::Windows => ".exe",
}
);

let asset = release
.assets
.iter()
.find(|asset| asset.name == asset_name)
.ok_or_else(|| format!("no asset found matching {:?}", asset_name))?;

let version_dir = format!("helm_ls_{}", release.version);
fs::create_dir_all(&version_dir)
.map_err(|err| format!("failed to create directory '{version_dir}': {err}"))?;
let binary_path = format!(
"{version_dir}/helm_ls_{os}_{arch}",
os = match platform {
zed::Os::Mac => "darwin",
zed::Os::Linux => "linux",
zed::Os::Windows => "windows",
},
arch = match arch {
zed::Architecture::Aarch64 => "arm64",
zed::Architecture::X86 => "x86",
zed::Architecture::X8664 => "x86_64",
},
);

if !fs::metadata(&binary_path).map_or(false, |stat| stat.is_file()) {
zed::set_language_server_installation_status(
language_server_id,
&zed::LanguageServerInstallationStatus::Downloading,
);

zed::download_file(
&asset.download_url,
&binary_path,
zed::DownloadedFileType::Uncompressed,
)
.map_err(|e| format!("failed to download file: {e}"))?;

zed::make_file_executable(&binary_path)?;

let entries =
fs::read_dir(".").map_err(|e| format!("failed to list working directory {e}"))?;
for entry in entries {
let entry = entry.map_err(|e| format!("failed to load directory entry {e}"))?;
if entry.file_name().to_str() != Some(&version_dir) {
fs::remove_dir_all(entry.path()).ok();
}
}
}

self.cached_binary_path = Some(binary_path.clone());
Ok(binary_path)
}
}

impl zed::Extension for HelmExtension {
fn new() -> Self {
Self
Self {
cached_binary_path: None,
}
}

fn language_server_command(
&mut self,
_config: zed::LanguageServerConfig,
language_server_id: &LanguageServerId,
worktree: &zed::Worktree,
) -> Result<zed::Command> {
let path = worktree
.which("helm_ls")
.ok_or_else(|| "The LSP for helm 'helm-ls' is not installed".to_string())?;

Ok(zed::Command {
command: path,
command: self.language_server_binary_path(language_server_id, worktree)?,
args: vec!["serve".to_string()],
env: Default::default(),
})
Expand Down