|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2003 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the lexer mixin class. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import Preferences |
|
13 |
|
14 |
|
15 class Lexer(object): |
|
16 """ |
|
17 Class to implement the lexer mixin class. |
|
18 """ |
|
19 def __init__(self): |
|
20 """ |
|
21 Constructor |
|
22 """ |
|
23 self.commentString = '' |
|
24 self.streamCommentString = { |
|
25 'start': '', |
|
26 'end': '' |
|
27 } |
|
28 self.boxCommentString = { |
|
29 'start': '', |
|
30 'middle': '', |
|
31 'end': '' |
|
32 } |
|
33 |
|
34 # last indented line wrapper |
|
35 self.lastIndented = -1 |
|
36 self.lastIndentedIndex = -1 |
|
37 |
|
38 # always keep tabs (for languages where tabs are esential |
|
39 self._alwaysKeepTabs = False |
|
40 |
|
41 # descriptions for keyword sets |
|
42 self.keywordSetDescriptions = [] |
|
43 |
|
44 def initProperties(self): |
|
45 """ |
|
46 Public slot to initialize the properties. |
|
47 """ |
|
48 # default implementation is a do nothing |
|
49 return |
|
50 |
|
51 def commentStr(self): |
|
52 """ |
|
53 Public method to return the comment string. |
|
54 |
|
55 @return comment string (string) |
|
56 """ |
|
57 return self.commentString |
|
58 |
|
59 def canBlockComment(self): |
|
60 """ |
|
61 Public method to determine, whether the lexer language supports a |
|
62 block comment. |
|
63 |
|
64 @return flag (boolean) |
|
65 """ |
|
66 return self.commentString != "" |
|
67 |
|
68 def streamCommentStr(self): |
|
69 """ |
|
70 Public method to return the stream comment strings. |
|
71 |
|
72 @return stream comment strings (dictionary with two strings) |
|
73 """ |
|
74 return self.streamCommentString |
|
75 |
|
76 def canStreamComment(self): |
|
77 """ |
|
78 Public method to determine, whether the lexer language supports a |
|
79 stream comment. |
|
80 |
|
81 @return flag (boolean) |
|
82 """ |
|
83 return \ |
|
84 (self.streamCommentString['start'] != "") and \ |
|
85 (self.streamCommentString['end'] != "") |
|
86 |
|
87 def boxCommentStr(self): |
|
88 """ |
|
89 Public method to return the box comment strings. |
|
90 |
|
91 @return box comment strings (dictionary with three QStrings) |
|
92 """ |
|
93 return self.boxCommentString |
|
94 |
|
95 def canBoxComment(self): |
|
96 """ |
|
97 Public method to determine, whether the lexer language supports a |
|
98 box comment. |
|
99 |
|
100 @return flag (boolean) |
|
101 """ |
|
102 return \ |
|
103 (self.boxCommentString['start'] != "") and \ |
|
104 (self.boxCommentString['middle'] != "") and \ |
|
105 (self.boxCommentString['end'] != "") |
|
106 |
|
107 def alwaysKeepTabs(self): |
|
108 """ |
|
109 Public method to check, if tab conversion is allowed. |
|
110 |
|
111 @return flag indicating to keep tabs (boolean) |
|
112 """ |
|
113 return self._alwaysKeepTabs |
|
114 |
|
115 def hasSmartIndent(self): |
|
116 """ |
|
117 Public method indicating whether lexer can do smart indentation. |
|
118 |
|
119 @return flag indicating availability of smartIndentLine and |
|
120 smartIndentSelection methods (boolean) |
|
121 """ |
|
122 return hasattr(self, 'getIndentationDifference') |
|
123 |
|
124 def smartIndentLine(self, editor): |
|
125 """ |
|
126 Public method to handle smart indentation for a line. |
|
127 |
|
128 @param editor reference to the QScintilla editor object |
|
129 """ |
|
130 cline, cindex = editor.getCursorPosition() |
|
131 |
|
132 # get leading spaces |
|
133 lead_spaces = editor.indentation(cline) |
|
134 |
|
135 # get the indentation difference |
|
136 indentDifference = self.getIndentationDifference(cline, editor) |
|
137 |
|
138 if indentDifference != 0: |
|
139 editor.setIndentation(cline, lead_spaces + indentDifference) |
|
140 editor.setCursorPosition(cline, cindex + indentDifference) |
|
141 |
|
142 self.lastIndented = cline |
|
143 |
|
144 def smartIndentSelection(self, editor): |
|
145 """ |
|
146 Public method to handle smart indentation for a selection of lines. |
|
147 |
|
148 Note: The assumption is, that the first line determines the new |
|
149 indentation level. |
|
150 |
|
151 @param editor reference to the QScintilla editor object |
|
152 """ |
|
153 if not editor.hasSelectedText(): |
|
154 return |
|
155 |
|
156 # get the selection |
|
157 lineFrom, indexFrom, lineTo, indexTo = editor.getSelection() |
|
158 if lineFrom != self.lastIndented: |
|
159 self.lastIndentedIndex = indexFrom |
|
160 |
|
161 if indexTo == 0: |
|
162 endLine = lineTo - 1 |
|
163 else: |
|
164 endLine = lineTo |
|
165 |
|
166 # get the indentation difference |
|
167 indentDifference = self.getIndentationDifference(lineFrom, editor) |
|
168 |
|
169 editor.beginUndoAction() |
|
170 # iterate over the lines |
|
171 for line in range(lineFrom, endLine + 1): |
|
172 editor.setIndentation( |
|
173 line, editor.indentation(line) + indentDifference) |
|
174 editor.endUndoAction() |
|
175 |
|
176 if self.lastIndentedIndex != 0: |
|
177 indexStart = indexFrom + indentDifference |
|
178 else: |
|
179 indexStart = 0 |
|
180 if indexStart < 0: |
|
181 indexStart = 0 |
|
182 indexEnd = indexTo != 0 and (indexTo + indentDifference) or 0 |
|
183 if indexEnd < 0: |
|
184 indexEnd = 0 |
|
185 editor.setSelection(lineFrom, indexStart, lineTo, indexEnd) |
|
186 |
|
187 self.lastIndented = lineFrom |
|
188 |
|
189 def autoCompletionWordSeparators(self): |
|
190 """ |
|
191 Public method to return the list of separators for autocompletion. |
|
192 |
|
193 @return list of separators (list of strings) |
|
194 """ |
|
195 return [] |
|
196 |
|
197 def isCommentStyle(self, style): |
|
198 """ |
|
199 Public method to check, if a style is a comment style. |
|
200 |
|
201 @param style style to check (integer) |
|
202 @return flag indicating a comment style (boolean) |
|
203 """ |
|
204 return True |
|
205 |
|
206 def isStringStyle(self, style): |
|
207 """ |
|
208 Public method to check, if a style is a string style. |
|
209 |
|
210 @param style style to check (integer) |
|
211 @return flag indicating a string style (boolean) |
|
212 """ |
|
213 return True |
|
214 |
|
215 def keywords(self, kwSet): |
|
216 """ |
|
217 Public method to get the keywords. |
|
218 |
|
219 @param kwSet number of the keyword set |
|
220 @type int |
|
221 @return space separated list of keywords |
|
222 @rtype str or None |
|
223 """ |
|
224 keywords_ = Preferences.getEditorKeywords(self.language()) |
|
225 if keywords_ and len(keywords_) > kwSet: |
|
226 kw = keywords_[kwSet] |
|
227 if kw == "": |
|
228 return self.defaultKeywords(kwSet) |
|
229 else: |
|
230 return kw |
|
231 else: |
|
232 return self.defaultKeywords(kwSet) |
|
233 |
|
234 def keywordsDescription(self, kwSet): |
|
235 """ |
|
236 Public method to get the description for a keywords set. |
|
237 |
|
238 @param kwSet number of the keyword set |
|
239 @type int |
|
240 @return description of the keyword set |
|
241 @rtype str |
|
242 """ |
|
243 if kwSet > len(self.keywordSetDescriptions): |
|
244 return "" |
|
245 else: |
|
246 return self.keywordSetDescriptions[kwSet - 1] |
|
247 |
|
248 def defaultKeywords(self, kwSet): |
|
249 """ |
|
250 Public method to get the default keywords. |
|
251 |
|
252 @param kwSet number of the keyword set |
|
253 @type int |
|
254 @return space separated list of keywords |
|
255 @rtype str or None |
|
256 """ |
|
257 return None # __IGNORE_WARNING_M831__ |
|
258 |
|
259 def maximumKeywordSet(self): |
|
260 """ |
|
261 Public method to get the maximum keyword set. |
|
262 |
|
263 Note: A return value of 0 indicates to determine this dynamically. |
|
264 |
|
265 @return maximum keyword set |
|
266 @rtype int |
|
267 """ |
|
268 return len(self.keywordSetDescriptions) |
|
269 |
|
270 def lexerName(self): |
|
271 """ |
|
272 Public method to return the lexer name. |
|
273 |
|
274 @return lexer name (string) |
|
275 """ |
|
276 return self.lexer() |
|
277 |
|
278 def hasSubstyles(self): |
|
279 """ |
|
280 Public method to indicate the support of sub-styles. |
|
281 |
|
282 @return flag indicating sub-styling support |
|
283 @rtype bool |
|
284 """ |
|
285 return False |