eric6/QScintilla/DocstringGenerator/PyDocstringGenerator.py

Sun, 11 Apr 2021 18:45:10 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 11 Apr 2021 18:45:10 +0200
changeset 8222
5994b80b8760
parent 8221
0572a215bd2f
child 8228
772103b14c18
permissions
-rw-r--r--

Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).

7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de>
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a docstring generator for Python.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import re
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import collections
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 from .BaseDocstringGenerator import (
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 BaseDocstringGenerator, FunctionInfo, getIndentStr
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 )
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 class PyDocstringGenerator(BaseDocstringGenerator):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 Class implementing a docstring generator for Python.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 def __init__(self, editor):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 Constructor
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 @param editor reference to the editor widget
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 @type Editor
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 """
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8205
diff changeset
29 super().__init__(editor)
7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 self.__quote3 = '"""'
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 self.__quote3Alternate = "'''"
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 def isFunctionStart(self, text):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 Public method to test, if a text is the start of a function or method
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 @param text line of text to be tested
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 @type str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 @return flag indicating that the given text starts a function or
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 method definition
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 @rtype bool
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 if isinstance(text, str):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 text = text.lstrip()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 if text.startswith(("def", "async def")):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 return True
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 return False
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 def hasFunctionDefinition(self, cursorPosition):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 Public method to test, if the cursor is right below a function
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 @param cursorPosition current cursor position (line and column)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 @type tuple of (int, int)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 @return flag indicating cursor is right below a function definition
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 @rtype bool
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 return (
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 self.__getFunctionDefinitionFromBelow(cursorPosition) is not None
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 )
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 def isDocstringIntro(self, cursorPosition):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 Public function to test, if the line up to the cursor position might be
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 introducing a docstring.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 @param cursorPosition current cursor position (line and column)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 @type tuple of (int, int)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 @return flag indicating a potential start of a docstring
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 @rtype bool
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 cline, cindex = cursorPosition
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 lineToCursor = self.editor.text(cline)[:cindex]
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 return self.__isTripleQuotesStart(lineToCursor)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 def __isTripleQuotesStart(self, text):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 Private method to test, if the given text is the start of a triple
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 quoted string.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 @param text text to be inspected
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 @type str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 @return flag indicating a triple quote start
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 @rtype bool
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 docstringTriggers = ('"""', 'r"""', "'''", "r'''")
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 if text.lstrip() in docstringTriggers:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 return True
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 return False
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 def insertDocstring(self, cursorPosition, fromStart=True):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 Public method to insert a docstring for the function at the cursor
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 position.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 @param cursorPosition position of the cursor (line and index)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 @type tuple of (int, int)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 @param fromStart flag indicating that the editor text cursor is placed
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 on the line starting the function definition
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 @type bool
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 if fromStart:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 self.__functionStartLine = cursorPosition[0]
8000
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
109 docstring, insertPos, newCursorLine = (
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
110 self.__generateDocstringFromStart()
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
111 )
7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 else:
8000
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
113 docstring, insertPos, newCursorLine = (
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
114 self.__generateDocstringFromBelow(cursorPosition)
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
115 )
7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 if docstring:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 self.editor.beginUndoAction()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 self.editor.insertAt(docstring, *insertPos)
8000
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
120
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
121 if not fromStart:
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
122 # correct triple quote indentation if neccessary
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
123 functionIndent = self.editor.indentation(
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
124 self.__functionStartLine)
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
125 quoteIndent = self.editor.indentation(insertPos[0])
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
126
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
127 # step 1: unindent quote line until indentation is zero
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
128 while quoteIndent > 0:
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
129 self.editor.unindent(insertPos[0])
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
130 quoteIndent = self.editor.indentation(insertPos[0])
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
131
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
132 # step 2: indent quote line until indentation is one greater
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
133 # than function definition line
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
134 while quoteIndent <= functionIndent:
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
135 self.editor.indent(insertPos[0])
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
136 quoteIndent = self.editor.indentation(insertPos[0])
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
137
7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 self.editor.endUndoAction()
8000
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
139 self.editor.setCursorPosition(
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
140 newCursorLine, len(self.editor.text(newCursorLine)) - 1
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
141 )
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
142
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
143 def insertDocstringFromShortcut(self, cursorPosition):
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
144 """
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
145 Public method to insert a docstring for the function at the cursor
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
146 position initiated via a keyboard shortcut.
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
147
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
148 @param cursorPosition position of the cursor (line and index)
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
149 @type tuple of (int, int)
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
150 """
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
151 result = self.__getFunctionDefinitionFromBelow(cursorPosition)
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
152 if result is not None:
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
153 # cursor is on the line after the function definition
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
154 cline = cursorPosition[0] - 1
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
155 while not self.isFunctionStart(self.editor.text(cline)):
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
156 cline -= 1
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
157 self.__functionStartLine = cline
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
158 elif self.isFunctionStart(self.editor.text(cursorPosition[0])):
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
159 # cursor is on the start line of the function definition
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
160 self.__functionStartLine = cursorPosition[0]
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
161 else:
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
162 # neither after the function definition nor at the start
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
163 # just do nothing
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
164 return
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
165
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
166 docstring, insertPos, newCursorLine = (
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
167 self.__generateDocstringFromStart()
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
168 )
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
169 if docstring:
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
170 self.editor.beginUndoAction()
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
171 self.editor.insertAt(docstring, *insertPos)
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
172 self.editor.endUndoAction()
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
173 self.editor.setCursorPosition(
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
174 newCursorLine, len(self.editor.text(newCursorLine)) - 1
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
175 )
7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 def __getIndentationInsertString(self, text):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 Private method to create the indentation string for the docstring.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 @param text text to based the indentation on
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 @type str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 @return indentation string for docstring
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 @rtype str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 indent = getIndentStr(text)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 indentWidth = self.editor.indentationWidth()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 if indentWidth == 0:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 indentWidth = self.editor.tabWidth()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 return indent + indentWidth * " "
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193 #######################################################################
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 ## Methods to generate the docstring when the text cursor is on the
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 ## line starting the function definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 #######################################################################
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 def __generateDocstringFromStart(self):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 Private method to generate a docstring based on the cursor being
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 placed on the first line of the definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 @return tuple containing the docstring and a tuple containing the
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 insertion line and index
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 @rtype tuple of (str, tuple(int, int))
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 result = self.__getFunctionDefinitionFromStart()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 if result:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 functionDefinition, functionDefinitionLength = result
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 insertLine = self.__functionStartLine + functionDefinitionLength
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 indentation = self.__getIndentationInsertString(functionDefinition)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 sep = self.editor.getLineSeparator()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 bodyStart = insertLine
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 docstringList = self.__generateDocstring(
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 '"', functionDefinition, bodyStart
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 )
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 if docstringList:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 if self.getDocstringType() == "ericdoc":
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 docstringList.insert(0, self.__quote3)
8000
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
222 newCursorLine = insertLine + 1
7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 else:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 docstringList[0] = self.__quote3 + docstringList[0]
8000
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
225 newCursorLine = insertLine
7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 docstringList.append(self.__quote3)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 return (
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 indentation +
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 "{0}{1}".format(sep, indentation).join(docstringList) +
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 sep
8000
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
231 ), (insertLine, 0), newCursorLine
7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232
8000
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
233 return "", (0, 0), 0
7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 def __getFunctionDefinitionFromStart(self):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 Private method to extract the function definition based on the cursor
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 being placed on the first line of the definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 @return text containing the function definition
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 @rtype str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 startLine = self.__functionStartLine
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 endLine = startLine + min(
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 self.editor.lines() - startLine,
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 20 # max. 20 lines of definition allowed
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 )
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 isFirstLine = True
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 functionIndent = ""
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 functionTextList = []
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 for lineNo in range(startLine, endLine):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 text = self.editor.text(lineNo).rstrip()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 if isFirstLine:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 if not self.isFunctionStart(text):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 return None
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 functionIndent = getIndentStr(text)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 isFirstLine = False
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 else:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261 currentIndent = getIndentStr(text)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 if (
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 currentIndent <= functionIndent or
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 self.isFunctionStart(text)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 ):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266 # no function body exists
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 return None
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 if text.strip() == "":
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 # empty line, illegal/incomplete function definition
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 return None
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 if text.endswith("\\"):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 text = text[:-1]
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 functionTextList.append(text)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 if text.endswith(":"):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 # end of function definition reached
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 functionDefinitionLength = len(functionTextList)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 # check, if function is decorated with a supported one
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 if startLine > 0:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283 decoratorLine = self.editor.text(startLine - 1)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 if (
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285 "@classmethod" in decoratorLine or
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286 "@staticmethod" in decoratorLine or
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287 "pyqtSlot" in decoratorLine or # PyQt slot
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
288 "Slot" in decoratorLine # PySide slot
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289 ):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 functionTextList.insert(0, decoratorLine)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292 return "".join(functionTextList), functionDefinitionLength
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294 return None
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296 #######################################################################
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 ## Methods to generate the docstring when the text cursor is on the
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 ## line after the function definition (e.g. after a triple quote).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299 #######################################################################
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301 def __generateDocstringFromBelow(self, cursorPosition):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
302 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
303 Private method to generate a docstring when the gicen position is on
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304 the line below the end of the definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
306 @param cursorPosition position of the cursor (line and index)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307 @type tuple of (int, int)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308 @return tuple containing the docstring and a tuple containing the
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
309 insertion line and index
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310 @rtype tuple of (str, tuple(int, int))
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
311 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
312 functionDefinition = self.__getFunctionDefinitionFromBelow(
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
313 cursorPosition)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
314 if functionDefinition:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
315 lineTextToCursor = (
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
316 self.editor.text(cursorPosition[0])[:cursorPosition[1]]
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
317 )
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
318 insertLine = cursorPosition[0]
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
319 indentation = self.__getIndentationInsertString(functionDefinition)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
320 sep = self.editor.getLineSeparator()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
321 bodyStart = insertLine
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
322
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
323 docstringList = self.__generateDocstring(
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
324 '"', functionDefinition, bodyStart
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
325 )
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
326 if docstringList:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
327 if self.__isTripleQuotesStart(lineTextToCursor):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328 if self.getDocstringType() == "ericdoc":
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329 docstringList.insert(0, "")
8000
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
330 newCursorLine = cursorPosition[0] + 1
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
331 else:
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
332 newCursorLine = cursorPosition[0]
7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
333 docstringList.append("")
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
334 else:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335 if self.getDocstringType() == "ericdoc":
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
336 docstringList.insert(0, self.__quote3)
8000
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
337 newCursorLine = cursorPosition[0] + 1
7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
338 else:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339 docstringList[0] = self.__quote3 + docstringList[0]
8000
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
340 newCursorLine = cursorPosition[0]
7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 docstringList.append(self.__quote3)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 docstring = (
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
343 "{0}{1}".format(sep, indentation).join(docstringList)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344 )
8000
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
345 return docstring, cursorPosition, newCursorLine
7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346
8000
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
347 return "", (0, 0), 0
7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 def __getFunctionDefinitionFromBelow(self, cursorPosition):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351 Private method to extract the function definition based on the cursor
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 being placed on the first line after the definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
353
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354 @param cursorPosition current cursor position (line and column)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
355 @type tuple of (int, int)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356 @return text containing the function definition
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357 @rtype str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359 startLine = cursorPosition[0] - 1
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 endLine = startLine - min(startLine, 20)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
361 # max. 20 lines of definition allowed
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362 isFirstLine = True
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363 functionTextList = []
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
364
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365 for lineNo in range(startLine, endLine, -1):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
366 text = self.editor.text(lineNo).rstrip()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
367 if isFirstLine:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
368 if not text.endswith(":"):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
369 return None
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
370 isFirstLine = False
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
371 elif text.endswith(":") or text == "":
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372 return None
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
374 if text.endswith("\\"):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375 text = text[:-1]
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377 functionTextList.insert(0, text)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
379 if self.isFunctionStart(text):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380 # start of function definition reached
8000
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
381 self.__functionStartLine = lineNo
47b15df088e4 Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7998
diff changeset
382
7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
383 # check, if function is decorated with a supported one
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
384 if lineNo > 0:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385 decoratorLine = self.editor.text(lineNo - 1)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
386 if (
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
387 "@classmethod" in decoratorLine or
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
388 "@staticmethod" in decoratorLine or
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
389 "pyqtSlot" in decoratorLine or # PyQt slot
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
390 "Slot" in decoratorLine # PySide slot
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
391 ):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
392 functionTextList.insert(0, decoratorLine)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
394 return "".join(functionTextList)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
395
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
396 return None
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
397
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
398 #######################################################################
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
399 ## Methods to generate the docstring contents.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
400 #######################################################################
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
401
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
402 def __getFunctionBody(self, functionIndent, startLine):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
403 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
404 Private method to get the function body.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
405
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
406 @param functionIndent indentation string of the function definition
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
407 @type str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
408 @param startLine starting line for the extraction process
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
409 @type int
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
410 @return text containing the function body
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
411 @rtype str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
412 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
413 bodyList = []
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
414
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
415 for line in range(startLine, self.editor.lines()):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
416 text = self.editor.text(line)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417 textIndent = getIndentStr(text)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
419 if text.strip() == "":
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
420 pass
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
421 elif len(textIndent) <= len(functionIndent):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
422 break
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
423
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
424 bodyList.append(text)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426 return "".join(bodyList)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
428 def __generateDocstring(self, quote, functionDef, bodyStartLine):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
430 Private method to generate the list of docstring lines.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
432 @param quote quote string
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433 @type str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
434 @param functionDef text containing the function definition
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
435 @type str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
436 @param bodyStartLine starting line of the function body
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
437 @type int
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
438 @return list of docstring lines
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
439 @rtype list of str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
440 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
441 quote3 = 3 * quote
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
442 if quote == '"':
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
443 quote3replace = 3 * "'"
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
444 elif quote == "'":
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
445 quote3replace = 3 * '"'
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
446 functionInfo = PyFunctionInfo()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
447 functionInfo.parseDefinition(functionDef, quote3, quote3replace)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
448
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
449 if functionInfo.hasInfo:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
450 functionBody = self.__getFunctionBody(functionInfo.functionIndent,
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
451 bodyStartLine)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
452
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
453 if functionBody:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
454 functionInfo.parseBody(functionBody)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
455
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
456 docstringType = self.getDocstringType()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
457 return self._generateDocstringList(functionInfo, docstringType)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
458
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
459 return []
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
460
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
461
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
462 class PyFunctionInfo(FunctionInfo):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
463 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
464 Class implementing an object to extract and store function information.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
465 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
466 def __init__(self):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
467 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
468 Constructor
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
469 """
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8205
diff changeset
470 super().__init__()
7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
471
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
472 def __isCharInPairs(self, posChar, pairs):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
473 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
474 Private method to test, if the given character position is between
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
475 pairs of brackets or quotes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
476
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
477 @param posChar character position to be tested
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
478 @type int
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
479 @param pairs list containing pairs of positions
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
480 @type list of tuple of (int, int)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
481 @return flag indicating the position is in between
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
482 @rtype bool
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
483 """
8221
0572a215bd2f Applied some more code simplifications suggested by the new Simplify checker (Y110, Y111: use any() or all()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
484 return any(posLeft < posChar < posRight
0572a215bd2f Applied some more code simplifications suggested by the new Simplify checker (Y110, Y111: use any() or all()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
485 for (posLeft, posRight) in pairs)
7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
486
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
487 def __findQuotePosition(self, text):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
488 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
489 Private method to find the start and end position of pairs of quotes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
490
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
491 @param text text to be parsed
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
492 @type str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
493 @return list of tuple with start and end position of pairs of quotes
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
494 @rtype list of tuple of (int, int)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
495 @exception IndexError raised when a matching close quote is missing
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
496 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
497 pos = []
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
498 foundLeftQuote = False
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
499
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
500 for index, character in enumerate(text):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
501 if foundLeftQuote is False:
8205
4a0f1f896341 Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8000
diff changeset
502 if character in ("'", '"'):
7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
503 foundLeftQuote = True
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
504 quote = character
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
505 leftPos = index
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
506 else:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
507 if character == quote and text[index - 1] != "\\":
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
508 pos.append((leftPos, index))
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
509 foundLeftQuote = False
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
510
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
511 if foundLeftQuote:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
512 raise IndexError("No matching close quote at: {0}".format(leftPos))
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
513
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
514 return pos
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
515
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
516 def __findBracketPosition(self, text, bracketLeft, bracketRight, posQuote):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
517 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
518 Private method to find the start and end position of pairs of brackets.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
519
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
520 https://stackoverflow.com/questions/29991917/
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
521 indices-of-matching-parentheses-in-python
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
522
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
523 @param text text to be parsed
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
524 @type str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
525 @param bracketLeft character of the left bracket
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
526 @type str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
527 @param bracketRight character of the right bracket
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
528 @type str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
529 @param posQuote list of tuple with start and end position of pairs
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
530 of quotes
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
531 @type list of tuple of (int, int)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
532 @return list of tuple with start and end position of pairs of brackets
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
533 @rtype list of tuple of (int, int)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
534 @exception IndexError raised when a closing or opening bracket is
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
535 missing
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
536 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
537 pos = []
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
538 pstack = []
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
539
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
540 for index, character in enumerate(text):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
541 if (
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
542 character == bracketLeft and
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
543 not self.__isCharInPairs(index, posQuote)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
544 ):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
545 pstack.append(index)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
546 elif (
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
547 character == bracketRight and
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
548 not self.__isCharInPairs(index, posQuote)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
549 ):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
550 if len(pstack) == 0:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
551 raise IndexError(
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
552 "No matching closing parens at: {0}".format(index))
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
553 pos.append((pstack.pop(), index))
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
554
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
555 if len(pstack) > 0:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
556 raise IndexError(
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
557 "No matching opening parens at: {0}".format(pstack.pop()))
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
558
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
559 return pos
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
560
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
561 def __splitArgumentToNameTypeValue(self, argumentsList,
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
562 quote, quoteReplace):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
563 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
564 Private method to split some argument text to name, type and value.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
565
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
566 @param argumentsList list of function argument definitions
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
567 @type list of str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
568 @param quote quote string to be replaced
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
569 @type str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
570 @param quoteReplace quote string to replace the original
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
571 @type str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
572 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
573 for arg in argumentsList:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
574 hasType = False
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
575 hasValue = False
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
576
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
577 colonPosition = arg.find(":")
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
578 equalPosition = arg.find("=")
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
579
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
580 if equalPosition > -1:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
581 hasValue = True
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
582
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
583 if colonPosition > -1:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
584 if not hasValue:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
585 hasType = True
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
586 elif equalPosition > colonPosition:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
587 # exception for def foo(arg1=":")
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
588 hasType = True
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
589
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
590 if hasValue and hasType:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
591 argName = arg[0:colonPosition].strip()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
592 argType = arg[colonPosition + 1:equalPosition].strip()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
593 argValue = arg[equalPosition + 1:].strip()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
594 elif not hasValue and hasType:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
595 argName = arg[0:colonPosition].strip()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
596 argType = arg[colonPosition + 1:].strip()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
597 argValue = None
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
598 elif hasValue and not hasType:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
599 argName = arg[0:equalPosition].strip()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
600 argType = None
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
601 argValue = arg[equalPosition + 1:].strip()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
602 else:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
603 argName = arg.strip()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
604 argType = None
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
605 argValue = None
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
606 if argValue and quote:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
607 # sanitize argValue with respect to quotes
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
608 argValue = argValue.replace(quote, quoteReplace)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
609
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
610 self.argumentsList.append((argName, argType, argValue))
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
611
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
612 def __splitArgumentsTextToList(self, argumentsText):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
613 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
614 Private method to split the given arguments text into a list of
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
615 arguments.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
616
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
617 This function uses a comma to separate arguments and ignores a comma in
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
618 brackets and quotes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
619
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
620 @param argumentsText text containing the list of arguments
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
621 @type str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
622 @return list of individual argument texts
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
623 @rtype list of str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
624 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
625 argumentsList = []
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
626 indexFindStart = 0
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
627 indexArgStart = 0
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
628
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
629 try:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
630 posQuote = self.__findQuotePosition(argumentsText)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
631 posRound = self.__findBracketPosition(
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
632 argumentsText, "(", ")", posQuote)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
633 posCurly = self.__findBracketPosition(
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
634 argumentsText, "{", "}", posQuote)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
635 posSquare = self.__findBracketPosition(
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
636 argumentsText, "[", "]", posQuote)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
637 except IndexError:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
638 return None
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
639
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
640 while True:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
641 posComma = argumentsText.find(",", indexFindStart)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
642
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
643 if posComma == -1:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
644 break
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
645
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
646 indexFindStart = posComma + 1
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
647
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
648 if (
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
649 self.__isCharInPairs(posComma, posRound) or
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
650 self.__isCharInPairs(posComma, posCurly) or
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
651 self.__isCharInPairs(posComma, posSquare) or
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
652 self.__isCharInPairs(posComma, posQuote)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
653 ):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
654 continue
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
655
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
656 argumentsList.append(argumentsText[indexArgStart:posComma])
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
657 indexArgStart = posComma + 1
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
658
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
659 if indexArgStart < len(argumentsText):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
660 argumentsList.append(argumentsText[indexArgStart:])
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
661
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
662 return argumentsList
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
663
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
664 def parseDefinition(self, text, quote, quoteReplace):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
665 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
666 Public method to parse the function definition text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
667
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
668 @param text text containing the function definition
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
669 @type str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
670 @param quote quote string to be replaced
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
671 @type str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
672 @param quoteReplace quote string to replace the original
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
673 @type str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
674 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
675 self.functionIndent = getIndentStr(text)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
676
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
677 textList = text.splitlines()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
678 if textList[0].lstrip().startswith("@"):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
679 # first line of function definition is a decorator
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
680 decorator = textList.pop(0).strip()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
681 if decorator == "@staticmethod":
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
682 self.functionType = "staticmethod"
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
683 elif decorator == "@classmethod":
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
684 self.functionType = "classmethod"
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
685 elif re.match(r"@(PyQt[456]\.)?(QtCore\.)?pyqtSlot", decorator):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
686 self.functionType = "qtslot"
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
687 elif re.match(r"@(PySide[26]\.)?(QtCore\.)?Slot", decorator):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
688 self.functionType = "qtslot"
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
689
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
690 text = "".join(textList).strip()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
691
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
692 if text.startswith("async def "):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
693 self.isAsync = True
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
694
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
695 returnType = re.search(r"->[ ]*([a-zA-Z0-9_,()\[\] ]*):$", text)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
696 if returnType:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
697 self.returnTypeAnnotated = returnType.group(1)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
698 textEnd = text.rfind(returnType.group(0))
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
699 else:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
700 self.returnTypeAnnotated = None
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
701 textEnd = len(text)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
702
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
703 positionArgumentsStart = text.find("(") + 1
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
704 positionArgumentsEnd = text.rfind(")", positionArgumentsStart,
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
705 textEnd)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
706
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
707 self.argumentsText = text[positionArgumentsStart:positionArgumentsEnd]
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
708
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
709 argumentsList = self.__splitArgumentsTextToList(self.argumentsText)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
710 if argumentsList is not None:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
711 self.hasInfo = True
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
712 self.__splitArgumentToNameTypeValue(
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
713 argumentsList, quote, quoteReplace)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
714
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
715 functionName = (
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
716 text[:positionArgumentsStart - 1]
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
717 .replace("async def ", "")
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
718 .replace("def ", "")
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
719 )
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
720 if functionName == "__init__":
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
721 self.functionType = "constructor"
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
722 elif functionName.startswith("__"):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
723 if functionName.endswith("__"):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
724 self.visibility = "special"
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
725 else:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
726 self.visibility = "private"
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
727 elif functionName.startswith("_"):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
728 self.visibility = "protected"
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
729 else:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
730 self.visibility = "public"
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
731
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
732 def parseBody(self, text):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
733 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
734 Public method to parse the function body text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
735
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
736 @param text function body text
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
737 @type str
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
738 """
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
739 raiseRe = re.findall(r"[ \t]raise ([a-zA-Z0-9_]*)", text)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
740 if len(raiseRe) > 0:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
741 self.raiseList = [x.strip() for x in raiseRe]
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
742 # remove duplicates from list while keeping it in the order
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
743 # stackoverflow.com/questions/7961363/removing-duplicates-in-lists
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
744 self.raiseList = list(collections.OrderedDict.fromkeys(
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
745 self.raiseList))
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
746
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
747 yieldRe = re.search(r"[ \t]yield ", text)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
748 if yieldRe:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
749 self.hasYield = True
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
750
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
751 # get return value
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
752 returnPattern = r"return |yield "
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
753 lineList = text.splitlines()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
754 returnFound = False
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
755 returnTmpLine = ""
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
756
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
757 for line in lineList:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
758 line = line.strip()
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
759
8222
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8221
diff changeset
760 if (
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8221
diff changeset
761 returnFound is False and
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8221
diff changeset
762 re.match(returnPattern, line)
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8221
diff changeset
763 ):
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8221
diff changeset
764 returnFound = True
7998
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
765
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
766 if returnFound:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
767 returnTmpLine += line
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
768 # check the integrity of line
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
769 try:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
770 quotePos = self.__findQuotePosition(returnTmpLine)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
771
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
772 if returnTmpLine.endswith("\\"):
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
773 returnTmpLine = returnTmpLine[:-1]
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
774 continue
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
775
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
776 self.__findBracketPosition(
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
777 returnTmpLine, "(", ")", quotePos)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
778 self.__findBracketPosition(
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
779 returnTmpLine, "{", "}", quotePos)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
780 self.__findBracketPosition(
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
781 returnTmpLine, "[", "]", quotePos)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
782 except IndexError:
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
783 continue
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
784
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
785 returnValue = re.sub(returnPattern, "", returnTmpLine)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
786 self.returnValueInBody.append(returnValue)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
787
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
788 returnFound = False
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
789 returnTmpLine = ""

eric ide

mercurial