eric7/QScintilla/Lexers/LexerCPP.py

branch
eric7
changeset 8312
800c432b34c8
parent 8243
cc717c2ae956
child 8318
962bce857696
equal deleted inserted replaced
8311:4e8b98454baa 8312:800c432b34c8
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2002 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a CPP lexer with some additional methods.
8 """
9
10 import contextlib
11
12 from PyQt5.Qsci import QsciLexerCPP, QsciScintilla
13
14 from .SubstyledLexer import SubstyledLexer
15 import Preferences
16
17
18 class LexerCPP(SubstyledLexer, QsciLexerCPP):
19 """
20 Subclass to implement some additional lexer dependant methods.
21 """
22 def __init__(self, parent=None, caseInsensitiveKeywords=False):
23 """
24 Constructor
25
26 @param parent parent widget of this lexer
27 @param caseInsensitiveKeywords flag indicating keywords are case
28 insensitive (boolean)
29 """
30 QsciLexerCPP.__init__(self, parent, caseInsensitiveKeywords)
31 SubstyledLexer.__init__(self)
32
33 self.commentString = "//"
34 self.streamCommentString = {
35 'start': '/* ',
36 'end': ' */'
37 }
38 self.boxCommentString = {
39 'start': '/* ',
40 'middle': ' * ',
41 'end': ' */'
42 }
43
44 self.keywordSetDescriptions = [
45 self.tr("Primary keywords and identifiers"),
46 self.tr("Secondary keywords and identifiers"),
47 self.tr("Documentation comment keywords"),
48 self.tr("Global classes and typedefs"),
49 self.tr("Preprocessor definitions"),
50 self.tr("Task marker and error marker keywords"),
51 ]
52
53 ##############################################################
54 ## default sub-style definitions
55 ##############################################################
56
57 diffToSecondary = 0x40
58 # This may need to be changed to be in line with Scintilla C++ lexer.
59
60 # list of style numbers, that support sub-styling
61 self.baseStyles = [11, 17, 11 + diffToSecondary, 17 + diffToSecondary]
62
63 self.defaultSubStyles = {
64 11: {
65 0: {
66 "Description": self.tr("Additional Identifier"),
67 "Words": "std map string vector",
68 "Style": {
69 "fore": 0xEE00AA,
70 }
71 },
72 },
73 17: {
74 0: {
75 "Description": self.tr("Additional JavaDoc keyword"),
76 "Words": "check",
77 "Style": {
78 "fore": 0x00AAEE,
79 }
80 },
81 },
82 11 + diffToSecondary: {
83 0: {
84 "Description": self.tr("Inactive additional identifier"),
85 "Words": "std map string vector",
86 "Style": {
87 "fore": 0xBB6666,
88 }
89 },
90 },
91 17 + diffToSecondary: {
92 0: {
93 "Description": self.tr(
94 "Inactive additional JavaDoc keyword"),
95 "Words": "check",
96 "Style": {
97 "fore": 0x6699AA,
98 }
99 },
100 },
101 }
102
103 def initProperties(self):
104 """
105 Public slot to initialize the properties.
106 """
107 self.setFoldComments(Preferences.getEditor("CppFoldComment"))
108 self.setFoldPreprocessor(Preferences.getEditor("CppFoldPreprocessor"))
109 self.setFoldAtElse(Preferences.getEditor("CppFoldAtElse"))
110 indentStyle = 0
111 if Preferences.getEditor("CppIndentOpeningBrace"):
112 indentStyle |= QsciScintilla.AiOpening
113 if Preferences.getEditor("CppIndentClosingBrace"):
114 indentStyle |= QsciScintilla.AiClosing
115 self.setAutoIndentStyle(indentStyle)
116 self.setFoldCompact(Preferences.getEditor("AllFoldCompact"))
117 with contextlib.suppress(AttributeError):
118 self.setDollarsAllowed(Preferences.getEditor("CppDollarsAllowed"))
119 with contextlib.suppress(AttributeError):
120 self.setStylePreprocessor(
121 Preferences.getEditor("CppStylePreprocessor"))
122 with contextlib.suppress(AttributeError):
123 self.setHighlightTripleQuotedStrings(
124 Preferences.getEditor("CppHighlightTripleQuotedStrings"))
125 with contextlib.suppress(AttributeError):
126 self.setHighlightHashQuotedStrings(
127 Preferences.getEditor("CppHighlightHashQuotedStrings"))
128 with contextlib.suppress(AttributeError):
129 self.setHighlightBackQuotedStrings(
130 Preferences.getEditor("CppHighlightBackQuotedStrings"))
131 with contextlib.suppress(AttributeError):
132 self.setHighlightEscapeSequences(
133 Preferences.getEditor("CppHighlightEscapeSequences"))
134 with contextlib.suppress(AttributeError):
135 self.setVerbatimStringEscapeSequencesAllowed(
136 Preferences.getEditor(
137 "CppVerbatimStringEscapeSequencesAllowed"))
138
139 def autoCompletionWordSeparators(self):
140 """
141 Public method to return the list of separators for autocompletion.
142
143 @return list of separators (list of strings)
144 """
145 return ['::', '->', '.']
146
147 def isCommentStyle(self, style):
148 """
149 Public method to check, if a style is a comment style.
150
151 @param style style to check (integer)
152 @return flag indicating a comment style (boolean)
153 """
154 return style in [QsciLexerCPP.Comment,
155 QsciLexerCPP.CommentDoc,
156 QsciLexerCPP.CommentLine,
157 QsciLexerCPP.CommentLineDoc]
158
159 def isStringStyle(self, style):
160 """
161 Public method to check, if a style is a string style.
162
163 @param style style to check (integer)
164 @return flag indicating a string style (boolean)
165 """
166 return style in [QsciLexerCPP.DoubleQuotedString,
167 QsciLexerCPP.SingleQuotedString,
168 QsciLexerCPP.UnclosedString,
169 QsciLexerCPP.VerbatimString]
170
171 def defaultKeywords(self, kwSet):
172 """
173 Public method to get the default keywords.
174
175 @param kwSet number of the keyword set (integer)
176 @return string giving the keywords (string) or None
177 """
178 return QsciLexerCPP.keywords(self, kwSet)
179
180 def maximumKeywordSet(self):
181 """
182 Public method to get the maximum keyword set.
183
184 @return maximum keyword set (integer)
185 """
186 return 4

eric ide

mercurial