eric6/QScintilla/TypingCompleters/CompleterPython.py

changeset 8222
5994b80b8760
parent 8218
7c09585bd960
child 8228
772103b14c18
equal deleted inserted replaced
8221:0572a215bd2f 8222:5994b80b8760
141 141
142 # closing parenthesis 142 # closing parenthesis
143 # skip matching closing parenthesis 143 # skip matching closing parenthesis
144 elif char in [')', '}', ']']: 144 elif char in [')', '}', ']']:
145 txt = self.editor.text(line) 145 txt = self.editor.text(line)
146 if col < len(txt) and char == txt[col]: 146 if col < len(txt) and char == txt[col] and self.__skipBrace:
147 if self.__skipBrace: 147 self.editor.setSelection(line, col, line, col + 1)
148 self.editor.setSelection(line, col, line, col + 1) 148 self.editor.removeSelectedText()
149 self.editor.removeSelectedText()
150 149
151 # space 150 # space
152 # insert import, dedent to if for elif, dedent to try for except, 151 # insert import, dedent to if for elif, dedent to try for except,
153 # dedent def 152 # dedent def
154 elif char == ' ': 153 elif char == ' ':
169 elif self.__dedentDef and self.__defOnlyRX.fullmatch(txt): 168 elif self.__dedentDef and self.__defOnlyRX.fullmatch(txt):
170 self.__dedentDefStatement() 169 self.__dedentDefStatement()
171 170
172 # comma 171 # comma
173 # insert blank 172 # insert blank
174 elif char == ',': 173 elif char == ',' and self.__insertBlank:
175 if self.__insertBlank: 174 self.editor.insert(' ')
176 self.editor.insert(' ') 175 self.editor.setCursorPosition(line, col + 1)
177 self.editor.setCursorPosition(line, col + 1)
178 176
179 # open curly brace 177 # open curly brace
180 # insert closing brace 178 # insert closing brace
181 elif char == '{': 179 elif char == '{' and self.__insertClosingBrace:
182 if self.__insertClosingBrace: 180 self.editor.insert('}')
183 self.editor.insert('}')
184 181
185 # open bracket 182 # open bracket
186 # insert closing bracket 183 # insert closing bracket
187 elif char == '[': 184 elif char == '[' and self.__insertClosingBrace:
188 if self.__insertClosingBrace: 185 self.editor.insert(']')
189 self.editor.insert(']')
190 186
191 # double quote 187 # double quote
192 # insert double quote 188 # insert double quote
193 elif char == '"': 189 elif char == '"' and self.__insertQuote:
194 if self.__insertQuote: 190 self.editor.insert('"')
195 self.editor.insert('"')
196 191
197 # quote 192 # quote
198 # insert quote 193 # insert quote
199 elif char == '\'': 194 elif char == '\'' and self.__insertQuote:
200 if self.__insertQuote: 195 self.editor.insert('\'')
201 self.editor.insert('\'')
202 196
203 # colon 197 # colon
204 # skip colon, dedent to if for else: 198 # skip colon, dedent to if for else:
205 elif char == ':': 199 elif char == ':':
206 text = self.editor.text(line) 200 text = self.editor.text(line)
217 elif self.__dedentExcept and self.__finallyRX.fullmatch(txt): 211 elif self.__dedentExcept and self.__finallyRX.fullmatch(txt):
218 self.__dedentFinallyToTry() 212 self.__dedentFinallyToTry()
219 213
220 # new line 214 # new line
221 # indent to opening brace 215 # indent to opening brace
222 elif char == '\n': 216 elif char == '\n' and self.__indentBrace:
223 if self.__indentBrace: 217 txt = self.editor.text(line - 1)
224 txt = self.editor.text(line - 1) 218 if re.search(":\r?\n", txt) is None:
225 if re.search(":\r?\n", txt) is None: 219 self.editor.beginUndoAction()
226 self.editor.beginUndoAction() 220 stxt = txt.strip()
227 stxt = txt.strip() 221 if stxt and stxt[-1] in ("(", "[", "{"):
228 if stxt and stxt[-1] in ("(", "[", "{"): 222 # indent one more level
229 # indent one more level 223 self.editor.indent(line)
230 self.editor.indent(line) 224 self.editor.editorCommand(QsciScintilla.SCI_VCHOME)
231 self.editor.editorCommand(QsciScintilla.SCI_VCHOME) 225 else:
232 else: 226 # indent to the level of the opening brace
233 # indent to the level of the opening brace 227 openCount = len(re.findall("[({[]", txt))
234 openCount = len(re.findall("[({[]", txt)) 228 closeCount = len(re.findall(r"[)}\]]", txt))
235 closeCount = len(re.findall(r"[)}\]]", txt)) 229 if openCount > closeCount:
236 if openCount > closeCount: 230 openCount = 0
237 openCount = 0 231 closeCount = 0
238 closeCount = 0 232 openList = list(re.finditer("[({[]", txt))
239 openList = list(re.finditer("[({[]", txt)) 233 index = len(openList) - 1
240 index = len(openList) - 1 234 while index > -1 and openCount == closeCount:
241 while index > -1 and openCount == closeCount: 235 lastOpenIndex = openList[index].start()
242 lastOpenIndex = openList[index].start() 236 txt2 = txt[lastOpenIndex:]
243 txt2 = txt[lastOpenIndex:] 237 openCount = len(re.findall("[({[]", txt2))
244 openCount = len(re.findall("[({[]", txt2)) 238 closeCount = len(re.findall(r"[)}\]]", txt2))
245 closeCount = len(re.findall(r"[)}\]]", txt2)) 239 index -= 1
246 index -= 1 240 if openCount > closeCount and lastOpenIndex > col:
247 if openCount > closeCount and lastOpenIndex > col: 241 self.editor.insert(
248 self.editor.insert( 242 ' ' * (lastOpenIndex - col + 1))
249 ' ' * (lastOpenIndex - col + 1)) 243 self.editor.setCursorPosition(
250 self.editor.setCursorPosition( 244 line, lastOpenIndex + 1)
251 line, lastOpenIndex + 1) 245 self.editor.endUndoAction()
252 self.editor.endUndoAction()
253 246
254 def __dedentToIf(self): 247 def __dedentToIf(self):
255 """ 248 """
256 Private method to dedent the last line to the last if statement with 249 Private method to dedent the last line to the last if statement with
257 less (or equal) indentation. 250 less (or equal) indentation.

eric ide

mercurial