-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: better formatting for call chains (#106)
* feat: better formatting for call chains * feat: correctly parse dot chain * fix: workaround weird typst parser issue * fix: break for loop iter and target into separate lines * test: add 80line test case * test: apply closure format * test: split compileable test * test: add 0 width test cases * fix: dont touch markup mode chain * test: add last arg no indent bad case * test: update ugrad math
- Loading branch information
1 parent
2cab26d
commit 0171085
Showing
52 changed files
with
1,461 additions
and
623 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use pretty::BoxDoc; | ||
use typst_syntax::ast::*; | ||
|
||
use crate::PrettyPrinter; | ||
|
||
impl PrettyPrinter { | ||
pub fn resolve_dot_chain<'a>( | ||
&'a self, | ||
field_access: FieldAccess<'a>, | ||
) -> Option<Vec<BoxDoc<'a, ()>>> { | ||
let mut nodes = vec![]; | ||
let mut push_node = |node: BoxDoc<'a, ()>, last_is_field_access: bool| { | ||
if last_is_field_access { | ||
nodes.push(node); | ||
} else { | ||
let last = nodes.pop().unwrap(); | ||
nodes.push(node.append(last)); | ||
} | ||
}; | ||
let mut current = field_access.to_untyped(); | ||
let mut last_is_field_access = true; | ||
loop { | ||
if let Some(field_access) = current.cast::<FieldAccess>() { | ||
let rhs = self.convert_ident(field_access.field()); | ||
push_node(rhs, last_is_field_access); | ||
last_is_field_access = true; | ||
current = field_access.target().to_untyped(); | ||
continue; | ||
} | ||
if let Some(func_call) = current.cast::<FuncCall>() { | ||
let args = self.convert_args(func_call.args()); | ||
push_node(args, last_is_field_access); | ||
last_is_field_access = false; | ||
current = func_call.callee().to_untyped(); | ||
continue; | ||
} | ||
let lhs = self.convert_expr(current.cast::<Expr>().unwrap()); | ||
push_node(lhs, last_is_field_access); | ||
break; | ||
} | ||
nodes.reverse(); | ||
Some(nodes) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use crate::PrettyPrinter; | ||
|
||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] | ||
pub enum Mode { | ||
#[default] | ||
Markup, | ||
Code, | ||
Math, | ||
} | ||
|
||
impl PrettyPrinter { | ||
pub(super) fn push_mode(&self, mode: Mode) { | ||
self.mode.borrow_mut().push(mode); | ||
} | ||
|
||
pub(super) fn pop_mode(&self) { | ||
self.mode.borrow_mut().pop(); | ||
} | ||
|
||
pub(super) fn current_mode(&self) -> Mode { | ||
*self.mode.borrow().last().unwrap_or(&Mode::Markup) | ||
} | ||
} | ||
|
||
pub(super) struct ModeGuard<'a>(&'a PrettyPrinter); | ||
|
||
impl PrettyPrinter { | ||
pub(super) fn with_mode(&self, mode: Mode) -> ModeGuard { | ||
self.push_mode(mode); | ||
ModeGuard(self) | ||
} | ||
} | ||
|
||
impl<'a> Drop for ModeGuard<'a> { | ||
fn drop(&mut self) { | ||
self.0.pop_mode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.