mirror of
https://github.com/erg-lang/erg.git
synced 2025-10-03 14:04:33 +00:00
chore: eliminate unwrap
s
This commit is contained in:
parent
f5a21cac8a
commit
d9e4dbe716
16 changed files with 65 additions and 26 deletions
|
@ -208,7 +208,10 @@ macro_rules! impl_sendable {
|
||||||
) -> Result<(), mpsc::SendError<WorkerMessage<$Params>>> {
|
) -> Result<(), mpsc::SendError<WorkerMessage<$Params>>> {
|
||||||
self.channels
|
self.channels
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.unwrap()
|
.ok_or_else(|| {
|
||||||
|
erg_common::lsp_log!("channels are closed");
|
||||||
|
mpsc::SendError(WorkerMessage::Kill)
|
||||||
|
})?
|
||||||
.$receiver
|
.$receiver
|
||||||
.send($crate::channels::WorkerMessage::Request(id, params))
|
.send($crate::channels::WorkerMessage::Request(id, params))
|
||||||
}
|
}
|
||||||
|
|
|
@ -416,12 +416,12 @@ impl CompletionCache {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, namespace: &str) -> Option<MappedRwLockReadGuard<Vec<CompletionItem>>> {
|
pub fn get(&self, namespace: &str) -> Option<MappedRwLockReadGuard<Vec<CompletionItem>>> {
|
||||||
if self.cache.borrow().get(namespace).is_none() {
|
if self.cache.borrow().get(namespace).is_some() {
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(RwLockReadGuard::map(self.cache.borrow(), |cache| {
|
Some(RwLockReadGuard::map(self.cache.borrow(), |cache| {
|
||||||
cache.get(namespace).unwrap()
|
cache.get(namespace).unwrap()
|
||||||
}))
|
}))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -73,7 +73,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if let Expr::Accessor(acc) = def.body.block.last().unwrap() {
|
} else if let Some(Expr::Accessor(acc)) = def.body.block.last() {
|
||||||
let vi = acc.var_info();
|
let vi = acc.var_info();
|
||||||
match (&vi.def_loc.module, util::loc_to_range(vi.def_loc.loc)) {
|
match (&vi.def_loc.module, util::loc_to_range(vi.def_loc.loc)) {
|
||||||
(Some(path), Some(range)) => {
|
(Some(path), Some(range)) => {
|
||||||
|
|
|
@ -160,7 +160,10 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let ast = self.build_ast(&uri).ok();
|
let ast = self.build_ast(&uri).ok();
|
||||||
let ctx = checker.pop_context().unwrap();
|
let Some(ctx) = checker.pop_context() else {
|
||||||
|
_log!(self, "context not found");
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
if mode == "declare" {
|
if mode == "declare" {
|
||||||
self.shared
|
self.shared
|
||||||
.py_mod_cache
|
.py_mod_cache
|
||||||
|
|
|
@ -318,7 +318,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
||||||
) -> ELSResult<InlayHint> {
|
) -> ELSResult<InlayHint> {
|
||||||
self.send_log(format!("inlay hint resolve request: {hint:?}"))?;
|
self.send_log(format!("inlay hint resolve request: {hint:?}"))?;
|
||||||
if let Some(data) = &hint.data {
|
if let Some(data) = &hint.data {
|
||||||
let Ok(uri) = data.as_str().unwrap().parse::<NormalizedUrl>() else {
|
let Some(uri) = data.as_str().and_then(|s| s.parse::<NormalizedUrl>().ok()) else {
|
||||||
return Ok(hint);
|
return Ok(hint);
|
||||||
};
|
};
|
||||||
if let Some(module) = self.get_mod_ctx(&uri) {
|
if let Some(module) = self.get_mod_ctx(&uri) {
|
||||||
|
|
|
@ -851,7 +851,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
||||||
HEALTH_CHECKER_ID => {
|
HEALTH_CHECKER_ID => {
|
||||||
self.channels
|
self.channels
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.unwrap()
|
.ok_or("channels not found")?
|
||||||
.health_check
|
.health_check
|
||||||
.send(WorkerMessage::Request(0, ()))?;
|
.send(WorkerMessage::Request(0, ()))?;
|
||||||
}
|
}
|
||||||
|
@ -982,7 +982,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
||||||
let mut ctxs = vec![];
|
let mut ctxs = vec![];
|
||||||
if let Ok(dir) = uri
|
if let Ok(dir) = uri
|
||||||
.to_file_path()
|
.to_file_path()
|
||||||
.and_then(|p| p.parent().unwrap().read_dir().map_err(|_| ()))
|
.and_then(|p| p.parent().ok_or(())?.read_dir().map_err(|_| ()))
|
||||||
{
|
{
|
||||||
for neighbor in dir {
|
for neighbor in dir {
|
||||||
let Ok(neighbor) = neighbor else {
|
let Ok(neighbor) = neighbor else {
|
||||||
|
|
|
@ -51,7 +51,7 @@ where
|
||||||
let most_similar_name =
|
let most_similar_name =
|
||||||
candidates.min_by_key(|v| levenshtein(v.borrow(), name, limit).unwrap_or(usize::MAX))?;
|
candidates.min_by_key(|v| levenshtein(v.borrow(), name, limit).unwrap_or(usize::MAX))?;
|
||||||
let dist = levenshtein(most_similar_name.borrow(), name, limit);
|
let dist = levenshtein(most_similar_name.borrow(), name, limit);
|
||||||
if dist.is_none() || dist.unwrap() >= limit {
|
if dist.map_or(true, |d| d >= limit) {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(most_similar_name)
|
Some(most_similar_name)
|
||||||
|
@ -69,7 +69,7 @@ where
|
||||||
let most_similar_name_and_some = candidates
|
let most_similar_name_and_some = candidates
|
||||||
.min_by_key(|(_, v)| levenshtein(v.borrow(), name, limit).unwrap_or(usize::MAX))?;
|
.min_by_key(|(_, v)| levenshtein(v.borrow(), name, limit).unwrap_or(usize::MAX))?;
|
||||||
let dist = levenshtein(most_similar_name_and_some.1.borrow(), name, limit);
|
let dist = levenshtein(most_similar_name_and_some.1.borrow(), name, limit);
|
||||||
if dist.is_none() || dist.unwrap() >= limit {
|
if dist.map_or(true, |d| d >= limit) {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(most_similar_name_and_some)
|
Some(most_similar_name_and_some)
|
||||||
|
|
|
@ -52,7 +52,7 @@ pub fn open_read(filename: &str) -> std::io::Result<String> {
|
||||||
|
|
||||||
pub fn read_file(mut f: std::fs::File) -> std::io::Result<String> {
|
pub fn read_file(mut f: std::fs::File) -> std::io::Result<String> {
|
||||||
let mut s = "".to_string();
|
let mut s = "".to_string();
|
||||||
std::io::Read::read_to_string(&mut f, &mut s).unwrap();
|
std::io::Read::read_to_string(&mut f, &mut s)?;
|
||||||
Ok(s)
|
Ok(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -183,7 +183,7 @@ pub fn unique_in_place<T: Eq + std::hash::Hash + Clone>(v: &mut Vec<T>) {
|
||||||
|
|
||||||
/// at least, this is necessary for Windows and macOS
|
/// at least, this is necessary for Windows and macOS
|
||||||
pub fn normalize_path(path: PathBuf) -> PathBuf {
|
pub fn normalize_path(path: PathBuf) -> PathBuf {
|
||||||
let verbatim_replaced = path.to_str().unwrap().replace("\\\\?\\", "");
|
let verbatim_replaced = path.to_string_lossy().replace("\\\\?\\", "");
|
||||||
let lower = if !CASE_SENSITIVE {
|
let lower = if !CASE_SENSITIVE {
|
||||||
verbatim_replaced.to_lowercase()
|
verbatim_replaced.to_lowercase()
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -233,6 +233,8 @@ impl Str {
|
||||||
/// assert_eq!(s.split_with(&[".", "::"]), vec!["a", "b", "c"]);
|
/// assert_eq!(s.split_with(&[".", "::"]), vec!["a", "b", "c"]);
|
||||||
/// let s = Str::rc("ああ.いい::うう");
|
/// let s = Str::rc("ああ.いい::うう");
|
||||||
/// assert_eq!(s.split_with(&[".", "::"]), vec!["ああ", "いい", "うう"]);
|
/// assert_eq!(s.split_with(&[".", "::"]), vec!["ああ", "いい", "うう"]);
|
||||||
|
/// let s = Str::rc("abc");
|
||||||
|
/// assert_eq!(s.split_with(&[".", "::"]), vec!["abc"]);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn split_with(&self, seps: &[&str]) -> Vec<&str> {
|
pub fn split_with(&self, seps: &[&str]) -> Vec<&str> {
|
||||||
let mut result = vec![];
|
let mut result = vec![];
|
||||||
|
|
|
@ -521,7 +521,9 @@ impl StyledStrings {
|
||||||
/// ```
|
/// ```
|
||||||
pub fn push_str(&mut self, s: &str) {
|
pub fn push_str(&mut self, s: &str) {
|
||||||
if self.color_is(Color::Gray) {
|
if self.color_is(Color::Gray) {
|
||||||
self.texts.last_mut().unwrap().text.push_str(s);
|
if let Some(ss) = self.texts.last_mut() {
|
||||||
|
ss.text.push_str(s)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
self.texts.push(StyledString::new(s, None, None));
|
self.texts.push(StyledString::new(s, None, None));
|
||||||
}
|
}
|
||||||
|
|
|
@ -336,7 +336,14 @@ impl<A: ASTBuildable> GenericASTLowerer<A> {
|
||||||
fn fake_lower_signature(&self, sig: ast::Signature) -> LowerResult<hir::Signature> {
|
fn fake_lower_signature(&self, sig: ast::Signature) -> LowerResult<hir::Signature> {
|
||||||
match sig {
|
match sig {
|
||||||
ast::Signature::Var(var) => {
|
ast::Signature::Var(var) => {
|
||||||
let ident = var.ident().unwrap().clone();
|
let Some(ident) = var.ident().cloned() else {
|
||||||
|
return Err(LowerErrors::from(LowerError::declare_error(
|
||||||
|
self.cfg().input.clone(),
|
||||||
|
line!() as usize,
|
||||||
|
var.loc(),
|
||||||
|
self.module.context.caused_by(),
|
||||||
|
)));
|
||||||
|
};
|
||||||
let ident = hir::Identifier::bare(ident);
|
let ident = hir::Identifier::bare(ident);
|
||||||
let t_spec = if let Some(ts) = var.t_spec {
|
let t_spec = if let Some(ts) = var.t_spec {
|
||||||
let expr = self.fake_lower_expr(*ts.t_spec_as_expr.clone())?;
|
let expr = self.fake_lower_expr(*ts.t_spec_as_expr.clone())?;
|
||||||
|
|
|
@ -1191,7 +1191,11 @@ impl_stream!(RecordAttrs, Def);
|
||||||
|
|
||||||
impl Locational for RecordAttrs {
|
impl Locational for RecordAttrs {
|
||||||
fn loc(&self) -> Location {
|
fn loc(&self) -> Location {
|
||||||
Location::concat(self.0.first().unwrap(), self.0.last().unwrap())
|
if self.is_empty() {
|
||||||
|
Location::Unknown
|
||||||
|
} else {
|
||||||
|
Location::concat(self.0.first().unwrap(), self.0.last().unwrap())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2319,8 +2323,8 @@ impl Def {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn def_kind(&self) -> DefKind {
|
pub fn def_kind(&self) -> DefKind {
|
||||||
match self.body.block.first().unwrap() {
|
match self.body.block.first() {
|
||||||
Expr::Call(call) => match call.obj.show_acc().as_ref().map(|n| &n[..]) {
|
Some(Expr::Call(call)) => match call.obj.show_acc().as_ref().map(|n| &n[..]) {
|
||||||
Some("Class") => DefKind::Class,
|
Some("Class") => DefKind::Class,
|
||||||
Some("Inherit") => DefKind::Inherit,
|
Some("Inherit") => DefKind::Inherit,
|
||||||
Some("Trait") => DefKind::Trait,
|
Some("Trait") => DefKind::Trait,
|
||||||
|
@ -2349,7 +2353,7 @@ impl Def {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_base(&self) -> Option<&Record> {
|
pub fn get_base(&self) -> Option<&Record> {
|
||||||
match self.body.block.first().unwrap() {
|
match self.body.block.first()? {
|
||||||
Expr::Call(call) => match call.obj.show_acc().as_ref().map(|n| &n[..]) {
|
Expr::Call(call) => match call.obj.show_acc().as_ref().map(|n| &n[..]) {
|
||||||
Some("Class") | Some("Trait") => {
|
Some("Class") | Some("Trait") => {
|
||||||
if let Some(Expr::Record(rec)) = call.args.get_left_or_key("Base") {
|
if let Some(Expr::Record(rec)) = call.args.get_left_or_key("Base") {
|
||||||
|
|
|
@ -188,7 +188,13 @@ impl ModuleCache {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_similar_name(&self, name: &str) -> Option<Str> {
|
pub fn get_similar_name(&self, name: &str) -> Option<Str> {
|
||||||
get_similar_name(self.cache.iter().map(|(v, _)| v.to_str().unwrap()), name).map(Str::rc)
|
get_similar_name(
|
||||||
|
self.cache
|
||||||
|
.iter()
|
||||||
|
.map(|(v, _)| v.to_str().unwrap_or_default()),
|
||||||
|
name,
|
||||||
|
)
|
||||||
|
.map(Str::rc)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rename_path(&mut self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
|
pub fn rename_path(&mut self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
|
||||||
|
|
|
@ -105,7 +105,7 @@ pub fn py_module(path: TyParam) -> Type {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn module_from_path<P: Into<PathBuf>>(path: P) -> Type {
|
pub fn module_from_path<P: Into<PathBuf>>(path: P) -> Type {
|
||||||
let s = ValueObj::Str(Str::rc(path.into().to_str().unwrap()));
|
let s = ValueObj::Str(Str::rc(&path.into().to_string_lossy()));
|
||||||
module(TyParam::Value(s))
|
module(TyParam::Value(s))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2770,9 +2770,9 @@ impl Type {
|
||||||
Self::Refinement(refine) => refine.t.namespace(),
|
Self::Refinement(refine) => refine.t.namespace(),
|
||||||
Self::Mono(name) | Self::Poly { name, .. } => {
|
Self::Mono(name) | Self::Poly { name, .. } => {
|
||||||
let namespaces = name.split_with(&[".", "::"]);
|
let namespaces = name.split_with(&[".", "::"]);
|
||||||
if namespaces.len() > 1 {
|
if let Some(last) = namespaces.last() {
|
||||||
Str::rc(
|
Str::rc(
|
||||||
name.trim_end_matches(namespaces.last().unwrap())
|
name.trim_end_matches(last)
|
||||||
.trim_end_matches('.')
|
.trim_end_matches('.')
|
||||||
.trim_end_matches("::"),
|
.trim_end_matches("::"),
|
||||||
)
|
)
|
||||||
|
|
|
@ -1205,7 +1205,11 @@ impl NestedDisplay for ClassAttrs {
|
||||||
|
|
||||||
impl Locational for ClassAttrs {
|
impl Locational for ClassAttrs {
|
||||||
fn loc(&self) -> Location {
|
fn loc(&self) -> Location {
|
||||||
Location::concat(self.0.first().unwrap(), self.0.last().unwrap())
|
if self.is_empty() {
|
||||||
|
Location::Unknown
|
||||||
|
} else {
|
||||||
|
Location::concat(self.0.first().unwrap(), self.0.last().unwrap())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1231,7 +1235,11 @@ impl NestedDisplay for RecordAttrs {
|
||||||
|
|
||||||
impl Locational for RecordAttrs {
|
impl Locational for RecordAttrs {
|
||||||
fn loc(&self) -> Location {
|
fn loc(&self) -> Location {
|
||||||
Location::concat(self.0.first().unwrap(), self.0.last().unwrap())
|
if self.is_empty() {
|
||||||
|
Location::Unknown
|
||||||
|
} else {
|
||||||
|
Location::concat(self.0.first().unwrap(), self.0.last().unwrap())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6125,7 +6133,11 @@ impl_display_from_nested!(Module);
|
||||||
|
|
||||||
impl Locational for Module {
|
impl Locational for Module {
|
||||||
fn loc(&self) -> Location {
|
fn loc(&self) -> Location {
|
||||||
Location::concat(self.0.first().unwrap(), self.0.last().unwrap())
|
if self.is_empty() {
|
||||||
|
Location::Unknown
|
||||||
|
} else {
|
||||||
|
Location::concat(self.0.first().unwrap(), self.0.last().unwrap())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue