This package contains the bases classes of pyqode and some utility functions.
Bases: PyQt5.QtWidgets.QPlainTextEdit
The editor widget is a simple extension to QPlainTextEdit.
It adds a few utility signals/methods and introduces the concepts of Managers, Modes and Panels.
A mode/panel is an editor extension that, once added to a CodeEdit instance, may modify its behaviour and appearance:
Modes are simple objects which connect to the editor signals to append new behaviours (such as automatic indentation, code completion, syntax checking,...)
Panels are the combination of a Mode and a QWidget. They are displayed in the CodeEdit’s content margins.
When you install a Panel on a CodeEdit, you can choose to install it in one of the four following zones:
A manager is an object that literally manage a specific aspect of pyqode.core.api.CodeEdit. There are managers to manage the list of modes/panels, to open/save file and to control the backend:
- pyqode.core.api.CodeEdit.file:
File manager. Use it to open/save files or access the opened file attribute.
- pyqode.core.api.CodeEdit.backend:
Backend manager. Use it to start/stop the backend or send a work request.
- pyqode.core.api.CodeEdit.modes:
Modes manager. Use it to append/remove modes on the editor.
- pyqode.core.api.CodeEdit.panels:
Modes manager. Use it to append/remove panels on the editor.
Starting from version 2.1, CodeEdit defines the pyqode.core.api.CodeEdit.mimetypes class attribute that can be used by IDE to determine which editor to use for a given mime type. This property is a list of supported mimetypes. An empty list means the CodeEdit is generic. Code editors specialised for a specific language should define the mime types they support!
Returns the list of actions/sepqrators of the context menu.
Adds an action to the editor’s context menu.
Parameters: | action – QtWidgets.QAction |
---|
Adds a sub-menu to the editor context menu.
Menu are put at the bottom of the context menu.
Note
to add a menu in the middle of the context menu, you can always add its menuAction().
Parameters: | menu – menu to add |
---|
Adds a sepqrator to the editor’s context menu.
Returns: | The sepator that has been added. |
---|---|
Return type: | QtWidgets.QAction |
Clone ourselves, return an instance of the same class, using the default QWidget constructor.
Closes the editor, stops the backend and removes any installed mode/panel.
This is also where we cache the cursor position.
Parameters: | clear – True to clear the editor content before closing. |
---|
Deletes the selected text
Duplicates the line under the cursor. If multiple lines are selected, only the last one is duplicated.
Overrides focusInEvent to emits the focused_in signal
Parameters: | event – QFocusEvent |
---|
Gets the editor context menu.
Returns: | QMenu |
---|
Shows the go to line dialog and go to the selected line.
Indents the text cursor or the selection.
Emits the pyqode.core.api.CodeEdit.indent_requested signal, the pyqode.core.modes.IndenterMode will perform the actual indentation.
Inserts an action to the editor’s context menu
Parameters: |
|
---|
Overrides the keyPressEvent to emit the key_pressed signal.
Also takes care of indenting and handling smarter home key.
Parameters: | event – QKeyEvent |
---|
Overrides keyReleaseEvent to emit the key_released signal.
Parameters: | event – QKeyEvent |
---|
Links the clone with its original. We copy the file manager infos (path, mimetype, ...) and setup the clone text document as reference to our text document.
Parameters: | clone – clone to link. |
---|
Overrides mouseMovedEvent to display any decoration tooltip and emits the mouse_moved event.
Parameters: | event – QMouseEvent |
---|
Overrides mousePressEvent to emits mouse_pressed signal
Parameters: | event – QMouseEvent |
---|
Emits mouse_released signal.
Parameters: | event – QMouseEvent |
---|
Overrides paint event to update the list of visible blocks and emit the painted event.
Parameters: | e – paint event |
---|
Calls rehighlight on the installed syntax highlighter mode.
Removes an action/separator from the editor’s context menu.
Parameters: | action – Action/seprator to remove. |
---|
Removes a sub-menu from the context menu. :param menu: Sub-menu to remove.
Resets the zoom level.
Overrides resize event to resize the editor’s panels.
Parameters: | e – resize event |
---|
Extends setPlainText to force the user to setup an encoding and a mime type.
Emits the new_text_set signal.
Parameters: |
|
---|
Changes the viewport’s cursor
Parameters: | cursor (QtWidgets.QCursor) – the mouse cursor to set. |
---|
Overrides showEvent to update the viewport margins
Show a tool tip at the specified position
Parameters: |
|
---|
Split the code editor widget, return a clone of the widget ready to be used (and synchronised with its original).
first we clone the widget, you can override clone if your widget needs additional arguments.
cloned instance (such as the watcher mode).
Un-indents the text cursor or the selection.
Emits the pyqode.core.api.CodeEdit.unindent_requested signal, the pyqode.core.modes.IndenterMode will perform the actual un-indentation.
Emits the mouse_wheel_activated signal.
Parameters: | event – QMouseEvent |
---|
Zooms in the editor (makes the font bigger).
Parameters: | increment – zoom level increment. Default is 1. |
---|
Zooms out the editor (makes the font smaller).
Parameters: | decrement – zoom level decrement. Default is 1. The value is given as an absolute value. |
---|
Returns a reference to the pyqode.core.managers.BackendManager used to control the backend process.
The editor background color (QColor)
Returns a reference to the pyqode.core.managers.TextDecorationManager used to manage the list of pyqode.core.api.TextDecoration
Tells whethere the content of editor has been modified.
(this is just a shortcut to QTextDocument.isModified
Type: | bool |
---|
Signal emitted when the dirty state changed
Returns a reference to the pyqode.core.managers.FileManager used to open/save file on the editor
Signal emitted when focusInEvent is is called
The editor font family name.
The font point size.
Note
Please, never use setFontPointSize/setFontFamily functions directly as the values you define there will be overwritten as soon as the user zoom the editor or as soon as a stylesheet property has changed.
The editor foreground color (QColor)
Signal emitted when the user press the TAB key
Signal emitted when a key is pressed
Signal emitted when a key is released
Store the list of mimetypes associated with the editor, for specialised editors.
Returns a reference to the pyqode.core.managers.ModesManager used to manage the collection of installed modes.
Signal emitted when a mouse double click event occured
Signal emitted when the mouse_moved
Signal emitted when a mouse button is pressed
Signal emitted when a mouse button is released
Signal emitted on a wheel event
Signal emitted when a new text is set on the widget
Paint hook
Returns a reference to the pyqode.core.managers.PanelsManager used to manage the collection of installed panels
Signal emitted at the end of the key_pressed event
Automatically saves editor content on focus out.
Default is False.
The editor selection’s background color.
The editor selection’s foreground color.
Shows/Hides virtual white spaces.
Returns a reference to the syntax highlighter mode currently used to highlight the editor content.
Returns: | pyqode.core.api.SyntaxHighlighter |
---|
Tab length, number of spaces.
Signal emitted when the text is saved to file
Signal emitted before the text is saved to file
Signal emitted when the user press the BACK-TAB (Shift+TAB) key
Use spaces instead of tabulations. Default is True.
Returns the list of visible blocks.
Each element in the list is a tuple made up of the line top position, the line number and the QTextBlock itself.
Returns: | A list of tuple(top_position, line_number, block) |
---|---|
Return type: | List of tuple(int, int, QtWidgets.QTextBlock) |
The editor white spaces’ foreground color. White spaces are highlighted by the syntax highlighter. You should call rehighlight to update their color. This is not done automatically to prevent multiple, useless call to rehighlight which can take some time on big files.
The list of word separators used by the code completion mode and the word clicked mode.
Gets/Sets the editor zoom level.
The zoom level is a value that is added to the current editor font size. Negative values are used to zoom out the editor, positive values are used to zoom in the editor.
Bases: builtins.object
Translates a pygments style into a dictionary of colors associated with a style key.
See pyqode.core.api.syntax_highligter.COLOR_SCHEM_KEYS for the available keys.
Gets the background color. :return:
Dictionary of formats colors (keys are the same as for pyqode.core.api.COLOR_SCHEME_KEYS
Gets the highlight color. :return:
Name of the color scheme, this is usually the name of the associated pygments style.
Bases: builtins.object
Utility class for running job after a certain delay. If a new request is made during this delay, the previous request is dropped and the timer is restarted for the new request.
We use this to implement a cooldown effect that prevents jobs from being executed while the IDE is not idle.
A job is a simple callable.
Cancels pending requests.
Bases: builtins.object
Base class for fold detectors.
A fold detector takes care of detecting the text blocks fold levels that are used by the FoldingPanel to render the document outline.
To use a FoldDetector, simply set it on a syntax_highlighter:
editor.syntax_highlighter.fold_detector = my_fold_detector
Detects the block fold level.
The default implementation is based on the block indentation.
Note
Blocks fold level must be contiguous, there cannot be a difference greater than 1 between two successive block fold levels.
Parameters: |
|
---|---|
Returns: | Fold level |
Processes a block and setup its folding info.
This method call detect_fold_level and handles most of the tricky corner cases so that all you have to do is focus on getting the proper fold level foreach meaningful block, skipping the blank ones.
Parameters: |
|
---|
Reference to the parent editor, automatically set by the syntax highlighter before process any block.
Fold level limit, any level greater or equal is skipped. Default is sys.maxsize (i.e. all levels are accepted)
Bases: builtins.object
Utility class for manipulating fold-able code scope (fold/unfold, get range, child and parent scopes and so on).
A scope is built from a fold trigger (QTextBlock).
This generator generates the list of blocks directly under the fold region. This list does not contain blocks from child regions.
Parameters: | ignore_blank_lines – True to ignore last blank lines. |
---|
This generator generates the list of direct child regions.
Find parent scope, if the block is not a fold trigger.
Parameters: | block – block from which the research will start |
---|
Folds the region.
Gets the fold region range (start and end line).
Note
Start line do no encompass the trigger line.
Parameters: | ignore_blank_lines – True to ignore blank lines at the end of the scope (the method will rewind to find that last meaningful block that is part of the fold scope). |
---|---|
Returns: | tuple(int, int) |
Return the parent scope.
Returns: | FoldScope or None |
---|
Get the scope text, with a possible maximum number of lines.
Parameters: | max_lines – limit the number of lines returned to a maximum. |
---|---|
Returns: | str |
Unfolds the region.
Returns True if the block is collasped, False if it is expanded.
Returns the fold level of the first block of the foldable scope ( just after the trigger)
Returns: |
---|
Returns the fold level of the block trigger :return:
Bases: pyqode.core.api.folding.FoldDetector
Simple fold detector based on the line indentation level
Detects fold level by looking at the block indentation.
Parameters: |
|
---|
Bases: builtins.object
Managers are typically created internally when you create a CodeEdit. You interact with them later, e.g. when you want to start the backend process or when you want to install/retrieve a mode or a panel.
editor = CodeEdit()
# use the backend manager to start the backend server editor.backend.start(...) editor.backend.send_request(...)
# use the panels controller to install a panel editor.panels.install(MyPanel(), MyPanel.Position.Right) my_panel = editor.panels.get(MyPanel)
# and so on
Return a reference to the parent code edit widget.
Bases: builtins.object
Base class for editor extensions. An extension is a “thing” that can be installed on an editor to add new behaviours or to modify its appearance.
A mode is added to an editor by using the ModesManager/PanelsManager:
- pyqode.core.api.CodeEdit.modes.append() or
- pyqode.core.api.CodeEdit.panels.append()
Subclasses may/should override the following methods:
..warning: The mode will be identified by its class name, this means that there cannot be two modes of the same type on the same editor instance!
Clone the settings from another mode (same class).
This method is called when splitting an editor widget.
Parameters: | original – other mode (must be the same class). |
---|
Note
The base method does not do anything, you must implement this method for every new mode/panel (if you plan on using the split feature). You should also make sure any properties will be propagated to the clones.
Installs the extension on the editor.
Parameters: | editor (pyqode.core.api.code_edit.CodeEdit) – editor widget instance |
---|
Note
This method is called by editor when you install a Mode. You should never call it yourself, even in a subclasss.
Warning
Don’t forget to call super when subclassing
Called when the enable state has changed.
This method does not do anything, you may override it if you need to connect/disconnect to the editor’s signals (connect when state is true and disconnect when it is false).
Parameters: | state (bool) – True = enabled, False = disabled |
---|
Uninstalls the mode from the editor.
Mode description
Returns a reference to the parent editor widget.
READ ONLY
Return type: | pyqode.core.api.code_edit.CodeEdit |
---|
Tells if the mode is enabled, pyqode.core.api.Mode.on_state_changed() will be called as soon as the mode state changed.
Type: | bool |
---|
Mode name/identifier. pyqode.core.api.CodeEdit uses
Bases: PyQt5.QtWidgets.QWidget, pyqode.core.api.mode.Mode
Base class for editor panels.
A panel is a mode and a QWidget.
Note
Use enabled to disable panel actions and setVisible to change the visibility of the panel.
Bases: builtins.object
Enumerates the possible panel positions
Returns possible positions as an iterable (list)
Bottom margin
Left margin
Right margin
Top margin
Extends pyqode.core.api.Mode.on_install() method to set the editor instance as the parent widget.
Warning
Don’t forget to call super if you override this method!
Parameters: | editor (pyqode.core.api.CodeEdit) – editor instance |
---|
Shows/Hides the panel
Automatically call CodeEdit.refresh_panels.
Parameters: | visible – Visible state |
---|
Panel order into the zone it is installed to. This value is automatically set when installing the panel but it can be changed later (negative values can also be used).
Position in the editor (top, left, right, bottom)
A scrollable panel will follow the editor’s scroll-bars. Left and right panels follow the vertical scrollbar. Top and bottom panels follow the horizontal scrollbar.
Type: | bool |
---|
Bases: PyQt5.QtGui.QSyntaxHighlighter, pyqode.core.api.mode.Mode
Abstract base class for syntax highlighter modes.
It fills up the document with our custom block data (fold levels, triggers,...).
It does not do any syntax highlighting, that task is left to sublasses such as pyqode.core.modes.PygmentsSyntaxHighlighter.
Subclasses must override the pyqode.core.api.SyntaxHighlighter.highlight_block() method to apply custom highlighting.
Note
Since version 2.1 and for performance reasons, we store all our data in the block user state as a bit-mask. You should always use pyqode.core.api.TextBlockHelper to retrieve or modify those data.
Highlights a block of text. Please do not override, this method. Instead you should implement pyqode.core.api.SyntaxHighlighter.highlight_block().
Parameters: | text – text to highlight. |
---|
Abstract method. Override this to apply syntax highlighting.
Parameters: |
|
---|
Refresh editor settings (background and highlight colors) when color scheme changed.
Parameters: | color_scheme – new color scheme. |
---|
Rehighlight the entire document, may be slow.
Signal emitted at the end of highlightBlock. Parameters are the highlighter instance and the current text block
Signal emitted at the start of highlightBlock. Parameters are the highlighter instance and the current text block
Returns a reference to the current color scheme.
Fold detector. Set it to a valid FoldDetector to get code folding to work. Default is None
Returns the color shcme formats dict.
Bases: builtins.object
Helps retrieving the various part of the user state bitmask.
This helper should be used to replace calls to QTextBlock.setUserState/QTextBlock.getUserState as well as QSyntaxHighlighter.setCurrentBlockState/ QSyntaxHighlighter.currentBlockState and QSyntaxHighlighter.previousBlockState.
The bitmask is made up of the following fields:
- bit0 -> bit26: User state (for syntax highlighting)
- bit26: fold trigger state
- bit27-bit29: fold level (8 level max)
- bit30: fold trigger flag
- bit0 -> bit15: 16 bits for syntax highlighter user state ( for syntax highlighting)
- bit16-bit25: 10 bits for the fold level (1024 levels)
- bit26: 1 bit for the fold trigger flag (trigger or not trigger)
- bit27: 1 bit for the fold trigger state (expanded/collapsed)
Gets the block fold level
Parameters: | block – block to access. |
---|---|
Returns: | The block fold level |
Gets the fold trigger state.
Parameters: | block – QTextBlock |
---|---|
Returns: | False for an open trigger, True for for closed trigger |
Gets the user state, generally used for syntax highlighting. :param block: block to access :return: The block state
Checks if the block is a fold trigger.
Parameters: | block – block to check |
---|---|
Returns: | True if the block is a fold trigger (represented as a node in the fold panel) |
Sets the block fold level.
Parameters: |
|
---|
Set the block fold trigger flag (True means the block is a fold trigger).
Parameters: |
|
---|
Sets the fold trigger state.
Parameters: |
|
---|
Sets the user state, generally used for syntax highlighting.
Parameters: |
|
---|---|
Returns: |
Bases: PyQt5.QtWidgets.ExtraSelection
Helper class to quickly create a text decoration. The text decoration is an utility class that adds a few utility methods to QTextEdit.ExtraSelection.
In addition to the helper methods, a tooltip can be added to a decoration. (useful for errors markers and so on...)
deco = TextDecoration()
deco.signals.clicked.connect(a_slot)
def a_slot(decoration):
print(decoration)
Bases: PyQt5.QtCore.QObject
Holds the signals for a TextDecoration (since we cannot make it a QObject, we need to store its signals in an external QObject).
Signal emitted when a TextDecoration has been clicked.
Checks if the textCursor is in the decoration
Parameters: | cursor (QtGui.QTextCursor) – The text cursor to test |
---|---|
Returns: | True if the cursor is over the selection |
Select the entire line but starts at the first non whitespace character and stops at the non-whitespace character. :return:
Uses bold text
Highlights text as a syntax error.
Parameters: | color (QtGui.QColor) – Underline color |
---|
Underlines text as a spellcheck error.
Parameters: | color (QtGui.QColor) – Underline color |
---|
Underlines the text
Parameters: | color – underline color. |
---|
Highlights text as a syntax warning
Parameters: | color (QtGui.QColor) – Underline color |
---|
Sets the background brush.
Parameters: | brush (QtGui.QBrush) – Brush |
---|
Sets the foreground color. :param color: Color :type color: QtGui.QColor
Enables FullWidthSelection (the selection does not stops at after the character instead it goes up to the right side of the widget).
Parameters: |
---|
Uses an outline rectangle.
Parameters: | color (QtGui.QColor) – Color of the outline rect |
---|
Bases: builtins.object
Utility class for running job after a certain delay. If a new request is made during this delay, the previous request is dropped and the timer is restarted for the new request.
We use this to implement a cooldown effect that prevents jobs from being executed while the IDE is not idle.
A job is a simple callable.
Cancels pending requests.
Request a job execution. The job will be executed after the delay specified in the DelayJobRunner contructor elapsed if no other job is requested until then.
Parameters: |
|
---|