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

@ -0,0 +1,12 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::ExprIpyEscapeCommand;
#[derive(Default)]
pub struct FormatExprIpyEscapeCommand;
impl FormatNodeRule<ExprIpyEscapeCommand> for FormatExprIpyEscapeCommand {
fn fmt_fields(&self, item: &ExprIpyEscapeCommand, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item)])
}
}

View file

@ -1,12 +0,0 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::ExprLineMagic;
#[derive(Default)]
pub struct FormatExprLineMagic;
impl FormatNodeRule<ExprLineMagic> for FormatExprLineMagic {
fn fmt_fields(&self, item: &ExprLineMagic, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item)])
}
}

View file

@ -31,8 +31,8 @@ pub(crate) mod expr_f_string;
pub(crate) mod expr_formatted_value;
pub(crate) mod expr_generator_exp;
pub(crate) mod expr_if_exp;
pub(crate) mod expr_ipy_escape_command;
pub(crate) mod expr_lambda;
pub(crate) mod expr_line_magic;
pub(crate) mod expr_list;
pub(crate) mod expr_list_comp;
pub(crate) mod expr_name;
@ -102,7 +102,7 @@ impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
Expr::List(expr) => expr.format().fmt(f),
Expr::Tuple(expr) => expr.format().fmt(f),
Expr::Slice(expr) => expr.format().fmt(f),
Expr::LineMagic(_) => todo!(),
Expr::IpyEscapeCommand(_) => todo!(),
});
let parenthesize = match parentheses {
@ -240,7 +240,7 @@ impl NeedsParentheses for Expr {
Expr::List(expr) => expr.needs_parentheses(parent, context),
Expr::Tuple(expr) => expr.needs_parentheses(parent, context),
Expr::Slice(expr) => expr.needs_parentheses(parent, context),
Expr::LineMagic(_) => todo!(),
Expr::IpyEscapeCommand(_) => todo!(),
}
}
}
@ -434,7 +434,7 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> {
| Expr::Starred(_)
| Expr::Name(_)
| Expr::Slice(_) => {}
Expr::LineMagic(_) => todo!(),
Expr::IpyEscapeCommand(_) => todo!(),
};
walk_expr(self, expr);

View file

@ -930,38 +930,38 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::StmtContinue {
}
}
impl FormatRule<ast::StmtLineMagic, PyFormatContext<'_>>
for crate::statement::stmt_line_magic::FormatStmtLineMagic
impl FormatRule<ast::StmtIpyEscapeCommand, PyFormatContext<'_>>
for crate::statement::stmt_ipy_escape_command::FormatStmtIpyEscapeCommand
{
#[inline]
fn fmt(&self, node: &ast::StmtLineMagic, f: &mut PyFormatter) -> FormatResult<()> {
FormatNodeRule::<ast::StmtLineMagic>::fmt(self, node, f)
fn fmt(&self, node: &ast::StmtIpyEscapeCommand, f: &mut PyFormatter) -> FormatResult<()> {
FormatNodeRule::<ast::StmtIpyEscapeCommand>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::StmtLineMagic {
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::StmtIpyEscapeCommand {
type Format<'a> = FormatRefWithRule<
'a,
ast::StmtLineMagic,
crate::statement::stmt_line_magic::FormatStmtLineMagic,
ast::StmtIpyEscapeCommand,
crate::statement::stmt_ipy_escape_command::FormatStmtIpyEscapeCommand,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::statement::stmt_line_magic::FormatStmtLineMagic::default(),
crate::statement::stmt_ipy_escape_command::FormatStmtIpyEscapeCommand::default(),
)
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::StmtLineMagic {
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::StmtIpyEscapeCommand {
type Format = FormatOwnedWithRule<
ast::StmtLineMagic,
crate::statement::stmt_line_magic::FormatStmtLineMagic,
ast::StmtIpyEscapeCommand,
crate::statement::stmt_ipy_escape_command::FormatStmtIpyEscapeCommand,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::statement::stmt_line_magic::FormatStmtLineMagic::default(),
crate::statement::stmt_ipy_escape_command::FormatStmtIpyEscapeCommand::default(),
)
}
}
@ -1930,38 +1930,38 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ExprSlice {
}
}
impl FormatRule<ast::ExprLineMagic, PyFormatContext<'_>>
for crate::expression::expr_line_magic::FormatExprLineMagic
impl FormatRule<ast::ExprIpyEscapeCommand, PyFormatContext<'_>>
for crate::expression::expr_ipy_escape_command::FormatExprIpyEscapeCommand
{
#[inline]
fn fmt(&self, node: &ast::ExprLineMagic, f: &mut PyFormatter) -> FormatResult<()> {
FormatNodeRule::<ast::ExprLineMagic>::fmt(self, node, f)
fn fmt(&self, node: &ast::ExprIpyEscapeCommand, f: &mut PyFormatter) -> FormatResult<()> {
FormatNodeRule::<ast::ExprIpyEscapeCommand>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::ExprLineMagic {
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::ExprIpyEscapeCommand {
type Format<'a> = FormatRefWithRule<
'a,
ast::ExprLineMagic,
crate::expression::expr_line_magic::FormatExprLineMagic,
ast::ExprIpyEscapeCommand,
crate::expression::expr_ipy_escape_command::FormatExprIpyEscapeCommand,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::expression::expr_line_magic::FormatExprLineMagic::default(),
crate::expression::expr_ipy_escape_command::FormatExprIpyEscapeCommand::default(),
)
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ExprLineMagic {
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ExprIpyEscapeCommand {
type Format = FormatOwnedWithRule<
ast::ExprLineMagic,
crate::expression::expr_line_magic::FormatExprLineMagic,
ast::ExprIpyEscapeCommand,
crate::expression::expr_ipy_escape_command::FormatExprIpyEscapeCommand,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::expression::expr_line_magic::FormatExprLineMagic::default(),
crate::expression::expr_ipy_escape_command::FormatExprIpyEscapeCommand::default(),
)
}
}

View file

@ -17,7 +17,7 @@ pub(crate) mod stmt_global;
pub(crate) mod stmt_if;
pub(crate) mod stmt_import;
pub(crate) mod stmt_import_from;
pub(crate) mod stmt_line_magic;
pub(crate) mod stmt_ipy_escape_command;
pub(crate) mod stmt_match;
pub(crate) mod stmt_nonlocal;
pub(crate) mod stmt_pass;
@ -61,7 +61,7 @@ impl FormatRule<Stmt, PyFormatContext<'_>> for FormatStmt {
Stmt::Break(x) => x.format().fmt(f),
Stmt::Continue(x) => x.format().fmt(f),
Stmt::TypeAlias(x) => x.format().fmt(f),
Stmt::LineMagic(_) => todo!(),
Stmt::IpyEscapeCommand(_) => todo!(),
}
}
}

View file

@ -0,0 +1,12 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::StmtIpyEscapeCommand;
#[derive(Default)]
pub struct FormatStmtIpyEscapeCommand;
impl FormatNodeRule<StmtIpyEscapeCommand> for FormatStmtIpyEscapeCommand {
fn fmt_fields(&self, item: &StmtIpyEscapeCommand, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item)])
}
}

View file

@ -1,12 +0,0 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::StmtLineMagic;
#[derive(Default)]
pub struct FormatStmtLineMagic;
impl FormatNodeRule<StmtLineMagic> for FormatStmtLineMagic {
fn fmt_fields(&self, item: &StmtLineMagic, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [verbatim_text(item)])
}
}