Use anyhow
more idiomatically (#31052)
https://github.com/zed-industries/zed/issues/30972 brought up another case where our context is not enough to track the actual source of the issue: we get a general top-level error without inner error. The reason for this was `.ok_or_else(|| anyhow!("failed to read HEAD SHA"))?; ` on the top level. The PR finally reworks the way we use anyhow to reduce such issues (or at least make it simpler to bubble them up later in a fix). On top of that, uses a few more anyhow methods for better readability. * `.ok_or_else(|| anyhow!("..."))`, `map_err` and other similar error conversion/option reporting cases are replaced with `context` and `with_context` calls * in addition to that, various `anyhow!("failed to do ...")` are stripped with `.context("Doing ...")` messages instead to remove the parasitic `failed to` text * `anyhow::ensure!` is used instead of `if ... { return Err(...); }` calls * `anyhow::bail!` is used instead of `return Err(anyhow!(...));` Release Notes: - N/A
This commit is contained in:
parent
1e51a7ac44
commit
16366cf9f2
294 changed files with 2037 additions and 2610 deletions
|
@ -24,7 +24,7 @@ pub mod buffer_tests;
|
|||
|
||||
pub use crate::language_settings::EditPredictionsMode;
|
||||
use crate::language_settings::SoftWrap;
|
||||
use anyhow::{Context as _, Result, anyhow};
|
||||
use anyhow::{Context as _, Result};
|
||||
use async_trait::async_trait;
|
||||
use collections::{HashMap, HashSet, IndexSet};
|
||||
use fs::Fs;
|
||||
|
@ -368,9 +368,7 @@ pub trait LspAdapter: 'static + Send + Sync {
|
|||
}
|
||||
}
|
||||
|
||||
if !binary_options.allow_binary_download {
|
||||
return Err(anyhow!("downloading language servers disabled"));
|
||||
}
|
||||
anyhow::ensure!(binary_options.allow_binary_download, "downloading language servers disabled");
|
||||
|
||||
if let Some(cached_binary) = cached_binary.as_ref() {
|
||||
return Ok(cached_binary.clone());
|
||||
|
@ -1296,17 +1294,13 @@ impl Language {
|
|||
}
|
||||
|
||||
pub fn with_highlights_query(mut self, source: &str) -> Result<Self> {
|
||||
let grammar = self
|
||||
.grammar_mut()
|
||||
.ok_or_else(|| anyhow!("cannot mutate grammar"))?;
|
||||
let grammar = self.grammar_mut().context("cannot mutate grammar")?;
|
||||
grammar.highlights_query = Some(Query::new(&grammar.ts_language, source)?);
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn with_runnable_query(mut self, source: &str) -> Result<Self> {
|
||||
let grammar = self
|
||||
.grammar_mut()
|
||||
.ok_or_else(|| anyhow!("cannot mutate grammar"))?;
|
||||
let grammar = self.grammar_mut().context("cannot mutate grammar")?;
|
||||
|
||||
let query = Query::new(&grammar.ts_language, source)?;
|
||||
let mut extra_captures = Vec::with_capacity(query.capture_names().len());
|
||||
|
@ -1329,9 +1323,7 @@ impl Language {
|
|||
}
|
||||
|
||||
pub fn with_outline_query(mut self, source: &str) -> Result<Self> {
|
||||
let grammar = self
|
||||
.grammar_mut()
|
||||
.ok_or_else(|| anyhow!("cannot mutate grammar"))?;
|
||||
let grammar = self.grammar_mut().context("cannot mutate grammar")?;
|
||||
let query = Query::new(&grammar.ts_language, source)?;
|
||||
let mut item_capture_ix = None;
|
||||
let mut name_capture_ix = None;
|
||||
|
@ -1368,9 +1360,7 @@ impl Language {
|
|||
}
|
||||
|
||||
pub fn with_text_object_query(mut self, source: &str) -> Result<Self> {
|
||||
let grammar = self
|
||||
.grammar_mut()
|
||||
.ok_or_else(|| anyhow!("cannot mutate grammar"))?;
|
||||
let grammar = self.grammar_mut().context("cannot mutate grammar")?;
|
||||
let query = Query::new(&grammar.ts_language, source)?;
|
||||
|
||||
let mut text_objects_by_capture_ix = Vec::new();
|
||||
|
@ -1388,9 +1378,7 @@ impl Language {
|
|||
}
|
||||
|
||||
pub fn with_embedding_query(mut self, source: &str) -> Result<Self> {
|
||||
let grammar = self
|
||||
.grammar_mut()
|
||||
.ok_or_else(|| anyhow!("cannot mutate grammar"))?;
|
||||
let grammar = self.grammar_mut().context("cannot mutate grammar")?;
|
||||
let query = Query::new(&grammar.ts_language, source)?;
|
||||
let mut item_capture_ix = None;
|
||||
let mut name_capture_ix = None;
|
||||
|
@ -1421,9 +1409,7 @@ impl Language {
|
|||
}
|
||||
|
||||
pub fn with_brackets_query(mut self, source: &str) -> Result<Self> {
|
||||
let grammar = self
|
||||
.grammar_mut()
|
||||
.ok_or_else(|| anyhow!("cannot mutate grammar"))?;
|
||||
let grammar = self.grammar_mut().context("cannot mutate grammar")?;
|
||||
let query = Query::new(&grammar.ts_language, source)?;
|
||||
let mut open_capture_ix = None;
|
||||
let mut close_capture_ix = None;
|
||||
|
@ -1458,9 +1444,7 @@ impl Language {
|
|||
}
|
||||
|
||||
pub fn with_indents_query(mut self, source: &str) -> Result<Self> {
|
||||
let grammar = self
|
||||
.grammar_mut()
|
||||
.ok_or_else(|| anyhow!("cannot mutate grammar"))?;
|
||||
let grammar = self.grammar_mut().context("cannot mutate grammar")?;
|
||||
let query = Query::new(&grammar.ts_language, source)?;
|
||||
let mut indent_capture_ix = None;
|
||||
let mut start_capture_ix = None;
|
||||
|
@ -1488,9 +1472,7 @@ impl Language {
|
|||
}
|
||||
|
||||
pub fn with_injection_query(mut self, source: &str) -> Result<Self> {
|
||||
let grammar = self
|
||||
.grammar_mut()
|
||||
.ok_or_else(|| anyhow!("cannot mutate grammar"))?;
|
||||
let grammar = self.grammar_mut().context("cannot mutate grammar")?;
|
||||
let query = Query::new(&grammar.ts_language, source)?;
|
||||
let mut language_capture_ix = None;
|
||||
let mut injection_language_capture_ix = None;
|
||||
|
@ -1508,18 +1490,14 @@ impl Language {
|
|||
language_capture_ix = match (language_capture_ix, injection_language_capture_ix) {
|
||||
(None, Some(ix)) => Some(ix),
|
||||
(Some(_), Some(_)) => {
|
||||
return Err(anyhow!(
|
||||
"both language and injection.language captures are present"
|
||||
));
|
||||
anyhow::bail!("both language and injection.language captures are present");
|
||||
}
|
||||
_ => language_capture_ix,
|
||||
};
|
||||
content_capture_ix = match (content_capture_ix, injection_content_capture_ix) {
|
||||
(None, Some(ix)) => Some(ix),
|
||||
(Some(_), Some(_)) => {
|
||||
return Err(anyhow!(
|
||||
"both content and injection.content captures are present"
|
||||
));
|
||||
anyhow::bail!("both content and injection.content captures are present")
|
||||
}
|
||||
_ => content_capture_ix,
|
||||
};
|
||||
|
@ -1553,10 +1531,7 @@ impl Language {
|
|||
|
||||
pub fn with_override_query(mut self, source: &str) -> anyhow::Result<Self> {
|
||||
let query = {
|
||||
let grammar = self
|
||||
.grammar
|
||||
.as_ref()
|
||||
.ok_or_else(|| anyhow!("no grammar for language"))?;
|
||||
let grammar = self.grammar.as_ref().context("no grammar for language")?;
|
||||
Query::new(&grammar.ts_language, source)?
|
||||
};
|
||||
|
||||
|
@ -1607,10 +1582,10 @@ impl Language {
|
|||
.values()
|
||||
.any(|entry| entry.name == *referenced_name)
|
||||
{
|
||||
Err(anyhow!(
|
||||
anyhow::bail!(
|
||||
"language {:?} has overrides in config not in query: {referenced_name:?}",
|
||||
self.config.name
|
||||
))?;
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1633,9 +1608,7 @@ impl Language {
|
|||
|
||||
self.config.brackets.disabled_scopes_by_bracket_ix.clear();
|
||||
|
||||
let grammar = self
|
||||
.grammar_mut()
|
||||
.ok_or_else(|| anyhow!("cannot mutate grammar"))?;
|
||||
let grammar = self.grammar_mut().context("cannot mutate grammar")?;
|
||||
grammar.override_config = Some(OverrideConfig {
|
||||
query,
|
||||
values: override_configs_by_id,
|
||||
|
@ -1644,9 +1617,7 @@ impl Language {
|
|||
}
|
||||
|
||||
pub fn with_redaction_query(mut self, source: &str) -> anyhow::Result<Self> {
|
||||
let grammar = self
|
||||
.grammar_mut()
|
||||
.ok_or_else(|| anyhow!("cannot mutate grammar"))?;
|
||||
let grammar = self.grammar_mut().context("cannot mutate grammar")?;
|
||||
|
||||
let query = Query::new(&grammar.ts_language, source)?;
|
||||
let mut redaction_capture_ix = None;
|
||||
|
@ -2190,18 +2161,16 @@ pub fn point_from_lsp(point: lsp::Position) -> Unclipped<PointUtf16> {
|
|||
}
|
||||
|
||||
pub fn range_to_lsp(range: Range<PointUtf16>) -> Result<lsp::Range> {
|
||||
if range.start > range.end {
|
||||
Err(anyhow!(
|
||||
"Inverted range provided to an LSP request: {:?}-{:?}",
|
||||
range.start,
|
||||
range.end
|
||||
))
|
||||
} else {
|
||||
Ok(lsp::Range {
|
||||
start: point_to_lsp(range.start),
|
||||
end: point_to_lsp(range.end),
|
||||
})
|
||||
}
|
||||
anyhow::ensure!(
|
||||
range.start <= range.end,
|
||||
"Inverted range provided to an LSP request: {:?}-{:?}",
|
||||
range.start,
|
||||
range.end
|
||||
);
|
||||
Ok(lsp::Range {
|
||||
start: point_to_lsp(range.start),
|
||||
end: point_to_lsp(range.end),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn range_from_lsp(range: lsp::Range) -> Range<Unclipped<PointUtf16>> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue