Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shortcut to move a line up and down #337

Merged
merged 1 commit into from
Oct 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 52 additions & 22 deletions ReText/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,35 +173,42 @@ def scrollContentsBy(self, dx, dy):
self.lineNumberArea.update()

def contextMenuEvent(self, event):
# Create base menu
menu = self.createStandardContextMenu()
text = self.toPlainText()
dictionary = self.tab.highlighter.dictionary
if (dictionary is None) or not text:
return QTextEdit.contextMenuEvent(self, event)
if not text:
menu.exec(event.globalPos())
return

# Check word under the cursor
oldcursor = self.textCursor()
cursor = self.cursorForPosition(event.pos())
pos = cursor.positionInBlock()
self.setTextCursor(cursor)
pos = cursor.position()
if pos == len(text):
pos -= 1
curchar = text[pos]
isalpha = curchar.isalpha()
cursor.select(QTextCursor.WordUnderCursor)
if not isalpha or (oldcursor.hasSelection() and
oldcursor.selectedText() != cursor.selectedText()):
return QTextEdit.contextMenuEvent(self, event)
self.setTextCursor(cursor)
word = cursor.selectedText()
if not word or dictionary.check(word):
self.setTextCursor(oldcursor)
return QTextEdit.contextMenuEvent(self, event)
suggestions = dictionary.suggest(word)
actions = [self.parent.act(sug, trig=self.fixWord(sug)) for sug in suggestions]
menu = self.createStandardContextMenu()
menu.insertSeparator(menu.actions()[0])
menu.insertAction(menu.actions()[0], self.parent.act(self.tr('Add to dictionary'),
trig=self.learnWord(word)))
menu.insertSeparator(menu.actions()[0])
for action in actions[::-1]:
menu.insertAction(menu.actions()[0], action)
dictionary = self.tab.highlighter.dictionary
word = None
if isalpha and not (oldcursor.hasSelection() and oldcursor.selectedText() != cursor.selectedText()):
cursor.select(QTextCursor.WordUnderCursor)
word = cursor.selectedText()

if word is not None and not dictionary.check(word):
self.setTextCursor(cursor)
suggestions = dictionary.suggest(word)
actions = [self.parent.act(sug, trig=self.fixWord(sug)) for sug in suggestions]
menu.insertSeparator(menu.actions()[0])
for action in actions[::-1]:
menu.insertAction(menu.actions()[0], action)
menu.insertSeparator(menu.actions()[0])
menu.insertAction(menu.actions()[0], self.parent.act(self.tr('Add to dictionary'), trig=self.learnWord(word)))

menu.addSeparator()
menu.addAction(self.parent.actionMoveUp)
menu.addAction(self.parent.actionMoveDown)

menu.exec(event.globalPos())

def fixWord(self, correctword):
Expand Down Expand Up @@ -277,6 +284,29 @@ def handleReturn(self, cursor):
cursor.insertText('\n' + matchedText)
self.ensureCursorVisible()

def moveLineUp(self):
self.moveLine(QTextCursor.PreviousBlock)

def moveLineDown(self):
self.moveLine(QTextCursor.NextBlock)

def moveLine(self, direction):
cursor = self.textCursor()
# Select the current block
cursor.movePosition(QTextCursor.StartOfBlock, QTextCursor.MoveAnchor)
cursor.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor)
text = cursor.selectedText()
# Remove it
cursor.removeSelectedText()
# Move to the wanted block
cursor.movePosition(direction, QTextCursor.MoveAnchor)
# Paste the line
cursor.insertText(text)
# Move to the pasted block
cursor.movePosition(QTextCursor.PreviousBlock, QTextCursor.MoveAnchor)
# Update cursor
self.setTextCursor(cursor)

def lineNumberAreaWidth(self):
if not globalSettings.lineNumbersEnabled:
return 0
Expand Down
7 changes: 7 additions & 0 deletions ReText/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ def __init__(self, parent=None):
lambda: self.currentTab.editBox.cut(), shct=QKeySequence.Cut)
self.actionPaste = self.act(self.tr('Paste'), 'edit-paste',
lambda: self.currentTab.editBox.paste(), shct=QKeySequence.Paste)
self.actionMoveUp = self.act(self.tr('Move line up'), 'go-up',
lambda: self.currentTab.editBox.moveLineUp(), shct=Qt.ALT+Qt.Key_Up)
self.actionMoveDown = self.act(self.tr('Move line down'), 'go-down',
lambda: self.currentTab.editBox.moveLineDown(), shct=Qt.ALT+Qt.Key_Down)
self.actionUndo.setEnabled(False)
self.actionRedo.setEnabled(False)
self.actionCopy.setEnabled(False)
Expand Down Expand Up @@ -287,6 +291,9 @@ def __init__(self, parent=None):
menuEdit.addAction(self.actionCopy)
menuEdit.addAction(self.actionPaste)
menuEdit.addSeparator()
menuEdit.addAction(self.actionMoveUp)
menuEdit.addAction(self.actionMoveDown)
menuEdit.addSeparator()
if enchant is not None:
menuSC = menuEdit.addMenu(self.tr('Spell check'))
menuSC.addAction(self.actionEnableSC)
Expand Down