Skip to content

Commit

Permalink
Wrap lines that are longer than 106 characters
Browse files Browse the repository at this point in the history
  • Loading branch information
mitya57 committed Jan 4, 2025
1 parent 3eafc8d commit c160002
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ jobs:
- name: Install ruff
run: python -m pip install ruff
- name: Run ruff check
run: ruff check --select F,E,W,I,UP --ignore E501 .
run: ruff check --select F,E,W,I,UP --line-length=106 .
5 changes: 4 additions & 1 deletion ReText/converterprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ def recvall(sock, remaining):
while remaining > 0:
data = sock.recv(remaining)
if len(data) == 0:
raise EOFError('Received 0 bytes from socket while more bytes were expected. Did the sender process exit unexpectedly?')
raise EOFError(
'Received 0 bytes from socket while more bytes were expected.'
' Did the sender process exit unexpectedly?'
)
alldata.extend(data)
remaining -= len(data)

Expand Down
4 changes: 3 additions & 1 deletion ReText/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def __init__(self, parent, defaultText=None):
self.checkBox = QCheckBox(self.tr('Set as default'), self)
verticalLayout.addWidget(self.checkBox)
buttonBox = QDialogButtonBox(self)
buttonBox.setStandardButtons(QDialogButtonBox.StandardButton.Cancel | QDialogButtonBox.StandardButton.Ok)
buttonBox.setStandardButtons(
QDialogButtonBox.StandardButton.Cancel | QDialogButtonBox.StandardButton.Ok
)
verticalLayout.addWidget(buttonBox)
buttonBox.accepted.connect(self.accept)
buttonBox.rejected.connect(self.reject)
Expand Down
26 changes: 21 additions & 5 deletions ReText/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ def contextMenuEvent(self, event):
curchar = self.document().characterAt(cursor.position())
isalpha = curchar.isalpha()
word = None
if isalpha and not (oldcursor.hasSelection() and oldcursor.selectedText() != cursor.selectedText()):
if isalpha and not (
oldcursor.hasSelection()
and oldcursor.selectedText() != cursor.selectedText()
):
cursor.select(QTextCursor.SelectionType.WordUnderCursor)
word = cursor.selectedText()

Expand All @@ -276,7 +279,10 @@ def contextMenuEvent(self, event):
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.insertAction(
menu.actions()[0],
self.parent.act(self.tr('Add to dictionary'), trig=self.learnWord(word)),
)

menu.addSeparator()
menu.addAction(self.parent.actionMoveUp)
Expand Down Expand Up @@ -384,7 +390,11 @@ def handleReturn(self, cursor):
if matchOL is not None:
matchedPrefix = matchOL.group(1)
matchedNumber = int(matchOL.group(2))
nextNumber = matchedNumber if self.settings.orderedListMode == 'repeat' else matchedNumber + 1
nextNumber = (
matchedNumber
if self.settings.orderedListMode == 'repeat'
else matchedNumber + 1
)
matchedText = matchedPrefix + str(nextNumber) + ". "
else:
matchedText = ''
Expand Down Expand Up @@ -455,15 +465,21 @@ def highlightCurrentLine(self):
if globalSettings.highlightCurrentLine == 'wrapped-line':
selections.append(QTextEdit.ExtraSelection())
selections[0].cursor.movePosition(QTextCursor.MoveOperation.StartOfBlock)
selections[0].cursor.movePosition(QTextCursor.MoveOperation.EndOfBlock, QTextCursor.MoveMode.KeepAnchor)
selections[0].cursor.movePosition(
QTextCursor.MoveOperation.EndOfBlock,
QTextCursor.MoveMode.KeepAnchor,
)
selections[1].format.setBackground(getColor('currentLineHighlight'))
selections[1].format.setProperty(QTextFormat.Property.FullWidthSelection, True)
selections[1].cursor = self.textCursor()
selections[1].cursor.movePosition(QTextCursor.MoveOperation.EndOfBlock)
elif selection.cursor.block().textDirection() == Qt.LayoutDirection.RightToLeft:
# FullWidthSelection does not work correctly for RTL direction
selection.cursor.movePosition(QTextCursor.MoveOperation.StartOfLine)
selection.cursor.movePosition(QTextCursor.MoveOperation.EndOfLine, QTextCursor.MoveMode.KeepAnchor)
selection.cursor.movePosition(
QTextCursor.MoveOperation.EndOfLine,
QTextCursor.MoveMode.KeepAnchor,
)
self.setExtraSelections(selections)

def enableTableMode(self, enable):
Expand Down
35 changes: 27 additions & 8 deletions ReText/tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,16 @@ def find(self, text, flags, replaceText=None, wrap=False):
if not newCursor.isNull():
if replaceText is not None:
newCursor.insertText(replaceText)
newCursor.movePosition(QTextCursor.MoveOperation.Left, QTextCursor.MoveMode.MoveAnchor, len(replaceText))
newCursor.movePosition(QTextCursor.MoveOperation.Right, QTextCursor.MoveMode.KeepAnchor, len(replaceText))
newCursor.movePosition(
QTextCursor.MoveOperation.Left,
QTextCursor.MoveMode.MoveAnchor,
len(replaceText),
)
newCursor.movePosition(
QTextCursor.MoveOperation.Right,
QTextCursor.MoveMode.KeepAnchor,
len(replaceText),
)
self.editBox.setTextCursor(newCursor)
if self.editBox.cursorRect().bottom() >= self.editBox.height() - 3:
scrollValue = self.editBox.verticalScrollBar().value()
Expand All @@ -439,8 +447,16 @@ def replaceAll(self, text, replaceText):
lastCursor = cursor
cursor = self.editBox.document().find(text, cursor, flags)
if not lastCursor.isNull():
lastCursor.movePosition(QTextCursor.MoveOperation.Left, QTextCursor.MoveMode.MoveAnchor, len(replaceText))
lastCursor.movePosition(QTextCursor.MoveOperation.Right, QTextCursor.MoveMode.KeepAnchor, len(replaceText))
lastCursor.movePosition(
QTextCursor.MoveOperation.Left,
QTextCursor.MoveMode.MoveAnchor,
len(replaceText),
)
lastCursor.movePosition(
QTextCursor.MoveOperation.Right,
QTextCursor.MoveMode.KeepAnchor,
len(replaceText),
)
self.editBox.setTextCursor(lastCursor)
self.editBox.textCursor().endEditBlock()
return not lastCursor.isNull()
Expand Down Expand Up @@ -468,10 +484,13 @@ def promptFileCreation(self, fileToCreate):
Prompt user if a file should be created for the clicked link,
and try to create it. Return True on success.
"""
buttonReply = QMessageBox.question(self, self.tr('Create missing file?'),
self.tr("The file '%s' does not exist.\n\nDo you want to create it?") % fileToCreate,
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.No)
buttonReply = QMessageBox.question(
self,
self.tr('Create missing file?'),
self.tr("The file '%s' does not exist.\n\nDo you want to create it?") % fileToCreate,
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.No,
)
if buttonReply == QMessageBox.StandardButton.Yes:
return self.createFile(fileToCreate)
elif buttonReply == QMessageBox.StandardButton.No:
Expand Down
22 changes: 18 additions & 4 deletions ReText/tablemode.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,33 @@ def _determineEditLists(rows, editedlineindex, offset, editsize, alignWithAnyEdg
if editsize < 0:
# How much an edge shifts to the left depends on how much room
# there is in the cells on any row that shares this edge.
leastLeftShift = min((-rowShift + _determineRoomInCell(row, currentedge, currentedgeindex, True)
for row, rowShift in zip(rows, rowShifts)))
leastLeftShift = min(
-rowShift + _determineRoomInCell(row, currentedge, currentedgeindex, True)
for row, rowShift in zip(rows, rowShifts)
)

shift = max(editsize, -leastLeftShift)
else:
# When shifting right, determine how much only once based on how
# much the edited cell needs to expand
if firstEdge:
room = _determineRoomInCell(rows[editedlineindex], currentedge, currentedgeindex, False, offset)
room = _determineRoomInCell(
rows[editedlineindex],
currentedge,
currentedgeindex,
False,
offset,
)
shift = max(0, editsize - room)

for i, row in enumerate(rows):
editList, newRowShift = _performShift(row, rowShifts[i], currentedge, currentedgeindex, shift)
editList, newRowShift = _performShift(
row,
rowShifts[i],
currentedge,
currentedgeindex,
shift,
)
rowShifts[i] = newRowShift
editLists[i].extend(editList)

Expand Down
34 changes: 27 additions & 7 deletions ReText/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ def __init__(self, parent=None):
self.actionPdf = self.act('PDF', 'application-pdf', self.savePdf)
self.actionOdf = self.act('ODT', 'x-office-document', self.saveOdf)
self.getExportExtensionsList()
self.actionQuit = self.act(self.tr('Quit'), 'application-exit', shct=QKeySequence.StandardKey.Quit)
self.actionQuit = self.act(
self.tr('Quit'),
'application-exit',
shct=QKeySequence.StandardKey.Quit,
)
self.actionQuit.setMenuRole(QAction.MenuRole.QuitRole)
self.actionQuit.triggered.connect(self.close)
self.actionUndo = self.act(self.tr('Undo'), 'edit-undo',
Expand All @@ -250,8 +254,12 @@ def __init__(self, parent=None):
lambda: self.currentTab.editBox.cut(), shct=QKeySequence.StandardKey.Cut)
self.actionPaste = self.act(self.tr('Paste'), 'edit-paste',
lambda: self.currentTab.editBox.paste(), shct=QKeySequence.StandardKey.Paste)
self.actionPasteImage = self.act(self.tr('Paste image'), 'edit-paste',
lambda: self.currentTab.editBox.pasteImage(), shct=Qt.Modifier.CTRL | Qt.Modifier.SHIFT | Qt.Key.Key_V)
self.actionPasteImage = self.act(
self.tr('Paste image'),
'edit-paste',
lambda: self.currentTab.editBox.pasteImage(),
shct=Qt.Modifier.CTRL | Qt.Modifier.SHIFT | Qt.Key.Key_V,
)
self.actionMoveUp = self.act(self.tr('Move line up'), 'go-up',
lambda: self.currentTab.editBox.moveLineUp(), shct=Qt.Modifier.ALT | Qt.Key.Key_Up)
self.actionMoveDown = self.act(self.tr('Move line down'), 'go-down',
Expand Down Expand Up @@ -615,7 +623,9 @@ def createTab(self, fileName):
previewState = PreviewDisabled # Opening empty document in preview mode makes no sense
self.currentTab = ReTextTab(self, fileName, previewState)
self.currentTab.fileNameChanged.connect(lambda: self.tabFileNameChanged(self.currentTab))
self.currentTab.modificationStateChanged.connect(lambda: self.tabModificationStateChanged(self.currentTab))
self.currentTab.modificationStateChanged.connect(
lambda: self.tabModificationStateChanged(self.currentTab)
)
self.currentTab.activeMarkupChanged.connect(lambda: self.tabActiveMarkupChanged(self.currentTab))
self.tabWidget.addTab(self.currentTab, self.tr("New document"))
self.currentTab.updateBoxesVisibility()
Expand Down Expand Up @@ -1096,7 +1106,12 @@ def savePdf(self):
if pageSize is None:
pageSize = QPageSize(QPageSize.PageSizeId.A4)
margins = QMarginsF(20, 20, 13, 20) # left, top, right, bottom (in millimeters)
layout = QPageLayout(pageSize, QPageLayout.Orientation.Portrait, margins, QPageLayout.Unit.Millimeter)
layout = QPageLayout(
pageSize,
QPageLayout.Orientation.Portrait,
margins,
QPageLayout.Unit.Millimeter,
)
preview.page().printToPdf(fileName, layout)
return
printer = self.standardPrinter(title)
Expand Down Expand Up @@ -1267,9 +1282,14 @@ def maybeSave(self, ind):
if not tab.editBox.document().isModified():
return True
self.tabWidget.setCurrentIndex(ind)
ret = QMessageBox.warning(self, '',
ret = QMessageBox.warning(
self,
'',
self.tr("The document has been modified.\nDo you want to save your changes?"),
QMessageBox.StandardButton.Save | QMessageBox.StandardButton.Discard | QMessageBox.StandardButton.Cancel)
QMessageBox.StandardButton.Save
| QMessageBox.StandardButton.Discard
| QMessageBox.StandardButton.Cancel,
)
if ret == QMessageBox.StandardButton.Save:
return self.saveFile(False)
elif ret == QMessageBox.StandardButton.Cancel:
Expand Down
73 changes: 60 additions & 13 deletions tests/test_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ def getText(self, key):
return '\''

def getEvent(self, key):
return QKeyEvent(QEvent.Type.KeyPress, key, Qt.KeyboardModifier.NoModifier, text=self.getText(key))
return QKeyEvent(
QEvent.Type.KeyPress,
key,
Qt.KeyboardModifier.NoModifier,
text=self.getText(key),
)

def test_isSurroundKey(self):
# close keys should not start a surrounding
Expand All @@ -176,12 +181,30 @@ def test_isSurroundKey(self):
self.assertTrue(self.editor.isSurroundKey(Qt.Key.Key_Apostrophe))

def test_getCloseKey(self):
self.assertEqual(self.editor.getCloseKey(self.getEvent(Qt.Key.Key_Underscore), Qt.Key.Key_Underscore), '_')
self.assertEqual(self.editor.getCloseKey(self.getEvent(Qt.Key.Key_Asterisk), Qt.Key.Key_Asterisk), '*')
self.assertEqual(self.editor.getCloseKey(self.getEvent(Qt.Key.Key_QuoteDbl), Qt.Key.Key_QuoteDbl), '"')
self.assertEqual(self.editor.getCloseKey(self.getEvent(Qt.Key.Key_Apostrophe), Qt.Key.Key_Apostrophe), '\'')
self.assertEqual(self.editor.getCloseKey(self.getEvent(Qt.Key.Key_ParenLeft), Qt.Key.Key_ParenLeft), ')')
self.assertEqual(self.editor.getCloseKey(self.getEvent(Qt.Key.Key_BracketLeft), Qt.Key.Key_BracketLeft), ']')
self.assertEqual(
self.editor.getCloseKey(self.getEvent(Qt.Key.Key_Underscore), Qt.Key.Key_Underscore),
'_',
)
self.assertEqual(
self.editor.getCloseKey(self.getEvent(Qt.Key.Key_Asterisk), Qt.Key.Key_Asterisk),
'*',
)
self.assertEqual(
self.editor.getCloseKey(self.getEvent(Qt.Key.Key_QuoteDbl), Qt.Key.Key_QuoteDbl),
'"',
)
self.assertEqual(
self.editor.getCloseKey(self.getEvent(Qt.Key.Key_Apostrophe), Qt.Key.Key_Apostrophe),
'\'',
)
self.assertEqual(
self.editor.getCloseKey(self.getEvent(Qt.Key.Key_ParenLeft), Qt.Key.Key_ParenLeft),
')',
)
self.assertEqual(
self.editor.getCloseKey(self.getEvent(Qt.Key.Key_BracketLeft), Qt.Key.Key_BracketLeft),
']',
)

def changeCursor(self, posI, posF):
self.cursor.setPosition(posI)
Expand All @@ -190,27 +213,51 @@ def changeCursor(self, posI, posF):
def test_surroundText(self):

self.changeCursor(0, 3)
self.editor.surroundText(self.cursor, self.getEvent(Qt.Key.Key_Underscore), Qt.Key.Key_Underscore)
self.editor.surroundText(
self.cursor,
self.getEvent(Qt.Key.Key_Underscore),
Qt.Key.Key_Underscore,
)
self.assertEqual(self.document.toPlainText(), '_foo_ bar baz qux corge grault')

self.changeCursor(6, 9)
self.editor.surroundText(self.cursor, self.getEvent(Qt.Key.Key_Asterisk), Qt.Key.Key_Asterisk)
self.editor.surroundText(
self.cursor,
self.getEvent(Qt.Key.Key_Asterisk),
Qt.Key.Key_Asterisk,
)
self.assertEqual(self.document.toPlainText(), '_foo_ *bar* baz qux corge grault')

self.changeCursor(12, 15)
self.editor.surroundText(self.cursor, self.getEvent(Qt.Key.Key_QuoteDbl), Qt.Key.Key_QuoteDbl)
self.editor.surroundText(
self.cursor,
self.getEvent(Qt.Key.Key_QuoteDbl),
Qt.Key.Key_QuoteDbl,
)
self.assertEqual(self.document.toPlainText(), '_foo_ *bar* "baz" qux corge grault')

self.changeCursor(18, 21)
self.editor.surroundText(self.cursor, self.getEvent(Qt.Key.Key_Apostrophe), Qt.Key.Key_Apostrophe)
self.editor.surroundText(
self.cursor,
self.getEvent(Qt.Key.Key_Apostrophe),
Qt.Key.Key_Apostrophe,
)
self.assertEqual(self.document.toPlainText(), '_foo_ *bar* "baz" \'qux\' corge grault')

self.changeCursor(24, 29)
self.editor.surroundText(self.cursor, self.getEvent(Qt.Key.Key_ParenLeft), Qt.Key.Key_ParenLeft)
self.editor.surroundText(
self.cursor,
self.getEvent(Qt.Key.Key_ParenLeft),
Qt.Key.Key_ParenLeft,
)
self.assertEqual(self.document.toPlainText(), '_foo_ *bar* "baz" \'qux\' (corge) grault')

self.changeCursor(32, 38)
self.editor.surroundText(self.cursor, self.getEvent(Qt.Key.Key_BracketLeft), Qt.Key.Key_BracketLeft)
self.editor.surroundText(
self.cursor,
self.getEvent(Qt.Key.Key_BracketLeft),
Qt.Key.Key_BracketLeft,
)
self.assertEqual(self.document.toPlainText(), '_foo_ *bar* "baz" \'qux\' (corge) [grault]')

class TestOrderedListMode(unittest.TestCase):
Expand Down
Loading

0 comments on commit c160002

Please sign in to comment.