Skip to content
Merged
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
29 changes: 27 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### Generated by gibo (https://github.com/simonwhitaker/gibo)
### https://raw.github.com/github/gitignore/ce5da10a3a43c4dd8bd9572eda17c0a37ee0eac1/Node.gitignore
### https://raw.github.com/github/gitignore/274c32303f1f86a42db55e2dab3107e560714010/Node.gitignore

# Logs
logs
Expand Down Expand Up @@ -131,5 +131,30 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
### Generated by gibo (https://github.com/simonwhitaker/gibo)
### https://raw.github.com/github/gitignore/274c32303f1f86a42db55e2dab3107e560714010/Rust.gitignore

# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# RustRover
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# User
### User
/crates/zed/extension.wasm
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[workspace]
members = ["crates/*"]
12 changes: 12 additions & 0 deletions crates/zed/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "css-modules-kit-zed"
version = "0.0.1"
edition = "2024"
license = "MIT"

[lib]
crate-type = ["cdylib"]

[dependencies]
serde = { version = "1.0.219", features = ["derive"] }
zed_extension_api = "0.5.0"
21 changes: 21 additions & 0 deletions crates/zed/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 mizdra

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 18 additions & 0 deletions crates/zed/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# css-modules-kit-zed

The Zed extension for CSS Modules Kit

## Installation

1. Install "CSS Modules Kit" extension on Zed.
2. Add the following to your `~./config/zed/settings.json` file:
```json
{
"languages": {
"CSS": {
"language_servers": ["vtsls", "..."]
}
}
}
```
3. Restart Zed.
11 changes: 11 additions & 0 deletions crates/zed/extension.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
id = "css-modules-kit"
name = "CSS Modules Kit"
version = "0.0.1"
schema_version = 1
authors = ["mizdra <pp.mizdra@gmail.com>"]
description = "The Zed extension for CSS Modules Kit"
repository = "https://github.com/mizdra/css-modules-kit"

[language_servers.css-modules-kit-language-server]
name = "CSS Modules Kit Language Server"
languages = ["TypeScript", "TSX", "JavaScript", "CSS"]
122 changes: 122 additions & 0 deletions crates/zed/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
use std::collections::HashMap;
use std::env;

use serde::Deserialize;
use zed_extension_api::{self as zed, Result, serde_json};

const TS_PLUGIN_PACKAGE_NAME: &str = "@css-modules-kit/ts-plugin";

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct PackageJson {
#[serde(default)]
dependencies: HashMap<String, String>,
#[serde(default)]
dev_dependencies: HashMap<String, String>,
}

struct CSSModulesKitExtension {}

impl CSSModulesKitExtension {
fn install_ts_plugin_if_needed(&self) -> Result<()> {
let installed_plugin_version = zed::npm_package_installed_version(TS_PLUGIN_PACKAGE_NAME)?;
let latest_plugin_version = zed::npm_package_latest_version(TS_PLUGIN_PACKAGE_NAME)?;

if installed_plugin_version.as_ref() != Some(&latest_plugin_version) {
println!("installing {TS_PLUGIN_PACKAGE_NAME}@{latest_plugin_version}");
zed::npm_install_package(TS_PLUGIN_PACKAGE_NAME, &latest_plugin_version)?;
} else {
println!("ts-plugin already installed");
}
Ok(())
}

fn get_ts_plugin_root_path(&self, worktree: &zed::Worktree) -> Result<Option<String>> {
let package_json = worktree.read_text_file("package.json")?;
let package_json: PackageJson = serde_json::from_str(&package_json)
.map_err(|err| format!("failed to parse package.json: {err}"))?;

let has_local_plugin = package_json
.dev_dependencies
.contains_key(TS_PLUGIN_PACKAGE_NAME)
|| package_json
.dependencies
.contains_key(TS_PLUGIN_PACKAGE_NAME);

if has_local_plugin {
println!("Using local installation of {TS_PLUGIN_PACKAGE_NAME}");
return Ok(None);
}

self.install_ts_plugin_if_needed()?;

println!("Using global installation of {TS_PLUGIN_PACKAGE_NAME}");
Ok(Some(
env::current_dir().unwrap().to_string_lossy().to_string(),
))
}
}

impl zed::Extension for CSSModulesKitExtension {
fn new() -> Self {
Self {}
}

fn language_server_command(
&mut self,
_language_server_id: &zed::LanguageServerId,
_worktree: &zed::Worktree,
) -> Result<zed::Command> {
// A language server is not normally required. However,
// Zed does not seem to load ts-plugin unless a language server is configured.
// ref: https://github.com/zed-industries/zed/issues/22410#issuecomment-2815582825
Ok(zed::Command {
command: zed::node_binary_path()?,
args: vec!["-e".into(), r#"import('data:text/javascript;base64,Y29uc3QgeyBzdGRpbiwgc3Rkb3V0IH0gPSBwcm9jZXNzOwoKc3RkaW4uc2V0RW5jb2RpbmcoInV0ZjgiKTsKCmZ1bmN0aW9uIHNlbmQobXNnKSB7CiAgY29uc3QganNvbiA9IEpTT04uc3RyaW5naWZ5KG1zZyk7CiAgY29uc3QgbGVuID0gQnVmZmVyLmJ5dGVMZW5ndGgoanNvbiwgInV0ZjgiKTsKICBzdGRvdXQud3JpdGUoYENvbnRlbnQtTGVuZ3RoOiAke2xlbn1cclxuXHJcbmApOwogIHN0ZG91dC53cml0ZShqc29uKTsKfQoKbGV0IGJ1ZmZlciA9ICIiOwpsZXQgY29udGVudExlbmd0aCA9IDA7CgpzdGRpbi5vbigiZGF0YSIsIChjaHVuaykgPT4gewogIGJ1ZmZlciArPSBjaHVuazsKCiAgd2hpbGUgKHRydWUpIHsKICAgIGlmIChjb250ZW50TGVuZ3RoID09PSAwKSB7CiAgICAgIGNvbnN0IGhlYWRlckVuZCA9IGJ1ZmZlci5pbmRleE9mKCJcclxuXHJcbiIpOwogICAgICBpZiAoaGVhZGVyRW5kID09PSAtMSkgcmV0dXJuOwoKICAgICAgY29uc3QgaGVhZGVyID0gYnVmZmVyLnNsaWNlKDAsIGhlYWRlckVuZCk7CiAgICAgIGNvbnN0IG1hdGNoID0gaGVhZGVyLm1hdGNoKC9Db250ZW50LUxlbmd0aDogKFxkKykvaSk7CiAgICAgIGlmICghbWF0Y2gpIHJldHVybjsgLy8gSWdub3JlIG1hbGZvcm1lZCBoZWFkZXJzCgogICAgICBjb250ZW50TGVuZ3RoID0gcGFyc2VJbnQobWF0Y2hbMV0sIDEwKTsKICAgICAgYnVmZmVyID0gYnVmZmVyLnNsaWNlKGhlYWRlckVuZCArIDQpOwogICAgfQoKICAgIC8vIFdhaXQgZm9yIHRoZSBmdWxsIG1lc3NhZ2UgYm9keQogICAgaWYgKGJ1ZmZlci5sZW5ndGggPCBjb250ZW50TGVuZ3RoKSByZXR1cm47CgogICAgY29uc3QgbWVzc2FnZUpzb24gPSBidWZmZXIuc2xpY2UoMCwgY29udGVudExlbmd0aCk7CiAgICBidWZmZXIgPSBidWZmZXIuc2xpY2UoY29udGVudExlbmd0aCk7CiAgICBjb250ZW50TGVuZ3RoID0gMDsgLy8gUmVzZXQgZm9yIHRoZSBuZXh0IG1lc3NhZ2UKCiAgICB0cnkgewogICAgICBjb25zdCByZXEgPSBKU09OLnBhcnNlKG1lc3NhZ2VKc29uKTsKCiAgICAgIGlmIChyZXEubWV0aG9kID09PSAiaW5pdGlhbGl6ZSIpIHsKICAgICAgICBzZW5kKHsKICAgICAgICAgIGpzb25ycGM6ICIyLjAiLAogICAgICAgICAgaWQ6IHJlcS5pZCwKICAgICAgICAgIHJlc3VsdDogewogICAgICAgICAgICBjYXBhYmlsaXRpZXM6IHt9LCAvLyBNaW5pbWFsIGNhcGFiaWxpdGllcwogICAgICAgICAgfSwKICAgICAgICB9KTsKICAgICAgfSBlbHNlIGlmIChyZXEubWV0aG9kID09PSAic2h1dGRvd24iKSB7CiAgICAgICAgLy8gUmVzcG9uZCB0byBzaHV0ZG93biBiZWZvcmUgZXhpdGluZwogICAgICAgIHNlbmQoewogICAgICAgICAganNvbnJwYzogIjIuMCIsCiAgICAgICAgICBpZDogcmVxLmlkLAogICAgICAgICAgcmVzdWx0OiBudWxsLAogICAgICAgIH0pOwogICAgICAgIC8vIFRoZSBjbGllbnQgc2hvdWxkIHNlbmQgJ2V4aXQnIGFmdGVyIHRoaXMsIGJ1dCB3ZSBjYW4gZXhpdCBoZXJlIGlmIG5lZWRlZCwKICAgICAgICAvLyB0aG91Z2ggdGVjaG5pY2FsbHkgTFNQIHNwZWMgc3VnZ2VzdHMgd2FpdGluZyBmb3IgJ2V4aXQnCiAgICAgICAgLy8gcHJvY2Vzcy5leGl0KDApOwogICAgICB9IGVsc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gY29uc29sZS5lcnJvcigiU3RkaW4gRXJyb3I6IiwgZXJyKTsKICBwcm9jZXNzLmV4aXQoMSk7Cn0pOwoKc3Rkb3V0Lm9uKCJlcnJvciIsIChlcnIpID0+IHsKICBjb25zb2xlLmVycm9yKCJTdGRvdXQgRXJyb3I6IiwgZXJyKTsKICBwcm9jZXNzLmV4aXQoMSk7Cn0pOwo=')"#.to_string()],
env: Default::default(),
})
}

fn language_server_additional_initialization_options(
&mut self,
_language_server_id: &zed::LanguageServerId,
target_language_server_id: &zed::LanguageServerId,
worktree: &zed::Worktree,
) -> Result<Option<serde_json::Value>> {
match target_language_server_id.as_ref() {
"typescript-language-server" => Ok(Some(serde_json::json!({
"plugins": [{
"name": TS_PLUGIN_PACKAGE_NAME,
"location": self.get_ts_plugin_root_path(worktree)?.unwrap_or_else(|| worktree.root_path()),
"languages": ["css"]
}],
}))),
_ => Ok(None),
}
}

fn language_server_additional_workspace_configuration(
&mut self,
_language_server_id: &zed::LanguageServerId,
target_language_server_id: &zed::LanguageServerId,
worktree: &zed::Worktree,
) -> Result<Option<serde_json::Value>> {
match target_language_server_id.as_ref() {
"vtsls" => Ok(Some(serde_json::json!({
"vtsls": {
"tsserver": {
"globalPlugins": [{
"name": TS_PLUGIN_PACKAGE_NAME,
"location": self.get_ts_plugin_root_path(worktree)?.unwrap_or_else(|| worktree.root_path()),
"enableForWorkspaceTypeScriptVersions": true,
"languages": ["css"]
}]
}
},
}))),
_ => Ok(None),
}
}
}

zed::register_extension!(CSSModulesKitExtension);
2 changes: 1 addition & 1 deletion docs/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ To enable CSS Modules language features in your editor, you need to install [`@c
- For Emacs:
- Not yet supported
- For Zed:
- Not yet supported
- See [crates/zed/README.md](../crates/zed/README.md)
- For WebStorm:
- Not yet supported
- For StackBlitz Codeflow:
Expand Down