Skip to content

Commit cefe1a9

Browse files
committed
Implement Zed extension
1 parent 3557206 commit cefe1a9

File tree

5 files changed

+120
-1
lines changed

5 files changed

+120
-1
lines changed

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@ Cargo.lock
1818
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
1919
# and can be added to the global gitignore or merged into this file. For a more nuclear
2020
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
21-
#.idea/
21+
#.idea/
22+
23+
extension.wasm
24+
__pycache__
25+
*.pyc
26+
.DS_Store
27+
*.egg-info

Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "zed-python-refactoring"
3+
version = "0.0.3"
4+
edition = "2021"
5+
6+
[lib]
7+
path = "src/python_refactoring.rs"
8+
crate-type = ["cdylib"]
9+
10+
[dependencies]
11+
zed_extension_api = "0.0.6"
12+
13+
[workspace]
14+
members = ["."]

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,42 @@
11
# zed-python-refactoring
22
Adds refactoring support for Python in Zed
3+
4+
## Installation Steps
5+
6+
### Install the LSP
7+
8+
You can find the LSP implementation at [cst-lsp](https://github.com/rowillia/cst-lsp)
9+
10+
```bash
11+
pip install cst-lsp
12+
rustup target add wasm32-wasi
13+
```
14+
15+
To make sure things are installed properly, run `cst_lsp --stdio`.
16+
It should run without error (you'll have to Ctrl-C to kill it manually)
17+
18+
19+
### Install Development Extension
20+
21+
1. Open Zed
22+
2. ⌘+Shift+P - "zed: install dev extension"
23+
3. Point to this directory
24+
25+
If things aren't working check out the logs with:
26+
27+
```bash
28+
tail -f ~/Library/Logs/Zed/Zed.log
29+
```
30+
31+
### Add as a language server
32+
```json
33+
{
34+
"languages": {
35+
"Python": {
36+
"language_servers": ["pyright", "python_refactoring"]
37+
}
38+
}
39+
}
40+
```
41+
42+
Once installed, you should see a code action to "Refactor" one you select some Python lines.

extension.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
id = "python_refactoring"
2+
name = "PythonRefactoring"
3+
description = "Refactoring support for Python."
4+
version = "0.0.3"
5+
schema_version = 1
6+
authors = ["Roy Williams <[email protected]>"]
7+
repository = "https://github.com/rowillia/zed-python-refactoring"
8+
9+
[language_servers.python_refactoring]
10+
name = "PythonRefactoring"
11+
languages = ["Python"]

src/python_refactoring.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use zed_extension_api::{self as zed, settings::LspSettings};
2+
3+
struct PythonRefactoring;
4+
5+
impl zed::Extension for PythonRefactoring {
6+
fn new() -> Self {
7+
Self {}
8+
}
9+
fn language_server_command(
10+
&mut self,
11+
_language_server_id: &zed_extension_api::LanguageServerId,
12+
worktree: &zed_extension_api::Worktree,
13+
) -> zed_extension_api::Result<zed_extension_api::Command> {
14+
let path = worktree
15+
.which("cst_lsp")
16+
.ok_or_else(|| "cst_lsp must be installed and available in $PATH.".to_string())?;
17+
Ok(zed::Command {
18+
command: path,
19+
args: vec!["--stdio".to_string()],
20+
env: Default::default(),
21+
})
22+
}
23+
// ref https://github.com/zed-industries/zed/blob/main/extensions/ruff/src/ruff.rs
24+
fn language_server_initialization_options(
25+
&mut self,
26+
language_server_id: &zed_extension_api::LanguageServerId,
27+
worktree: &zed_extension_api::Worktree,
28+
) -> zed_extension_api::Result<Option<zed_extension_api::serde_json::Value>> {
29+
let settings = LspSettings::for_worktree(language_server_id.as_ref(), worktree)
30+
.ok()
31+
.and_then(|lsp_settings| lsp_settings.initialization_options.clone())
32+
.unwrap_or_default();
33+
Ok(Some(settings))
34+
}
35+
fn language_server_workspace_configuration(
36+
&mut self,
37+
language_server_id: &zed_extension_api::LanguageServerId,
38+
worktree: &zed_extension_api::Worktree,
39+
) -> zed_extension_api::Result<Option<zed_extension_api::serde_json::Value>> {
40+
let settings = LspSettings::for_worktree(language_server_id.as_ref(), worktree)
41+
.ok()
42+
.and_then(|lsp_settings| lsp_settings.settings.clone())
43+
.unwrap_or_default();
44+
Ok(Some(settings))
45+
}
46+
}
47+
48+
zed::register_extension!(PythonRefactoring);

0 commit comments

Comments
 (0)