Incomplete refactor to allow for multiple adapters per language

This commit is contained in:
Julia 2023-04-07 11:46:05 -04:00 committed by Max Brunsfeld
parent 21e39e7523
commit ba7233f265
10 changed files with 818 additions and 577 deletions

View File

@ -2764,6 +2764,15 @@ impl MultiBufferSnapshot {
.and_then(|(buffer, offset)| buffer.language_scope_at(offset))
}
pub fn language_indent_size_at<T: ToOffset>(
&self,
position: T,
cx: &AppContext,
) -> Option<IndentSize> {
let (buffer_snapshot, offset) = self.point_to_buffer_offset(position)?;
Some(buffer_snapshot.language_indent_size_at(offset, cx))
}
pub fn is_dirty(&self) -> bool {
self.is_dirty
}

View File

@ -156,6 +156,7 @@ pub struct Completion {
#[derive(Clone, Debug)]
pub struct CodeAction {
pub server_id: usize,
pub range: Range<Anchor>,
pub lsp_action: lsp::CodeAction,
}

View File

@ -414,7 +414,7 @@ pub struct BracketPair {
pub struct Language {
pub(crate) config: LanguageConfig,
pub(crate) grammar: Option<Arc<Grammar>>,
pub(crate) adapter: Option<Arc<CachedLspAdapter>>,
pub(crate) adapters: Vec<Arc<CachedLspAdapter>>,
#[cfg(any(test, feature = "test-support"))]
fake_adapter: Option<(
@ -492,7 +492,7 @@ struct AvailableLanguage {
path: &'static str,
config: LanguageConfig,
grammar: tree_sitter::Language,
lsp_adapter: Option<Arc<dyn LspAdapter>>,
lsp_adapters: Vec<Arc<dyn LspAdapter>>,
get_queries: fn(&str) -> LanguageQueries,
}
@ -513,6 +513,7 @@ pub struct LanguageRegistry {
}
struct LanguageRegistryState {
next_language_server_id: usize,
languages: Vec<Arc<Language>>,
available_languages: Vec<AvailableLanguage>,
next_available_language_id: AvailableLanguageId,
@ -522,11 +523,17 @@ struct LanguageRegistryState {
version: usize,
}
pub struct PendingLanguageServer {
pub server_id: usize,
pub task: Task<Result<lsp::LanguageServer>>,
}
impl LanguageRegistry {
pub fn new(login_shell_env_loaded: Task<()>) -> Self {
let (lsp_binary_statuses_tx, lsp_binary_statuses_rx) = async_broadcast::broadcast(16);
Self {
state: RwLock::new(LanguageRegistryState {
next_language_server_id: 0,
languages: vec![PLAIN_TEXT.clone()],
available_languages: Default::default(),
next_available_language_id: 0,
@ -558,7 +565,7 @@ impl LanguageRegistry {
path: &'static str,
config: LanguageConfig,
grammar: tree_sitter::Language,
lsp_adapter: Option<Arc<dyn LspAdapter>>,
lsp_adapters: Vec<Arc<dyn LspAdapter>>,
get_queries: fn(&str) -> LanguageQueries,
) {
let state = &mut *self.state.write();
@ -567,7 +574,7 @@ impl LanguageRegistry {
path,
config,
grammar,
lsp_adapter,
lsp_adapters,
get_queries,
});
}
@ -590,12 +597,13 @@ impl LanguageRegistry {
state
.available_languages
.iter()
.filter_map(|l| l.lsp_adapter.clone())
.flat_map(|l| l.lsp_adapters.clone())
.chain(
state
.languages
.iter()
.filter_map(|l| l.adapter.as_ref().map(|a| a.adapter.clone())),
.flat_map(|language| &language.adapters)
.map(|adapter| adapter.adapter.clone()),
)
.collect::<Vec<_>>()
};
@ -721,7 +729,7 @@ impl LanguageRegistry {
let queries = (language.get_queries)(&language.path);
let language =
Language::new(language.config, Some(language.grammar))
.with_lsp_adapter(language.lsp_adapter)
.with_lsp_adapters(language.lsp_adapters)
.await;
let name = language.name();
match language.with_queries(queries) {
@ -774,18 +782,16 @@ impl LanguageRegistry {
self.state.read().languages.iter().cloned().collect()
}
pub fn start_language_server(
pub fn start_language_servers(
self: &Arc<Self>,
server_id: usize,
language: Arc<Language>,
root_path: Arc<Path>,
http_client: Arc<dyn HttpClient>,
cx: &mut AppContext,
) -> Option<Task<Result<lsp::LanguageServer>>> {
) -> Vec<PendingLanguageServer> {
#[cfg(any(test, feature = "test-support"))]
if language.fake_adapter.is_some() {
let language = language;
return Some(cx.spawn(|cx| async move {
let task = cx.spawn(|cx| async move {
let (servers_tx, fake_adapter) = language.fake_adapter.as_ref().unwrap();
let (server, mut fake_server) = lsp::LanguageServer::fake(
fake_adapter.name.to_string(),
@ -810,53 +816,71 @@ impl LanguageRegistry {
})
.detach();
Ok(server)
}));
});
return vec![PendingLanguageServer { server_id: 0, task }];
}
let download_dir = self
.language_server_download_dir
.clone()
.ok_or_else(|| anyhow!("language server download directory has not been assigned"))
.log_err()?;
.log_err();
let download_dir = match download_dir {
Some(download_dir) => download_dir,
None => return Vec::new(),
};
let this = self.clone();
let adapter = language.adapter.clone()?;
let lsp_binary_statuses = self.lsp_binary_statuses_tx.clone();
let login_shell_env_loaded = self.login_shell_env_loaded.clone();
let mut results = Vec::new();
Some(cx.spawn(|cx| async move {
login_shell_env_loaded.await;
for adapter in &language.adapters {
let this = self.clone();
let language = language.clone();
let http_client = http_client.clone();
let download_dir = download_dir.clone();
let root_path = root_path.clone();
let adapter = adapter.clone();
let lsp_binary_statuses = self.lsp_binary_statuses_tx.clone();
let login_shell_env_loaded = self.login_shell_env_loaded.clone();
let server_id = post_inc(&mut self.state.write().next_language_server_id);
let mut lock = this.lsp_binary_paths.lock();
let entry = lock
.entry(adapter.name.clone())
.or_insert_with(|| {
get_binary(
adapter.clone(),
language.clone(),
http_client,
download_dir,
lsp_binary_statuses,
)
.map_err(Arc::new)
.boxed()
.shared()
})
.clone();
drop(lock);
let binary = entry.clone().map_err(|e| anyhow!(e)).await?;
let task = cx.spawn(|cx| async move {
login_shell_env_loaded.await;
let server = lsp::LanguageServer::new(
server_id,
&binary.path,
&binary.arguments,
&root_path,
adapter.code_action_kinds(),
cx,
)?;
let mut lock = this.lsp_binary_paths.lock();
let entry = lock
.entry(adapter.name.clone())
.or_insert_with(|| {
get_binary(
adapter.clone(),
language.clone(),
http_client,
download_dir,
lsp_binary_statuses,
)
.map_err(Arc::new)
.boxed()
.shared()
})
.clone();
drop(lock);
let binary = entry.clone().map_err(|e| anyhow!(e)).await?;
Ok(server)
}))
let server = lsp::LanguageServer::new(
server_id,
&binary.path,
&binary.arguments,
&root_path,
adapter.code_action_kinds(),
cx,
)?;
Ok(server)
});
results.push(PendingLanguageServer { server_id, task });
}
results
}
pub fn language_server_binary_statuses(
@ -974,15 +998,15 @@ impl Language {
highlight_map: Default::default(),
})
}),
adapter: None,
adapters: Vec::new(),
#[cfg(any(test, feature = "test-support"))]
fake_adapter: None,
}
}
pub fn lsp_adapter(&self) -> Option<Arc<CachedLspAdapter>> {
self.adapter.clone()
pub fn lsp_adapters(&self) -> &[Arc<CachedLspAdapter>] {
&self.adapters
}
pub fn id(&self) -> Option<usize> {
@ -1209,9 +1233,9 @@ impl Language {
Arc::get_mut(self.grammar.as_mut().unwrap()).unwrap()
}
pub async fn with_lsp_adapter(mut self, lsp_adapter: Option<Arc<dyn LspAdapter>>) -> Self {
if let Some(adapter) = lsp_adapter {
self.adapter = Some(CachedLspAdapter::new(adapter).await);
pub async fn with_lsp_adapters(mut self, lsp_adapters: Vec<Arc<dyn LspAdapter>>) -> Self {
for adapter in lsp_adapters {
self.adapters.push(CachedLspAdapter::new(adapter).await);
}
self
}
@ -1224,7 +1248,7 @@ impl Language {
let (servers_tx, servers_rx) = mpsc::unbounded();
self.fake_adapter = Some((servers_tx, fake_lsp_adapter.clone()));
let adapter = CachedLspAdapter::new(Arc::new(fake_lsp_adapter)).await;
self.adapter = Some(adapter);
self.adapters = vec![adapter];
servers_rx
}
@ -1233,28 +1257,31 @@ impl Language {
}
pub async fn disk_based_diagnostic_sources(&self) -> &[String] {
match self.adapter.as_ref() {
match self.adapters.first().as_ref() {
Some(adapter) => &adapter.disk_based_diagnostic_sources,
None => &[],
}
}
pub async fn disk_based_diagnostics_progress_token(&self) -> Option<&str> {
if let Some(adapter) = self.adapter.as_ref() {
adapter.disk_based_diagnostics_progress_token.as_deref()
} else {
None
for adapter in &self.adapters {
let token = adapter.disk_based_diagnostics_progress_token.as_deref();
if token.is_some() {
return token;
}
}
None
}
pub async fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams) {
if let Some(processor) = self.adapter.as_ref() {
processor.process_diagnostics(diagnostics).await;
for adapter in &self.adapters {
adapter.process_diagnostics(diagnostics).await;
}
}
pub async fn process_completion(self: &Arc<Self>, completion: &mut lsp::CompletionItem) {
if let Some(adapter) = self.adapter.as_ref() {
for adapter in &self.adapters {
adapter.process_completion(completion).await;
}
}
@ -1263,7 +1290,8 @@ impl Language {
self: &Arc<Self>,
completion: &lsp::CompletionItem,
) -> Option<CodeLabel> {
self.adapter
self.adapters
.first()
.as_ref()?
.label_for_completion(completion, self)
.await
@ -1274,7 +1302,8 @@ impl Language {
name: &str,
kind: lsp::SymbolKind,
) -> Option<CodeLabel> {
self.adapter
self.adapters
.first()
.as_ref()?
.label_for_symbol(name, kind, self)
.await
@ -1595,7 +1624,7 @@ mod tests {
..Default::default()
},
tree_sitter_json::language(),
None,
vec![],
|_| Default::default(),
);
languages.register(
@ -1606,7 +1635,7 @@ mod tests {
..Default::default()
},
tree_sitter_rust::language(),
None,
vec![],
|_| Default::default(),
);
assert_eq!(

View File

@ -462,6 +462,7 @@ pub async fn deserialize_completion(
pub fn serialize_code_action(action: &CodeAction) -> proto::CodeAction {
proto::CodeAction {
server_id: action.server_id as u64,
start: Some(serialize_anchor(&action.range.start)),
end: Some(serialize_anchor(&action.range.end)),
lsp_action: serde_json::to_vec(&action.lsp_action).unwrap(),
@ -479,6 +480,7 @@ pub fn deserialize_code_action(action: proto::CodeAction) -> Result<CodeAction>
.ok_or_else(|| anyhow!("invalid end"))?;
let lsp_action = serde_json::from_slice(&action.lsp_action)?;
Ok(CodeAction {
server_id: action.server_id as usize,
range: start..end,
lsp_action,
})

View File

@ -33,21 +33,25 @@ pub(crate) trait LspCommand: 'static + Sized {
language_server: &Arc<LanguageServer>,
cx: &AppContext,
) -> <Self::LspRequest as lsp::request::Request>::Params;
async fn response_from_lsp(
self,
message: <Self::LspRequest as lsp::request::Request>::Result,
project: ModelHandle<Project>,
buffer: ModelHandle<Buffer>,
server_id: usize,
cx: AsyncAppContext,
) -> Result<Self::Response>;
fn to_proto(&self, project_id: u64, buffer: &Buffer) -> Self::ProtoRequest;
async fn from_proto(
message: Self::ProtoRequest,
project: ModelHandle<Project>,
buffer: ModelHandle<Buffer>,
cx: AsyncAppContext,
) -> Result<Self>;
fn response_to_proto(
response: Self::Response,
project: &mut Project,
@ -55,6 +59,7 @@ pub(crate) trait LspCommand: 'static + Sized {
buffer_version: &clock::Global,
cx: &mut AppContext,
) -> <Self::ProtoRequest as proto::RequestMessage>::Response;
async fn response_from_proto(
self,
message: <Self::ProtoRequest as proto::RequestMessage>::Response,
@ -62,6 +67,7 @@ pub(crate) trait LspCommand: 'static + Sized {
buffer: ModelHandle<Buffer>,
cx: AsyncAppContext,
) -> Result<Self::Response>;
fn buffer_id_from_proto(message: &Self::ProtoRequest) -> u64;
}
@ -137,6 +143,7 @@ impl LspCommand for PrepareRename {
message: Option<lsp::PrepareRenameResponse>,
_: ModelHandle<Project>,
buffer: ModelHandle<Buffer>,
_: usize,
cx: AsyncAppContext,
) -> Result<Option<Range<Anchor>>> {
buffer.read_with(&cx, |buffer, _| {
@ -263,10 +270,12 @@ impl LspCommand for PerformRename {
message: Option<lsp::WorkspaceEdit>,
project: ModelHandle<Project>,
buffer: ModelHandle<Buffer>,
server_id: usize,
mut cx: AsyncAppContext,
) -> Result<ProjectTransaction> {
if let Some(edit) = message {
let (lsp_adapter, lsp_server) = language_server_for_buffer(&project, &buffer, &mut cx)?;
let (lsp_adapter, lsp_server) =
language_server_for_buffer(&project, &buffer, server_id, &mut cx)?;
Project::deserialize_workspace_edit(
project,
edit,
@ -380,9 +389,10 @@ impl LspCommand for GetDefinition {
message: Option<lsp::GotoDefinitionResponse>,
project: ModelHandle<Project>,
buffer: ModelHandle<Buffer>,
server_id: usize,
cx: AsyncAppContext,
) -> Result<Vec<LocationLink>> {
location_links_from_lsp(message, project, buffer, cx).await
location_links_from_lsp(message, project, buffer, server_id, cx).await
}
fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::GetDefinition {
@ -472,9 +482,10 @@ impl LspCommand for GetTypeDefinition {
message: Option<lsp::GotoTypeDefinitionResponse>,
project: ModelHandle<Project>,
buffer: ModelHandle<Buffer>,
server_id: usize,
cx: AsyncAppContext,
) -> Result<Vec<LocationLink>> {
location_links_from_lsp(message, project, buffer, cx).await
location_links_from_lsp(message, project, buffer, server_id, cx).await
}
fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::GetTypeDefinition {
@ -537,12 +548,13 @@ impl LspCommand for GetTypeDefinition {
fn language_server_for_buffer(
project: &ModelHandle<Project>,
buffer: &ModelHandle<Buffer>,
server_id: usize,
cx: &mut AsyncAppContext,
) -> Result<(Arc<CachedLspAdapter>, Arc<LanguageServer>)> {
project
.read_with(cx, |project, cx| {
project
.language_server_for_buffer(buffer.read(cx), cx)
.language_server_for_buffer(buffer.read(cx), server_id, cx)
.map(|(adapter, server)| (adapter.clone(), server.clone()))
})
.ok_or_else(|| anyhow!("no language server found for buffer"))
@ -614,6 +626,7 @@ async fn location_links_from_lsp(
message: Option<lsp::GotoDefinitionResponse>,
project: ModelHandle<Project>,
buffer: ModelHandle<Buffer>,
server_id: usize,
mut cx: AsyncAppContext,
) -> Result<Vec<LocationLink>> {
let message = match message {
@ -642,7 +655,8 @@ async fn location_links_from_lsp(
}
}
let (lsp_adapter, language_server) = language_server_for_buffer(&project, &buffer, &mut cx)?;
let (lsp_adapter, language_server) =
language_server_for_buffer(&project, &buffer, server_id, &mut cx)?;
let mut definitions = Vec::new();
for (origin_range, target_uri, target_range) in unresolved_links {
let target_buffer_handle = project
@ -756,11 +770,12 @@ impl LspCommand for GetReferences {
locations: Option<Vec<lsp::Location>>,
project: ModelHandle<Project>,
buffer: ModelHandle<Buffer>,
server_id: usize,
mut cx: AsyncAppContext,
) -> Result<Vec<Location>> {
let mut references = Vec::new();
let (lsp_adapter, language_server) =
language_server_for_buffer(&project, &buffer, &mut cx)?;
language_server_for_buffer(&project, &buffer, server_id, &mut cx)?;
if let Some(locations) = locations {
for lsp_location in locations {
@ -917,6 +932,7 @@ impl LspCommand for GetDocumentHighlights {
lsp_highlights: Option<Vec<lsp::DocumentHighlight>>,
_: ModelHandle<Project>,
buffer: ModelHandle<Buffer>,
_: usize,
cx: AsyncAppContext,
) -> Result<Vec<DocumentHighlight>> {
buffer.read_with(&cx, |buffer, _| {
@ -1062,6 +1078,7 @@ impl LspCommand for GetHover {
message: Option<lsp::Hover>,
_: ModelHandle<Project>,
buffer: ModelHandle<Buffer>,
_: usize,
cx: AsyncAppContext,
) -> Result<Self::Response> {
Ok(message.and_then(|hover| {
@ -1283,6 +1300,7 @@ impl LspCommand for GetCompletions {
completions: Option<lsp::CompletionResponse>,
_: ModelHandle<Project>,
buffer: ModelHandle<Buffer>,
_: usize,
cx: AsyncAppContext,
) -> Result<Vec<Completion>> {
let completions = if let Some(completions) = completions {
@ -1502,6 +1520,7 @@ impl LspCommand for GetCodeActions {
actions: Option<lsp::CodeActionResponse>,
_: ModelHandle<Project>,
_: ModelHandle<Buffer>,
server_id: usize,
_: AsyncAppContext,
) -> Result<Vec<CodeAction>> {
Ok(actions
@ -1510,6 +1529,7 @@ impl LspCommand for GetCodeActions {
.filter_map(|entry| {
if let lsp::CodeActionOrCommand::CodeAction(lsp_action) = entry {
Some(CodeAction {
server_id,
range: self.range.clone(),
lsp_action,
})

File diff suppressed because it is too large Load Diff

View File

@ -1573,6 +1573,7 @@ async fn test_edits_from_lsp_with_past_version(cx: &mut gpui::TestAppContext) {
new_text: "".into(),
},
],
0,
Some(lsp_document_version),
cx,
)
@ -1667,6 +1668,7 @@ async fn test_edits_from_lsp_with_edits_on_adjacent_lines(cx: &mut gpui::TestApp
new_text: "".into(),
},
],
0,
None,
cx,
)
@ -1770,6 +1772,7 @@ async fn test_invalid_edits_from_lsp(cx: &mut gpui::TestAppContext) {
.unindent(),
},
],
0,
None,
cx,
)
@ -2258,7 +2261,7 @@ async fn test_save_as(cx: &mut gpui::TestAppContext) {
..Default::default()
},
tree_sitter_rust::language(),
None,
vec![],
|_| Default::default(),
);

View File

@ -684,9 +684,10 @@ message SearchProjectResponse {
}
message CodeAction {
Anchor start = 1;
Anchor end = 2;
bytes lsp_action = 3;
uint64 server_id = 1;
Anchor start = 2;
Anchor end = 3;
bytes lsp_action = 4;
}
message ProjectTransaction {

View File

@ -37,121 +37,107 @@ pub fn init(
themes: Arc<ThemeRegistry>,
node_runtime: Arc<NodeRuntime>,
) {
for (name, grammar, lsp_adapter) in [
fn adapter_arc(adapter: impl LspAdapter) -> Arc<dyn LspAdapter> {
Arc::new(adapter)
}
let languages_list = [
(
"c",
tree_sitter_c::language(),
Some(Arc::new(c::CLspAdapter) as Arc<dyn LspAdapter>),
vec![adapter_arc(c::CLspAdapter)],
),
(
"cpp",
tree_sitter_cpp::language(),
Some(Arc::new(c::CLspAdapter)),
),
(
"css",
tree_sitter_css::language(),
None, //
vec![adapter_arc(c::CLspAdapter)],
),
("css", tree_sitter_css::language(), vec![]),
(
"elixir",
tree_sitter_elixir::language(),
Some(Arc::new(elixir::ElixirLspAdapter)),
vec![adapter_arc(elixir::ElixirLspAdapter)],
),
(
"go",
tree_sitter_go::language(),
Some(Arc::new(go::GoLspAdapter)),
vec![adapter_arc(go::GoLspAdapter)],
),
(
"json",
tree_sitter_json::language(),
Some(Arc::new(json::JsonLspAdapter::new(
vec![adapter_arc(json::JsonLspAdapter::new(
node_runtime.clone(),
languages.clone(),
themes.clone(),
))),
),
(
"markdown",
tree_sitter_markdown::language(),
None, //
))],
),
("markdown", tree_sitter_markdown::language(), vec![]),
(
"python",
tree_sitter_python::language(),
Some(Arc::new(python::PythonLspAdapter::new(
vec![adapter_arc(python::PythonLspAdapter::new(
node_runtime.clone(),
))),
))],
),
(
"rust",
tree_sitter_rust::language(),
Some(Arc::new(rust::RustLspAdapter)),
),
(
"toml",
tree_sitter_toml::language(),
None, //
vec![adapter_arc(rust::RustLspAdapter)],
),
("toml", tree_sitter_toml::language(), vec![]),
(
"tsx",
tree_sitter_typescript::language_tsx(),
Some(Arc::new(typescript::TypeScriptLspAdapter::new(
vec![adapter_arc(typescript::TypeScriptLspAdapter::new(
node_runtime.clone(),
))),
))],
),
(
"typescript",
tree_sitter_typescript::language_typescript(),
Some(Arc::new(typescript::TypeScriptLspAdapter::new(
vec![adapter_arc(typescript::TypeScriptLspAdapter::new(
node_runtime.clone(),
))),
))],
),
(
"javascript",
tree_sitter_typescript::language_tsx(),
Some(Arc::new(typescript::TypeScriptLspAdapter::new(
vec![adapter_arc(typescript::TypeScriptLspAdapter::new(
node_runtime.clone(),
))),
))],
),
(
"html",
tree_sitter_html::language(),
Some(Arc::new(html::HtmlLspAdapter::new(node_runtime.clone()))),
vec![adapter_arc(html::HtmlLspAdapter::new(node_runtime.clone()))],
),
(
"ruby",
tree_sitter_ruby::language(),
Some(Arc::new(ruby::RubyLanguageServer)),
vec![adapter_arc(ruby::RubyLanguageServer)],
),
(
"erb",
tree_sitter_embedded_template::language(),
Some(Arc::new(ruby::RubyLanguageServer)),
),
(
"scheme",
tree_sitter_scheme::language(),
None, //
),
(
"racket",
tree_sitter_racket::language(),
None, //
vec![adapter_arc(ruby::RubyLanguageServer)],
),
("scheme", tree_sitter_scheme::language(), vec![]),
("racket", tree_sitter_racket::language(), vec![]),
(
"lua",
tree_sitter_lua::language(),
Some(Arc::new(lua::LuaLspAdapter)),
vec![adapter_arc(lua::LuaLspAdapter)],
),
(
"yaml",
tree_sitter_yaml::language(),
Some(Arc::new(yaml::YamlLspAdapter::new(node_runtime.clone()))),
vec![adapter_arc(yaml::YamlLspAdapter::new(node_runtime.clone()))],
),
] {
languages.register(name, load_config(name), grammar, lsp_adapter, load_queries);
];
for (name, grammar, lsp_adapters) in languages_list {
languages.register(name, load_config(name), grammar, lsp_adapters, load_queries);
}
}
@ -163,7 +149,7 @@ pub async fn language(
) -> Arc<Language> {
Arc::new(
Language::new(load_config(name), Some(grammar))
.with_lsp_adapter(lsp_adapter)
.with_lsp_adapters(lsp_adapter)
.await
.with_queries(load_queries(name))
.unwrap(),

View File

@ -37,7 +37,7 @@ impl TypeScriptLspAdapter {
}
}
struct Versions {
struct TypeScriptVersions {
typescript_version: String,
server_version: String,
}
@ -52,7 +52,8 @@ impl LspAdapter for TypeScriptLspAdapter {
&self,
_: Arc<dyn HttpClient>,
) -> Result<Box<dyn 'static + Send + Any>> {
Ok(Box::new(Versions {
dbg!();
Ok(Box::new(TypeScriptVersions {
typescript_version: self.node.npm_package_latest_version("typescript").await?,
server_version: self
.node
@ -67,7 +68,8 @@ impl LspAdapter for TypeScriptLspAdapter {
_: Arc<dyn HttpClient>,
container_dir: PathBuf,
) -> Result<LanguageServerBinary> {
let versions = versions.downcast::<Versions>().unwrap();
dbg!();
let versions = versions.downcast::<TypeScriptVersions>().unwrap();
let server_path = container_dir.join(Self::NEW_SERVER_PATH);
if fs::metadata(&server_path).await.is_err() {
@ -92,18 +94,10 @@ impl LspAdapter for TypeScriptLspAdapter {
}
async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<LanguageServerBinary> {
dbg!();
(|| async move {
let mut last_version_dir = None;
let mut entries = fs::read_dir(&container_dir).await?;
while let Some(entry) = entries.next().await {
let entry = entry?;
if entry.file_type().await?.is_dir() {
last_version_dir = Some(entry.path());
}
}
let last_version_dir = last_version_dir.ok_or_else(|| anyhow!("no cached binary"))?;
let old_server_path = last_version_dir.join(Self::OLD_SERVER_PATH);
let new_server_path = last_version_dir.join(Self::NEW_SERVER_PATH);
let old_server_path = container_dir.join(Self::OLD_SERVER_PATH);
let new_server_path = container_dir.join(Self::NEW_SERVER_PATH);
if new_server_path.exists() {
Ok(LanguageServerBinary {
path: self.node.binary_path().await?,
@ -117,7 +111,7 @@ impl LspAdapter for TypeScriptLspAdapter {
} else {
Err(anyhow!(
"missing executable in directory {:?}",
last_version_dir
container_dir
))
}
})()
@ -170,6 +164,86 @@ impl LspAdapter for TypeScriptLspAdapter {
}
}
pub struct EsLintLspAdapter {
node: Arc<NodeRuntime>,
}
impl EsLintLspAdapter {
const SERVER_PATH: &'static str = "node_modules/typescript-language-server/lib/cli.mjs";
pub fn new(node: Arc<NodeRuntime>) -> Self {
EsLintLspAdapter { node }
}
}
#[async_trait]
impl LspAdapter for EsLintLspAdapter {
async fn name(&self) -> LanguageServerName {
LanguageServerName("eslint".into())
}
async fn fetch_latest_server_version(
&self,
_: Arc<dyn HttpClient>,
) -> Result<Box<dyn 'static + Send + Any>> {
Ok(Box::new(
self.node.npm_package_latest_version("eslint").await?,
))
}
async fn fetch_server_binary(
&self,
versions: Box<dyn 'static + Send + Any>,
_: Arc<dyn HttpClient>,
container_dir: PathBuf,
) -> Result<LanguageServerBinary> {
let version = versions.downcast::<String>().unwrap();
let server_path = container_dir.join(Self::SERVER_PATH);
if fs::metadata(&server_path).await.is_err() {
self.node
.npm_install_packages([("eslint", version.as_str())], &container_dir)
.await?;
}
Ok(LanguageServerBinary {
path: self.node.binary_path().await?,
arguments: server_binary_arguments(&server_path),
})
}
async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<LanguageServerBinary> {
(|| async move {
let server_path = container_dir.join(Self::SERVER_PATH);
if server_path.exists() {
Ok(LanguageServerBinary {
path: self.node.binary_path().await?,
arguments: server_binary_arguments(&server_path),
})
} else {
Err(anyhow!(
"missing executable in directory {:?}",
container_dir
))
}
})()
.await
.log_err()
}
async fn label_for_completion(
&self,
item: &lsp::CompletionItem,
language: &Arc<language::Language>,
) -> Option<language::CodeLabel> {
None
}
async fn initialization_options(&self) -> Option<serde_json::Value> {
None
}
}
#[cfg(test)]
mod tests {
use gpui::TestAppContext;