Skip to content

Commit 8d6a30e

Browse files
committed
Integrate with PowerShellEditorServices
Signed-off-by: Thanabodee Charoenpiriyakij <[email protected]>
1 parent d31fec9 commit 8d6a30e

File tree

4 files changed

+100
-6
lines changed

4 files changed

+100
-6
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
11
# PowerShell extension for Zed editor
2+
3+
## Setting up language server
4+
5+
Currently, the extension didn't support automatic installation (it'll soon land). You need
6+
to set the bundle path through `lsp` configuration:
7+
8+
```json
9+
{
10+
"lsp": {
11+
"powershell-es": {
12+
"binary": {
13+
"path": "<path to PowerShellEditorServices>"
14+
}
15+
}
16+
}
17+
}

extension.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ repository = "https://github.com/zed-industries/zed"
99
[grammars.powershell]
1010
repository = "https://github.com/airbus-cert/tree-sitter-powershell"
1111
commit = "804d86fd4ad286bd0cc1c1f0f7b28bd7af6755ad"
12+
13+
[language_servers.powershell-es]
14+
name = "PowerShell Editor Services"
15+
languages = ["PowerShell"]

src/language_server.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use zed_extension_api::{self as zed, Result};
2+
3+
pub struct PowerShellEditorServices;
4+
5+
impl PowerShellEditorServices {
6+
/// Install the PowerShellEditorServices.
7+
///
8+
/// Returns the bundle path after installed.
9+
pub fn install(language_server_id: &zed_extension_api::LanguageServerId) -> Result<String> {
10+
let release = zed::latest_github_release(
11+
"PowerShell/PowerShellEditorServices",
12+
zed::GithubReleaseOptions {
13+
require_assets: true,
14+
pre_release: false,
15+
},
16+
)?;
17+
18+
let asset = release
19+
.assets
20+
.iter()
21+
.find(|asset| asset.name == "PowerShellEditorServices.zip")
22+
.ok_or_else(|| format!("no PowerShellEditorServices.zip found"))?;
23+
24+
let path = format!("powershell-es-{}", release.version);
25+
26+
// TODO: do download when no bundle path found.
27+
// TODO: cache the bundle path.
28+
29+
zed::set_language_server_installation_status(
30+
&language_server_id,
31+
&zed::LanguageServerInstallationStatus::Downloading,
32+
);
33+
34+
zed::download_file(&asset.download_url, &path, zed::DownloadedFileType::Zip)
35+
.map_err(|err| format!("download error {}", err))?;
36+
37+
Ok(path.to_string())
38+
}
39+
}

src/powershell.rs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,56 @@
1-
use zed_extension_api::{self as zed};
1+
mod language_server;
22

3-
struct PowerShellExtension;
3+
use zed_extension_api::{self as zed, settings::LspSettings};
4+
5+
use crate::language_server::PowerShellEditorServices as EditorServices;
6+
7+
struct PowerShellExtension {
8+
/// The PowerShell binary, default to `pwsh`.
9+
// TODO: allow to configure via Zed settings.
10+
powershell_bin: Option<String>,
11+
}
412

513
impl zed::Extension for PowerShellExtension {
614
fn new() -> Self
715
where
816
Self: Sized,
917
{
10-
Self
18+
Self {
19+
powershell_bin: Some("pwsh".to_string()),
20+
}
1121
}
1222

1323
fn language_server_command(
1424
&mut self,
15-
_language_server_id: &zed_extension_api::LanguageServerId,
16-
_worktree: &zed_extension_api::Worktree,
25+
language_server_id: &zed_extension_api::LanguageServerId,
26+
worktree: &zed_extension_api::Worktree,
1727
) -> zed_extension_api::Result<zed_extension_api::Command> {
18-
todo!()
28+
let pwsh_bin = worktree
29+
.which(self.powershell_bin.clone().unwrap().as_str())
30+
.ok_or_else(|| "No PowerShell command found")?;
31+
32+
let bundle_path = LspSettings::for_worktree("powershell-es", worktree)
33+
.ok()
34+
.and_then(|lsp_settings| lsp_settings.binary)
35+
.and_then(|binary| binary.path)
36+
.unwrap();
37+
38+
// TODO: make remote install works.
39+
// let bundle_path = EditorServices::install(language_server_id)
40+
// .map_err(|err| format!("failed to get editor services: {}", err))?;
41+
42+
let command = format!("{bundle_path}/PowerShellEditorServices/Start-EditorServices.ps1 -BundledModulesPath {bundle_path} -Stdio -SessionDetailsPath {bundle_path}/powershell-es.session.json -LogPath {bundle_path}/logs -FeatureFlags @() -AdditionalModules @() -HostName zed -HostProfileId 0 -HostVersion 1.0.0 -LogLevel Diagnostic");
43+
44+
Ok(zed::Command {
45+
command: pwsh_bin,
46+
args: vec![
47+
"-NoLogo".to_string(),
48+
"-NoProfile".to_string(),
49+
"-Command".to_string(),
50+
command.to_string(),
51+
],
52+
env: Default::default(),
53+
})
1954
}
2055
}
2156

0 commit comments

Comments
 (0)