#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
**inputAccelerators.py**
**Platform:**
Windows, Linux, Mac Os X.
**Description:**
Defines the Application input accelerators objects.
**Others:**
"""
#**********************************************************************************************************************
#*** Future imports.
#**********************************************************************************************************************
from __future__ import unicode_literals
#**********************************************************************************************************************
#*** External imports.
#**********************************************************************************************************************
import re
from PyQt4.QtCore import Qt
from PyQt4.QtGui import QTextCursor
#**********************************************************************************************************************
#*** Internal imports.
#**********************************************************************************************************************
import foundations.common
import foundations.strings
import foundations.verbose
#**********************************************************************************************************************
#*** Module attributes.
#**********************************************************************************************************************
__author__ = "Thomas Mansencal"
__copyright__ = "Copyright (C) 2008 - 2014 - Thomas Mansencal"
__license__ = "GPL V3.0 - http://www.gnu.org/licenses/"
__maintainer__ = "Thomas Mansencal"
__email__ = "[email protected]"
__status__ = "Production"
__all__ = ["LOGGER",
"getEditorCapability",
"isSymbolsPairComplete",
"performCompletion",
"indentationPreEventInputAccelerators",
"indentationPostEventInputAccelerators",
"completionPreEventInputAccelerators",
"completionPostEventInputAccelerators",
"symbolsExpandingPreEventInputAccelerators"]
LOGGER = foundations.verbose.installLogger()
#**********************************************************************************************************************
#*** Module classes and definitions.
#**********************************************************************************************************************
[docs]def getEditorCapability(editor, capability):
"""
Returns given editor capability.
:param editor: Document editor.
:type editor: QWidget
:param capability: Capability to retrieve.
:type capability: unicode
:return: Capability.
:rtype: object
"""
if not hasattr(editor, "language"):
return
return editor.language.get(capability)
[docs]def isSymbolsPairComplete(editor, symbol):
"""
Returns if the symbols pair is complete on current editor line.
:param editor: Document editor.
:type editor: QWidget
:param symbol: Symbol to check.
:type symbol: unicode
:return: Is symbols pair complete.
:rtype: bool
"""
symbolsPairs = getEditorCapability(editor, "symbolsPairs")
if not symbolsPairs:
return
cursor = editor.textCursor()
cursor.movePosition(QTextCursor.StartOfLine, QTextCursor.MoveAnchor)
cursor.movePosition(QTextCursor.EndOfLine, QTextCursor.KeepAnchor)
selectedText = foundations.strings.toString(cursor.selectedText())
if symbol == symbolsPairs[symbol]:
return selectedText.count(symbol) % 2 == 0
else:
return selectedText.count(symbol) == selectedText.count(symbolsPairs[symbol])
[docs]def indentationPostEventInputAccelerators(editor, event):
"""
Implements indentation post event input accelerators.
:param editor: Document editor.
:type editor: QWidget
:param event: Event being handled.
:type event: QEvent
:return: Method success.
:rtype: bool
"""
if event.key() in (Qt.Key_Enter, Qt.Key_Return):
cursor = editor.textCursor()
block = cursor.block().previous()
if block.isValid():
indent = match = re.match(r"(\s*)", foundations.strings.toString(block.text())).group(1)
cursor.insertText(indent)
indentationSymbols = getEditorCapability(editor, "indentationSymbols")
if not indentationSymbols:
return True
if not block.text():
return True
if not foundations.strings.toString(block.text())[-1] in indentationSymbols:
return True
symbolsPairs = getEditorCapability(editor, "symbolsPairs")
if not symbolsPairs:
return True
cursor.insertText(editor.indentMarker)
position = cursor.position()
cursor.movePosition(QTextCursor.PreviousBlock, QTextCursor.MoveAnchor)
cursor.movePosition(QTextCursor.EndOfLine, QTextCursor.MoveAnchor)
cursor.movePosition(QTextCursor.PreviousCharacter, QTextCursor.KeepAnchor)
previousCharacter = foundations.strings.toString(cursor.selectedText())
cursor.setPosition(position)
nextCharacter = editor.getNextCharacter()
if previousCharacter in symbolsPairs:
if nextCharacter in symbolsPairs.values():
cursor.insertBlock()
cursor.insertText(match)
cursor.movePosition(QTextCursor.PreviousBlock, QTextCursor.MoveAnchor)
cursor.movePosition(QTextCursor.EndOfLine, QTextCursor.MoveAnchor)
editor.setTextCursor(cursor)
return True
[docs]def completionPostEventInputAccelerators(editor, event):
"""
Implements completion post event input accelerators.
:param editor: Document editor.
:type editor: QWidget
:param event: Event being handled.
:type event: QEvent
:return: Process event.
:rtype: bool
"""
if editor.completer:
if editor.completer.popup().isVisible():
performCompletion(editor)
return True