Permalink
Browse files

fix cursor positioning after expansion (need to be ported to the kate…

…/pate playground git).
  • Loading branch information...
1 parent f2e5737 commit 2a44586b8e9d7533b3ab0469b70f232258039a33 @zaufi committed Jun 9, 2012
Showing with 98 additions and 69 deletions.
  1. +16 −8 expand/expand.py
  2. +71 −49 expand/text_x-c++src.expand
  3. +11 −12 format.py
View
24 expand/expand.py
@@ -270,10 +270,10 @@ def replaceAbsolutePathWithLinkCallback(match):
replacement = replacement.replace('\n', '\n' + whitespace)
# cursor position set?
cursorAdvancement = None
- if '\1' in replacement:
- cursorAdvancement = replacement.index('\1')
- # strip around that byte
- replacement = replacement[:cursorAdvancement] + replacement[cursorAdvancement + 1:]
+ if '%{cursor}' in replacement:
+ cursorAdvancement = replacement.index('%{cursor}')
+ # strip around that word
+ replacement = replacement[:cursorAdvancement] + replacement[cursorAdvancement + 9:]
# make the removal and insertion an atomic operation
document.startEditing()
if argument_range is not None:
@@ -284,10 +284,18 @@ def replaceAbsolutePathWithLinkCallback(match):
document.endEditing()
if cursorAdvancement is not None:
- # print 'advancing', cursorAdvancement
- smart = document.smartInterface().newSmartCursor(insertPosition)
- smart.advance(cursorAdvancement)
- view.setCursorPosition(smart)
+ # TODO The smartInterface isn't available anymore!
+ # But it's successor (movingInterface) isn't available yet in
+ # PyKDE4 <= 4.8.3 (at least) :(
+ while True:
+ currentLength = document.lineLength(insertPosition.line())
+ if cursorAdvancement <= currentLength:
+ break
+ else:
+ insertPosition.setLine(insertPosition.line() + 1)
+ cursorAdvancement -= currentLength + 1 # NOTE +1 for every \n char
+ insertPosition.setColumn(cursorAdvancement)
+ view.setCursorPosition(insertPosition)
# kate: space-indent on;
View
120 expand/text_x-c++src.expand
@@ -1,16 +1,19 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
+from libkatepate import ui
+
def main():
return \
'''int main(int argc, const char* const argv[])
{
+ %{cursor}
return 0;
}
'''
def fori(count):
- return 'for (int i = 0; i < ' + unicode(count) + '; ++i)\n'
+ return 'for (int i = 0; i < ' + count + '; ++i)\n ${cursor}'
def fore(typename, name):
template = \
@@ -21,73 +24,86 @@ def fore(typename, name):
; ++it
)
{{
-
+ %{{cursor}}
}}
'''
return template.format(typename, name)
-def cl(name):
+def st(name, *templateParams):
+ ''' Insert a structure definition
+
+ A first mandatory parameter is a structure name.
+ Everything after that will be template parameters.
+ Template parameter declaration may contain a default value:
+ st(foo, T, U = T, V = int)
+
+ will expand into:
+ template <typename T, typename U = T, typename V = int>
+ struct foo
+ {
+ };
+ '''
+ if len(templateParams):
+ template = \
+'''/**
+ * \\brief {0}
+ */
+template <{1}>
+struct {0}
+{{
+}};
+'''
+ return template.format(name, 'typename ' + ', typename '.join(templateParams))
template = \
'''/**
- * \\brief
- *
+ * \\brief {0}
*/
-class {0}
+struct {0}
{{
-public:
- /// Default constructor
- {0}()
- {{
- }}
- virtual ~{0}()
- {{
- }}
}};
'''
return template.format(name)
-def clt(name):
- template = \
+def cl(name, *templateParams):
+ ''' Insert a class definition
+
+ TODO It would be neat to have syntax like this:
+ cl(test, T, U, d=v, cc=del, mv=def)
+ i.e. define a class 'test' w/ template params T and U,
+ w/ virtual detor, w/ deleted copy ctor/assign and w/
+ defaulted move ctor/assign.
+
+ Isn't it?
+ '''
+ if len(templateParams):
+ template = \
'''/**
- * \\brief
- *
+ * \\brief {0}
*/
-template <typename T>
+template <{1}>
class {0}
{{
-public:
- /// Default constructor
- {0}()
+ %{{cursor}}explicit {0}()
{{
}}
virtual ~{0}()
{{
}}
}};
'''
- return template.format(name)
-
-def st(name):
- template = \
-'''/**
- * \\brief
- *
- */
-struct {0}
-{{
-}};
-'''
- return template.format(name)
-
-def stt(name):
+ return template.format(name, 'typename ' + ', typename '.join(templateParams))
template = \
'''/**
- * \\brief
- *
+ * \\brief {0}
*/
-template <typename T>
-struct {0}
+class {0}
{{
+ %{{cursor}}explicit {0}()
+ {{
+ }}
+ virtual ~{0}()
+ {{
+ }}
}};
'''
return template.format(name)
@@ -98,11 +114,13 @@ def t():
def brief():
return \
'''/**
- * \\brief
+ * \\brief %{cursor}
*
*/'''
def ns(name = None):
+ ''' Insert named or anonymous namespace
+ '''
if bool(name):
nsList = name.split('::')
if len(nsList) > 1:
@@ -119,19 +137,21 @@ def ns(name = None):
else:
template = \
'''namespace {0} {{
-
+%{{cursor}}
}} // namespace {0}
'''
else:
name = 'anonymous'
template = \
'''namespace {{
-
+%{{cursor}}
}} // {0} namespace
'''
return template.format(name)
-def gs(type, name, ref = None):
+def gs(typeT, name, ref = None):
+ ''' Insert getter/setter for mem
+ '''
template = \
'''{0} {1}() const
{{
@@ -140,15 +160,17 @@ def gs(type, name, ref = None):
void {1}({0} value)
{{
{2} = value;
-}}
+}}%{{cursor}}
'''
if ref == None:
- arg = type
+ arg = typeT
else:
- arg = 'const ' + type + ref
+ arg = 'const ' + typeT + ref
- if name.startswith('m_'):
+ if name.startswith('m_'): # Usual code style: members start w/ 'm_' prefix
pure_name = name[2:]
+ elif name.endswith('_'): # Google code style: members end w/ '_' suffix
+ pure_name = name[0:-1]
else:
pure_name = name
View
23 format.py
@@ -18,7 +18,6 @@
#
import kate
-import kate.gui
from PyKDE4.ktexteditor import KTextEditor
from libkatepate import ui
@@ -105,7 +104,7 @@ def getRangeTopology(breakChars):
nrl, nrc, isT = stack.pop() # remove last position from the stack
if not isT:
nestedRanges.append( # and append a nested range
- kate.KTextEditor.Range(cl, cc, nrl, nrc)
+ KTextEditor.Range(cl, cc, nrl, nrc)
)
else:
raise LookupError(
@@ -134,7 +133,7 @@ def getRangeTopology(breakChars):
nrl, nrc, isT = stack.pop() # remove last position from the stack
if isT:
nestedRanges.append( # and append a nested range
- kate.KTextEditor.Range(cl, cc, nrl, nrc)
+ KTextEditor.Range(cl, cc, nrl, nrc)
)
else:
raise LookupError(
@@ -149,13 +148,13 @@ def getRangeTopology(breakChars):
break
continue
if lineStr[cc] in breakChars and len(stack) == 0:
- breakPositions.append(kate.KTextEditor.Cursor(cl, cc))
+ breakPositions.append(KTextEditor.Cursor(cl, cc))
# Did we found smth on the current line?
if found:
break # Yep! Break the outer loop
if not found:
- return (kate.KTextEditor.Range(), list(), list()) # Return empty ranges if nothing found
+ return (KTextEditor.Range(), list(), list()) # Return empty ranges if nothing found
assert(len(stack) == 0) # stack expected to be empty!
@@ -184,7 +183,7 @@ def getRangeTopology(breakChars):
nrl, nrc, isT = stack.pop() # remove last position from stack
if not isT:
nestedRanges.append( # and append a nested range
- kate.KTextEditor.Range(nrl, nrc, cl, cc)
+ KTextEditor.Range(nrl, nrc, cl, cc)
)
else:
raise LookupError(
@@ -213,7 +212,7 @@ def getRangeTopology(breakChars):
nrl, nrc, isT = stack.pop() # remove last position from the stack
if isT:
nestedRanges.append( # and append a nested range
- kate.KTextEditor.Range(cl, cc, nrl, nrc)
+ KTextEditor.Range(cl, cc, nrl, nrc)
)
else:
raise LookupError(
@@ -227,13 +226,13 @@ def getRangeTopology(breakChars):
break
continue
if lineStr[cc] in breakChars and len(stack) == 0:
- breakPositions.append(kate.KTextEditor.Cursor(cl, cc))
+ breakPositions.append(KTextEditor.Cursor(cl, cc))
# Did we found smth on the current line?
if found:
break # Yep! Break the outer loop
if not found:
- return (kate.KTextEditor.Range(), list(), list()) # Return empty ranges if nothing found
+ return (KTextEditor.Range(), list(), list()) # Return empty ranges if nothing found
assert(len(stack) == 0) # stack expected to be empty!
@@ -243,7 +242,7 @@ def getRangeTopology(breakChars):
" and " + str(closePos[0] + 1) + ',' + str(closePos[1] + 1)
)
- return (kate.KTextEditor.Range(openPos[0], openPos[1], closePos[0], closePos[1]), nestedRanges, breakPositions)
+ return (KTextEditor.Range(openPos[0], openPos[1], closePos[0], closePos[1]), nestedRanges, breakPositions)
def boostFormatText(textRange, indent, breakPositions):
@@ -260,7 +259,7 @@ def boostFormatText(textRange, indent, breakPositions):
for b in breakPositions:
print("* prev pos: " + str(prevPos.line()) + ", " + str(prevPos.column()))
print("* current pos: " + str(b.line()) + ", " + str(b.column()))
- chunk = (document.text(kate.KTextEditor.Range(prevPos, b))).strip()
+ chunk = (document.text(KTextEditor.Range(prevPos, b))).strip()
print("* current chunk:\n'" + chunk + "'")
t = ('\n ').join(chunk.splitlines())
print("* current line:\n'" + t + "'")
@@ -270,7 +269,7 @@ def boostFormatText(textRange, indent, breakPositions):
outText = '\n' + indentStr + ' ' + t + '\n'
breakCh = document.character(b)
- prevPos = kate.KTextEditor.Cursor(b.line(), b.column() + 1)
+ prevPos = KTextEditor.Cursor(b.line(), b.column() + 1)
outText += indentStr

0 comments on commit 2a44586

Please sign in to comment.