@@ -5,6 +5,28 @@ import {delimitedIndent, indentNodeProp, TreeIndentContext,
55import { globalCompletion , localCompletionSource } from "./complete"
66export { globalCompletion , localCompletionSource }
77
8+ function innerBody ( context : TreeIndentContext ) {
9+ let { node, pos} = context
10+ let lineIndent = context . lineIndent ( pos , - 1 )
11+ let found = null
12+ for ( ; ; ) {
13+ let before = node . childBefore ( pos )
14+ if ( ! before ) {
15+ break
16+ } else if ( before . name == "Comment" ) {
17+ pos = before . from
18+ } else if ( before . name == "Body" ) {
19+ if ( context . baseIndentFor ( before ) + context . unit <= lineIndent ) found = before
20+ node = before
21+ } else if ( before . type . is ( "Statement" ) ) {
22+ node = before
23+ } else {
24+ break
25+ }
26+ }
27+ return found
28+ }
29+
830function indentBody ( context : TreeIndentContext , node : SyntaxNode ) {
931 let base = context . baseIndentFor ( node )
1032 let line = context . lineAt ( context . pos , - 1 ) , to = line . from + line . text . length
@@ -31,7 +53,10 @@ export const pythonLanguage = LRLanguage.define({
3153 parser : parser . configure ( {
3254 props : [
3355 indentNodeProp . add ( {
34- Body : context => indentBody ( context , context . node ) ?? context . continue ( ) ,
56+ Body : context => {
57+ let inner = innerBody ( context )
58+ return indentBody ( context , inner || context . node ) ?? context . continue ( )
59+ } ,
3560 IfStatement : cx => / ^ \s * ( e l s e : | e l i f ) / . test ( cx . textAfter ) ? cx . baseIndent : cx . continue ( ) ,
3661 "ForStatement WhileStatement" : cx => / ^ \s * e l s e : / . test ( cx . textAfter ) ? cx . baseIndent : cx . continue ( ) ,
3762 TryStatement : cx => / ^ \s * ( e x c e p t | f i n a l l y : | e l s e : ) / . test ( cx . textAfter ) ? cx . baseIndent : cx . continue ( ) ,
@@ -40,19 +65,8 @@ export const pythonLanguage = LRLanguage.define({
4065 "ArrayExpression ArrayComprehensionExpression" : delimitedIndent ( { closing : "]" } ) ,
4166 "String FormatString" : ( ) => null ,
4267 Script : context => {
43- if ( context . pos + / \s * / . exec ( context . textAfter ) ! [ 0 ] . length >= context . node . to ) {
44- let endBody = null
45- for ( let cur : SyntaxNode | null = context . node , to = cur . to ; ; ) {
46- cur = cur . lastChild
47- if ( ! cur || cur . to != to ) break
48- if ( cur . type . name == "Body" ) endBody = cur
49- }
50- if ( endBody ) {
51- let bodyIndent = indentBody ( context , endBody )
52- if ( bodyIndent != null ) return bodyIndent
53- }
54- }
55- return context . continue ( )
68+ let inner = innerBody ( context )
69+ return ( inner && indentBody ( context , inner ) ) ?? context . continue ( )
5670 }
5771 } ) ,
5872 foldNodeProp . add ( {
0 commit comments