QScintilla/Lexers/LexerPython.py

changeset 0
de9c2efb9d02
child 12
1d8dd9706f46
equal deleted inserted replaced
-1:000000000000 0:de9c2efb9d02
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2002 - 2009 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a Python lexer with some additional methods.
8 """
9
10 import re
11
12 from PyQt4.Qsci import QsciLexerPython, QsciScintilla
13
14 from Lexer import Lexer
15 import Preferences
16
17 class LexerPython(QsciLexerPython, Lexer):
18 """
19 Subclass to implement some additional lexer dependant methods.
20 """
21 def __init__(self, parent=None):
22 """
23 Constructor
24
25 @param parent parent widget of this lexer
26 """
27 QsciLexerPython.__init__(self, parent)
28 Lexer.__init__(self)
29
30 self.commentString = "#"
31
32 def initProperties(self):
33 """
34 Public slot to initialize the properties.
35 """
36 if Preferences.getEditor("PythonBadIndentation"):
37 self.setIndentationWarning(QsciLexerPython.Inconsistent)
38 else:
39 self.setIndentationWarning(QsciLexerPython.NoWarning)
40 self.setFoldComments(Preferences.getEditor("PythonFoldComment"))
41 self.setFoldQuotes(Preferences.getEditor("PythonFoldString"))
42 if not Preferences.getEditor("PythonAutoIndent"):
43 self.setAutoIndentStyle(QsciScintilla.AiMaintain)
44 try:
45 self.setV2UnicodeAllowed(Preferences.getEditor("PythonAllowV2Unicode"))
46 self.setV3BinaryOctalAllowed(Preferences.getEditor("PythonAllowV3Binary"))
47 self.setV3BytesAllowed(Preferences.getEditor("PythonAllowV3Bytes"))
48 except AttributeError:
49 pass
50
51 def getIndentationDifference(self, line, editor):
52 """
53 Private method to determine the difference for the new indentation.
54
55 @param line line to perform the calculation for (integer)
56 @param editor QScintilla editor
57 @return amount of difference in indentation (integer)
58 """
59 indent_width = Preferences.getEditor('IndentWidth')
60
61 lead_spaces = editor.indentation(line)
62
63 pline = line - 1
64 while pline >= 0 and re.match('^\s*(#.*)?$', editor.text(pline)):
65 pline -= 1
66
67 if pline < 0:
68 last = 0
69 else:
70 previous_lead_spaces = editor.indentation(pline)
71 # trailing spaces
72 m = re.search(':\s*(#.*)?$', editor.text(pline))
73 last = previous_lead_spaces
74 if m:
75 last += indent_width
76 else:
77 # special cases, like pass (unindent) or return (also unindent)
78 m = re.search('(pass\s*(#.*)?$)|(^[^#]return)',
79 editor.text(pline))
80 if m:
81 last -= indent_width
82
83 if lead_spaces % indent_width != 0 or lead_spaces == 0 \
84 or self.lastIndented != line:
85 indentDifference = last - lead_spaces
86 else:
87 indentDifference = -indent_width
88
89 return indentDifference
90
91 def autoCompletionWordSeparators(self):
92 """
93 Public method to return the list of separators for autocompletion.
94
95 @return list of separators (list of strings)
96 """
97 return ['.']
98
99 def isCommentStyle(self, style):
100 """
101 Public method to check, if a style is a comment style.
102
103 @return flag indicating a comment style (boolean)
104 """
105 return style in [QsciLexerPython.Comment,
106 QsciLexerPython.CommentBlock]
107
108 def isStringStyle(self, style):
109 """
110 Public method to check, if a style is a string style.
111
112 @return flag indicating a string style (boolean)
113 """
114 return style in [QsciLexerPython.DoubleQuotedString,
115 QsciLexerPython.SingleQuotedString,
116 QsciLexerPython.TripleDoubleQuotedString,
117 QsciLexerPython.TripleSingleQuotedString,
118 QsciLexerPython.UnclosedString]

eric ide

mercurial