| 53 | | |
| 54 | | |
| 55 | | class ElectricColon(TextModificationAction): |
| 56 | | name = "Electric Colon" |
| 57 | | tooltip = "Indent the current line when a colon is pressed" |
| 58 | | key_bindings = {'default': 'S-;',} # FIXME: doesn't work to specify ':' |
| 59 | | |
| 60 | | @classmethod |
| 61 | | def worksWithMajorMode(cls, mode): |
| 62 | | return mode.keyword == 'Python' |
| 63 | | |
| 64 | | def action(self, index=-1, multiplier=1): |
| 65 | | s = self.mode |
| 66 | | style = s.GetStyleAt(s.GetSelectionEnd()) |
| 67 | | s.BeginUndoAction() |
| 68 | | s.ReplaceSelection(":") |
| 69 | | if s.isStyleComment(style) or s.isStyleString(style): |
| 70 | | self.dprint("within comment or string: not indenting") |
| 71 | | pass |
| 72 | | else: |
| 73 | | # folding info not automatically updated after a Replace, so |
| 74 | | # do it manually |
| 75 | | linestart = s.PositionFromLine(s.GetCurrentLine()) |
| 76 | | s.Colourise(linestart, s.GetSelectionEnd()) |
| 77 | | s.autoindent.reindentLine(s, dedent_only=True) |
| 78 | | s.EndUndoAction() |
| | 192 | def electricChar(self, stc, uchar): |
| | 193 | """Reindent the line and insert a newline when special chars are typed. |
| | 194 | |
| | 195 | For python mode, a colon should reindent the line (for example, after |
| | 196 | an else statement, it should dedent it one level) |
| | 197 | """ |
| | 198 | if uchar == u':': |
| | 199 | pos = stc.GetCurrentPos() |
| | 200 | s = stc.GetStyleAt(pos) |
| | 201 | if not stc.isStyleComment(s) and not stc.isStyleString(s): |
| | 202 | stc.BeginUndoAction() |
| | 203 | stc.AddText(uchar) |
| | 204 | self.reindentLine(stc, dedent_only=True) |
| | 205 | stc.EndUndoAction() |
| | 206 | return True |
| | 207 | return False |
| | 208 | |