This commit is contained in:
Kovid Goyal 2014-11-24 08:38:00 +05:30
parent 4fb982471f
commit cec29eb6d8

View File

@ -17,12 +17,14 @@ from calibre.gui2.tweak_book.editor.smarts.utils import (
get_leading_whitespace_on_block = lambda editor, previous=False: expand_tabs(lw(editor, previous=previous)) get_leading_whitespace_on_block = lambda editor, previous=False: expand_tabs(lw(editor, previous=previous))
tw = 4 # The tab width (hardcoded to the pep8 value)
def expand_tabs(text): def expand_tabs(text):
return text.replace('\t', ' '*4) return text.replace('\t', ' ' * tw)
class Smarts(NullSmarts): class Smarts(NullSmarts):
override_tab_stop_width = 4 override_tab_stop_width = tw
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
NullSmarts.__init__(self, *args, **kwargs) NullSmarts.__init__(self, *args, **kwargs)
@ -38,12 +40,12 @@ class Smarts(NullSmarts):
if not text.lstrip(): if not text.lstrip():
# cursor is preceded by whitespace # cursor is preceded by whitespace
text = expand_tabs(text) text = expand_tabs(text)
spclen = len(text) - (len(text) % 4) + 4 spclen = len(text) - (len(text) % tw) + tw
cursor.insertText(' ' * spclen) cursor.insertText(' ' * spclen)
editor.setTextCursor(cursor) editor.setTextCursor(cursor)
else: else:
cursor = editor.textCursor() cursor = editor.textCursor()
cursor.insertText(' ' * 4) cursor.insertText(' ' * tw)
editor.setTextCursor(cursor) editor.setTextCursor(cursor)
return True return True
@ -52,7 +54,7 @@ class Smarts(NullSmarts):
if text and not text.lstrip(): if text and not text.lstrip():
# cursor is preceded by whitespace # cursor is preceded by whitespace
text = expand_tabs(text) text = expand_tabs(text)
spclen = max(0, len(text) - (len(text) % 4) - 4) spclen = max(0, len(text) - (len(text) % tw) - tw)
cursor.insertText(' ' * spclen) cursor.insertText(' ' * spclen)
editor.setTextCursor(cursor) editor.setTextCursor(cursor)
return True return True
@ -62,9 +64,9 @@ class Smarts(NullSmarts):
cursor = editor.textCursor() cursor = editor.textCursor()
line = cursor.block().text() line = cursor.block().text()
if line.rstrip().endswith(':'): if line.rstrip().endswith(':'):
ls += ' ' * 4 ls += ' ' * tw
elif self.escape_scope_pat.match(line) is not None: elif self.escape_scope_pat.match(line) is not None:
ls = ls[:-4] ls = ls[:-tw]
cursor.insertText('\n' + ls) cursor.insertText('\n' + ls)
editor.setTextCursor(cursor) editor.setTextCursor(cursor)
return True return True
@ -75,7 +77,7 @@ class Smarts(NullSmarts):
ls = get_leading_whitespace_on_block(editor) ls = get_leading_whitespace_on_block(editor)
pls = get_leading_whitespace_on_block(editor, previous=True) pls = get_leading_whitespace_on_block(editor, previous=True)
if ls and ls >= pls: if ls and ls >= pls:
ls = ls[:-4] ls = ls[:-tw]
text = ls + text.lstrip() + ':' text = ls + text.lstrip() + ':'
cursor.insertText(text) cursor.insertText(text)
editor.setTextCursor(cursor) editor.setTextCursor(cursor)