chore(els): impl Display for ASTDiff/HIRDiff

This commit is contained in:
Shunsuke Shibayama 2023-05-13 19:08:35 +09:00
parent 28a6dd72eb
commit df5b3bb7d0
2 changed files with 25 additions and 2 deletions

View file

@ -87,14 +87,14 @@ impl<Checker: BuildRunnable> Server<Checker> {
return Ok(()); return Ok(());
}; };
let ast_diff = ASTDiff::diff(old, &new); let ast_diff = ASTDiff::diff(old, &new);
crate::_log!("diff: {ast_diff:?}"); crate::_log!("diff: {ast_diff}");
if let Some(mut lowerer) = self.get_lowerer(&uri) { if let Some(mut lowerer) = self.get_lowerer(&uri) {
let hir = self let hir = self
.artifacts .artifacts
.get_mut(&uri) .get_mut(&uri)
.and_then(|r| r.artifact.object.as_mut()); .and_then(|r| r.artifact.object.as_mut());
if let Some((hir_diff, hir)) = HIRDiff::new(ast_diff, &mut lowerer).zip(hir) { if let Some((hir_diff, hir)) = HIRDiff::new(ast_diff, &mut lowerer).zip(hir) {
crate::_log!("hir_diff: {hir_diff:?}"); crate::_log!("hir_diff: {hir_diff}");
hir_diff.update(hir); hir_diff.update(hir);
} }
self.restore_mod_ctx(&uri, lowerer.pop_mod_ctx().unwrap()); self.restore_mod_ctx(&uri, lowerer.pop_mod_ctx().unwrap());

View file

@ -1,4 +1,5 @@
use std::cmp::Ordering::*; use std::cmp::Ordering::*;
use std::fmt;
use erg_common::traits::Stream; use erg_common::traits::Stream;
use erg_compiler::erg_parser::ast; use erg_compiler::erg_parser::ast;
@ -15,6 +16,17 @@ pub enum ASTDiff {
Nop, Nop,
} }
impl fmt::Display for ASTDiff {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Deletion(idx) => write!(f, "Deletion({idx})"),
Self::Addition(idx, expr) => write!(f, "Addition({idx}, {expr})"),
Self::Modification(idx, expr) => write!(f, "Modification({idx}, {expr})"),
Self::Nop => write!(f, "Nop"),
}
}
}
/// diff(old: {x, y, z}, new: {x, a, y, z}) => ASTDiff::Addition(1) /// diff(old: {x, y, z}, new: {x, a, y, z}) => ASTDiff::Addition(1)
/// diff(old: {x, y}, new: {x, y, a}) => ASTDiff::Addition(2) /// diff(old: {x, y}, new: {x, y, a}) => ASTDiff::Addition(2)
/// diff(old: {x, y}, new: {x}) => ASTDiff::Deletion(1) /// diff(old: {x, y}, new: {x}) => ASTDiff::Deletion(1)
@ -57,6 +69,17 @@ pub enum HIRDiff {
Nop, Nop,
} }
impl fmt::Display for HIRDiff {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Deletion(idx) => write!(f, "Deletion({idx})"),
Self::Addition(idx, expr) => write!(f, "Addition({idx}, {expr})"),
Self::Modification(idx, expr) => write!(f, "Modification({idx}, {expr})"),
Self::Nop => write!(f, "Nop"),
}
}
}
impl HIRDiff { impl HIRDiff {
pub fn new(diff: ASTDiff, lowerer: &mut ASTLowerer) -> Option<Self> { pub fn new(diff: ASTDiff, lowerer: &mut ASTLowerer) -> Option<Self> {
match diff { match diff {