Skip to content

Use new process API for starting language servers #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
43 changes: 41 additions & 2 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ path = "src/ruby.rs"
crate-type = ["cdylib"]

[dependencies]
zed_extension_api = "0.2.0"
regex = "1.11.1"
zed_extension_api = "0.3.0"
20 changes: 20 additions & 0 deletions extension.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,23 @@ commit = "332262529bc51abf5746317b2255ccc2fff778f8"
[grammars.rbs]
repository = "https://github.com/joker1007/tree-sitter-rbs"
commit = "de893b166476205b09e79cd3689f95831269579a"

[[capabilities]]
kind = "process:exec"
command = "gem"
args = ["install", "--no-user-install", "--no-format-executable", "--no-document", "*", "--norc"]

[[capabilities]]
kind = "process:exec"
command = "gem"
args = ["list", "--exact", "*", "--norc"]

[[capabilities]]
kind = "process:exec"
command = "bundle"
args = ["info", "--version", "*"]

[[capabilities]]
kind = "process:exec"
command = "gem"
args = ["outdated", "--norc"]
48 changes: 48 additions & 0 deletions src/bundler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::path::Path;

use zed_extension_api::{Command, Result};

/// A simple wrapper around the `bundle` command.
pub struct Bundler {
pub working_dir: String,
}

impl Bundler {
pub fn new(working_dir: String) -> Self {
Bundler { working_dir }
}

pub fn installed_gem_version(&self, name: &str) -> Result<String> {
let args = vec!["info", "--version", name];

self.execute_gem_command(args)
.map_err(|e| format!("Failed to get version for gem '{}': {}", name, e))
}

fn execute_gem_command(&self, args: Vec<&str>) -> Result<String> {
let bundle_gemfile_path = Path::new(&self.working_dir).join("Gemfile");
let bundle_gemfile = bundle_gemfile_path
.to_str()
.ok_or("Invalid path to Gemfile")?;

Command::new("bundle")
.args(args)
.env("BUNDLE_GEMFILE", bundle_gemfile)
.output()
.map_err(|e| format!("Failed to execute 'bundle' command: {}", e))
.and_then(|output| match output.status {
Some(0) => Ok(String::from_utf8_lossy(&output.stdout).to_string()),
Some(status) => {
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
Err(format!(
"'bundle' command failed (status: {})\nError: {}",
status, stderr
))
}
None => {
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
Err(format!("Failed to execute 'bundle' command: {}", stderr))
}
})
}
}
106 changes: 106 additions & 0 deletions src/gemset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use regex::Regex;
use zed_extension_api::{Command, Result};

/// A simple wrapper around the `gem` command.
pub struct Gemset {
pub gem_home: String,
}

impl Gemset {
pub fn new(gem_home: String) -> Self {
Self { gem_home }
}

pub fn install_gem(&self, name: String) -> Result<()> {
let args = vec![
"install",
"--no-user-install", // Do not install gems in user's home directory
"--no-format-executable", // Do not make installed executable names match Ruby
"--no-document", // Do not generate documentation
// "--env-shebang", // Use /usr/bin/env as a shebang
&name,
];

self.execute_gem_command(args)
.map_err(|e| format!("Failed to install gem '{}': {}", name, e))?;

Ok(())
}

pub fn update_gem(&self, name: String) -> Result<()> {
let args = vec!["update", &name];

self.execute_gem_command(args)
.map_err(|e| format!("Failed to update gem '{}': {}", name, e))?;

Ok(())
}

pub fn installed_gem_version(&self, name: String) -> Result<Option<String>> {
// Example output from `gem list`:
/*
*** LOCAL GEMS ***
abbrev (0.1.2)
prism (default: 1.2.0)
test-unit (3.6.7)
*/
let re = Regex::new(r"^(\S+) \((\S+)\)$")
.map_err(|e| format!("Failed to compile regex: {}", e))?;

let args = vec!["list", "--exact", &name];
let output = self
.execute_gem_command(args)
.map_err(|e| format!("Failed to get version for gem '{}': {}", name, e))?;

for line in output.lines() {
let captures = match re.captures(line) {
Some(c) => c,
None => continue,
};

let gem_package = captures.get(1).map(|m| m.as_str());
let version = captures.get(2).map(|m| m.as_str());

if gem_package == Some(&name) {
return Ok(version.map(|v| v.to_owned()));
}
}

Ok(None)
}

pub fn is_outdated_gem(&self, name: String) -> Result<bool> {
let args = vec!["outdated"];
let output = self
.execute_gem_command(args)
.map_err(|e| format!("Failed to check if gem is outdated: {}", e))?;

Ok(output
.lines()
.any(|line| line.split_whitespace().next().is_some_and(|n| n == name)))
}

fn execute_gem_command(&self, args: Vec<&str>) -> Result<String> {
Command::new("gem")
.env("GEM_PATH", &self.gem_home)
.env("GEM_HOME", &self.gem_home)
.args(args)
.arg("--norc")
.output()
.map_err(|e| format!("Failed to execute gem command: {}", e))
.and_then(|output| match output.status {
Some(0) => Ok(String::from_utf8_lossy(&output.stdout).to_string()),
Some(status) => {
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
Err(format!(
"Gem command failed (status: {})\nError: {}",
status, stderr
))
}
None => {
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
Err(format!("Failed to execute gem command: {}", stderr))
}
})
}
}
Loading