def on_install(self, editor): super().on_install(editor) self.action = QAction(editor) self.action.setText(_("Calculate PIC offsets")) self.action.setIcon(QIcon.fromTheme('accessories-calculator')) self.action.setShortcut('Ctrl+Shift+O') self.action.setToolTip(_('Compute the PIC offset of the fields in the ' 'selected text')) editor.add_action(self.action, sub_menu='COBOL') self.action.triggered.connect(self._compute_offsets)
def __init__(self): QObject.__init__(self) Mode.__init__(self) self._previous_cursor_start = -1 self._previous_cursor_end = -1 self._definition = None self._deco = None self._pending = False self.action_goto = QAction(_("Go to assignments"), self) self.action_goto.setShortcut('F7') self.action_goto.triggered.connect(self.request_goto) self.word_clicked.connect(self.request_goto) self._timer = DelayJobRunner(delay=200)
class OffsetCalculatorMode(QObject, Mode): """ This modes computes the selected PIC fields offsets. It adds a "Calculate PIC offsets" action to the editor context menu and emits the signal |pic_infos_available| when the the user triggered the action and the pic infos have been computed. """ pic_infos_available = Signal(list) def __init__(self): if os.environ['QT_API'] in (PYQT4_API + PYSIDE_API): QObject.__init__(self) Mode.__init__(self) else: super().__init__() def on_install(self, editor): super().on_install(editor) self.action = QAction(editor) self.action.setText(_("Calculate PIC offsets")) self.action.setIcon(QIcon.fromTheme('accessories-calculator')) self.action.setShortcut('Ctrl+Shift+O') self.action.setToolTip(_('Compute the PIC offset of the fields in the ' 'selected text')) editor.add_action(self.action, sub_menu='COBOL') self.action.triggered.connect(self._compute_offsets) @Slot() def _compute_offsets(self): original_tc = self.editor.textCursor() tc = self.editor.textCursor() start = tc.selectionStart() end = tc.selectionEnd() tc.setPosition(start) start_line = tc.blockNumber() tc.setPosition(end) end_line = tc.blockNumber() th = TextHelper(self.editor) th.select_lines(start=start_line, end=end_line, apply_selection=True) source = th.selected_text() results = get_field_infos(source, self.editor.free_format) self.editor.setTextCursor(original_tc) self.pic_infos_available.emit(results)
def __init__(self, parent=None): super(InteractiveConsole, self).__init__(parent) self.panels = PanelsManager(self) self.decorations = TextDecorationsManager(self) from pyqode.core.panels import SearchAndReplacePanel self.panels.append(SearchAndReplacePanel(), SearchAndReplacePanel.Position.TOP) self._stdout_col = QColor("#404040") self._app_msg_col = QColor("#4040FF") self._stdin_col = QColor("#22AA22") self._stderr_col = QColor("#FF0000") self._write_app_messages = True self._process_name = '' self.process = None self._args = None self._usr_buffer = "" self._clear_on_start = True self._merge_outputs = False self._running = False self._writer = self.write self._user_stop = False font = "monospace" if sys.platform == "win32": font = "Consolas" elif sys.platform == "darwin": font = 'Monaco' self._font_family = font self.setFont(QFont(font, 10)) self.setReadOnly(True) self._mask_user_input = False action = QAction('Copy', self) action.setShortcut(QKeySequence.Copy) action.triggered.connect(self.copy) self.add_action(action) action = QAction('Paste', self) action.setShortcut(QKeySequence.Paste) action.triggered.connect(self.paste) self.add_action(action)
class GoToDefinitionMode(Mode, QObject): """ Go to the definition of the symbol under the word cursor. """ #: Signal emitted when a word is clicked. The parameter is a #: QTextCursor with the clicked word set as the selected text. word_clicked = Signal(QTextCursor) def __init__(self): QObject.__init__(self) Mode.__init__(self) self._previous_cursor_start = -1 self._previous_cursor_end = -1 self._definition = None self._deco = None self._pending = False self.action_goto = QAction(_("Go to assignments"), self) self.action_goto.setShortcut('F7') self.action_goto.triggered.connect(self.request_goto) self.word_clicked.connect(self.request_goto) self._timer = DelayJobRunner(delay=200) def on_state_changed(self, state): """ Connects/disconnects slots to/from signals when the mode state changed. """ super(GoToDefinitionMode, self).on_state_changed(state) if state: self.editor.mouse_moved.connect(self._on_mouse_moved) self.editor.mouse_pressed.connect(self._on_mouse_pressed) self.editor.add_action(self.action_goto, sub_menu='COBOL') self.editor.mouse_double_clicked.connect( self._timer.cancel_requests) else: self.editor.mouse_moved.disconnect(self._on_mouse_moved) self.editor.mouse_pressed.disconnect(self._on_mouse_pressed) self.editor.remove_action(self.action_goto, sub_menu='Python') self.editor.mouse_double_clicked.disconnect( self._timer.cancel_requests) def _select_word_under_mouse_cursor(self): """ Selects the word under the mouse cursor. """ cursor = TextHelper(self.editor).word_under_mouse_cursor() if (self._previous_cursor_start != cursor.selectionStart() and self._previous_cursor_end != cursor.selectionEnd()): self._remove_decoration() self._add_decoration(cursor) self._previous_cursor_start = cursor.selectionStart() self._previous_cursor_end = cursor.selectionEnd() def _on_mouse_moved(self, event): """ mouse moved callback """ if event.modifiers() & Qt.ControlModifier: self._select_word_under_mouse_cursor() else: self._remove_decoration() self.editor.set_mouse_cursor(Qt.IBeamCursor) self._previous_cursor_start = -1 self._previous_cursor_end = -1 def _on_mouse_pressed(self, event): """ mouse pressed callback """ if event.button() == 1 and self._deco: cursor = TextHelper(self.editor).word_under_mouse_cursor() if cursor and cursor.selectedText(): self._timer.request_job(self.word_clicked.emit, cursor) def find_definition(self, symbol, definition): if symbol in TextHelper(self.editor).line_text(definition.line): return definition for ch in definition.children: d = self.find_definition(symbol, ch) if d is not None: return d return None def select_word(self, cursor): symbol = cursor.selectedText() analyser = self.editor.outline_mode for definition in analyser.definitions: node = self.find_definition(symbol, definition) if node is not None: break else: node = None self._definition = None if node and node.line != cursor.block().blockNumber(): self._definition = node if self._deco is None: if cursor.selectedText(): self._deco = TextDecoration(cursor) self._deco.set_foreground(Qt.blue) self._deco.set_as_underlined() self.editor.decorations.append(self._deco) return True return False def _add_decoration(self, cursor): """ Adds a decoration for the word under ``cursor``. """ if self.select_word(cursor): self.editor.set_mouse_cursor(Qt.PointingHandCursor) else: self.editor.set_mouse_cursor(Qt.IBeamCursor) def _remove_decoration(self): """ Removes the word under cursor's decoration """ if self._deco is not None: self.editor.decorations.remove(self._deco) self._deco = None def request_goto(self, tc=None): """ Request a go to assignment. :param tc: Text cursor which contains the text that we must look for its assignment. Can be None to go to the text that is under the text cursor. :type tc: QtGui.QTextCursor """ print('request goto') if not tc: tc = TextHelper(self.editor).word_under_cursor( select_whole_word=True) if not self._definition or isinstance(self.sender(), QAction): self.select_word(tc) if self._definition is not None: QTimer.singleShot(100, self._goto_def) def _goto_def(self): line = self._definition.line col = self._definition.column TextHelper(self.editor).goto_line(line, move=True, column=col)
class GoToDefinitionMode(Mode, QObject): """ Go to the definition of the symbol under the word cursor. """ #: Signal emitted when a word is clicked. The parameter is a #: QTextCursor with the clicked word set as the selected text. word_clicked = Signal(QTextCursor) def __init__(self): QObject.__init__(self) Mode.__init__(self) self._previous_cursor_start = -1 self._previous_cursor_end = -1 self._definition = None self._deco = None self._pending = False self.action_goto = QAction(_("Go to assignments"), self) self.action_goto.setShortcut('F7') self.action_goto.triggered.connect(self.request_goto) self.word_clicked.connect(self.request_goto) self._timer = DelayJobRunner(delay=200) def on_state_changed(self, state): """ Connects/disconnects slots to/from signals when the mode state changed. """ super(GoToDefinitionMode, self).on_state_changed(state) if state: self.editor.mouse_moved.connect(self._on_mouse_moved) self.editor.mouse_released.connect(self._on_mouse_released) self.editor.add_action(self.action_goto, sub_menu='COBOL') self.editor.mouse_double_clicked.connect( self._timer.cancel_requests) else: self.editor.mouse_moved.disconnect(self._on_mouse_moved) self.editor.mouse_released.disconnect(self._on_mouse_released) self.editor.remove_action(self.action_goto, sub_menu='Python') self.editor.mouse_double_clicked.disconnect( self._timer.cancel_requests) def _select_word_under_mouse_cursor(self): """ Selects the word under the mouse cursor. """ cursor = TextHelper(self.editor).word_under_mouse_cursor() if (self._previous_cursor_start != cursor.selectionStart() and self._previous_cursor_end != cursor.selectionEnd()): self._remove_decoration() self._add_decoration(cursor) self._previous_cursor_start = cursor.selectionStart() self._previous_cursor_end = cursor.selectionEnd() def _on_mouse_moved(self, event): """ mouse moved callback """ if event.modifiers() & Qt.ControlModifier: self._select_word_under_mouse_cursor() else: self._remove_decoration() self.editor.set_mouse_cursor(Qt.IBeamCursor) self._previous_cursor_start = -1 self._previous_cursor_end = -1 def _on_mouse_released(self, event): """ mouse pressed callback """ if event.button() == 1 and self._deco: cursor = TextHelper(self.editor).word_under_mouse_cursor() if cursor and cursor.selectedText(): self._timer.request_job(self.word_clicked.emit, cursor) def find_definition(self, symbol, definition): if symbol.lower() == definition.name.lower().replace( " section", "").replace(" division", ""): return definition for ch in definition.children: d = self.find_definition(symbol, ch) if d is not None: return d return None def select_word(self, cursor): symbol = cursor.selectedText() analyser = self.editor.outline_mode for definition in analyser.definitions: node = self.find_definition(symbol, definition) if node is not None: break else: node = None self._definition = None if node and node.line != cursor.block().blockNumber(): self._definition = node if self._deco is None: if cursor.selectedText(): self._deco = TextDecoration(cursor) self._deco.set_foreground(Qt.blue) self._deco.set_as_underlined() self.editor.decorations.append(self._deco) return True return False def _add_decoration(self, cursor): """ Adds a decoration for the word under ``cursor``. """ if self.select_word(cursor): self.editor.set_mouse_cursor(Qt.PointingHandCursor) else: self.editor.set_mouse_cursor(Qt.IBeamCursor) def _remove_decoration(self): """ Removes the word under cursor's decoration """ if self._deco is not None: self.editor.decorations.remove(self._deco) self._deco = None def request_goto(self, tc=None): """ Request a go to assignment. :param tc: Text cursor which contains the text that we must look for its assignment. Can be None to go to the text that is under the text cursor. :type tc: QtGui.QTextCursor """ if not tc: tc = TextHelper( self.editor).word_under_cursor(select_whole_word=True) if not self._definition or isinstance(self.sender(), QAction): self.select_word(tc) if self._definition is not None: QTimer.singleShot(100, self._goto_def) def _goto_def(self): if self._definition: line = self._definition.line col = self._definition.column TextHelper(self.editor).goto_line(line, move=True, column=col)