Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
9b5ce62c
Commit
9b5ce62c
authored
Jul 11, 2019
by
Tal Einat
Committed by
GitHub
Jul 11, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-36390: simplify classifyws(), rename it and add unit tests (GH-14500)
parent
79042ac4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
23 deletions
+77
-23
Lib/idlelib/editor.py
Lib/idlelib/editor.py
+16
-23
Lib/idlelib/idle_test/test_editor.py
Lib/idlelib/idle_test/test_editor.py
+61
-0
No files found.
Lib/idlelib/editor.py
View file @
9b5ce62c
...
...
@@ -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
):
...
...
Lib/idlelib/idle_test/test_editor.py
View file @
9b5ce62c
...
...
@@ -42,5 +42,66 @@ class EditorFunctionTest(unittest.TestCase):
self
.
assertEqual
(
func
(
dummy
,
inp
),
out
)
class
TestGetLineIndent
(
unittest
.
TestCase
):
def
test_empty_lines
(
self
):
for
tabwidth
in
[
1
,
2
,
4
,
6
,
8
]:
for
line
in
[
''
,
'
\
n
'
]:
with
self
.
subTest
(
line
=
line
,
tabwidth
=
tabwidth
):
self
.
assertEqual
(
editor
.
get_line_indent
(
line
,
tabwidth
=
tabwidth
),
(
0
,
0
),
)
def
test_tabwidth_4
(
self
):
# (line, (raw, effective))
tests
=
((
'no spaces'
,
(
0
,
0
)),
# Internal space isn't counted.
(
' space test'
,
(
4
,
4
)),
(
'
\
t
tab test'
,
(
1
,
4
)),
(
'
\
t
\
t
double tabs test'
,
(
2
,
8
)),
# Different results when mixing tabs and spaces.
(
'
\
t
mixed test'
,
(
5
,
8
)),
(
'
\
t
mixed test'
,
(
5
,
6
)),
(
'
\
t
mixed test'
,
(
5
,
8
)),
# Spaces not divisible by tabwidth.
(
'
\
t
mixed test'
,
(
3
,
4
)),
(
'
\
t
mixed test'
,
(
3
,
5
)),
(
'
\
t
mixed test'
,
(
3
,
6
)),
# Only checks spaces and tabs.
(
'
\
n
newline test'
,
(
0
,
0
)))
for
line
,
expected
in
tests
:
with
self
.
subTest
(
line
=
line
):
self
.
assertEqual
(
editor
.
get_line_indent
(
line
,
tabwidth
=
4
),
expected
,
)
def
test_tabwidth_8
(
self
):
# (line, (raw, effective))
tests
=
((
'no spaces'
,
(
0
,
0
)),
# Internal space isn't counted.
(
' space test'
,
(
8
,
8
)),
(
'
\
t
tab test'
,
(
1
,
8
)),
(
'
\
t
\
t
double tabs test'
,
(
2
,
16
)),
# Different results when mixing tabs and spaces.
(
'
\
t
mixed test'
,
(
9
,
16
)),
(
'
\
t
mixed test'
,
(
9
,
10
)),
(
'
\
t
mixed test'
,
(
9
,
16
)),
# Spaces not divisible by tabwidth.
(
'
\
t
mixed test'
,
(
3
,
8
)),
(
'
\
t
mixed test'
,
(
3
,
9
)),
(
'
\
t
mixed test'
,
(
3
,
10
)),
# Only checks spaces and tabs.
(
'
\
n
newline test'
,
(
0
,
0
)))
for
line
,
expected
in
tests
:
with
self
.
subTest
(
line
=
line
):
self
.
assertEqual
(
editor
.
get_line_indent
(
line
,
tabwidth
=
8
),
expected
,
)
if
__name__
==
'__main__'
:
unittest
.
main
(
verbosity
=
2
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment