Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleFixer.py

changeset 5147
d39dd5cee0c8
parent 4830
f609a22f43bd
child 5389
9b1c800daff3
equal deleted inserted replaced
5146:f96c32abd120 5147:d39dd5cee0c8
19 import tokenize 19 import tokenize
20 20
21 # CodeStyleCheckerDialog tries to import FixableCodeStyleIssues what fail under 21 # CodeStyleCheckerDialog tries to import FixableCodeStyleIssues what fail under
22 # Python3. So ignore it. 22 # Python3. So ignore it.
23 try: 23 try:
24 import pep8 24 import pycodestyle
25 except ImportError: 25 except ImportError:
26 pass 26 pass
27 27
28 FixableCodeStyleIssues = [ 28 FixableCodeStyleIssues = [
29 "D111", "D112", "D113", "D121", "D131", "D141", 29 "D111", "D112", "D113", "D121", "D131", "D141",
1290 fix (integer) 1290 fix (integer)
1291 """ 1291 """
1292 line = line - 1 1292 line = line - 1
1293 text = self.__source[line] 1293 text = self.__source[line]
1294 1294
1295 # This is necessary since pep8 sometimes reports columns that goes 1295 # This is necessary since pycodestyle sometimes reports columns that
1296 # past the end of the physical line. This happens in cases like, 1296 # goes past the end of the physical line. This happens in cases like,
1297 # foo(bar\n=None) 1297 # foo(bar\n=None)
1298 col = min(pos, len(text) - 1) 1298 col = min(pos, len(text) - 1)
1299 if text[col].strip(): 1299 if text[col].strip():
1300 newText = text 1300 newText = text
1301 else: 1301 else:
1489 line = line - 1 1489 line = line - 1
1490 text = self.__source[line] 1490 text = self.__source[line]
1491 if not text.lstrip().startswith("import"): 1491 if not text.lstrip().startswith("import"):
1492 return (0, "", 0) 1492 return (0, "", 0)
1493 1493
1494 # pep8 (1.3.1) reports false positive if there is an import 1494 # pycodestyle (1.3.1) reports false positive if there is an import
1495 # statement followed by a semicolon and some unrelated 1495 # statement followed by a semicolon and some unrelated
1496 # statement with commas in it. 1496 # statement with commas in it.
1497 if ';' in text: 1497 if ';' in text:
1498 return (0, "", 0) 1498 return (0, "", 0)
1499 1499
2092 Private method to build a logical line from a list of tokens. 2092 Private method to build a logical line from a list of tokens.
2093 2093
2094 @param tokens list of tokens as generated by tokenize.generate_tokens 2094 @param tokens list of tokens as generated by tokenize.generate_tokens
2095 @return logical line (string) 2095 @return logical line (string)
2096 """ 2096 """
2097 # from pep8.py with minor modifications 2097 # from pycodestyle.py with minor modifications
2098 logical = [] 2098 logical = []
2099 previous = None 2099 previous = None
2100 for t in tokens: 2100 for t in tokens:
2101 token_type, text = t[0:2] 2101 token_type, text = t[0:2]
2102 if token_type in self.SKIP_TOKENS: 2102 if token_type in self.SKIP_TOKENS:
2119 assert logical_line.rstrip() == logical_line 2119 assert logical_line.rstrip() == logical_line
2120 return logical_line 2120 return logical_line
2121 2121
2122 def pep8Expected(self): 2122 def pep8Expected(self):
2123 """ 2123 """
2124 Public method to replicate logic in pep8.py, to know what level to 2124 Public method to replicate logic in pycodestyle.py, to know what level
2125 indent things to. 2125 to indent things to.
2126 2126
2127 @return list of lists, where each list represents valid indent levels 2127 @return list of lists, where each list represents valid indent levels
2128 for the line in question, relative from the initial indent. However, 2128 for the line in question, relative from the initial indent. However,
2129 the first entry is the indent level which was expected. 2129 the first entry is the indent level which was expected.
2130 """ 2130 """
2131 # What follows is an adjusted version of 2131 # What follows is an adjusted version of
2132 # pep8.py:continuation_line_indentation. All of the comments have been 2132 # pycodestyle.py:continuation_line_indentation. All of the comments
2133 # stripped and the 'yield' statements replaced with 'pass'. 2133 # have been stripped and the 'yield' statements replaced with 'pass'.
2134 if not self.tokens: 2134 if not self.tokens:
2135 return 2135 return
2136 2136
2137 first_row = self.tokens[0][2][0] 2137 first_row = self.tokens[0][2][0]
2138 nrows = 1 + self.tokens[-1][2][0] - first_row 2138 nrows = 1 + self.tokens[-1][2][0] - first_row
2231 vi = sorted(vi) 2231 vi = sorted(vi)
2232 2232
2233 valid_indents[row] = vi 2233 valid_indents[row] = vi
2234 2234
2235 # Returning to original continuation_line_indentation() from 2235 # Returning to original continuation_line_indentation() from
2236 # pep8. 2236 # pycodestyle.
2237 visual_indent = indent_chances.get(start[1]) 2237 visual_indent = indent_chances.get(start[1])
2238 last_indent = start 2238 last_indent = start
2239 rel_indent[row] = pep8.expand_indent(line) - indent_level 2239 rel_indent[row] = \
2240 pycodestyle.expand_indent(line) - indent_level
2240 hang = rel_indent[row] - rel_indent[open_row] 2241 hang = rel_indent[row] - rel_indent[open_row]
2241 2242
2242 if token_type == tokenize.OP and text in ']})': 2243 if token_type == tokenize.OP and text in ']})':
2243 pass 2244 pass
2244 elif visual_indent is True: 2245 elif visual_indent is True:

eric ide

mercurial