Package pallavi :: Package plugins :: Module ProgrammerActions
[hide private]
[frames] | no frames]

Source Code for Module pallavi.plugins.ProgrammerActions

  1  # Copyright (c) 2006 Dusty Phillips 
  2   
  3  # Permission is hereby granted, free of charge, to any person obtaining a 
  4  # copy of this software and associated documentation files (the "Software"), 
  5  # to deal in the Software without restriction, including without limitation 
  6  # the rights to use, copy, modify, merge, publish, distribute, sublicense, 
  7  # and/or sell copies of the Software, and to permit persons to whom the 
  8  # Software is furnished to do so, subject to the following conditions: 
  9   
 10  # The above copyright notice and this permission notice shall be included in 
 11  # all copies or substantial portions of the Software. 
 12   
 13  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 14  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 15  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 16  # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 17  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 18  # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 19  # DEALINGS IN THE SOFTWARE. 
 20   
 21  '''This plugin will collect generic actions that will be of use to 
 22  programmers but not to others. Such actions include go to line,  
 23  block commenting, etc. 
 24   
 25  This plugin provides the following actions: 
 26  GoToLine 
 27          Go to a specific line number in the file. Prompt for the line 
 28          number if none is specified. 
 29  CommentLines 
 30          Insert a comment string at the beginning of each of the selected 
 31          lines. 
 32  UncommentLines 
 33          Removes a comment character from the beginning of each of the selected 
 34          lines. 
 35  ShiftIndentRight 
 36          Insert indent characters at the beginning of each of the selected lines 
 37  ShiftIndentLeft 
 38          Remove indent characters from the beginning of each of the selected lines 
 39  JoinLines 
 40          Remove the newline between the current and next line 
 41           
 42  This plugin invokes the following actions: 
 43  RequestUserInput 
 44          Request the line number from the user 
 45  GetLexerItem 
 46          Request the comment string from the Lexer''' 
 47  from pallavi import EventActionManager 
 48  import wx 
 49   
 50   
51 -def setup():
52 '''Set up the actions that the plugin provides''' 53 actions.AddAction(EventActionManager.Action("GoToLine", GoToLine, "Go To Line", "Prompt for a line number to visit")) 54 actions.AddAction(EventActionManager.Action("CommentLines", CommentLines, "Comment Lines", "Comment out selected lines")) 55 actions.AddAction(EventActionManager.Action("UncommentLines", UncommentLines, "Uncomment Lines", "Remove comment from selected lines")) 56 actions.AddAction(EventActionManager.Action("ShiftIndentRight", ShiftIndentRight, "Increase Indent", "Insert indentation characters at the beginning of selected lines")) 57 actions.AddAction(EventActionManager.Action("ShiftIndentLeft", ShiftIndentLeft, "Decrease Indent", "Remove indentation characters from the beginning of selected lines")) 58 actions.AddAction(EventActionManager.Action("JoinLines", JoinLines, "Join Lines", "Remove new line between current and next line"))
59
60 -def GoToLine(data=None):
61 '''If data is specified, assume it is a line-number to go to 62 otherwise prompt using an ugly dialog box''' 63 def gotocallback(lineNo): 64 try: 65 linenum = int(lineNo) - 1 66 pallavi.focusedView.focusedTextView.GotoLine(linenum) 67 except: pass
68 69 if data == None: 70 actions.Invoke("RequestUserInput", "Go To Line:", gotocallback) 71 else: 72 gotocallback(data) 73
74 -def CommentLines(data=None):
75 '''Comment the lines by retrieving the comment character from the lexer''' 76 def commentline(textview, lineNo, linestart, comment_string): 77 if comment_string == None: comment_string = "#" 78 textview.InsertText(linestart, comment_string)
79 actions.Invoke("GetLexerItem", "comment_line", lambda comment_string: ActionOnSelected(commentline, comment_string)) 80
81 -def UncommentLines(data=None):
82 '''Remove commenting from selected lines by retrieving a comment character from the lexer 83 and determining if the line starts with it''' 84 def uncommentline(textview, lineNo, linestart, comment_string): 85 if comment_string == None: comment_string = "#" 86 line = textview.GetLine(lineNo) 87 if line.startswith(comment_string): 88 textview.SetSelection(linestart, linestart + len(comment_string)) 89 textview.ReplaceSelection("")
90 91 actions.Invoke("GetLexerItem", "comment_line", lambda comment_string: ActionOnSelected(uncommentline, comment_string)) 92
93 -def ShiftIndentRight(data=None):
94 '''Increase indentation by retrieving the indent string from the lexer''' 95 def indentline(textview, lineNo, linestart, indent_string): 96 if indent_string == None: indent_string = "\t" 97 textview.InsertText(linestart, indent_string)
98 actions.Invoke("GetLexerItem", "indent_string", lambda indent_string: ActionOnSelected(indentline, indent_string)) 99
100 -def ShiftIndentLeft(data=None):
101 '''Decrease indentation byretrieving the indent string character from the lexer 102 and determining if the line starts with it''' 103 def dedentline(textview, lineNo, linestart, indent_string): 104 if indent_string == None: indent_string = "\t" 105 line = textview.GetLine(lineNo) 106 if line.startswith(indent_string): 107 textview.SetSelection(linestart, linestart + len(indent_string)) 108 textview.ReplaceSelection("")
109 110 actions.Invoke("GetLexerItem", "indent_string", lambda indent_string: ActionOnSelected(dedentline, indent_string)) 111
112 -def JoinLines(data=None):
113 '''Join the current line with the next one.''' 114 tv = pallavi.focusedView.focusedTextView 115 tv.SetTargetStart(tv.GetLineEndPosition(tv.LineFromPosition(tv.GetCurrentPos()))) 116 tv.SetTargetEnd(tv.PositionFromLine(tv.LineFromPosition(tv.GetCurrentPos()) + 1 )) 117 tv.ReplaceTarget("")
118
119 -def ActionOnSelected(function, *args):
120 '''Calculate the line numbers of each line that is selected and call 121 function on that line number. Function should accept a reference to 122 the textview, linenumber, and line start position plus any additional non-keyword arguments''' 123 tv = pallavi.focusedView.focusedTextView 124 start = tv.LineFromPosition(tv.GetSelectionStart()) 125 end = tv.LineFromPosition(tv.GetSelectionEnd()) 126 for lineNo in range(start, end+1): 127 linestart = tv.PositionFromLine(lineNo) 128 function(tv, lineNo, linestart, *args)
129