@@ -14,20 +14,33 @@ describe("edits", () => {
1414 additional,
1515 expected,
1616 line,
17+ indentLevelHint,
1718 type,
1819 } : {
1920 initial : string ;
2021 additional : string ;
2122 expected : string ;
2223 line ?: number ;
24+ indentLevelHint ?: number ;
2325 type ?: CodeInsertType ;
2426 } ) => {
27+ // Tabs are more maintainable in the examples but we actually work with 4 space indents.
28+ initial = initial . replaceAll ( "\t" , " " ) ;
29+ additional = additional . replaceAll ( "\t" , " " ) ;
30+ expected = expected . replaceAll ( "\t" , " " ) ;
31+
2532 const state = EditorState . create ( {
2633 doc : initial ,
2734 extensions : [ python ( ) ] ,
2835 } ) ;
2936 const transaction = state . update (
30- calculateChanges ( state , additional , type ?? "example" , line )
37+ calculateChanges (
38+ state ,
39+ additional ,
40+ type ?? "example" ,
41+ line ,
42+ indentLevelHint
43+ )
3144 ) ;
3245 const actual = transaction . newDoc . sliceString ( 0 ) ;
3346 const expectedSelection = expected . indexOf ( "█" ) ;
@@ -224,6 +237,15 @@ describe("edits", () => {
224237 type : "example" ,
225238 } ) ;
226239 } ) ;
240+ it ( "while True inside while True is a special case even when inserting after document end" , ( ) => {
241+ check ( {
242+ line : 10 ,
243+ initial : "while True:\n a = 1\n" ,
244+ additional : "while True:\n b = 2\n" ,
245+ expected : `while True:\n a = 1\n${ "\n" . repeat ( 7 ) } b = 2\n` ,
246+ type : "example" ,
247+ } ) ;
248+ } ) ;
227249 it ( "inside while False is not a special case" , ( ) => {
228250 check ( {
229251 line : 2 ,
@@ -387,4 +409,88 @@ describe("edits", () => {
387409 type : "example" ,
388410 } ) ;
389411 } ) ;
412+ it ( "is not misled by whitespace on blank lines - 1" , ( ) => {
413+ check ( {
414+ line : 3 ,
415+ initial : "while True:\n print('Hi')\n \n" ,
416+ additional : "print('Bye')" ,
417+ expected : "while True:\n print('Hi')\n print('Bye')\n \n" ,
418+ type : "example" ,
419+ } ) ;
420+ } ) ;
421+ it ( "is not misled by whitespace on blank lines - 2" , ( ) => {
422+ check ( {
423+ line : 2 ,
424+ initial : "while True:\n \n print('Hi')\n" ,
425+ additional : "print('Bye')" ,
426+ expected : "while True:\n print('Bye')\n \n print('Hi')\n" ,
427+ type : "example" ,
428+ } ) ;
429+ } ) ;
430+ it ( "uses indent hint" , ( ) => {
431+ check ( {
432+ line : 3 ,
433+ initial : "if True:\n\tprint('a')\n" ,
434+ additional : "print('b')" ,
435+ expected : "if True:\n\tprint('a')\nprint('b')\n" ,
436+ type : "example" ,
437+ // This pulls it back a level
438+ indentLevelHint : 0 ,
439+ } ) ;
440+
441+ check ( {
442+ line : 3 ,
443+ initial : "if True:\n\tprint('a')\n" ,
444+ additional : "print('b')" ,
445+ expected : "if True:\n\tprint('a')\n\tprint('b')\n" ,
446+ type : "example" ,
447+ indentLevelHint : 1 ,
448+ } ) ;
449+ } ) ;
450+ it ( "ignores indent hint when greater than calculated indent" , ( ) => {
451+ check ( {
452+ line : 3 ,
453+ initial : "if True:\n\tprint('a')\n" ,
454+ additional : "print('b')" ,
455+ expected : "if True:\n\tprint('a')\n\tprint('b')\n" ,
456+ type : "example" ,
457+ // This is ignored
458+ indentLevelHint : 2 ,
459+ } ) ;
460+ } ) ;
461+ it ( "ignores indent hint when appending to while true" , ( ) => {
462+ check ( {
463+ line : 3 ,
464+ initial : "while True:\n\tprint('a')\n" ,
465+ additional : "print('b')" ,
466+ expected : "while True:\n\tprint('a')\n\tprint('b')\n" ,
467+ type : "example" ,
468+ // This is ignored to make it easy to build typical while True micro:bit programs.
469+ indentLevelHint : 0 ,
470+ } ) ;
471+ } ) ;
472+ it ( "uses indent hint when nested in while True" , ( ) => {
473+ check ( {
474+ line : 4 ,
475+ initial : "while True:\n\tif True:\n\t\tprint('a')\n\tpass\n" ,
476+ additional : "print('b')" ,
477+ expected :
478+ "while True:\n\tif True:\n\t\tprint('a')\n\tprint('b')\n\tpass\n" ,
479+ type : "example" ,
480+ // By default it would indent under the if.
481+ indentLevelHint : 1 ,
482+ } ) ;
483+ } ) ;
484+ it ( "limits use of indent hint based on following line indent" , ( ) => {
485+ check ( {
486+ line : 4 ,
487+ initial : "if True:\n\tif True:\n\t\tprint('a')\n\tprint('b')\n" ,
488+ additional : "print('c')" ,
489+ expected :
490+ "if True:\n\tif True:\n\t\tprint('a')\n\tprint('c')\n\tprint('b')\n" ,
491+ type : "example" ,
492+ // By default it would indent under the if.
493+ indentLevelHint : 0 ,
494+ } ) ;
495+ } ) ;
390496} ) ;
0 commit comments