eric6/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleFixer.py

changeset 7639
422fd05e9c91
parent 7628
f904d0eef264
child 7836
2f0d208b8137
equal deleted inserted replaced
7638:176145438b1e 7639:422fd05e9c91
5 5
6 """ 6 """
7 Module implementing a class to fix certain code style issues. 7 Module implementing a class to fix certain code style issues.
8 """ 8 """
9 9
10 from __future__ import unicode_literals
11 try:
12 # Python 2
13 from StringIO import StringIO # __IGNORE_EXCEPTION__
14 except ImportError:
15 # Python 3
16 from io import StringIO # __IGNORE_WARNING__
17 import os 10 import os
18 import re 11 import re
19 import tokenize 12 import tokenize
13 from io import StringIO
20 14
21 # CodeStyleCheckerDialog tries to import FixableCodeStyleIssues which fails 15 # CodeStyleCheckerDialog tries to import FixableCodeStyleIssues which fails
22 # under Python3. So ignore it. 16 # under Python3. So ignore it.
23 try: 17 try:
24 import pycodestyle 18 import pycodestyle
25 except ImportError: 19 except ImportError:
26 pass 20 pass
27 21
28 FixableCodeStyleIssues = [ 22 FixableCodeStyleIssues = [
29 "D111", "D112", "D113", "D121", "D131", "D141", 23 "D111", "D112", "D121", "D131", "D141", "D142",
30 "D142", "D143", "D144", "D145", 24 "D143", "D144", "D145",
31 "D221", "D222", "D231", "D242", "D243", "D244", 25 "D221", "D222", "D231", "D242", "D243", "D244",
32 "D245", "D246", "D247", 26 "D245", "D246", "D247",
33 "E101", "E111", "E121", "E122", "E123", "E124", 27 "E101", "E111", "E121", "E122", "E123", "E124",
34 "E125", "E126", "E127", "E128", "E133", "E201", 28 "E125", "E126", "E127", "E128", "E133", "E201",
35 "E202", "E203", "E211", "E221", "E222", "E223", 29 "E202", "E203", "E211", "E221", "E222", "E223",
106 self.__eol = eol 100 self.__eol = eol
107 101
108 self.__fixes = { 102 self.__fixes = {
109 "D111": self.__fixD111, 103 "D111": self.__fixD111,
110 "D112": self.__fixD112, 104 "D112": self.__fixD112,
111 "D113": self.__fixD112,
112 "D121": self.__fixD121, 105 "D121": self.__fixD121,
113 "D131": self.__fixD131, 106 "D131": self.__fixD131,
114 "D141": self.__fixD141, 107 "D141": self.__fixD141,
115 "D142": self.__fixD142, 108 "D142": self.__fixD142,
116 "D143": self.__fixD143, 109 "D143": self.__fixD143,
555 # Triple single quotes converted to triple double quotes. 548 # Triple single quotes converted to triple double quotes.
556 return (1, "FIXD111", [], 0) 549 return (1, "FIXD111", [], 0)
557 550
558 def __fixD112(self, code, line, pos): 551 def __fixD112(self, code, line, pos):
559 """ 552 """
560 Private method to fix docstring 'r' or 'u' in leading quotes. 553 Private method to fix docstring 'r' in leading quotes.
561 554
562 Codes: D112, D113 555 Codes: D112
563 556
564 @param code code of the issue 557 @param code code of the issue
565 @type str 558 @type str
566 @param line line number of the issue 559 @param line line number of the issue
567 @type int 560 @type int
573 @rtype tuple of (int, str, list or int, int) 566 @rtype tuple of (int, str, list or int, int)
574 """ 567 """
575 line = line - 1 568 line = line - 1
576 if code == "D112": 569 if code == "D112":
577 insertChar = "r" 570 insertChar = "r"
578 elif code == "D113":
579 insertChar = "u"
580 else: 571 else:
581 return (0, "", 0) 572 return (0, "", 0)
582 573
583 newText = ( 574 newText = (
584 self.__getIndent(self.__source[line]) + 575 self.__getIndent(self.__source[line]) +

eric ide

mercurial