@@ -655,6 +655,12 @@ where
655
655
// Detect indentation levels
656
656
if self . at_begin_of_line {
657
657
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
+ }
658
664
}
659
665
660
666
self . consume_normal ( ) ?;
@@ -703,10 +709,6 @@ where
703
709
spaces = 0 ;
704
710
tabs = 0 ;
705
711
}
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
- }
710
712
Some ( '\x0C' ) => {
711
713
// Form feed character!
712
714
// Reset indentation for the Emacs user.
@@ -1485,10 +1487,13 @@ mod tests {
1485
1487
let tokens = lex_jupyter_source ( & source) ;
1486
1488
assert_eq ! (
1487
1489
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
+ ]
1492
1497
)
1493
1498
}
1494
1499
@@ -1512,10 +1517,13 @@ mod tests {
1512
1517
let tokens = lex_jupyter_source ( & source) ;
1513
1518
assert_eq ! (
1514
1519
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
+ ]
1519
1527
)
1520
1528
}
1521
1529
@@ -1545,54 +1553,47 @@ mod tests {
1545
1553
value: "" . to_string( ) ,
1546
1554
kind: MagicKind :: Magic ,
1547
1555
} ,
1548
- #[ cfg( feature = "full-lexer" ) ]
1549
- Tok :: NonLogicalNewline ,
1556
+ Tok :: Newline ,
1550
1557
Tok :: MagicCommand {
1551
1558
value: "" . to_string( ) ,
1552
1559
kind: MagicKind :: Magic2 ,
1553
1560
} ,
1554
- #[ cfg( feature = "full-lexer" ) ]
1555
- Tok :: NonLogicalNewline ,
1561
+ Tok :: Newline ,
1556
1562
Tok :: MagicCommand {
1557
1563
value: "" . to_string( ) ,
1558
1564
kind: MagicKind :: Shell ,
1559
1565
} ,
1560
- #[ cfg( feature = "full-lexer" ) ]
1561
- Tok :: NonLogicalNewline ,
1566
+ Tok :: Newline ,
1562
1567
Tok :: MagicCommand {
1563
1568
value: "" . to_string( ) ,
1564
1569
kind: MagicKind :: ShCap ,
1565
1570
} ,
1566
- #[ cfg( feature = "full-lexer" ) ]
1567
- Tok :: NonLogicalNewline ,
1571
+ Tok :: Newline ,
1568
1572
Tok :: MagicCommand {
1569
1573
value: "" . to_string( ) ,
1570
1574
kind: MagicKind :: Help ,
1571
1575
} ,
1572
- #[ cfg( feature = "full-lexer" ) ]
1573
- Tok :: NonLogicalNewline ,
1576
+ Tok :: Newline ,
1574
1577
Tok :: MagicCommand {
1575
1578
value: "" . to_string( ) ,
1576
1579
kind: MagicKind :: Help2 ,
1577
1580
} ,
1578
- #[ cfg( feature = "full-lexer" ) ]
1579
- Tok :: NonLogicalNewline ,
1581
+ Tok :: Newline ,
1580
1582
Tok :: MagicCommand {
1581
1583
value: "" . to_string( ) ,
1582
1584
kind: MagicKind :: Paren ,
1583
1585
} ,
1584
- #[ cfg( feature = "full-lexer" ) ]
1585
- Tok :: NonLogicalNewline ,
1586
+ Tok :: Newline ,
1586
1587
Tok :: MagicCommand {
1587
1588
value: "" . to_string( ) ,
1588
1589
kind: MagicKind :: Quote ,
1589
1590
} ,
1590
- #[ cfg( feature = "full-lexer" ) ]
1591
- Tok :: NonLogicalNewline ,
1591
+ Tok :: Newline ,
1592
1592
Tok :: MagicCommand {
1593
1593
value: "" . to_string( ) ,
1594
1594
kind: MagicKind :: Quote2 ,
1595
1595
} ,
1596
+ Tok :: Newline ,
1596
1597
]
1597
1598
)
1598
1599
}
@@ -1611,10 +1612,8 @@ mod tests {
1611
1612
!!cd /Users/foo/Library/Application\ Support/
1612
1613
/foo 1 2
1613
1614
,foo 1 2
1614
- ;foo 1 2
1615
- !ls
1616
- "
1617
- . trim ( ) ;
1615
+ ;foo 1 2"
1616
+ . trim ( ) ;
1618
1617
let tokens = lex_jupyter_source ( source) ;
1619
1618
assert_eq ! (
1620
1619
tokens,
@@ -1623,66 +1622,78 @@ mod tests {
1623
1622
value: "foo" . to_string( ) ,
1624
1623
kind: MagicKind :: Help ,
1625
1624
} ,
1626
- #[ cfg( feature = "full-lexer" ) ]
1627
- Tok :: NonLogicalNewline ,
1625
+ Tok :: Newline ,
1628
1626
Tok :: MagicCommand {
1629
1627
value: "foo" . to_string( ) ,
1630
1628
kind: MagicKind :: Help2 ,
1631
1629
} ,
1632
- #[ cfg( feature = "full-lexer" ) ]
1633
- Tok :: NonLogicalNewline ,
1630
+ Tok :: Newline ,
1634
1631
Tok :: MagicCommand {
1635
1632
value: "timeit a = b" . to_string( ) ,
1636
1633
kind: MagicKind :: Magic ,
1637
1634
} ,
1638
- #[ cfg( feature = "full-lexer" ) ]
1639
- Tok :: NonLogicalNewline ,
1635
+ Tok :: Newline ,
1640
1636
Tok :: MagicCommand {
1641
1637
value: "timeit a % 3" . to_string( ) ,
1642
1638
kind: MagicKind :: Magic ,
1643
1639
} ,
1644
- #[ cfg( feature = "full-lexer" ) ]
1645
- Tok :: NonLogicalNewline ,
1640
+ Tok :: Newline ,
1646
1641
Tok :: MagicCommand {
1647
1642
value: "matplotlib --inline" . to_string( ) ,
1648
1643
kind: MagicKind :: Magic ,
1649
1644
} ,
1650
- #[ cfg( feature = "full-lexer" ) ]
1651
- Tok :: NonLogicalNewline ,
1645
+ Tok :: Newline ,
1652
1646
Tok :: MagicCommand {
1653
1647
value: "pwd && ls -a | sed 's/^/\\ \\ /'" . to_string( ) ,
1654
1648
kind: MagicKind :: Shell ,
1655
1649
} ,
1656
- #[ cfg( feature = "full-lexer" ) ]
1657
- Tok :: NonLogicalNewline ,
1650
+ Tok :: Newline ,
1658
1651
Tok :: MagicCommand {
1659
1652
value: "cd /Users/foo/Library/Application\\ Support/" . to_string( ) ,
1660
1653
kind: MagicKind :: ShCap ,
1661
1654
} ,
1662
- #[ cfg( feature = "full-lexer" ) ]
1663
- Tok :: NonLogicalNewline ,
1655
+ Tok :: Newline ,
1664
1656
Tok :: MagicCommand {
1665
1657
value: "foo 1 2" . to_string( ) ,
1666
1658
kind: MagicKind :: Paren ,
1667
1659
} ,
1668
- #[ cfg( feature = "full-lexer" ) ]
1669
- Tok :: NonLogicalNewline ,
1660
+ Tok :: Newline ,
1670
1661
Tok :: MagicCommand {
1671
1662
value: "foo 1 2" . to_string( ) ,
1672
1663
kind: MagicKind :: Quote ,
1673
1664
} ,
1674
- #[ cfg( feature = "full-lexer" ) ]
1675
- Tok :: NonLogicalNewline ,
1665
+ Tok :: Newline ,
1676
1666
Tok :: MagicCommand {
1677
1667
value: "foo 1 2" . to_string( ) ,
1678
1668
kind: MagicKind :: Quote2 ,
1679
1669
} ,
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 ,
1682
1691
Tok :: MagicCommand {
1683
- value: "ls " . to_string( ) ,
1684
- kind: MagicKind :: Shell ,
1692
+ value: "matplotlib --inline " . to_string( ) ,
1693
+ kind: MagicKind :: Magic ,
1685
1694
} ,
1695
+ Tok :: Newline ,
1696
+ Tok :: Dedent ,
1686
1697
]
1687
1698
)
1688
1699
}
0 commit comments