Rename Magic* to IpyEscape* (#6395)

## Summary

This PR renames the `MagicCommand` token to `IpyEscapeCommand` token and
`MagicKind` to `IpyEscapeKind` type to better reflect the purpose of the
token and type. Similarly, it renames the AST nodes from `LineMagic` to
`IpyEscapeCommand` prefixed with `Stmt`/`Expr` wherever necessary.

It also makes renames from using `jupyter_magic` to
`ipython_escape_commands` in various function names.

The mode value is still `Mode::Jupyter` because the escape commands are
part of the IPython syntax but the lexing/parsing is done for a Jupyter
notebook.

### Motivation behind the rename:
* IPython codebase defines it as "EscapeCommand" / "Escape Sequences":
* Escape Sequences:
292e3a2345/IPython/core/inputtransformer2.py (L329-L333)
* Escape command:
292e3a2345/IPython/core/inputtransformer2.py (L410-L411)
* The word "magic" is used mainly for the actual magic commands i.e.,
the ones starting with `%`/`%%`
(https://ipython.readthedocs.io/en/stable/interactive/reference.html#magic-command-system).
So, this avoids any confusion between the Magic token (`%`, `%%`) and
the escape command itself.
## Test Plan

* `cargo test` to make sure all renames are done correctly.
* `grep` for `jupyter_escape`/`magic` to make sure all renames are done
correctly.
This commit is contained in:
Dhruv Manilawala 2023-08-09 18:58:18 +05:30 committed by GitHub
parent 3bf1c66cda
commit 6a64f2289b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 949 additions and 946 deletions

View file

@ -672,8 +672,8 @@ pub struct ExprSlice<'a> {
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ExprLineMagic<'a> {
kind: ast::MagicKind,
pub struct ExprIpyEscapeCommand<'a> {
kind: ast::IpyEscapeKind,
value: &'a str,
}
@ -706,7 +706,7 @@ pub enum ComparableExpr<'a> {
List(ExprList<'a>),
Tuple(ExprTuple<'a>),
Slice(ExprSlice<'a>),
LineMagic(ExprLineMagic<'a>),
IpyEscapeCommand(ExprIpyEscapeCommand<'a>),
}
impl<'a> From<&'a Box<ast::Expr>> for Box<ComparableExpr<'a>> {
@ -936,11 +936,11 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
upper: upper.as_ref().map(Into::into),
step: step.as_ref().map(Into::into),
}),
ast::Expr::LineMagic(ast::ExprLineMagic {
ast::Expr::IpyEscapeCommand(ast::ExprIpyEscapeCommand {
kind,
value,
range: _,
}) => Self::LineMagic(ExprLineMagic {
}) => Self::IpyEscapeCommand(ExprIpyEscapeCommand {
kind: *kind,
value: value.as_str(),
}),
@ -1165,8 +1165,8 @@ pub struct StmtExpr<'a> {
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct StmtLineMagic<'a> {
kind: ast::MagicKind,
pub struct StmtIpyEscapeCommand<'a> {
kind: ast::IpyEscapeKind,
value: &'a str,
}
@ -1193,7 +1193,7 @@ pub enum ComparableStmt<'a> {
ImportFrom(StmtImportFrom<'a>),
Global(StmtGlobal<'a>),
Nonlocal(StmtNonlocal<'a>),
LineMagic(StmtLineMagic<'a>),
IpyEscapeCommand(StmtIpyEscapeCommand<'a>),
Expr(StmtExpr<'a>),
Pass,
Break,
@ -1394,11 +1394,11 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
names: names.iter().map(ast::Identifier::as_str).collect(),
})
}
ast::Stmt::LineMagic(ast::StmtLineMagic {
ast::Stmt::IpyEscapeCommand(ast::StmtIpyEscapeCommand {
kind,
value,
range: _,
}) => Self::LineMagic(StmtLineMagic {
}) => Self::IpyEscapeCommand(StmtIpyEscapeCommand {
kind: *kind,
value: value.as_str(),
}),

View file

@ -108,7 +108,7 @@ where
| Expr::Subscript(_)
| Expr::Yield(_)
| Expr::YieldFrom(_)
| Expr::LineMagic(_)
| Expr::IpyEscapeCommand(_)
)
})
}
@ -247,7 +247,7 @@ where
.is_some_and(|value| any_over_expr(value, func))
}
Expr::Name(_) | Expr::Constant(_) => false,
Expr::LineMagic(_) => false,
Expr::IpyEscapeCommand(_) => false,
}
}
@ -534,7 +534,7 @@ where
Stmt::Nonlocal(_) => false,
Stmt::Expr(ast::StmtExpr { value, range: _ }) => any_over_expr(value, func),
Stmt::Pass(_) | Stmt::Break(_) | Stmt::Continue(_) => false,
Stmt::LineMagic(_) => false,
Stmt::IpyEscapeCommand(_) => false,
}
}

View file

@ -48,7 +48,7 @@ pub enum AnyNode {
StmtPass(ast::StmtPass),
StmtBreak(ast::StmtBreak),
StmtContinue(ast::StmtContinue),
StmtLineMagic(ast::StmtLineMagic),
StmtIpyEscapeCommand(ast::StmtIpyEscapeCommand),
ExprBoolOp(ast::ExprBoolOp),
ExprNamedExpr(ast::ExprNamedExpr),
ExprBinOp(ast::ExprBinOp),
@ -76,7 +76,7 @@ pub enum AnyNode {
ExprList(ast::ExprList),
ExprTuple(ast::ExprTuple),
ExprSlice(ast::ExprSlice),
ExprLineMagic(ast::ExprLineMagic),
ExprIpyEscapeCommand(ast::ExprIpyEscapeCommand),
ExceptHandlerExceptHandler(ast::ExceptHandlerExceptHandler),
PatternMatchValue(ast::PatternMatchValue),
PatternMatchSingleton(ast::PatternMatchSingleton),
@ -131,7 +131,7 @@ impl AnyNode {
AnyNode::StmtPass(node) => Some(Stmt::Pass(node)),
AnyNode::StmtBreak(node) => Some(Stmt::Break(node)),
AnyNode::StmtContinue(node) => Some(Stmt::Continue(node)),
AnyNode::StmtLineMagic(node) => Some(Stmt::LineMagic(node)),
AnyNode::StmtIpyEscapeCommand(node) => Some(Stmt::IpyEscapeCommand(node)),
AnyNode::ModModule(_)
| AnyNode::ModExpression(_)
@ -162,7 +162,7 @@ impl AnyNode {
| AnyNode::ExprList(_)
| AnyNode::ExprTuple(_)
| AnyNode::ExprSlice(_)
| AnyNode::ExprLineMagic(_)
| AnyNode::ExprIpyEscapeCommand(_)
| AnyNode::ExceptHandlerExceptHandler(_)
| AnyNode::PatternMatchValue(_)
| AnyNode::PatternMatchSingleton(_)
@ -219,7 +219,7 @@ impl AnyNode {
AnyNode::ExprList(node) => Some(Expr::List(node)),
AnyNode::ExprTuple(node) => Some(Expr::Tuple(node)),
AnyNode::ExprSlice(node) => Some(Expr::Slice(node)),
AnyNode::ExprLineMagic(node) => Some(Expr::LineMagic(node)),
AnyNode::ExprIpyEscapeCommand(node) => Some(Expr::IpyEscapeCommand(node)),
AnyNode::ModModule(_)
| AnyNode::ModExpression(_)
@ -248,7 +248,7 @@ impl AnyNode {
| AnyNode::StmtPass(_)
| AnyNode::StmtBreak(_)
| AnyNode::StmtContinue(_)
| AnyNode::StmtLineMagic(_)
| AnyNode::StmtIpyEscapeCommand(_)
| AnyNode::ExceptHandlerExceptHandler(_)
| AnyNode::PatternMatchValue(_)
| AnyNode::PatternMatchSingleton(_)
@ -306,7 +306,7 @@ impl AnyNode {
| AnyNode::StmtPass(_)
| AnyNode::StmtBreak(_)
| AnyNode::StmtContinue(_)
| AnyNode::StmtLineMagic(_)
| AnyNode::StmtIpyEscapeCommand(_)
| AnyNode::ExprBoolOp(_)
| AnyNode::ExprNamedExpr(_)
| AnyNode::ExprBinOp(_)
@ -334,7 +334,7 @@ impl AnyNode {
| AnyNode::ExprList(_)
| AnyNode::ExprTuple(_)
| AnyNode::ExprSlice(_)
| AnyNode::ExprLineMagic(_)
| AnyNode::ExprIpyEscapeCommand(_)
| AnyNode::ExceptHandlerExceptHandler(_)
| AnyNode::PatternMatchValue(_)
| AnyNode::PatternMatchSingleton(_)
@ -400,7 +400,7 @@ impl AnyNode {
| AnyNode::StmtPass(_)
| AnyNode::StmtBreak(_)
| AnyNode::StmtContinue(_)
| AnyNode::StmtLineMagic(_)
| AnyNode::StmtIpyEscapeCommand(_)
| AnyNode::ExprBoolOp(_)
| AnyNode::ExprNamedExpr(_)
| AnyNode::ExprBinOp(_)
@ -428,7 +428,7 @@ impl AnyNode {
| AnyNode::ExprList(_)
| AnyNode::ExprTuple(_)
| AnyNode::ExprSlice(_)
| AnyNode::ExprLineMagic(_)
| AnyNode::ExprIpyEscapeCommand(_)
| AnyNode::ExceptHandlerExceptHandler(_)
| AnyNode::Comprehension(_)
| AnyNode::Arguments(_)
@ -479,7 +479,7 @@ impl AnyNode {
| AnyNode::StmtPass(_)
| AnyNode::StmtBreak(_)
| AnyNode::StmtContinue(_)
| AnyNode::StmtLineMagic(_)
| AnyNode::StmtIpyEscapeCommand(_)
| AnyNode::ExprBoolOp(_)
| AnyNode::ExprNamedExpr(_)
| AnyNode::ExprBinOp(_)
@ -507,7 +507,7 @@ impl AnyNode {
| AnyNode::ExprList(_)
| AnyNode::ExprTuple(_)
| AnyNode::ExprSlice(_)
| AnyNode::ExprLineMagic(_)
| AnyNode::ExprIpyEscapeCommand(_)
| AnyNode::PatternMatchValue(_)
| AnyNode::PatternMatchSingleton(_)
| AnyNode::PatternMatchSequence(_)
@ -583,7 +583,7 @@ impl AnyNode {
Self::StmtPass(node) => AnyNodeRef::StmtPass(node),
Self::StmtBreak(node) => AnyNodeRef::StmtBreak(node),
Self::StmtContinue(node) => AnyNodeRef::StmtContinue(node),
Self::StmtLineMagic(node) => AnyNodeRef::StmtLineMagic(node),
Self::StmtIpyEscapeCommand(node) => AnyNodeRef::StmtIpyEscapeCommand(node),
Self::ExprBoolOp(node) => AnyNodeRef::ExprBoolOp(node),
Self::ExprNamedExpr(node) => AnyNodeRef::ExprNamedExpr(node),
Self::ExprBinOp(node) => AnyNodeRef::ExprBinOp(node),
@ -611,7 +611,7 @@ impl AnyNode {
Self::ExprList(node) => AnyNodeRef::ExprList(node),
Self::ExprTuple(node) => AnyNodeRef::ExprTuple(node),
Self::ExprSlice(node) => AnyNodeRef::ExprSlice(node),
Self::ExprLineMagic(node) => AnyNodeRef::ExprLineMagic(node),
Self::ExprIpyEscapeCommand(node) => AnyNodeRef::ExprIpyEscapeCommand(node),
Self::ExceptHandlerExceptHandler(node) => AnyNodeRef::ExceptHandlerExceptHandler(node),
Self::PatternMatchValue(node) => AnyNodeRef::PatternMatchValue(node),
Self::PatternMatchSingleton(node) => AnyNodeRef::PatternMatchSingleton(node),
@ -1429,12 +1429,12 @@ impl AstNode for ast::StmtContinue {
AnyNode::from(self)
}
}
impl AstNode for ast::StmtLineMagic {
impl AstNode for ast::StmtIpyEscapeCommand {
fn cast(kind: AnyNode) -> Option<Self>
where
Self: Sized,
{
if let AnyNode::StmtLineMagic(node) = kind {
if let AnyNode::StmtIpyEscapeCommand(node) = kind {
Some(node)
} else {
None
@ -1442,7 +1442,7 @@ impl AstNode for ast::StmtLineMagic {
}
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
if let AnyNodeRef::StmtLineMagic(node) = kind {
if let AnyNodeRef::StmtIpyEscapeCommand(node) = kind {
Some(node)
} else {
None
@ -2213,12 +2213,12 @@ impl AstNode for ast::ExprSlice {
AnyNode::from(self)
}
}
impl AstNode for ast::ExprLineMagic {
impl AstNode for ast::ExprIpyEscapeCommand {
fn cast(kind: AnyNode) -> Option<Self>
where
Self: Sized,
{
if let AnyNode::ExprLineMagic(node) = kind {
if let AnyNode::ExprIpyEscapeCommand(node) = kind {
Some(node)
} else {
None
@ -2226,7 +2226,7 @@ impl AstNode for ast::ExprLineMagic {
}
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
if let AnyNodeRef::ExprLineMagic(node) = kind {
if let AnyNodeRef::ExprIpyEscapeCommand(node) = kind {
Some(node)
} else {
None
@ -2915,7 +2915,7 @@ impl From<Stmt> for AnyNode {
Stmt::Pass(node) => AnyNode::StmtPass(node),
Stmt::Break(node) => AnyNode::StmtBreak(node),
Stmt::Continue(node) => AnyNode::StmtContinue(node),
Stmt::LineMagic(node) => AnyNode::StmtLineMagic(node),
Stmt::IpyEscapeCommand(node) => AnyNode::StmtIpyEscapeCommand(node),
}
}
}
@ -2950,7 +2950,7 @@ impl From<Expr> for AnyNode {
Expr::List(node) => AnyNode::ExprList(node),
Expr::Tuple(node) => AnyNode::ExprTuple(node),
Expr::Slice(node) => AnyNode::ExprSlice(node),
Expr::LineMagic(node) => AnyNode::ExprLineMagic(node),
Expr::IpyEscapeCommand(node) => AnyNode::ExprIpyEscapeCommand(node),
}
}
}
@ -3155,9 +3155,9 @@ impl From<ast::StmtContinue> for AnyNode {
}
}
impl From<ast::StmtLineMagic> for AnyNode {
fn from(node: ast::StmtLineMagic) -> Self {
AnyNode::StmtLineMagic(node)
impl From<ast::StmtIpyEscapeCommand> for AnyNode {
fn from(node: ast::StmtIpyEscapeCommand) -> Self {
AnyNode::StmtIpyEscapeCommand(node)
}
}
@ -3323,9 +3323,9 @@ impl From<ast::ExprSlice> for AnyNode {
}
}
impl From<ast::ExprLineMagic> for AnyNode {
fn from(node: ast::ExprLineMagic) -> Self {
AnyNode::ExprLineMagic(node)
impl From<ast::ExprIpyEscapeCommand> for AnyNode {
fn from(node: ast::ExprIpyEscapeCommand) -> Self {
AnyNode::ExprIpyEscapeCommand(node)
}
}
@ -3486,7 +3486,7 @@ impl Ranged for AnyNode {
AnyNode::StmtPass(node) => node.range(),
AnyNode::StmtBreak(node) => node.range(),
AnyNode::StmtContinue(node) => node.range(),
AnyNode::StmtLineMagic(node) => node.range(),
AnyNode::StmtIpyEscapeCommand(node) => node.range(),
AnyNode::ExprBoolOp(node) => node.range(),
AnyNode::ExprNamedExpr(node) => node.range(),
AnyNode::ExprBinOp(node) => node.range(),
@ -3514,7 +3514,7 @@ impl Ranged for AnyNode {
AnyNode::ExprList(node) => node.range(),
AnyNode::ExprTuple(node) => node.range(),
AnyNode::ExprSlice(node) => node.range(),
AnyNode::ExprLineMagic(node) => node.range(),
AnyNode::ExprIpyEscapeCommand(node) => node.range(),
AnyNode::ExceptHandlerExceptHandler(node) => node.range(),
AnyNode::PatternMatchValue(node) => node.range(),
AnyNode::PatternMatchSingleton(node) => node.range(),
@ -3572,7 +3572,7 @@ pub enum AnyNodeRef<'a> {
StmtPass(&'a ast::StmtPass),
StmtBreak(&'a ast::StmtBreak),
StmtContinue(&'a ast::StmtContinue),
StmtLineMagic(&'a ast::StmtLineMagic),
StmtIpyEscapeCommand(&'a ast::StmtIpyEscapeCommand),
ExprBoolOp(&'a ast::ExprBoolOp),
ExprNamedExpr(&'a ast::ExprNamedExpr),
ExprBinOp(&'a ast::ExprBinOp),
@ -3600,7 +3600,7 @@ pub enum AnyNodeRef<'a> {
ExprList(&'a ast::ExprList),
ExprTuple(&'a ast::ExprTuple),
ExprSlice(&'a ast::ExprSlice),
ExprLineMagic(&'a ast::ExprLineMagic),
ExprIpyEscapeCommand(&'a ast::ExprIpyEscapeCommand),
ExceptHandlerExceptHandler(&'a ast::ExceptHandlerExceptHandler),
PatternMatchValue(&'a ast::PatternMatchValue),
PatternMatchSingleton(&'a ast::PatternMatchSingleton),
@ -3657,7 +3657,7 @@ impl AnyNodeRef<'_> {
AnyNodeRef::StmtPass(node) => NonNull::from(*node).cast(),
AnyNodeRef::StmtBreak(node) => NonNull::from(*node).cast(),
AnyNodeRef::StmtContinue(node) => NonNull::from(*node).cast(),
AnyNodeRef::StmtLineMagic(node) => NonNull::from(*node).cast(),
AnyNodeRef::StmtIpyEscapeCommand(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExprBoolOp(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExprNamedExpr(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExprBinOp(node) => NonNull::from(*node).cast(),
@ -3685,7 +3685,7 @@ impl AnyNodeRef<'_> {
AnyNodeRef::ExprList(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExprTuple(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExprSlice(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExprLineMagic(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExprIpyEscapeCommand(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExceptHandlerExceptHandler(node) => NonNull::from(*node).cast(),
AnyNodeRef::PatternMatchValue(node) => NonNull::from(*node).cast(),
AnyNodeRef::PatternMatchSingleton(node) => NonNull::from(*node).cast(),
@ -3748,7 +3748,7 @@ impl AnyNodeRef<'_> {
AnyNodeRef::StmtPass(_) => NodeKind::StmtPass,
AnyNodeRef::StmtBreak(_) => NodeKind::StmtBreak,
AnyNodeRef::StmtContinue(_) => NodeKind::StmtContinue,
AnyNodeRef::StmtLineMagic(_) => NodeKind::StmtLineMagic,
AnyNodeRef::StmtIpyEscapeCommand(_) => NodeKind::StmtIpyEscapeCommand,
AnyNodeRef::ExprBoolOp(_) => NodeKind::ExprBoolOp,
AnyNodeRef::ExprNamedExpr(_) => NodeKind::ExprNamedExpr,
AnyNodeRef::ExprBinOp(_) => NodeKind::ExprBinOp,
@ -3776,7 +3776,7 @@ impl AnyNodeRef<'_> {
AnyNodeRef::ExprList(_) => NodeKind::ExprList,
AnyNodeRef::ExprTuple(_) => NodeKind::ExprTuple,
AnyNodeRef::ExprSlice(_) => NodeKind::ExprSlice,
AnyNodeRef::ExprLineMagic(_) => NodeKind::ExprLineMagic,
AnyNodeRef::ExprIpyEscapeCommand(_) => NodeKind::ExprIpyEscapeCommand,
AnyNodeRef::ExceptHandlerExceptHandler(_) => NodeKind::ExceptHandlerExceptHandler,
AnyNodeRef::PatternMatchValue(_) => NodeKind::PatternMatchValue,
AnyNodeRef::PatternMatchSingleton(_) => NodeKind::PatternMatchSingleton,
@ -3831,7 +3831,7 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::StmtPass(_)
| AnyNodeRef::StmtBreak(_)
| AnyNodeRef::StmtContinue(_)
| AnyNodeRef::StmtLineMagic(_) => true,
| AnyNodeRef::StmtIpyEscapeCommand(_) => true,
AnyNodeRef::ModModule(_)
| AnyNodeRef::ModExpression(_)
@ -3862,7 +3862,7 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::ExprList(_)
| AnyNodeRef::ExprTuple(_)
| AnyNodeRef::ExprSlice(_)
| AnyNodeRef::ExprLineMagic(_)
| AnyNodeRef::ExprIpyEscapeCommand(_)
| AnyNodeRef::ExceptHandlerExceptHandler(_)
| AnyNodeRef::PatternMatchValue(_)
| AnyNodeRef::PatternMatchSingleton(_)
@ -3919,7 +3919,7 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::ExprList(_)
| AnyNodeRef::ExprTuple(_)
| AnyNodeRef::ExprSlice(_)
| AnyNodeRef::ExprLineMagic(_) => true,
| AnyNodeRef::ExprIpyEscapeCommand(_) => true,
AnyNodeRef::ModModule(_)
| AnyNodeRef::ModExpression(_)
@ -3948,7 +3948,7 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::StmtPass(_)
| AnyNodeRef::StmtBreak(_)
| AnyNodeRef::StmtContinue(_)
| AnyNodeRef::StmtLineMagic(_)
| AnyNodeRef::StmtIpyEscapeCommand(_)
| AnyNodeRef::ExceptHandlerExceptHandler(_)
| AnyNodeRef::PatternMatchValue(_)
| AnyNodeRef::PatternMatchSingleton(_)
@ -4005,7 +4005,7 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::StmtPass(_)
| AnyNodeRef::StmtBreak(_)
| AnyNodeRef::StmtContinue(_)
| AnyNodeRef::StmtLineMagic(_)
| AnyNodeRef::StmtIpyEscapeCommand(_)
| AnyNodeRef::ExprBoolOp(_)
| AnyNodeRef::ExprNamedExpr(_)
| AnyNodeRef::ExprBinOp(_)
@ -4033,7 +4033,7 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::ExprList(_)
| AnyNodeRef::ExprTuple(_)
| AnyNodeRef::ExprSlice(_)
| AnyNodeRef::ExprLineMagic(_)
| AnyNodeRef::ExprIpyEscapeCommand(_)
| AnyNodeRef::ExceptHandlerExceptHandler(_)
| AnyNodeRef::PatternMatchValue(_)
| AnyNodeRef::PatternMatchSingleton(_)
@ -4099,7 +4099,7 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::StmtPass(_)
| AnyNodeRef::StmtBreak(_)
| AnyNodeRef::StmtContinue(_)
| AnyNodeRef::StmtLineMagic(_)
| AnyNodeRef::StmtIpyEscapeCommand(_)
| AnyNodeRef::ExprBoolOp(_)
| AnyNodeRef::ExprNamedExpr(_)
| AnyNodeRef::ExprBinOp(_)
@ -4127,7 +4127,7 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::ExprList(_)
| AnyNodeRef::ExprTuple(_)
| AnyNodeRef::ExprSlice(_)
| AnyNodeRef::ExprLineMagic(_)
| AnyNodeRef::ExprIpyEscapeCommand(_)
| AnyNodeRef::ExceptHandlerExceptHandler(_)
| AnyNodeRef::Comprehension(_)
| AnyNodeRef::Arguments(_)
@ -4178,7 +4178,7 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::StmtPass(_)
| AnyNodeRef::StmtBreak(_)
| AnyNodeRef::StmtContinue(_)
| AnyNodeRef::StmtLineMagic(_)
| AnyNodeRef::StmtIpyEscapeCommand(_)
| AnyNodeRef::ExprBoolOp(_)
| AnyNodeRef::ExprNamedExpr(_)
| AnyNodeRef::ExprBinOp(_)
@ -4206,7 +4206,7 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::ExprList(_)
| AnyNodeRef::ExprTuple(_)
| AnyNodeRef::ExprSlice(_)
| AnyNodeRef::ExprLineMagic(_)
| AnyNodeRef::ExprIpyEscapeCommand(_)
| AnyNodeRef::PatternMatchValue(_)
| AnyNodeRef::PatternMatchSingleton(_)
| AnyNodeRef::PatternMatchSequence(_)
@ -4429,9 +4429,9 @@ impl<'a> From<&'a ast::StmtContinue> for AnyNodeRef<'a> {
}
}
impl<'a> From<&'a ast::StmtLineMagic> for AnyNodeRef<'a> {
fn from(node: &'a ast::StmtLineMagic) -> Self {
AnyNodeRef::StmtLineMagic(node)
impl<'a> From<&'a ast::StmtIpyEscapeCommand> for AnyNodeRef<'a> {
fn from(node: &'a ast::StmtIpyEscapeCommand) -> Self {
AnyNodeRef::StmtIpyEscapeCommand(node)
}
}
@ -4597,9 +4597,9 @@ impl<'a> From<&'a ast::ExprSlice> for AnyNodeRef<'a> {
}
}
impl<'a> From<&'a ast::ExprLineMagic> for AnyNodeRef<'a> {
fn from(node: &'a ast::ExprLineMagic) -> Self {
AnyNodeRef::ExprLineMagic(node)
impl<'a> From<&'a ast::ExprIpyEscapeCommand> for AnyNodeRef<'a> {
fn from(node: &'a ast::ExprIpyEscapeCommand) -> Self {
AnyNodeRef::ExprIpyEscapeCommand(node)
}
}
@ -4714,7 +4714,7 @@ impl<'a> From<&'a Stmt> for AnyNodeRef<'a> {
Stmt::Pass(node) => AnyNodeRef::StmtPass(node),
Stmt::Break(node) => AnyNodeRef::StmtBreak(node),
Stmt::Continue(node) => AnyNodeRef::StmtContinue(node),
Stmt::LineMagic(node) => AnyNodeRef::StmtLineMagic(node),
Stmt::IpyEscapeCommand(node) => AnyNodeRef::StmtIpyEscapeCommand(node),
}
}
}
@ -4749,7 +4749,7 @@ impl<'a> From<&'a Expr> for AnyNodeRef<'a> {
Expr::List(node) => AnyNodeRef::ExprList(node),
Expr::Tuple(node) => AnyNodeRef::ExprTuple(node),
Expr::Slice(node) => AnyNodeRef::ExprSlice(node),
Expr::LineMagic(node) => AnyNodeRef::ExprLineMagic(node),
Expr::IpyEscapeCommand(node) => AnyNodeRef::ExprIpyEscapeCommand(node),
}
}
}
@ -4874,7 +4874,7 @@ impl Ranged for AnyNodeRef<'_> {
AnyNodeRef::StmtPass(node) => node.range(),
AnyNodeRef::StmtBreak(node) => node.range(),
AnyNodeRef::StmtContinue(node) => node.range(),
AnyNodeRef::StmtLineMagic(node) => node.range(),
AnyNodeRef::StmtIpyEscapeCommand(node) => node.range(),
AnyNodeRef::ExprBoolOp(node) => node.range(),
AnyNodeRef::ExprNamedExpr(node) => node.range(),
AnyNodeRef::ExprBinOp(node) => node.range(),
@ -4902,7 +4902,7 @@ impl Ranged for AnyNodeRef<'_> {
AnyNodeRef::ExprList(node) => node.range(),
AnyNodeRef::ExprTuple(node) => node.range(),
AnyNodeRef::ExprSlice(node) => node.range(),
AnyNodeRef::ExprLineMagic(node) => node.range(),
AnyNodeRef::ExprIpyEscapeCommand(node) => node.range(),
AnyNodeRef::ExceptHandlerExceptHandler(node) => node.range(),
AnyNodeRef::PatternMatchValue(node) => node.range(),
AnyNodeRef::PatternMatchSingleton(node) => node.range(),
@ -4958,7 +4958,7 @@ pub enum NodeKind {
StmtImportFrom,
StmtGlobal,
StmtNonlocal,
StmtLineMagic,
StmtIpyEscapeCommand,
StmtExpr,
StmtPass,
StmtBreak,
@ -4990,7 +4990,7 @@ pub enum NodeKind {
ExprList,
ExprTuple,
ExprSlice,
ExprLineMagic,
ExprIpyEscapeCommand,
ExceptHandlerExceptHandler,
PatternMatchValue,
PatternMatchSingleton,

View file

@ -95,20 +95,20 @@ pub enum Stmt {
Continue(StmtContinue),
// Jupyter notebook specific
#[is(name = "line_magic_stmt")]
LineMagic(StmtLineMagic),
#[is(name = "ipy_escape_command_stmt")]
IpyEscapeCommand(StmtIpyEscapeCommand),
}
#[derive(Clone, Debug, PartialEq)]
pub struct StmtLineMagic {
pub struct StmtIpyEscapeCommand {
pub range: TextRange,
pub kind: MagicKind,
pub kind: IpyEscapeKind,
pub value: String,
}
impl From<StmtLineMagic> for Stmt {
fn from(payload: StmtLineMagic) -> Self {
Stmt::LineMagic(payload)
impl From<StmtIpyEscapeCommand> for Stmt {
fn from(payload: StmtIpyEscapeCommand) -> Self {
Stmt::IpyEscapeCommand(payload)
}
}
@ -570,20 +570,20 @@ pub enum Expr {
Slice(ExprSlice),
// Jupyter notebook specific
#[is(name = "line_magic_expr")]
LineMagic(ExprLineMagic),
#[is(name = "ipy_escape_command_expr")]
IpyEscapeCommand(ExprIpyEscapeCommand),
}
#[derive(Clone, Debug, PartialEq)]
pub struct ExprLineMagic {
pub struct ExprIpyEscapeCommand {
pub range: TextRange,
pub kind: MagicKind,
pub kind: IpyEscapeKind,
pub value: String,
}
impl From<ExprLineMagic> for Expr {
fn from(payload: ExprLineMagic) -> Self {
Expr::LineMagic(payload)
impl From<ExprIpyEscapeCommand> for Expr {
fn from(payload: ExprIpyEscapeCommand) -> Self {
Expr::IpyEscapeCommand(payload)
}
}
@ -2253,103 +2253,103 @@ impl Parameters {
}
}
/// The kind of magic command as defined in [IPython Syntax] in the IPython codebase.
/// The kind of escape command as defined in [IPython Syntax] in the IPython codebase.
///
/// [IPython Syntax]: https://github.com/ipython/ipython/blob/635815e8f1ded5b764d66cacc80bbe25e9e2587f/IPython/core/inputtransformer2.py#L335-L343
#[derive(PartialEq, Eq, Debug, Clone, Hash, Copy)]
pub enum MagicKind {
/// Send line to underlying system shell.
pub enum IpyEscapeKind {
/// Send line to underlying system shell (`!`).
Shell,
/// Send line to system shell and capture output.
/// Send line to system shell and capture output (`!!`).
ShCap,
/// Show help on object.
/// Show help on object (`?`).
Help,
/// Show help on object, with extra verbosity.
/// Show help on object, with extra verbosity (`??`).
Help2,
/// Call magic function.
/// Call magic function (`%`).
Magic,
/// Call cell magic function.
/// Call cell magic function (`%%`).
Magic2,
/// Call first argument with rest of line as arguments after splitting on whitespace
/// and quote each as string.
/// and quote each as string (`,`).
Quote,
/// Call first argument with rest of line as an argument quoted as a single string.
/// Call first argument with rest of line as an argument quoted as a single string (`;`).
Quote2,
/// Call first argument with rest of line as arguments.
/// Call first argument with rest of line as arguments (`/`).
Paren,
}
impl TryFrom<char> for MagicKind {
impl TryFrom<char> for IpyEscapeKind {
type Error = String;
fn try_from(ch: char) -> Result<Self, Self::Error> {
match ch {
'!' => Ok(MagicKind::Shell),
'?' => Ok(MagicKind::Help),
'%' => Ok(MagicKind::Magic),
',' => Ok(MagicKind::Quote),
';' => Ok(MagicKind::Quote2),
'/' => Ok(MagicKind::Paren),
'!' => Ok(IpyEscapeKind::Shell),
'?' => Ok(IpyEscapeKind::Help),
'%' => Ok(IpyEscapeKind::Magic),
',' => Ok(IpyEscapeKind::Quote),
';' => Ok(IpyEscapeKind::Quote2),
'/' => Ok(IpyEscapeKind::Paren),
_ => Err(format!("Unexpected magic escape: {ch}")),
}
}
}
impl TryFrom<[char; 2]> for MagicKind {
impl TryFrom<[char; 2]> for IpyEscapeKind {
type Error = String;
fn try_from(ch: [char; 2]) -> Result<Self, Self::Error> {
match ch {
['!', '!'] => Ok(MagicKind::ShCap),
['?', '?'] => Ok(MagicKind::Help2),
['%', '%'] => Ok(MagicKind::Magic2),
['!', '!'] => Ok(IpyEscapeKind::ShCap),
['?', '?'] => Ok(IpyEscapeKind::Help2),
['%', '%'] => Ok(IpyEscapeKind::Magic2),
[c1, c2] => Err(format!("Unexpected magic escape: {c1}{c2}")),
}
}
}
impl fmt::Display for MagicKind {
impl fmt::Display for IpyEscapeKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl MagicKind {
/// Returns the length of the magic command prefix.
impl IpyEscapeKind {
/// Returns the length of the escape kind token.
pub fn prefix_len(self) -> TextSize {
let len = match self {
MagicKind::Shell
| MagicKind::Magic
| MagicKind::Help
| MagicKind::Quote
| MagicKind::Quote2
| MagicKind::Paren => 1,
MagicKind::ShCap | MagicKind::Magic2 | MagicKind::Help2 => 2,
IpyEscapeKind::Shell
| IpyEscapeKind::Magic
| IpyEscapeKind::Help
| IpyEscapeKind::Quote
| IpyEscapeKind::Quote2
| IpyEscapeKind::Paren => 1,
IpyEscapeKind::ShCap | IpyEscapeKind::Magic2 | IpyEscapeKind::Help2 => 2,
};
len.into()
}
/// Returns `true` if the kind is a help command i.e., `?` or `??`.
/// Returns `true` if the escape kind is help i.e., `?` or `??`.
pub const fn is_help(self) -> bool {
matches!(self, MagicKind::Help | MagicKind::Help2)
matches!(self, IpyEscapeKind::Help | IpyEscapeKind::Help2)
}
/// Returns `true` if the kind is a magic command i.e., `%` or `%%`.
/// Returns `true` if the escape kind is magic i.e., `%` or `%%`.
pub const fn is_magic(self) -> bool {
matches!(self, MagicKind::Magic | MagicKind::Magic2)
matches!(self, IpyEscapeKind::Magic | IpyEscapeKind::Magic2)
}
pub fn as_str(self) -> &'static str {
match self {
MagicKind::Shell => "!",
MagicKind::ShCap => "!!",
MagicKind::Help => "?",
MagicKind::Help2 => "??",
MagicKind::Magic => "%",
MagicKind::Magic2 => "%%",
MagicKind::Quote => ",",
MagicKind::Quote2 => ";",
MagicKind::Paren => "/",
IpyEscapeKind::Shell => "!",
IpyEscapeKind::ShCap => "!!",
IpyEscapeKind::Help => "?",
IpyEscapeKind::Help2 => "??",
IpyEscapeKind::Magic => "%",
IpyEscapeKind::Magic2 => "%%",
IpyEscapeKind::Quote => ",",
IpyEscapeKind::Quote2 => ";",
IpyEscapeKind::Paren => "/",
}
}
}
@ -2686,7 +2686,7 @@ impl Ranged for crate::nodes::StmtContinue {
self.range
}
}
impl Ranged for StmtLineMagic {
impl Ranged for StmtIpyEscapeCommand {
fn range(&self) -> TextRange {
self.range
}
@ -2719,7 +2719,7 @@ impl Ranged for crate::Stmt {
Self::Pass(node) => node.range(),
Self::Break(node) => node.range(),
Self::Continue(node) => node.range(),
Stmt::LineMagic(node) => node.range(),
Stmt::IpyEscapeCommand(node) => node.range(),
}
}
}
@ -2859,7 +2859,7 @@ impl Ranged for crate::nodes::ExprSlice {
self.range
}
}
impl Ranged for ExprLineMagic {
impl Ranged for ExprIpyEscapeCommand {
fn range(&self) -> TextRange {
self.range
}
@ -2894,7 +2894,7 @@ impl Ranged for crate::Expr {
Self::List(node) => node.range(),
Self::Tuple(node) => node.range(),
Self::Slice(node) => node.range(),
Expr::LineMagic(node) => node.range(),
Expr::IpyEscapeCommand(node) => node.range(),
}
}
}

View file

@ -199,7 +199,7 @@ pub fn relocate_expr(expr: &mut Expr, location: TextRange) {
relocate_expr(expr, location);
}
}
Expr::LineMagic(nodes::ExprLineMagic { range, .. }) => {
Expr::IpyEscapeCommand(nodes::ExprIpyEscapeCommand { range, .. }) => {
*range = location;
}
}

View file

@ -312,7 +312,7 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
Stmt::Global(_) => {}
Stmt::Nonlocal(_) => {}
Stmt::Expr(ast::StmtExpr { value, range: _ }) => visitor.visit_expr(value),
Stmt::Pass(_) | Stmt::Break(_) | Stmt::Continue(_) | Stmt::LineMagic(_) => {}
Stmt::Pass(_) | Stmt::Break(_) | Stmt::Continue(_) | Stmt::IpyEscapeCommand(_) => {}
}
}
@ -543,7 +543,7 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
visitor.visit_expr(expr);
}
}
Expr::LineMagic(_) => {}
Expr::IpyEscapeCommand(_) => {}
}
}

View file

@ -418,7 +418,7 @@ where
| Stmt::Continue(_)
| Stmt::Global(_)
| Stmt::Nonlocal(_)
| Stmt::LineMagic(_) => {}
| Stmt::IpyEscapeCommand(_) => {}
}
}
@ -719,7 +719,7 @@ where
visitor.visit_expr(expr);
}
}
Expr::LineMagic(_) => (),
Expr::IpyEscapeCommand(_) => (),
}
}