8000 Fix tab-based indentation tracking · jrdek/python@51af501 · GitHub
[go: up one dir, main page]

Skip to content

Commit 51af501

Browse files
committed
Fix tab-based indentation tracking
FIX: Fix a bug where indentation with tabs was tracked incorrectly, leading to spurious indent tokens and malformed trees. Issue codemirror/dev#770
1 parent 3c02a74 commit 51af501

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/tokens.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,20 @@ function IndentLevel(parent, depth) {
6161

6262
const topIndent = new IndentLevel(null, 0)
6363

64+
function countIndent(space) {
65+
let depth = 0
66+
for (let i = 0; i < space.length; i++)
67+
depth += space.charCodeAt(i) == tab ? 8 - (depth % 8) : 1
68+
return depth
69+
}
70+
6471
export const trackIndent = new ContextTracker({
6572
start: topIndent,
6673
reduce(context, term) {
6774
return context.depth < 0 && bracketed.indexOf(term) > -1 ? context.parent : context
6875
},
6976
shift(context, term, stack, input) {
70-
if (term == indent) return new IndentLevel(context, stack.pos - input.pos)
77+
if (term == indent) return new IndentLevel(context, countIndent(input.read(input.pos, stack.pos)))
7178
if (term == dedent) return context.parent
7279
if (term == ParenL || term == BracketL || term == BraceL) return new IndentLevel(context, -1)
7380
return context

test/statement.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,22 @@ def x():
329329
==>
330330

331331
Script(FunctionDefinition(def,VariableName,ParamList,Body(ExpressionStatement(VariableName),ExpressionStatement(VariableName))))
332+
333+
# Can handle tab indentation
334+
335+
class Employee:
336+
first_name: str
337+
last_name: str
338+
339+
def __init__(self, a):
340+
self.first_name = a
341+
self.last_name = a
342+
343+
==>
344+
345+
Script(ClassDefinition(class,VariableName,Body(
346+
AssignStatement(VariableName,TypeDef(VariableName)),
347+
AssignStatement(VariableName,TypeDef(VariableName)),
348+
FunctionDefinition(def,VariableName,ParamList(VariableName,VariableName),Body(
349+
AssignStatement(MemberExpression(VariableName,PropertyName),AssignOp,VariableName),
350+
AssignStatement(MemberExpression(VariableName,PropertyName),AssignOp,VariableName))))))

0 commit comments

Comments
 (0)
0