diff options
author | Tal Einat <taleinat@gmail.com> | 2019-07-11 17:20:14 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-11 17:20:14 +0300 |
commit | 9b5ce62cac27fec9dea473865d79c2c654312957 (patch) | |
tree | dc55645a8581162532bbeda67e8830db9512f396 /Lib/idlelib/editor.py | |
parent | 79042ac4348ccc09344014f20dd49401579f8795 (diff) | |
download | cpython-git-9b5ce62cac27fec9dea473865d79c2c654312957.tar.gz |
bpo-36390: simplify classifyws(), rename it and add unit tests (GH-14500)
Diffstat (limited to 'Lib/idlelib/editor.py')
-rw-r--r-- | Lib/idlelib/editor.py | 39 |
1 files changed, 16 insertions, 23 deletions
diff --git a/Lib/idlelib/editor.py b/Lib/idlelib/editor.py index 606de71a6a..9b5364f0c7 100644 --- a/Lib/idlelib/editor.py +++ b/Lib/idlelib/editor.py @@ -1281,7 +1281,7 @@ class EditorWindow(object): text.delete(first, last) text.mark_set("insert", first) prefix = text.get("insert linestart", "insert") - raw, effective = classifyws(prefix, self.tabwidth) + raw, effective = get_line_indent(prefix, self.tabwidth) if raw == len(prefix): # only whitespace to the left self.reindent_to(effective + self.indentwidth) @@ -1415,7 +1415,7 @@ class EditorWindow(object): for pos in range(len(lines)): line = lines[pos] if line: - raw, effective = classifyws(line, self.tabwidth) + raw, effective = get_line_indent(line, self.tabwidth) effective = effective + self.indentwidth lines[pos] = self._make_blanks(effective) + line[raw:] self.set_region(head, tail, chars, lines) @@ -1426,7 +1426,7 @@ class EditorWindow(object): for pos in range(len(lines)): line = lines[pos] if line: - raw, effective = classifyws(line, self.tabwidth) + raw, effective = get_line_indent(line, self.tabwidth) effective = max(effective - self.indentwidth, 0) lines[pos] = self._make_blanks(effective) + line[raw:] self.set_region(head, tail, chars, lines) @@ -1461,7 +1461,7 @@ class EditorWindow(object): for pos in range(len(lines)): line = lines[pos] if line: - raw, effective = classifyws(line, tabwidth) + raw, effective = get_line_indent(line, tabwidth) ntabs, nspaces = divmod(effective, tabwidth) lines[pos] = '\t' * ntabs + ' ' * nspaces + line[raw:] self.set_region(head, tail, chars, lines) @@ -1575,8 +1575,8 @@ class EditorWindow(object): def guess_indent(self): opener, indented = IndentSearcher(self.text, self.tabwidth).run() if opener and indented: - raw, indentsmall = classifyws(opener, self.tabwidth) - raw, indentlarge = classifyws(indented, self.tabwidth) + raw, indentsmall = get_line_indent(opener, self.tabwidth) + raw, indentlarge = get_line_indent(indented, self.tabwidth) else: indentsmall = indentlarge = 0 return indentlarge - indentsmall @@ -1585,23 +1585,16 @@ class EditorWindow(object): def index2line(index): return int(float(index)) -# Look at the leading whitespace in s. -# Return pair (# of leading ws characters, -# effective # of leading blanks after expanding -# tabs to width tabwidth) - -def classifyws(s, tabwidth): - raw = effective = 0 - for ch in s: - if ch == ' ': - raw = raw + 1 - effective = effective + 1 - elif ch == '\t': - raw = raw + 1 - effective = (effective // tabwidth + 1) * tabwidth - else: - break - return raw, effective + +_line_indent_re = re.compile(r'[ \t]*') +def get_line_indent(line, tabwidth): + """Return a line's indentation as (# chars, effective # of spaces). + + The effective # of spaces is the length after properly "expanding" + the tabs into spaces, as done by str.expandtabs(tabwidth). + """ + m = _line_indent_re.match(line) + return m.end(), len(m.group().expandtabs(tabwidth)) class IndentSearcher(object): |