More work on transitioning to async, need to figure out when to stop

This commit is contained in:
Isaac Clayton 2022-07-04 17:40:08 +02:00
parent feb6cf6789
commit f4b4212932
8 changed files with 109 additions and 113 deletions

View file

@ -13,15 +13,16 @@ use util::{ResultExt, TryFutureExt};
pub struct CLspAdapter;
#[async_trait]
impl super::LspAdapter for CLspAdapter {
fn name(&self) -> LanguageServerName {
async fn name(&self) -> LanguageServerName {
LanguageServerName("clangd".into())
}
fn fetch_latest_server_version(
async fn fetch_latest_server_version(
&self,
http: Arc<dyn HttpClient>,
) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
) -> Result<Box<dyn 'static + Send + Any>> {
async move {
let release = latest_github_release("clangd/clangd", http).await?;
let asset_name = format!("clangd-mac-{}.zip", release.name);
@ -39,12 +40,12 @@ impl super::LspAdapter for CLspAdapter {
.boxed()
}
fn fetch_server_binary(
async fn fetch_server_binary(
&self,
version: Box<dyn 'static + Send + Any>,
http: Arc<dyn HttpClient>,
container_dir: Arc<Path>,
) -> BoxFuture<'static, Result<PathBuf>> {
container_dir: PathBuf,
) -> Result<PathBuf> {
let version = version.downcast::<GitHubLspBinaryVersion>().unwrap();
async move {
let zip_path = container_dir.join(format!("clangd_{}.zip", version.name));
@ -92,10 +93,7 @@ impl super::LspAdapter for CLspAdapter {
.boxed()
}
fn cached_server_binary(
&self,
container_dir: Arc<Path>,
) -> BoxFuture<'static, Option<PathBuf>> {
async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf> {
async move {
let mut last_clangd_dir = None;
let mut entries = fs::read_dir(&container_dir).await?;
@ -120,7 +118,7 @@ impl super::LspAdapter for CLspAdapter {
.boxed()
}
fn label_for_completion(
async fn label_for_completion(
&self,
completion: &lsp::CompletionItem,
language: &Language,
@ -197,7 +195,7 @@ impl super::LspAdapter for CLspAdapter {
Some(CodeLabel::plain(label.to_string(), None))
}
fn label_for_symbol(
async fn label_for_symbol(
&self,
name: &str,
kind: lsp::SymbolKind,

View file

@ -22,19 +22,20 @@ lazy_static! {
static ref GOPLS_VERSION_REGEX: Regex = Regex::new(r"\d+\.\d+\.\d+").unwrap();
}
#[async_trait]
impl super::LspAdapter for GoLspAdapter {
fn name(&self) -> LanguageServerName {
async fn name(&self) -> LanguageServerName {
LanguageServerName("gopls".into())
}
fn server_args(&self) -> &[&str] {
&["-mode=stdio"]
async fn server_args(&self) -> Vec<String> {
vec!["-mode=stdio".into()]
}
fn fetch_latest_server_version(
async fn fetch_latest_server_version(
&self,
http: Arc<dyn HttpClient>,
) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
) -> Result<Box<dyn 'static + Send + Any>> {
async move {
let release = latest_github_release("golang/tools", http).await?;
let version: Option<String> = release.name.strip_prefix("gopls/v").map(str::to_string);
@ -49,12 +50,12 @@ impl super::LspAdapter for GoLspAdapter {
.boxed()
}
fn fetch_server_binary(
async fn fetch_server_binary(
&self,
version: Box<dyn 'static + Send + Any>,
_: Arc<dyn HttpClient>,
container_dir: Arc<Path>,
) -> BoxFuture<'static, Result<PathBuf>> {
container_dir: PathBuf,
) -> Result<PathBuf> {
let version = version.downcast::<Option<String>>().unwrap();
let this = *self;
@ -115,10 +116,7 @@ impl super::LspAdapter for GoLspAdapter {
.boxed()
}
fn cached_server_binary(
&self,
container_dir: Arc<Path>,
) -> BoxFuture<'static, Option<PathBuf>> {
async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf> {
async move {
let mut last_binary_path = None;
let mut entries = fs::read_dir(&container_dir).await?;
@ -144,7 +142,7 @@ impl super::LspAdapter for GoLspAdapter {
.boxed()
}
fn label_for_completion(
async fn label_for_completion(
&self,
completion: &lsp::CompletionItem,
language: &Language,
@ -244,7 +242,7 @@ impl super::LspAdapter for GoLspAdapter {
None
}
fn label_for_symbol(
async fn label_for_symbol(
&self,
name: &str,
kind: lsp::SymbolKind,

View file

@ -19,31 +19,32 @@ impl JsonLspAdapter {
"node_modules/vscode-json-languageserver/bin/vscode-json-languageserver";
}
#[async_trait]
impl LspAdapter for JsonLspAdapter {
fn name(&self) -> LanguageServerName {
async fn name(&self) -> LanguageServerName {
LanguageServerName("vscode-json-languageserver".into())
}
fn server_args(&self) -> Vec<String> {
async fn server_args(&self) -> Vec<String> {
vec!["--stdio".into()]
}
fn fetch_latest_server_version(
async fn fetch_latest_server_version(
&self,
_: Arc<dyn HttpClient>,
) -> BoxFuture<'static, Result<Box<dyn 'static + Any + Send>>> {
) -> Result<Box<dyn 'static + Any + Send>> {
async move {
Ok(Box::new(npm_package_latest_version("vscode-json-languageserver").await?) as Box<_>)
}
.boxed()
}
fn fetch_server_binary(
async fn fetch_server_binary(
&self,
version: Box<dyn 'static + Send + Any>,
_: Arc<dyn HttpClient>,
container_dir: Arc<Path>,
) -> BoxFuture<'static, Result<PathBuf>> {
container_dir: PathBuf,
) -> Result<PathBuf> {
let version = version.downcast::<String>().unwrap();
async move {
let version_dir = container_dir.join(version.as_str());
@ -76,10 +77,7 @@ impl LspAdapter for JsonLspAdapter {
.boxed()
}
fn cached_server_binary(
&self,
container_dir: Arc<Path>,
) -> BoxFuture<'static, Option<PathBuf>> {
async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf> {
async move {
let mut last_version_dir = None;
let mut entries = fs::read_dir(&container_dir).await?;
@ -104,13 +102,13 @@ impl LspAdapter for JsonLspAdapter {
.boxed()
}
fn initialization_options(&self) -> Option<serde_json::Value> {
async fn initialization_options(&self) -> Option<serde_json::Value> {
Some(json!({
"provideFormatter": true
}))
}
fn id_for_language(&self, name: &str) -> Option<String> {
async fn id_for_language(&self, name: &str) -> Option<String> {
if name == "JSON" {
Some("jsonc".into())
} else {

View file

@ -17,28 +17,29 @@ impl PythonLspAdapter {
const BIN_PATH: &'static str = "node_modules/pyright/langserver.index.js";
}
#[async_trait]
impl LspAdapter for PythonLspAdapter {
fn name(&self) -> LanguageServerName {
async fn name(&self) -> LanguageServerName {
LanguageServerName("pyright".into())
}
fn server_args(&self) -> &[&str] {
&["--stdio"]
async fn server_args(&self) -> Vec<String> {
vec!["--stdio".into()]
}
fn fetch_latest_server_version(
async fn fetch_latest_server_version(
&self,
_: Arc<dyn HttpClient>,
) -> BoxFuture<'static, Result<Box<dyn 'static + Any + Send>>> {
) -> Result<Box<dyn 'static + Any + Send>> {
async move { Ok(Box::new(npm_package_latest_version("pyright").await?) as Box<_>) }.boxed()
}
fn fetch_server_binary(
async fn fetch_server_binary(
&self,
version: Box<dyn 'static + Send + Any>,
_: Arc<dyn HttpClient>,
container_dir: Arc<Path>,
) -> BoxFuture<'static, Result<PathBuf>> {
container_dir: PathBuf,
) -> Result<PathBuf> {
let version = version.downcast::<String>().unwrap();
async move {
let version_dir = container_dir.join(version.as_str());
@ -67,10 +68,7 @@ impl LspAdapter for PythonLspAdapter {
.boxed()
}
fn cached_server_binary(
&self,
container_dir: Arc<Path>,
) -> BoxFuture<'static, Option<PathBuf>> {
async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf> {
async move {
let mut last_version_dir = None;
let mut entries = fs::read_dir(&container_dir).await?;
@ -95,7 +93,7 @@ impl LspAdapter for PythonLspAdapter {
.boxed()
}
fn label_for_completion(
async fn label_for_completion(
&self,
item: &lsp::CompletionItem,
language: &language::Language,
@ -116,7 +114,7 @@ impl LspAdapter for PythonLspAdapter {
})
}
fn label_for_symbol(
async fn label_for_symbol(
&self,
name: &str,
kind: lsp::SymbolKind,

View file

@ -20,14 +20,14 @@ use util::{ResultExt, TryFutureExt};
pub struct RustLspAdapter;
impl LspAdapter for RustLspAdapter {
fn name(&self) -> LanguageServerName {
async fn name(&self) -> LanguageServerName {
LanguageServerName("rust-analyzer".into())
}
fn fetch_latest_server_version(
async fn fetch_latest_server_version(
&self,
http: Arc<dyn HttpClient>,
) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
) -> Result<Box<dyn 'static + Send + Any>> {
async move {
let release = latest_github_release("rust-analyzer/rust-analyzer", http).await?;
let asset_name = format!("rust-analyzer-{}-apple-darwin.gz", consts::ARCH);
@ -45,12 +45,12 @@ impl LspAdapter for RustLspAdapter {
.boxed()
}
fn fetch_server_binary(
async fn fetch_server_binary(
&self,
version: Box<dyn 'static + Send + Any>,
http: Arc<dyn HttpClient>,
container_dir: Arc<Path>,
) -> BoxFuture<'static, Result<PathBuf>> {
container_dir: PathBuf,
) -> Result<PathBuf> {
async move {
let version = version.downcast::<GitHubLspBinaryVersion>().unwrap();
let destination_path = container_dir.join(format!("rust-analyzer-{}", version.name));
@ -86,9 +86,9 @@ impl LspAdapter for RustLspAdapter {
.boxed()
}
fn cached_server_binary(
async fn cached_server_binary(
&self,
container_dir: Arc<Path>,
container_dir: PathBuf,
) -> BoxFuture<'static, Option<PathBuf>> {
async move {
let mut last = None;
@ -102,15 +102,15 @@ impl LspAdapter for RustLspAdapter {
.boxed()
}
fn disk_based_diagnostic_sources(&self) -> &'static [&'static str] {
&["rustc"]
async fn disk_based_diagnostic_sources(&self) -> Vec<String> {
vec!["rustc".into()]
}
fn disk_based_diagnostics_progress_token(&self) -> Option<&'static str> {
Some("rustAnalyzer/cargo check")
async fn disk_based_diagnostics_progress_token(&self) -> Option<String> {
Some("rustAnalyzer/cargo check".into())
}
fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) {
async fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) {
lazy_static! {
static ref REGEX: Regex = Regex::new("(?m)`([^`]+)\n`$").unwrap();
}
@ -130,7 +130,7 @@ impl LspAdapter for RustLspAdapter {
}
}
fn label_for_completion(
async fn label_for_completion(
&self,
completion: &lsp::CompletionItem,
language: &Language,
@ -206,7 +206,7 @@ impl LspAdapter for RustLspAdapter {
None
}
fn label_for_symbol(
async fn label_for_symbol(
&self,
name: &str,
kind: lsp::SymbolKind,

View file

@ -23,22 +23,23 @@ struct Versions {
server_version: String,
}
#[async_trait]
impl LspAdapter for TypeScriptLspAdapter {
fn name(&self) -> LanguageServerName {
async fn name(&self) -> LanguageServerName {
LanguageServerName("typescript-language-server".into())
}
fn server_args(&self) -> Vec<String> {
async fn server_args(&self) -> Vec<String> {
["--stdio", "--tsserver-path", "node_modules/typescript/lib"]
.into_iter()
.map(str::to_string)
.collect()
}
fn fetch_latest_server_version(
async fn fetch_latest_server_version(
&self,
_: Arc<dyn HttpClient>,
) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
) -> Result<Box<dyn 'static + Send + Any>> {
async move {
Ok(Box::new(Versions {
typescript_version: npm_package_latest_version("typescript").await?,
@ -48,12 +49,12 @@ impl LspAdapter for TypeScriptLspAdapter {
.boxed()
}
fn fetch_server_binary(
async fn fetch_server_binary(
&self,
versions: Box<dyn 'static + Send + Any>,
_: Arc<dyn HttpClient>,
container_dir: Arc<Path>,
) -> BoxFuture<'static, Result<PathBuf>> {
container_dir: PathBuf,
) -> Result<PathBuf> {
let versions = versions.downcast::<Versions>().unwrap();
async move {
let version_dir = container_dir.join(&format!(
@ -95,10 +96,7 @@ impl LspAdapter for TypeScriptLspAdapter {
.boxed()
}
fn cached_server_binary(
&self,
container_dir: Arc<Path>,
) -> BoxFuture<'static, Option<PathBuf>> {
async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf> {
async move {
let mut last_version_dir = None;
let mut entries = fs::read_dir(&container_dir).await?;
@ -123,7 +121,7 @@ impl LspAdapter for TypeScriptLspAdapter {
.boxed()
}
fn label_for_completion(
async fn label_for_completion(
&self,
item: &lsp::CompletionItem,
language: &language::Language,
@ -146,7 +144,7 @@ impl LspAdapter for TypeScriptLspAdapter {
})
}
fn initialization_options(&self) -> Option<serde_json::Value> {
async fn initialization_options(&self) -> Option<serde_json::Value> {
Some(json!({
"provideFormatter": true
}))