Skip to content
This repository was archived by the owner on Jul 27, 2023. It is now read-only.

Commit 7a222ad

Browse files
committed
Revert "Emit Newline and handle indentation for Magic commands"
This reverts commit 0a909cc. This will be added in the next PR.
1 parent 71c2aa9 commit 7a222ad

File tree

1 file changed

+54
-65
lines changed

1 file changed

+54
-65
lines changed

parser/src/lexer.rs

Lines changed: 54 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -655,12 +655,6 @@ where
655655
// Detect indentation levels
656656
if self.at_begin_of_line {
657657
self.handle_indentations()?;
658-
if self.mode == Mode::Jupyter
659-
// https://github.com/ipython/ipython/blob/635815e8f1ded5b764d66cacc80bbe25e9e2587f/IPython/core/inputtransformer2.py#L345
660-
&& matches!(self.window[0], Some('%' | '!' | '?' | '/' | ';' | ','))
661-
{
662-
self.lex_and_emit_magic_command();
663-
}
664658
}
665659

666660
self.consume_normal()?;
@@ -709,6 +703,10 @@ where
709703
spaces = 0;
710704
tabs = 0;
711705
}
706+
// https://github.com/ipython/ipython/blob/635815e8f1ded5b764d66cacc80bbe25e9e2587f/IPython/core/inputtransformer2.py#L345
707+
Some('%' | '!' | '?' | '/' | ';' | ',') if self.mode == Mode::Jupyter => {
708+
self.lex_and_emit_magic_command();
709+
}
712710
Some('\x0C') => {
713711
// Form feed character!
714712
// Reset indentation for the Emacs user.
@@ -1487,13 +1485,10 @@ mod tests {
14871485
let tokens = lex_jupyter_source(&source);
14881486
assert_eq!(
14891487
tokens,
1490-
vec![
1491-
Tok::MagicCommand {
1492-
value: "matplotlib --inline".to_string(),
1493-
kind: MagicKind::Magic
1494-
},
1495-
Tok::Newline
1496-
]
1488+
vec![Tok::MagicCommand {
1489+
value: "matplotlib --inline".to_string(),
1490+
kind: MagicKind::Magic
1491+
},]
14971492
)
14981493
}
14991494

@@ -1517,13 +1512,10 @@ mod tests {
15171512
let tokens = lex_jupyter_source(&source);
15181513
assert_eq!(
15191514
tokens,
1520-
vec![
1521-
Tok::MagicCommand {
1522-
value: "matplotlib ".to_string(),
1523-
kind: MagicKind::Magic
1524-
},
1525-
Tok::Newline
1526-
]
1515+
vec![Tok::MagicCommand {
1516+
value: "matplotlib ".to_string(),
1517+
kind: MagicKind::Magic
1518+
},]
15271519
)
15281520
}
15291521

@@ -1553,47 +1545,54 @@ mod tests {
15531545
value: "".to_string(),
15541546
kind: MagicKind::Magic,
15551547
},
1556-
Tok::Newline,
1548+
#[cfg(feature = "full-lexer")]
1549+
Tok::NonLogicalNewline,
15571550
Tok::MagicCommand {
15581551
value: "".to_string(),
15591552
kind: MagicKind::Magic2,
15601553
},
1561-
Tok::Newline,
1554+
#[cfg(feature = "full-lexer")]
1555+
Tok::NonLogicalNewline,
15621556
Tok::MagicCommand {
15631557
value: "".to_string(),
15641558
kind: MagicKind::Shell,
15651559
},
1566-
Tok::Newline,
1560+
#[cfg(feature = "full-lexer")]
1561+
Tok::NonLogicalNewline,
15671562
Tok::MagicCommand {
15681563
value: "".to_string(),
15691564
kind: MagicKind::ShCap,
15701565
},
1571-
Tok::Newline,
1566+
#[cfg(feature = "full-lexer")]
1567+
Tok::NonLogicalNewline,
15721568
Tok::MagicCommand {
15731569
value: "".to_string(),
15741570
kind: MagicKind::Help,
15751571
},
1576-
Tok::Newline,
1572+
#[cfg(feature = "full-lexer")]
1573+
Tok::NonLogicalNewline,
15771574
Tok::MagicCommand {
15781575
value: "".to_string(),
15791576
kind: MagicKind::Help2,
15801577
},
1581-
Tok::Newline,
1578+
#[cfg(feature = "full-lexer")]
1579+
Tok::NonLogicalNewline,
15821580
Tok::MagicCommand {
15831581
value: "".to_string(),
15841582
kind: MagicKind::Paren,
15851583
},
1586-
Tok::Newline,
1584+
#[cfg(feature = "full-lexer")]
1585+
Tok::NonLogicalNewline,
15871586
Tok::MagicCommand {
15881587
value: "".to_string(),
15891588
kind: MagicKind::Quote,
15901589
},
1591-
Tok::Newline,
1590+
#[cfg(feature = "full-lexer")]
1591+
Tok::NonLogicalNewline,
15921592
Tok::MagicCommand {
15931593
value: "".to_string(),
15941594
kind: MagicKind::Quote2,
15951595
},
1596-
Tok::Newline,
15971596
]
15981597
)
15991598
}
@@ -1612,8 +1611,10 @@ mod tests {
16121611
!!cd /Users/foo/Library/Application\ Support/
16131612
/foo 1 2
16141613
,foo 1 2
1615-
;foo 1 2"
1616-
.trim();
1614+
;foo 1 2
1615+
!ls
1616+
"
1617+
.trim();
16171618
let tokens = lex_jupyter_source(source);
16181619
assert_eq!(
16191620
tokens,
@@ -1622,78 +1623,66 @@ mod tests {
16221623
value: "foo".to_string(),
16231624
kind: MagicKind::Help,
16241625
},
1625-
Tok::Newline,
1626+
#[cfg(feature = "full-lexer")]
1627+
Tok::NonLogicalNewline,
16261628
Tok::MagicCommand {
16271629
value: "foo".to_string(),
16281630
kind: MagicKind::Help2,
16291631
},
1630-
Tok::Newline,
1632+
#[cfg(feature = "full-lexer")]
1633+
Tok::NonLogicalNewline,
16311634
Tok::MagicCommand {
16321635
value: "timeit a = b".to_string(),
16331636
kind: MagicKind::Magic,
16341637
},
1635-
Tok::Newline,
1638+
#[cfg(feature = "full-lexer")]
1639+
Tok::NonLogicalNewline,
16361640
Tok::MagicCommand {
16371641
value: "timeit a % 3".to_string(),
16381642
kind: MagicKind::Magic,
16391643
},
1640-
Tok::Newline,
1644+
#[cfg(feature = "full-lexer")]
1645+
Tok::NonLogicalNewline,
16411646
Tok::MagicCommand {
16421647
value: "matplotlib --inline".to_string(),
16431648
kind: MagicKind::Magic,
16441649
},
1645-
Tok::Newline,
1650+
#[cfg(feature = "full-lexer")]
1651+
Tok::NonLogicalNewline,
16461652
Tok::MagicCommand {
16471653
value: "pwd && ls -a | sed 's/^/\\\\ /'".to_string(),
16481654
kind: MagicKind::Shell,
16491655
},
1650-
Tok::Newline,
1656+
#[cfg(feature = "full-lexer")]
1657+
Tok::NonLogicalNewline,
16511658
Tok::MagicCommand {
16521659
value: "cd /Users/foo/Library/Application\\ Support/".to_string(),
16531660
kind: MagicKind::ShCap,
16541661
},
1655-
Tok::Newline,
1662+
#[cfg(feature = "full-lexer")]
1663+
Tok::NonLogicalNewline,
16561664
Tok::MagicCommand {
16571665
value: "foo 1 2".to_string(),
16581666
kind: MagicKind::Paren,
16591667
},
1660-
Tok::Newline,
1668+
#[cfg(feature = "full-lexer")]
1669+
Tok::NonLogicalNewline,
16611670
Tok::MagicCommand {
16621671
value: "foo 1 2".to_string(),
16631672
kind: MagicKind::Quote,
16641673
},
1665-
Tok::Newline,
1674+
#[cfg(feature = "full-lexer")]
1675+
Tok::NonLogicalNewline,
16661676
Tok::MagicCommand {
16671677
value: "foo 1 2".to_string(),
16681678
kind: MagicKind::Quote2,
16691679
},
1670-
Tok::Newline,
1671-
]
1672-
)
1673-
}
1674-
1675-
#[test]
1676-
fn test_jupyter_magic_indentation() {
1677-
let source = r"
1678-
if True:
1679-
%matplotlib \
1680-
--inline"
1681-
.trim();
1682-
let tokens = lex_jupyter_source(source);
1683-
assert_eq!(
1684-
tokens,
1685-
vec![
1686-
Tok::If,
1687-
Tok::True,
1688-
Tok::Colon,
1689-
Tok::Newline,
1690-
Tok::Indent,
1680+
#[cfg(feature = "full-lexer")]
1681+
Tok::NonLogicalNewline,
16911682
Tok::MagicCommand {
1692-
value: "matplotlib --inline".to_string(),
1693-
kind: MagicKind::Magic,
1683+
value: "ls".to_string(),
1684+
kind: MagicKind::Shell,
16941685
},
1695-
Tok::Newline,
1696-
Tok::Dedent,
16971686
]
16981687
)
16991688
}

0 commit comments

Comments
 (0)