Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "powershell"
version = "0.4.0"
Comment thread
MrSubidubi marked this conversation as resolved.
version = "0.5.0"
edition = "2021"

[lib]
Expand Down
2 changes: 1 addition & 1 deletion extension.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
id = "powershell"
name = "PowerShell"
version = "0.4.0"
version = "0.5.0"
schema_version = 1
authors = ["Thanabodee Charoenpiriyakij <wingyminus@gmail.com>"]
description = "PowerShell support"
Expand Down
42 changes: 30 additions & 12 deletions src/powershell.rs
Comment thread
MrSubidubi marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::env;
use std::fs;
use std::path;
use zed_extension_api::{self as zed, Result};
use zed_extension_api::settings::LspSettings;

struct PowerShellExtension {
/// The PowerShell binary, default to `pwsh`.
Expand All @@ -27,7 +28,7 @@ impl zed::Extension for PowerShellExtension {

let bundle_path = self
.language_server_path(language_server_id)
.map_err(|err| format!("failed to get editor services: {err}"))?;
.map_err(|err| format!("failed to get editor services: {}", err))?;

let command = format!(
"Import-Module ( \
Expand Down Expand Up @@ -56,18 +57,36 @@ impl zed::Extension for PowerShellExtension {
env: Default::default(),
})
}

fn language_server_initialization_options(
&mut self,
language_server_id: &zed_extension_api::LanguageServerId,
worktree: &zed_extension_api::Worktree,
) -> zed_extension_api::Result<Option<zed_extension_api::serde_json::Value>> {
let settings = LspSettings::for_worktree(language_server_id.as_ref(), worktree)?;
Ok(settings.initialization_options)
}

fn language_server_workspace_configuration(
&mut self,
language_server_id: &zed_extension_api::LanguageServerId,
worktree: &zed_extension_api::Worktree,
) -> zed_extension_api::Result<Option<zed_extension_api::serde_json::Value>> {
let settings = LspSettings::for_worktree(language_server_id.as_ref(), worktree)?;
Ok(settings.settings)
}
}
Comment thread
MrSubidubi marked this conversation as resolved.

impl PowerShellExtension {
fn powershell_binary_path(&mut self, worktree: &zed::Worktree) -> Result<String> {
let pwsh_path = match &self.powershell_bin {
Some(path) if fs::metadata(path).is_ok_and(|stat| stat.is_file()) => path.clone(),
Some(path) if fs::metadata(path).map_or(false, |stat| stat.is_file()) => path.clone(),
Some(path) => worktree
.which(path.clone().as_str())
.ok_or("PowerShell must be installed for PowerShell Extension")?,
.ok_or_else(|| "PowerShell must be installed for PowerShell Extension")?,
None => worktree
.which("pwsh")
.ok_or("PowerShell must be installed for PowerShell Extension")?,
.ok_or_else(|| "PowerShell must be installed for PowerShell Extension")?,
};
self.powershell_bin = Some(pwsh_path.clone());
Ok(pwsh_path)
Expand All @@ -94,23 +113,23 @@ impl PowerShellExtension {
.assets
.iter()
.find(|asset| asset.name == "PowerShellEditorServices.zip")
.ok_or_else(|| "no PowerShellEditorServices.zip found".to_string())?;
.ok_or_else(|| format!("no PowerShellEditorServices.zip found"))?;

let version_dir = format!("powershell-es-{}", release.version);
let lsp_path = format!("{version_dir}/PowerShellEditorServices/Start-EditorServices.ps1");

if !fs::metadata(&lsp_path).is_ok_and(|stat| stat.is_file()) {
if !fs::metadata(&lsp_path).map_or(false, |stat| stat.is_file()) {
// Download the asset
zed::set_language_server_installation_status(
language_server_id,
&language_server_id,
&zed::LanguageServerInstallationStatus::Downloading,
);
zed::download_file(
&asset.download_url,
&version_dir,
zed::DownloadedFileType::Zip,
)
.map_err(|err| format!("download error {err}"))?;
.map_err(|err| format!("download error {}", err))?;

// Ensure the binary exists
let entries =
Expand All @@ -123,9 +142,8 @@ impl PowerShellExtension {
}
}

let abs_path = env::current_dir()
.map_err(|_| "Failed to get current path".to_string())?
.join(version_dir);
let abs_path =
path::absolute(&version_dir).map_err(|e| format!("failed to get absolute path {e}"))?;
Ok(abs_path.display().to_string())
}
}
Expand Down
Loading