Extract Ruby extension (#11360)

This PR extracts Ruby and ERB support into an extension and removes the
built-in Ruby and Ruby support from Zed.

As part of this, the new extension is prepared for adding support for
the `Ruby LSP` which has some blockers. See
https://github.com/zed-industries/zed/pull/8613 I was thinking of adding
an initial support for Ruby LSP but I think it would be better to start
with extracting the Ruby extension for now.

The implementation, as the 1st step, matches the bundled version but
with 3 differences:

1. Added signature output to the completion popup. See my comment below.
![CleanShot 2024-05-04 at 09 17
37@2x](https://github.com/zed-industries/zed/assets/1894248/486b7a48-ea0c-44ce-b0c9-9f8f5d3ad42d)

3. Use the shell environment for starting the `solargraph` executable.
See my comment below.
4. Bumped the tree sitter version for Ruby to the latest available
version.

Additionally, I plan to tweak this extension a bit in the future but I
think we should do this bit by bit. Thanks!

Release Notes:

- Removed built-in support for Ruby, in favor of making it available as
an extension.

---------

Co-authored-by: Marshall Bowers <elliott.codes@gmail.com>
This commit is contained in:
Vitaly Slobodin 2024-05-10 17:53:11 +02:00 committed by GitHub
parent df00854bbc
commit 400e938997
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 230 additions and 211 deletions

View file

@ -0,0 +1,121 @@
use zed::lsp::{Completion, CompletionKind, Symbol, SymbolKind};
use zed::{CodeLabel, CodeLabelSpan};
use zed_extension_api::{self as zed, Result};
pub struct Solargraph {}
impl Solargraph {
pub const LANGUAGE_SERVER_ID: &'static str = "solargraph";
pub fn new() -> Self {
Self {}
}
pub fn server_script_path(&mut self, worktree: &zed::Worktree) -> Result<String> {
let path = worktree
.which("solargraph")
.ok_or_else(|| "solargraph must be installed manually".to_string())?;
Ok(path)
}
pub fn label_for_completion(&self, completion: Completion) -> Option<CodeLabel> {
match completion.kind? {
CompletionKind::Method => {
let highlight_name = match completion.kind? {
CompletionKind::Class | CompletionKind::Module => "type",
CompletionKind::Constant => "constant",
CompletionKind::Method => "function.method",
CompletionKind::Keyword => {
if completion.label.starts_with(':') {
"string.special.symbol"
} else {
"keyword"
}
}
CompletionKind::Variable => {
if completion.label.starts_with('@') {
"property"
} else {
return None;
}
}
_ => return None,
};
let len = completion.label.len();
let name_span =
CodeLabelSpan::literal(completion.label, Some(highlight_name.to_string()));
Some(CodeLabel {
code: Default::default(),
spans: if let Some(detail) = completion.detail {
vec![
name_span,
CodeLabelSpan::literal(" ", None),
CodeLabelSpan::literal(detail, None),
]
} else {
vec![name_span]
},
filter_range: (0..len).into(),
})
}
_ => None,
}
}
pub fn label_for_symbol(&self, symbol: Symbol) -> Option<CodeLabel> {
let name = &symbol.name;
return match symbol.kind {
SymbolKind::Method => {
let mut parts = name.split('#');
let container_name = parts.next()?;
let method_name = parts.next()?;
if parts.next().is_some() {
return None;
}
let filter_range = 0..name.len();
let spans = vec![
CodeLabelSpan::literal(container_name, Some("type".to_string())),
CodeLabelSpan::literal("#", None),
CodeLabelSpan::literal(method_name, Some("function.method".to_string())),
];
Some(CodeLabel {
code: name.to_string(),
spans,
filter_range: filter_range.into(),
})
}
SymbolKind::Class | SymbolKind::Module => {
let class = "class ";
let code = format!("{class}{name}");
let filter_range = 0..name.len();
let display_range = class.len()..class.len() + name.len();
Some(CodeLabel {
code,
spans: vec![CodeLabelSpan::code_range(display_range)],
filter_range: filter_range.into(),
})
}
SymbolKind::Constant => {
let code = name.to_uppercase().to_string();
let filter_range = 0..name.len();
let display_range = 0..name.len();
Some(CodeLabel {
code,
spans: vec![CodeLabelSpan::code_range(display_range)],
filter_range: filter_range.into(),
})
}
_ => None,
};
}
}