Skip to content
This repository was archived by the owner on Apr 15, 2026. It is now read-only.

Commit 0468622

Browse files
committed
Base continued body in indentation on the indentation of the line before
FIX: Improve the way indentation for the current body is preserved when inenting new lines. Issue codemirror/dev#1370
1 parent cc69f68 commit 0468622

1 file changed

Lines changed: 28 additions & 14 deletions

File tree

src/python.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@ import {delimitedIndent, indentNodeProp, TreeIndentContext,
55
import {globalCompletion, localCompletionSource} from "./complete"
66
export {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+
830
function 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*(else:|elif )/.test(cx.textAfter) ? cx.baseIndent : cx.continue(),
3661
"ForStatement WhileStatement": cx => /^\s*else:/.test(cx.textAfter) ? cx.baseIndent : cx.continue(),
3762
TryStatement: cx => /^\s*(except |finally:|else:)/.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

Comments
 (0)