def move(self, event): ''' Moves the cursor @arg dict event containing a movement dictionary created from the keyPressEvent and it's corresponding keyMap json object @ret bool success True if the cursor was successfully moved ''' success = True try: moveOperation = getattr(QTextCursor, event['details']['MoveOperation'], False) if moveOperation is not False: cursor = self.editor.textCursor() cursor.movePosition(moveOperation, self.defaultCursorMoveType, event['details']['N']) self.editor.setTextCursor(cursor) else: raise Exception('Invalid move operation: {}'.format( stackTrace())) except Exception: logger.warning('Error while moving: {}'.format(stackTrace())) success = False return success
def interceptKeyEvent(event): ''' Intercepts all key press events and determines how to handle said events depending on whether or not the user is in normal mode or insert mode ''' try: #If the key was the escape key or the user is in normal mode take over the #event handling #TODO: Add in a check for user defined key binding exceptions if event.key( ) == Qt.Key_Escape or self.mode == self.NORMAL_MODE: self.normalKeyEventMapper(event.key()) return elif self.mode == self.DELETE_MODE or self.mode == self.YANK_MODE: self.bufferKeyEventMapper(event.key()) return except Exception: logger.warning( 'There was an error in processing key: {} - trace:\n{}'. format(event.key(), stackTrace())) #Otherwise allow the editor to handle said event in the default manner return function(event)
def paste(self, event, bufferName=0): ''' Selects the appropriate text then adds it to the appropriate buffer then deletes it if it was cut event. @arg dict event containing a buffer dictionary created from the keyPressEvent and it's corresponding keyMap json object @arg mixed bufferName the index for the buffer to be added to @ret mixed True if copy/cut was success False otherwise ''' try: logger.info('pasting: {}'.format(self.copyPasteBuffer[bufferName])) #get the cursor and prepare to edit the file cursor = self.editor.textCursor() cursor.beginEditBlock() #if we are pasting a whole line we need to create an empty line above/below #the current line if self.copyPasteBuffer[bufferName]['isLine']: logger.info('in if') #if we are pasting before the cursor we need to move up so as to create #an empty line above the current one if not event['details']['after']: logger.info('in if not') self.move({ 'details': self.keyMap[Qt.Key_K], 'key': Qt.Key_K }) #create a new line and move the cursor to the beginning of it to ignore #the auto indentation self.addNewLine(cursor) cursor.movePosition(QTextCursor.StartOfLine, QTextCursor.MoveAnchor) #if we are not pasting a whole line and are pasting after the cursor #we need to move the cursor to the right elif event['details']['after']: cursor.movePosition(QTextCursor.Right, QTextCursor.MoveAnchor) #insert the buffered text into the file cursor.insertText(self.copyPasteBuffer[bufferName]['text']) except Exception: logger.warning('pasting error: {}'.format(stackTrace())) cursor.endEditBlock()
def bufferChars(self, event, bufferName=0): ''' Selects the appropriate text then adds it to the appropriate buffer then deletes it if it was cut event. @arg dict event containing a buffer dictionary created from the keyPressEvent and it's corresponding keyMap json object @arg mixed bufferName the index for the buffer to be added to @ret mixed True if copy/cut was success False otherwise ''' try: #get the cursor and prepare to edit the file cursor = self.editor.textCursor() cursor.beginEditBlock() #perform the appropriate selection event['details']['MoveOperation'](cursor) #add the text to the buffer self.copyPasteBuffer[bufferName]['text'] = cursor.selectedText() #if the text was a full line special behaviour is expected for pasting self.copyPasteBuffer[bufferName]['isLine'] = event['details'][ 'isLine'] logger.info('text: "{}"'.format( self.copyPasteBuffer[bufferName]['text'])) logger.info('isLine: {}'.format( self.copyPasteBuffer[bufferName]['isLine'])) #if we are in delete mode or key pressed was x remove the text if self.mode == self.DELETE_MODE or event['key'] == Qt.Key_X: cursor.removeSelectedText() #if we are removing a whole line make sure to remove the new line chr if event['details']['isLine']: cursor.deleteChar() except Exception: logger.warning('copy/cut error: {}'.format(stackTrace())) cursor.endEditBlock()
def switchMode(self, event): ''' Changes the mode of the editor @arg dict event containing a mode dictionary created from the keyPressEvent and it's corresponding keyMap json object @ret bool success returns True if there were no errors, False otherwise ''' success = True try: self.mode = event['details']['Mode'] self.defaultCursorMoveType = event['details']['Anchor'] self.editor.setCursorWidth(event['details']['CursorWidth']) except Exception: logger.warning('Error while switching mode: {}'.format( stackTrace())) success = False return success
import logging logger = logging.getLogger(LOG_FILE) hdlr = logging.FileHandler(os.path.join(PATH, '..', LOG_FILE)) hdlr.setFormatter( logging.Formatter( '%(levelname)-8s %(asctime)s %(name)s:%(lineno)-4d %(message)s')) logger.addHandler(hdlr) logger.setLevel(logging.INFO) except Exception as e: errMessage = '' try: if isinstance(e, ImportError) and stackTrace is not None: errMessage = 'Import Error: {}'.format(stackTrace()) else: errMessage = 'Unknown Error during import process: {}'.format( stackTrace()) finally: raise Exception(errMessage) class Vimja(plugin.Plugin): ''' A vim plugin for the Ninja-IDE. Gives Basic vim functionality such as the vim movements and standard commands. '''