code: def __duplicate_line_or_selection(self, after_current_line=True):
          
          cursor = self.textCursor()
          cursor.beginEditBlock()
          start_pos, end_pos = self.__save_selection()
          if to_text_string(cursor.selectedText()):
              cursor.setPosition(end_pos)
              # Check if end_pos is at the start of a block: if so, starting
              # changes from the previous block
              cursor.movePosition(QTextCursor.StartOfBlock,
                                  QTextCursor.KeepAnchor)
              if not to_text_string(cursor.selectedText()):
                  cursor.movePosition(QTextCursor.PreviousBlock)
                  end_pos = cursor.position()
  
          cursor.setPosition(start_pos)
          cursor.movePosition(QTextCursor.StartOfBlock)
          while cursor.position() <= end_pos:
              cursor.movePosition(QTextCursor.EndOfBlock, QTextCursor.KeepAnchor)
              if cursor.atEnd():
                  cursor_temp = QTextCursor(cursor)
                  cursor_temp.clearSelection()
                  cursor_temp.insertText(self.get_line_separator())
                  break
              cursor.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor)
          text = cursor.selectedText()
          cursor.clearSelection()
  
          if not after_current_line:
              # Moving cursor before current line/selected text
              cursor.setPosition(start_pos)
              cursor.movePosition(QTextCursor.StartOfBlock)
              start_pos += len(text)
              end_pos += len(text)
  
          cursor.insertText(text)
          cursor.endEditBlock()
          self.setTextCursor(cursor)
          self.__restore_selection(start_pos, end_pos)
docs: Duplicate current line or selected text