|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | + ColumnLexer - Demo |
| 4 | +
|
| 5 | + A column based lexer, odd and even columns will be styled differently. |
| 6 | + |
| 7 | + Usage: |
| 8 | + Default column separator is comma, redefine COLUMN_SEPARATOR if needed. |
| 9 | + Run script. |
| 10 | +
|
| 11 | + Note: By commenting or deleting everything, including, |
| 12 | + the <comment_or_delete> tags it is ready to be used |
| 13 | + as an additional lexer |
| 14 | +
|
| 15 | +""" |
| 16 | + |
| 17 | +from Npp import editor, notepad, LEXER, SCINTILLANOTIFICATION, NOTIFICATION, LANGTYPE |
| 18 | + |
| 19 | +try: |
| 20 | + # on first run this will generate an NameError exception |
| 21 | + COLUMN_LEXER().main() |
| 22 | +except NameError: |
| 23 | + |
| 24 | + class COLUMN_LEXER_SINGLETON(type): |
| 25 | + ''' Ensures that only one column lexer instance exists and |
| 26 | + prevents of getting multiple callbacks |
| 27 | + ''' |
| 28 | + _instance = None |
| 29 | + def __call__(cls, *args, **kwargs): |
| 30 | + ''' The real constructor and first method called when |
| 31 | + a new instance should be created. |
| 32 | + On first instantiation class variable _instance gets itself assigned, |
| 33 | + every subsequent instantiation try returns this object |
| 34 | + ''' |
| 35 | + if cls._instance is None: |
| 36 | + cls._instance = super(COLUMN_LEXER_SINGLETON, cls).__call__(*args, **kwargs) |
| 37 | + return cls._instance |
| 38 | + |
| 39 | + class COLUMN_LEXER(object): |
| 40 | + ''' A column based lexer implementation. |
| 41 | + Odd and even columns do get different styles assigned. |
| 42 | + Only one separator can be defined. |
| 43 | + Line-based approach - styling takes place when new line has been added |
| 44 | + ''' |
| 45 | + __metaclass__ = COLUMN_LEXER_SINGLETON |
| 46 | + |
| 47 | + DEFAULT = 0 # don't change it |
| 48 | + ODD_COLUMN_STYLE = 50 |
| 49 | + EVEN_COLUMN_STYLE = 51 |
| 50 | + COLUMN_SEPARATOR = ',' |
| 51 | + |
| 52 | + def __init__(self): |
| 53 | + ''' Register needed callbacks on first class instantiation ''' |
| 54 | + editor.callbackSync(self.styleneeded_callback, [SCINTILLANOTIFICATION.STYLENEEDED]) |
| 55 | + notepad.callback(self.bufferactivated_callback, [NOTIFICATION.BUFFERACTIVATED]) |
| 56 | + notepad.callback(self.langchanged_callback, [NOTIFICATION.LANGCHANGED]) |
| 57 | + |
| 58 | + |
| 59 | + def column_lexer(self, start_pos, end_pos): |
| 60 | + ''' Main lexing logic. |
| 61 | + Gets called by styleneeded callback |
| 62 | + ''' |
| 63 | + |
| 64 | + def style_it(start, length, STYLE): |
| 65 | + ''' Inform scintilla to do the styling''' |
| 66 | + if length >= 0: |
| 67 | + editor.startStyling(start, 31) |
| 68 | + editor.setStyling(length, STYLE) |
| 69 | + |
| 70 | + # first everything will be styled with default style |
| 71 | + style_it(start_pos, end_pos-start_pos, self.DEFAULT) |
| 72 | + |
| 73 | + # loop over line indexes from start_pos and end_pos |
| 74 | + for line in range(editor.lineFromPosition(start_pos), editor.lineFromPosition(end_pos)): |
| 75 | + # get start position for each line |
| 76 | + line_start_pos = editor.positionFromLine(line) |
| 77 | + # split current line into columns |
| 78 | + columns = editor.getLine(line).split(self.COLUMN_SEPARATOR) |
| 79 | + if columns > 0: |
| 80 | + # iterate over all columns |
| 81 | + for i, column in enumerate(columns): |
| 82 | + # get the width of the current column |
| 83 | + col_length = len(column) |
| 84 | + if i % 2 == 0: |
| 85 | + # even column |
| 86 | + style_it(line_start_pos,col_length,self.EVEN_COLUMN_STYLE) |
| 87 | + else: |
| 88 | + # odd column |
| 89 | + style_it(line_start_pos,col_length, self.ODD_COLUMN_STYLE) |
| 90 | + # recalculate start position for next column |
| 91 | + line_start_pos += col_length+1 |
| 92 | + |
| 93 | + # this needs to stay and to be the last line, to signal scintilla we are done. |
| 94 | + editor.startStyling(end_pos,31) |
| 95 | + |
| 96 | + def init_scintilla(self): |
| 97 | + ''' Initialize configured styles ''' |
| 98 | + editor.setMarginWidthN(0,38) |
| 99 | + editor.setMarginWidthN(1,14) |
| 100 | + editor.setMarginWidthN(2,0) |
| 101 | + |
| 102 | + if editor.getLexer() != LEXER.CONTAINER: |
| 103 | + editor.setLexer(LEXER.CONTAINER) |
| 104 | + |
| 105 | + editor.styleSetFore(self.ODD_COLUMN_STYLE, (54,125,198)) |
| 106 | + editor.styleSetFore(self.EVEN_COLUMN_STYLE, (87,166,74)) |
| 107 | + editor.styleSetBack(self.EVEN_COLUMN_STYLE, (0,50,20)) |
| 108 | + |
| 109 | + |
| 110 | + def set_lexer_doc(self, bool_value): |
| 111 | + ''' Assign the class name as an additional property |
| 112 | + to every document which should be handled by this lexer |
| 113 | + A value of 1 indicates it should be handled. |
| 114 | + ''' |
| 115 | + editor.setProperty(self.__class__.__name__, 1 if bool_value is True else 0) |
| 116 | + |
| 117 | + |
| 118 | + def is_lexer_doc(self): |
| 119 | + ''' Check if the current document is of interest |
| 120 | + by reading the class name property. |
| 121 | + ''' |
| 122 | + return True if editor.getPropertyInt(self.__class__.__name__) == 1 else False |
| 123 | + |
| 124 | + |
| 125 | + def styleneeded_callback(self,args): |
| 126 | + ''' Called by scintilla to inform the lexer |
| 127 | + about the need to style the document. |
| 128 | + If document is of interest call main logic (column_lexer) function |
| 129 | + Ensures that the start position is really the first position per line |
| 130 | + ''' |
| 131 | + if self.is_lexer_doc(): |
| 132 | + startPos = editor.getEndStyled() |
| 133 | + lineNumber = editor.lineFromPosition(startPos) |
| 134 | + startPos = editor.positionFromLine(lineNumber) |
| 135 | + self.column_lexer(startPos, args['position']) |
| 136 | + |
| 137 | + |
| 138 | + def bufferactivated_callback(self,args): |
| 139 | + ''' Called by notepad when document switch happens |
| 140 | + If document is of interest styles need to be reinitialized |
| 141 | + ''' |
| 142 | + if self.is_lexer_doc(): |
| 143 | + self.init_scintilla() |
| 144 | + |
| 145 | + |
| 146 | + def langchanged_callback(self,args): |
| 147 | + ''' Called by notepad when a built-in or udl language switch happens |
| 148 | + If document was previously styled by this lexer it will be reset |
| 149 | + and therefore will not be styled by this lexer anymore until |
| 150 | + script gets executed on this document again. |
| 151 | + ''' |
| 152 | + if self.is_lexer_doc(): |
| 153 | + self.set_lexer_doc(False) |
| 154 | + |
| 155 | + |
| 156 | + def main(self): |
| 157 | + ''' Main entry point |
| 158 | + To prevent issues with other lexers document language will |
| 159 | + be set to normal text, then document does get the class name |
| 160 | + property assigned, styles do get initialized and main lexing |
| 161 | + function does get called on whole document |
| 162 | + ''' |
| 163 | + notepad.setLangType(LANGTYPE.TXT) |
| 164 | + self.set_lexer_doc(True) |
| 165 | + self.init_scintilla() |
| 166 | + self.column_lexer(0, editor.getTextLength()) |
| 167 | + |
| 168 | + # <comment_or_delete> |
| 169 | + # just some demo text not really needed by lexer |
| 170 | + notepad.new() |
| 171 | + editor.appendText(''' |
| 172 | +col1,col2,col3,col4 |
| 173 | +col1 , col2, col3 , col4 |
| 174 | +,,col3,col4 |
| 175 | +col1,column2,col3,colum4 |
| 176 | +jdjkslddskjfd,jfdksldlskdfkjklsdfs,jfkdslldkjfds,jkfdjskdlskdfj |
| 177 | +''' |
| 178 | + ) |
| 179 | + # </comment_or_delete> |
| 180 | + |
| 181 | + COLUMN_LEXER().main() |
0 commit comments