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

Commit 0a909cc

Browse files
committed
Emit Newline and handle indentation for Magic commands
1 parent c645115 commit 0a909cc

File tree

1 file changed

+65
-54
lines changed

1 file changed

+65
-54
lines changed

parser/src/lexer.rs

Lines changed: 65 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,12 @@ 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+
}
658664
}
659665

660666
self.consume_normal()?;
@@ -703,10 +709,6 @@ where
703709
spaces = 0;
704710
tabs = 0;
705711
}
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-
}
710712
Some('\x0C') => {
711713
// Form feed character!
712714
// Reset indentation for the Emacs user.
@@ -1485,10 +1487,13 @@ mod tests {
14851487
let tokens = lex_jupyter_source(&source);
14861488
assert_eq!(
14871489
tokens,
1488-
vec![Tok::MagicCommand {
1489-
value: "matplotlib --inline".to_string(),
1490-
kind: MagicKind::Magic
1491-
},]
1490+
vec![
1491+
Tok::MagicCommand {
1492+
value: "matplotlib --inline".to_string(),
1493+
kind: MagicKind::Magic
1494+
},
1495+
Tok::Newline
1496+
]
14921497
)
14931498
}
14941499

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

@@ -1545,54 +1553,47 @@ mod tests {
15451553
value: "".to_string(),
15461554
kind: MagicKind::Magic,
15471555
},
1548-
#[cfg(feature = "full-lexer")]
1549-
Tok::NonLogicalNewline,
1556+
Tok::Newline,
15501557
Tok::MagicCommand {
15511558
value: "".to_string(),
15521559
kind: MagicKind::Magic2,
15531560
},
1554-
#[cfg(feature = "full-lexer")]
1555-
Tok::NonLogicalNewline,
1561+
Tok::Newline,
15561562
Tok::MagicCommand {
15571563
value: "".to_string(),
15581564
kind: MagicKind::Shell,
15591565
},
1560-
#[cfg(feature = "full-lexer")]
1561-
Tok::NonLogicalNewline,
1566+
Tok::Newline,
15621567
Tok::MagicCommand {
15631568
value: "".to_string(),
15641569
kind: MagicKind::ShCap,
15651570
},
1566-
#[cfg(feature = "full-lexer")]
1567-
Tok::NonLogicalNewline,
1571+
Tok::Newline,
15681572
Tok::MagicCommand {
15691573
value: "".to_string(),
15701574
kind: MagicKind::Help,
15711575
},
1572-
#[cfg(feature = "full-lexer")]
1573-
Tok::NonLogicalNewline,
1576+
Tok::Newline,
15741577
Tok::MagicCommand {
15751578
value: "".to_string(),
15761579
kind: MagicKind::Help2,
15771580
},
1578-
#[cfg(feature = "full-lexer")]
1579-
Tok::NonLogicalNewline,
1581+
Tok::Newline,
15801582
Tok::MagicCommand {
15811583
value: "".to_string(),
15821584
kind: MagicKind::Paren,
15831585
},
1584-
#[cfg(feature = "full-lexer")]
1585-
Tok::NonLogicalNewline,
1586+
Tok::Newline,
15861587
Tok::MagicCommand {
15871588
value: "".to_string(),
15881589
kind: MagicKind::Quote,
15891590
},
1590-
#[cfg(feature = "full-lexer")]
1591-
Tok::NonLogicalNewline,
1591+
Tok::Newline,
15921592
Tok::MagicCommand {
15931593
value: "".to_string(),
15941594
kind: MagicKind::Quote2,
15951595
},
1596+
Tok::Newline,
15961597
]
15971598
)
15981599
}
@@ -1611,10 +1612,8 @@ mod tests {
16111612
!!cd /Users/foo/Library/Application\ Support/
16121613
/foo 1 2
16131614
,foo 1 2
1614-
;foo 1 2
1615-
!ls
1616-
"
1617-
.trim();
1615+
;foo 1 2"
1616+
.trim();
16181617
let tokens = lex_jupyter_source(source);
16191618
assert_eq!(
16201619
tokens,
@@ -1623,66 +1622,78 @@ mod tests {
16231622
value: "foo".to_string(),
16241623
kind: MagicKind::Help,
16251624
},
1626-
#[cfg(feature = "full-lexer")]
1627-
Tok::NonLogicalNewline,
1625+
Tok::Newline,
16281626
Tok::MagicCommand {
16291627
value: "foo".to_string(),
16301628
kind: MagicKind::Help2,
16311629
},
1632-
#[cfg(feature = "full-lexer")]
1633-
Tok::NonLogicalNewline,
1630+
Tok::Newline,
16341631
Tok::MagicCommand {
16351632
value: "timeit a = b".to_string(),
16361633
kind: MagicKind::Magic,
16371634
},
1638-
#[cfg(feature = "full-lexer")]
1639-
Tok::NonLogicalNewline,
1635+
Tok::Newline,
16401636
Tok::MagicCommand {
16411637
value: "timeit a % 3".to_string(),
16421638
kind: MagicKind::Magic,
16431639
},
1644-
#[cfg(feature = "full-lexer")]
1645-
Tok::NonLogicalNewline,
1640+
Tok::Newline,
16461641
Tok::MagicCommand {
16471642
value: "matplotlib --inline".to_string(),
16481643
kind: MagicKind::Magic,
16491644
},
1650-
#[cfg(feature = "full-lexer")]
1651-
Tok::NonLogicalNewline,
1645+
Tok::Newline,
16521646
Tok::MagicCommand {
16531647
value: "pwd && ls -a | sed 's/^/\\\\ /'".to_string(),
16541648
kind: MagicKind::Shell,
16551649
},
1656-
#[cfg(feature = "full-lexer")]
1657-
Tok::NonLogicalNewline,
1650+
Tok::Newline,
16581651
Tok::MagicCommand {
16591652
value: "cd /Users/foo/Library/Application\\ Support/".to_string(),
16601653
kind: MagicKind::ShCap,
16611654
},
1662-
#[cfg(feature = "full-lexer")]
1663-
Tok::NonLogicalNewline,
1655+
Tok::Newline,
16641656
Tok::MagicCommand {
16651657
value: "foo 1 2".to_string(),
16661658
kind: MagicKind::Paren,
16671659
},
1668-
#[cfg(feature = "full-lexer")]
1669-
Tok::NonLogicalNewline,
1660+
Tok::Newline,
16701661
Tok::MagicCommand {
16711662
value: "foo 1 2".to_string(),
16721663
kind: MagicKind::Quote,
16731664
},
1674-
#[cfg(feature = "full-lexer")]
1675-
Tok::NonLogicalNewline,
1665+
Tok::Newline,
16761666
Tok::MagicCommand {
16771667
value: "foo 1 2".to_string(),
16781668
kind: MagicKind::Quote2,
16791669
},
1680-
#[cfg(feature = "full-lexer")]
1681-
Tok::NonLogicalNewline,
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,
16821691
Tok::MagicCommand {
1683-
value: "ls".to_string(),
1684-
kind: MagicKind::Shell,
1692+
value: "matplotlib --inline".to_string(),
1693+
kind: MagicKind::Magic,
16851694
},
1695+
Tok::Newline,
1696+
Tok::Dedent,
16861697
]
16871698
)
16881699
}

0 commit comments

Comments
 (0)