pyqode.core.modes package

Module content

This package contains the core modes.

Classes

AutoCompleteMode

class pyqode.core.modes.AutoCompleteMode

Bases: pyqode.core.api.mode.Mode

Automatically complete quotes and parentheses

Generic auto complete mode that automatically completes the following symbols:

  • ” -> “
  • ‘ -> ‘
  • ( -> )
  • [ -> ]
  • { -> }
on_state_changed(state)
MAPPING = {'"': '"', '(': ')', "'": "'", '[': ']', '{': '}'}

Auto complete mapping, maps input key with completion text.

QUOTES_FORMATS = {'"': '%s', "'": '%s', '(': '%s', '[': '%s', '{': '%s'}

The format to use for each symbol in mapping when there is no selection

SELECTED_QUOTES_FORMATS = {'"': '%s%s%s', "'": '%s%s%s', '(': '%s%s%s', '[': '%s%s%s', '{': '%s%s%s'}

The format to use for each symbol in mapping when there is a selection

AutoIndentMode

class pyqode.core.modes.AutoIndentMode

Bases: pyqode.core.api.mode.Mode

Indents text automatically. Generic indenter mode that indents the text when the user press RETURN.

You can customize this mode by overriding pyqode.core.modes.AutoIndentMode._get_indent()

on_state_changed(state)

CaretLineHighlighterMode

class pyqode.core.modes.CaretLineHighlighterMode

Bases: pyqode.core.api.mode.Mode

Highlights the caret line

clone_settings(original)
on_install(editor)
on_state_changed(state)
refresh()

Updates the current line decoration

background

Background color of the caret line. Default is to use a color slightly darker/lighter than the background color. You can override the automatic color by setting up this property

CaseConverterMode

class pyqode.core.modes.CaseConverterMode

Bases: pyqode.core.api.mode.Mode

Provides context actions for converting case of the selected text.

Converts selected text to lower case or UPPER case.

It does so by adding two new menu entries to the editor’s context menu:
  • Convert to lower case: ctrl-u
  • Convert to UPPER CASE: ctrl+shift+u
on_state_changed(state)
to_lower()

Converts selected text to lower

to_upper()

Converts selected text to upper

CheckerMode

class pyqode.core.modes.CheckerMode(worker, delay=500, show_tooltip=True)

Bases: pyqode.core.api.mode.Mode, PyQt5.QtCore.QObject

Performs a user defined code analysis job using the backend and display the results on the editor instance.

The user defined code analysis job is a simple function with the following signature:

def analysisProcess(data)

where data is the request data:

request_data = {
        'code': self.editor.toPlainText(),
        'path': self.editor.file.path,
        'encoding': self.editor.file.encoding
    }

and the return value is a tuple made up of the following elements:

(description, status, line, [col], [icon], [color], [path])

The background process is ran when the text changed and the ide is an idle state for a few seconds.

You can also request an analysis manually using pyqode.core.modes.CheckerMode.request_analysis()

Messages are displayed as text decorations on the editor. A checker panel will take care of display message icons next to each line.

add_messages(messages)

Adds a message or a list of message.

Parameters:messages – A list of messages or a single message
clear_messages()

Clears all messages.

on_state_changed(state)
remove_message(message)

Removes a message.

Parameters:message – Message to remove
request_analysis()

Requests an analysis.

messages

Returns the entire list of checker messages.

CheckerMessage

class pyqode.core.modes.CheckerMessage(description, status, line, col=None, icon=None, color=None, path=None)

Bases: builtins.object

Holds data for a message displayed by the pyqode.core.modes.CheckerMode.

classmethod status_to_string(status)

Converts a message status to a string.

Parameters:status – Status to convert (p yqode.core.modes.CheckerMessages)
Returns:The status string.
Return type:str
COLORS = {0: '#4040DD', 1: '#DDDD40', 2: '#DD4040'}

Default colors foreach message status

block = None

store a reference to the associated QTextBlock, for quick acces

col = None

The start column (used for the text decoration). If the col is None, the whole line is highlighted.

color = None

The color used for the text decoration. If None, the default color is used (pyqode.core.CheckerMessage.COLORS)

description = None

The description of the message, used as a tooltip.

line = None

The line of the message

status = None
The status associated with the message. One of:
status_string

Returns the message status as a string.

Returns:The status string.

CheckerMessages

class pyqode.core.modes.CheckerMessages

Bases: builtins.object

Enumerates the possible checker message types.

ERROR = 2

Status value for an error message.

INFO = 0

Status value for an information message.

WARNING = 1

Status value for a warning message.

CodeCompletionMode

class pyqode.core.modes.CodeCompletionMode

Bases: pyqode.core.api.mode.Mode, PyQt5.QtCore.QObject

Provides code completions when typing or when pressing Ctrl+Space.

This mode provides a code completion system which is extensible. µ It takes care of running the completion request in a background process using one or more completion provider.

To implement a code completion for a specific language, you only need to implement new pyqode.core.backend.workers.CodeCompletionWorker.Provider

The completion popup is shown the user press ctrl+space or automatically while the user is typing some code (this can be configured using a series of properties).

clone_settings(original)
on_install(editor)
on_state_changed(state)
on_uninstall()
request_completion()

Requests a code completion at the current cursor position.

static strip_control_characters(input_txt)

Strips control character from input_txt :param input_txt: text to strip. :return: stripped text

case_sensitive

True to performs case sensitive completion matching.

completion_prefix

Returns the current completion prefix

show_tooltips

True to show tooltips next to the current completion.

trigger_key

The key that triggers code completion (Default is Space: Ctrl + Space).

trigger_length

The trigger length defines the word length required to run code completion.

trigger_symbols

Defines the list of symbols that immediately trigger a code completion requiest. BY default, this list contains the dot character.

For C++, we would add the ‘->’ operator to that list.

ExtendedSelectionMode

class pyqode.core.modes.ExtendedSelectionMode

Bases: pyqode.core.api.mode.Mode

Adds extended selection capabilities (Ctrl/Alt + Double click).

This mode adds extended selections capabilities to CodeEdit.

Extended selection is a feature that can be found in the Ulipad editor: https://code.google.com/p/ulipad

It consists in adding a few shortcuts and contextual action to do some smarter selections. This mode adds the following new kind of selections:

  • word selection: select word under cursor
  • extended word selection: select word under cursor including continuation characters such as ”.”.
  • matched selection: select text inside quotes or parenthesis
  • line selection: select the whole line
  • select all: select entire document

Extended selection and matched selection can be performed by combining ctrl or alt with a double click (modifiers are configurable through extended_sel_modifier or matched_sel_modifier).

create_menu()

Creates the extended selection menu.

on_install(editor)
on_state_changed(state)
perform_extended_selection(event=None)

Performs extended word selection. :param event: QMouseEvent

perform_line_selection()

Performs line selection (select the entire line).

perform_matched_selection(event)

Performs matched selection. :param event: QMouseEvent

perform_word_selection(event=None)

Performs word selection :param event: QMouseEvent

FileWatcherMode

class pyqode.core.modes.FileWatcherMode

Bases: pyqode.core.api.mode.Mode, PyQt5.QtCore.QObject

Watches the current file for external modifications.

FileWatcher mode, check if the opened file has changed externally.

clone_settings(original)
on_state_changed(state)
auto_reload

Automatically reloads changed files

file_deleted

Signal emitted when the file has been deleted. The Signal is emitted with the current editor instance so that user have a chance to close the editor.

file_reloaded

Signal emitted when the file has been reloaded in the editor.

IndenterMode

class pyqode.core.modes.IndenterMode

Bases: pyqode.core.api.mode.Mode

Implements classic indentation/tabulation (Tab/Shift+Tab)

It inserts/removes tabulations (a series of spaces defined by the tabLength settings) at the cursor position if there is no selection, otherwise it fully indents/un-indents selected lines.

To trigger an indentation/un-indentation programatically, you must emit pyqode.core.api.CodeEdit.indent_requested or pyqode.core.api.CodeEdit.unindent_requested.

count_deletable_spaces(cursor, max_spaces)
indent()

Indents text at cursor position.

indent_selection(cursor)

Indent selected text

Parameters:cursor – QTextCursor
on_state_changed(state)
unindent()

Un-indents text at cursor position.

unindent_selection(cursor)

Un-indents selected text

Parameters:cursor – QTextCursor

OccurrencesHighlighterMode

class pyqode.core.modes.OccurrencesHighlighterMode

Bases: pyqode.core.api.mode.Mode

Highlights occurrences of the word under the text text cursor.

The delay before searching for occurrences is configurable.

clone_settings(original)
on_state_changed(state)
background

Background or underline color (if underlined is True).

delay

Delay before searching for occurrences. The timer is rearmed as soon as the cursor position changed.

foreground

Foreground color of occurences, not used if underlined is True.

timer = None

Timer used to run the search request with a specific delay

underlined

True to use to underlined occurrences instead of changing the background. Default is True.

If this mode is ON, the foreground color is ignored, the background color is then used as the underline color.

PygmentsSH

class pyqode.core.modes.PygmentsSH(document, lexer=None, color_scheme=None)

Bases: pyqode.core.api.syntax_highlighter.SyntaxHighlighter

Highlights code using the pygments parser.

This mode enable syntax highlighting using the pygments library. This is a generic syntax highlighter, it is slower than a native highlighter and does not do any code folding detection. Use it as a fallback for languages that do not have a native highlighter available. Check the other pyqode namespace packages to see what other languages are available (at the time of writing, only python has specialised support).

Warning

There are some issues with multi-line comments, they are not properly highlighted until a full re-highlight is triggered. The text is automatically re-highlighted on save.

highlight_block(text, block)

Highlights the block using a pygments lexer.

Parameters:
  • text – text of the block to highlith
  • block – block to highlight
on_install(editor)
on_state_changed(state)
set_lexer_from_filename(filename)

Change the lexer based on the filename (actually only the extension is needed)

Parameters:filename – Filename or extension
set_lexer_from_mime_type(mime, **options)

Sets the pygments lexer from mime type.

Parameters:
  • mime – mime type
  • options – optional addtional options.
set_mime_type(mime_type)

Update the highlighter lexer based on a mime type.

Parameters:mime_type – mime type of the new lexer to setup.
DESCRIPTION = 'Apply syntax highlighting to the editor using pygments'

Mode description

pygments_style

Gets/Sets the pygments style

RightMarginMode

class pyqode.core.modes.RightMarginMode

Bases: pyqode.core.api.mode.Mode

Displays a right margin at column the specified position.

clone_settings(original)
on_state_changed(state)

Connects/Disconnects to the painted event of the editor

Parameters:state – Enable state
color

Gets/sets the color of the margin

position

Gets/sets the position of the margin

SmartBackSpaceMode

class pyqode.core.modes.SmartBackSpaceMode

Bases: pyqode.core.api.mode.Mode

Improves backspace behaviour.

When you press backspace and there are spaces on the left of the cursor, those spaces will be deleted (at most tab_len spaces).

Basically this turns backspace into Shitf+Tab

on_state_changed(state)

SymbolMatcherMode

class pyqode.core.modes.SymbolMatcherMode

Bases: pyqode.core.api.mode.Mode

Highlights matching symbols (parentheses, braces,...)

Note

This mode requires the document to be filled with pyqode.core.api.TextBlockUserData, i.e. a pyqode.core.api.SyntaxHighlighter must be installed on the editor instance.

clone_settings(original)
do_symbols_matching()

Performs symbols matching.

on_state_changed(state)
symbol_pos(cursor, character_type=0, symbol_type=0)

Find the corresponding symbol position (line, column) of the specified symbol. If symbol type is PAREN and character_type is OPEN, the function will look for ‘(‘.

Parameters:
  • cursor – QTextCursor
  • character_type – character type to look for (open or close char)
  • symbol_type – symbol type (index in the SYMBOLS map).
SYMBOLS = {0: ('(', ')'), 1: ('[', ']'), 2: ('{', '}')}

known symbols {SYMBOL: (OPEN, CLOSE)}, you can customise this map to add support for other symbols

match_background

Background color of matching symbols.

match_foreground

Foreground color of matching symbols.

unmatch_background

Background color of non-matching symbols.

unmatch_foreground

Foreground color of matching symbols.

WordClickMode

class pyqode.core.modes.WordClickMode

Bases: pyqode.core.api.mode.Mode, PyQt5.QtCore.QObject

Adds support for word click events.

It will highlight the click-able word when the user press control and move the mouse over a word.

Detecting whether a word is click-able is the responsability of the subclasses. You must override _check_word_cursor and call _select_word_cursor if this is a click-able word (this process might be asynchrone) otherwise _clear_selection.

pyqode.core.modes.WordClickMode.word_clicked is emitted when the word is clicked by the user (while keeping control pressed).

on_state_changed(state)
word_clicked

Signal emitted when a word is clicked. The parameter is a QTextCursor with the clicked word set as the selected text.

ZoomMode

class pyqode.core.modes.ZoomMode

Bases: pyqode.core.api.mode.Mode

Zooms/Unzooms the editor (Ctrl+mouse wheel or Ctrl + 0 to reset).

This mode make it possible to zoom in/out the editor view.

Here are the controls:
  • zoom out: ctrl+- or ctrl+mouse wheel backward
  • zoom in: ctrl++ or ctrl+mouse wheel forward
  • reset: ctrl + 0
on_state_changed(state)