1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
79 actions.Invoke("GetLexerItem", "comment_line", lambda comment_string: ActionOnSelected(commentline, comment_string))
80
90
91 actions.Invoke("GetLexerItem", "comment_line", lambda comment_string: ActionOnSelected(uncommentline, comment_string))
92
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
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
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
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