FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonVSCode/pythonFiles/rope/contrib/fixsyntax.py at master · modulexcite/pythonVSCode · GitHub
modulexcite
/
pythonVSCode
Public
forked from
DonJayamanne/pythonVSCode
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonVSCode
/
pythonFiles
/
rope
/
contrib
/
fixsyntax.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
181 lines (157 loc) · 6.72 KB
Breadcrumbs
pythonVSCode
/
pythonFiles
/
rope
/
contrib
/
fixsyntax.py
Copy path
File metadata and controls
181 lines (157 loc) · 6.72 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import
rope
.
base
.
codeanalyze
import
rope
.
base
.
evaluate
from
rope
.
base
import
exceptions
from
rope
.
base
import
libutils
from
rope
.
base
import
utils
from
rope
.
base
import
worder
from
rope
.
base
.
codeanalyze
import
ArrayLinesAdapter
,
LogicalLineFinder
class
FixSyntax
(
object
):
def
__init__
(
self
,
project
,
code
,
resource
,
maxfixes
=
1
):
self
.
project
=
project
self
.
code
=
code
self
.
resource
=
resource
self
.
maxfixes
=
maxfixes
@
utils
.
saveit
def
get_pymodule
(
self
):
"""Get a `PyModule`"""
msg
=
None
code
=
self
.
code
tries
=
0
while
True
:
try
:
if
tries
==
0
and
self
.
resource
is
not
None
and
\
self
.
resource
.
read
()
==
code
:
return
self
.
project
.
get_pymodule
(
self
.
resource
,
force_errors
=
True
)
return
libutils
.
get_string_module
(
self
.
project
,
code
,
resource
=
self
.
resource
,
force_errors
=
True
)
except
exceptions
.
ModuleSyntaxError
as
e
:
if
msg
is
None
:
msg
=
'%s:%s %s'
%
(
e
.
filename
,
e
.
lineno
,
e
.
message_
)
if
tries
<
self
.
maxfixes
:
tries
+=
1
self
.
commenter
.
comment
(
e
.
lineno
)
code
=
'
\n
'
.
join
(
self
.
commenter
.
lines
)
else
:
raise
exceptions
.
ModuleSyntaxError
(
e
.
filename
,
e
.
lineno
,
'Failed to fix error: {0}'
.
format
(
msg
))
@
property
@
utils
.
saveit
def
commenter
(
self
):
return
_Commenter
(
self
.
code
)
def
pyname_at
(
self
,
offset
):
pymodule
=
self
.
get_pymodule
()
def
old_pyname
():
word_finder
=
worder
.
Worder
(
self
.
code
,
True
)
expression
=
word_finder
.
get_primary_at
(
offset
)
expression
=
expression
.
replace
(
'
\\
\n
'
,
' '
).
replace
(
'
\n
'
,
' '
)
lineno
=
self
.
code
.
count
(
'
\n
'
,
0
,
offset
)
scope
=
pymodule
.
get_scope
().
get_inner_scope_for_line
(
lineno
)
return
rope
.
base
.
evaluate
.
eval_str
(
scope
,
expression
)
new_code
=
pymodule
.
source_code
def
new_pyname
():
newoffset
=
self
.
commenter
.
transfered_offset
(
offset
)
return
rope
.
base
.
evaluate
.
eval_location
(
pymodule
,
newoffset
)
if
new_code
.
startswith
(
self
.
code
[:
offset
+
1
]):
return
new_pyname
()
result
=
old_pyname
()
if
result
is
None
:
return
new_pyname
()
return
result
class
_Commenter
(
object
):
def
__init__
(
self
,
code
):
self
.
code
=
code
self
.
lines
=
self
.
code
.
split
(
'
\n
'
)
self
.
lines
.
append
(
'
\n
'
)
self
.
origs
=
list
(
range
(
len
(
self
.
lines
)
+
1
))
self
.
diffs
=
[
0
]
*
(
len
(
self
.
lines
)
+
1
)
def
comment
(
self
,
lineno
):
start
=
_logical_start
(
self
.
lines
,
lineno
,
check_prev
=
True
)
-
1
# using self._get_stmt_end() instead of self._get_block_end()
# to lower commented lines
end
=
self
.
_get_stmt_end
(
start
)
indents
=
_get_line_indents
(
self
.
lines
[
start
])
if
0
<
start
:
last_lineno
=
self
.
_last_non_blank
(
start
-
1
)
last_line
=
self
.
lines
[
last_lineno
]
if
last_line
.
rstrip
().
endswith
(
':'
):
indents
=
_get_line_indents
(
last_line
)
+
4
self
.
_set
(
start
,
' '
*
indents
+
'pass'
)
for
line
in
range
(
start
+
1
,
end
+
1
):
self
.
_set
(
line
,
self
.
lines
[
start
])
self
.
_fix_incomplete_try_blocks
(
lineno
,
indents
)
def
transfered_offset
(
self
,
offset
):
lineno
=
self
.
code
.
count
(
'
\n
'
,
0
,
offset
)
diff
=
sum
(
self
.
diffs
[:
lineno
])
return
offset
+
diff
def
_last_non_blank
(
self
,
start
):
while
start
>
0
and
self
.
lines
[
start
].
strip
()
==
''
:
start
-=
1
return
start
def
_get_block_end
(
self
,
lineno
):
end_line
=
lineno
base_indents
=
_get_line_indents
(
self
.
lines
[
lineno
])
for
i
in
range
(
lineno
+
1
,
len
(
self
.
lines
)):
if
_get_line_indents
(
self
.
lines
[
i
])
>=
base_indents
:
end_line
=
i
else
:
break
return
end_line
def
_get_stmt_end
(
self
,
lineno
):
base_indents
=
_get_line_indents
(
self
.
lines
[
lineno
])
for
i
in
range
(
lineno
+
1
,
len
(
self
.
lines
)):
if
_get_line_indents
(
self
.
lines
[
i
])
<=
base_indents
:
return
i
-
1
return
lineno
def
_fix_incomplete_try_blocks
(
self
,
lineno
,
indents
):
block_start
=
lineno
last_indents
=
indents
while
block_start
>
0
:
block_start
=
rope
.
base
.
codeanalyze
.
get_block_start
(
ArrayLinesAdapter
(
self
.
lines
),
block_start
)
-
1
if
self
.
lines
[
block_start
].
strip
().
startswith
(
'try:'
):
indents
=
_get_line_indents
(
self
.
lines
[
block_start
])
if
indents
>
last_indents
:
continue
last_indents
=
indents
block_end
=
self
.
_find_matching_deindent
(
block_start
)
line
=
self
.
lines
[
block_end
].
strip
()
if
not
(
line
.
startswith
(
'finally:'
)
or
line
.
startswith
(
'except '
)
or
line
.
startswith
(
'except:'
)):
self
.
_insert
(
block_end
,
' '
*
indents
+
'finally:'
)
self
.
_insert
(
block_end
+
1
,
' '
*
indents
+
' pass'
)
def
_find_matching_deindent
(
self
,
line_number
):
indents
=
_get_line_indents
(
self
.
lines
[
line_number
])
current_line
=
line_number
+
1
while
current_line
<
len
(
self
.
lines
):
line
=
self
.
lines
[
current_line
]
if
not
line
.
strip
().
startswith
(
'#'
)
and
not
line
.
strip
()
==
''
:
# HACK: We should have used logical lines here
if
_get_line_indents
(
self
.
lines
[
current_line
])
<=
indents
:
return
current_line
current_line
+=
1
return
len
(
self
.
lines
)
-
1
def
_set
(
self
,
lineno
,
line
):
self
.
diffs
[
self
.
origs
[
lineno
]]
+=
len
(
line
)
-
len
(
self
.
lines
[
lineno
])
self
.
lines
[
lineno
]
=
line
def
_insert
(
self
,
lineno
,
line
):
self
.
diffs
[
self
.
origs
[
lineno
]]
+=
len
(
line
)
+
1
self
.
origs
.
insert
(
lineno
,
self
.
origs
[
lineno
])
self
.
lines
.
insert
(
lineno
,
line
)
def
_logical_start
(
lines
,
lineno
,
check_prev
=
False
):
logical_finder
=
LogicalLineFinder
(
ArrayLinesAdapter
(
lines
))
if
check_prev
:
prev
=
lineno
-
1
while
prev
>
0
:
start
,
end
=
logical_finder
.
logical_line_in
(
prev
)
if
end
is
None
or
start
<=
lineno
<
end
:
return
start
if
start
<=
prev
:
break
prev
-=
1
return
logical_finder
.
logical_line_in
(
lineno
)[
0
]
def
_get_line_indents
(
line
):
return
rope
.
base
.
codeanalyze
.
count_line_indents
(
line
)
Back
|
FazBrowse Home
|
New Git URL