Permalink
Browse files

If selection on the only line, extend it to whole line before do comm…

…enting.

Add sw(expr[, case1[, ..., caseN]]) expansion to generate switch statements.
  • Loading branch information...
1 parent aa615ee commit 7c6aa4626428dc924771cfb4b3230c5c06c97d3b @zaufi committed Jun 22, 2012
Showing with 98 additions and 41 deletions.
  1. +9 −0 commentar.py
  2. +2 −6 expand/expand.py
  3. +87 −35 expand/text_x-c++src.expand
View
9 commentar.py
@@ -226,6 +226,15 @@ def commentar():
commentCh = getCommentStyleForDoc(document)
if view.selection():
+ # If selected smth on a single line...
+ selectedRange = view.selectionRange()
+ if selectedRange.start().line() == selectedRange.end().line():
+ # ... extend selection to whole line, before do smth
+ selectedRange.start().setColumn(0)
+ selectedRange.end().setColumn(0)
+ selectedRange.end().setLine(selectedRange.end().line() + 1)
+ view.setSelection(selectedRange)
+
selectedText = view.selectionText().split('\n')
if not bool(selectedText[len(selectedText) - 1]):
selectedText = selectedText[0:-1]
View
8 expand/expand.py
@@ -262,15 +262,11 @@ def replaceAbsolutePathWithLinkCallback(match):
kate.gui.popup('<p style="white-space:pre">%s</p>' % s, icon='dialog-error', timeout=5, maxTextWidth=None, minTextWidth=300)
return
- try:
- replacement = unicode(replacement)
- except UnicodeEncodeError:
- replacement = repr(replacement)
#KateDocumentConfig::cfReplaceTabsDyn
indentCharacters = indentationCharacters(document)
# convert newlines followed by tab characters to whatever spacing
# the user... uses.
- for i in xrange(100):
+ for i in range(100):
if '\n' + (indentCharacters * i) + '\t' in replacement:
replacement = replacement.replace('\n' + (indentCharacters * i) + '\t', '\n' + (indentCharacters * (i + 1)))
insertPosition = word_range.start()
@@ -284,7 +280,7 @@ def replaceAbsolutePathWithLinkCallback(match):
else:
break
replacement = replacement.replace('\n', '\n' + whitespace)
- # cursor position set?
+ # is desired cursor position set?
cursorAdvancement = None
if '%{cursor}' in replacement:
cursorAdvancement = replacement.index('%{cursor}')
View
122 expand/text_x-c++src.expand
@@ -22,44 +22,57 @@
# Insert simple getter/setter implementation for given member and type
#
#
-# main
-# Insert simple main() function
+# main([args=True])
+# Insert simple main() function.
#
#
# ns([name[::nested[::name]]])
# Insert namespace(s). If name ommited anonymous namespace will be rendered.
#
#
+# sw(expr[, case1[, case2[, ..., caseN]]])
+# Render switch statement w/ a given expression and possible cases.
+# If caseX == 'd' or 'default' --> render `default: break;' instead of case,
+# so its better to put 'd' at the end of parameters list.
+#
from libkatepate import ui
-_BRIEF_DOC_TPL = '''
-/**
+_BRIEF_DOC_TPL = \
+'''/**
* \\brief Class \c {0}
*/'''
+
_TEMPLATE_PARAMS_TPL = '''
template <{1}>'''
+
_CLASS_BODY_TPL = '''
class {0}
{{
public:
- /// Default constructor
- %{{cursor}}explicit {0}()
- {{
- }}
- /// Destructor
- virtual ~{0}()
- {{
- }}
+\t/// Default constructor
+\t%{{cursor}}explicit {0}()
+\t{{
+\t}}
+\t/// Destructor
+\tvirtual ~{0}()
+\t{{
+\t}}
}};
'''
+
_STRUCT_BODY_TPL = '''
struct {0}
{{
}};
'''
+_PURE_BRIEF_DOC_TPL = '''/**
+ * \\brief %{cursor}
+ *
+ */'''
+
_NS_TPL = '''namespace {0} {{
%{{cursor}}
{1}{2}// namespace {3}
@@ -70,6 +83,40 @@ _ANONYMOUS_NS_TPL = '''namespace {{
}} // anonymous namespace
'''
+_GET_SET_TPL = '''{0} {1}() const
+{{
+ return {2};
+}}
+void {1}({0} value)
+{{
+ {2} = value;
+}}%{{cursor}}
+'''
+
+_MAIN_WITH_ARGS_TPL = '''int main(int argc, const char* const argv[])
+{
+\t%{cursor}
+\treturn 0;
+}
+'''
+
+_MAIN_TPL = '''int main()
+{
+\treturn 0;
+}'''
+
+_EMPTY_SWITCH_TPL = '''switch (%{cursor})
+{
+\tcase :
+\t\tbreak;
+\tdefault:
+\t\tbreak;
+}'''
+
+_SWITCH_TPL = '''switch ({0})
+{{
+{1}}}'''
+
def st(name, *templateParams):
''' Insert a structure definition
@@ -122,11 +169,7 @@ def t(*params):
def brief():
''' Render a Doxygen brief header
'''
- return \
-'''/**
- * \\brief %{cursor}
- *
- */'''
+ return _PURE_BRIEF_DOC_TPL
def ns(name = None):
@@ -151,16 +194,8 @@ def gs(typeT, name, ref = None):
NOTE If member has a one of "standard" prefix 'm_' or suffix '_',
accessor name will be w/o it!
'''
- template = \
-'''{0} {1}() const
-{{
- return {2};
-}}
-void {1}({0} value)
-{{
- {2} = value;
-}}%{{cursor}}
-'''
+ template = _GET_SET_TPL
+
if ref == None:
arg = typeT
else:
@@ -176,13 +211,30 @@ void {1}({0} value)
return template.format(arg, pure_name, name)
-def main():
- return \
-'''int main(int argc, const char* const argv[])
-{
- %{cursor}
- return 0;
-}
-'''
+def main(args=True):
+ '''Render main() function'''
+ if args:
+ return _MAIN_WITH_ARGS_TPL
+ return _MAIN_TPL
+
+
+def sw(expr = None, *caseList):
+ ''' Render switch() statement w/ a list of cases'''
+ if expr:
+ if caseList:
+ caseStr = ''
+ for case in caseList:
+ if case == 'd' or case == 'def' or case == 'default':
+ caseStr += '\tdefault:\n\t\tbreak;\n'
+ else:
+ caseStr += '\tcase ' + case +':\n\t\tbreak;\n'
+ caseStr += '%{cursor}'
+ else:
+ caseStr = '\tcase :%{cursor}\n\t\tbreak;\n'
+ return _SWITCH_TPL.format(expr, caseStr)
+ else:
+ return _EMPTY_SWITCH_TPL
+def test(expr):
+ return '{\n\t%{cursor}' + expr + '\n}'
# kate: hl python;

0 comments on commit 7c6aa46

Please sign in to comment.