Show
Ignore:
Timestamp:
03/14/08 17:19:59 (8 months ago)
Author:
rob
Message:

Added electricChar to autoindent
* electricChar is used in the OnChar? method of FundamentalMode? to automatically reindent a line if a certain char is pressed
* converted PythonMode?'s ElectricColon? into electricChar method of PythonAutoindent?
* added CStyleAutoindent's electricChar to handle opening and closing braces and semicolon

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/peppy/plugins/python_mode.py

    r1166 r1170  
    5151    def action(self, index=-1, multiplier=1): 
    5252        self.frame.open("about:sample.py") 
    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() 
    7953 
    8054 
     
    216190        return indent 
    217191 
     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 
    218209 
    219210class PythonMode(JobControlMixin, SimpleFoldFunctionMatchMixin, 
     
    296287 
    297288    def getActions(self): 
    298         return [SamplePython, ElectricColon] 
     289        return [SamplePython]