src/eric7/QScintilla/TypingCompleters/CompleterRuby.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
18 18
19 class CompleterRuby(CompleterBase): 19 class CompleterRuby(CompleterBase):
20 """ 20 """
21 Class implementing typing completer for Ruby. 21 Class implementing typing completer for Ruby.
22 """ 22 """
23
23 def __init__(self, editor, parent=None): 24 def __init__(self, editor, parent=None):
24 """ 25 """
25 Constructor 26 Constructor
26 27
27 @param editor reference to the editor object (QScintilla.Editor) 28 @param editor reference to the editor object (QScintilla.Editor)
28 @param parent reference to the parent object (QObject) 29 @param parent reference to the parent object (QObject)
29 """ 30 """
30 super().__init__(editor, parent) 31 super().__init__(editor, parent)
31 32
32 self.__beginRX = re.compile(r"""^=begin """) 33 self.__beginRX = re.compile(r"""^=begin """)
33 self.__beginNlRX = re.compile(r"""^=begin\r?\n""") 34 self.__beginNlRX = re.compile(r"""^=begin\r?\n""")
34 self.__hereRX = re.compile(r"""<<-?['"]?(\w*)['"]?\r?\n""") 35 self.__hereRX = re.compile(r"""<<-?['"]?(\w*)['"]?\r?\n""")
35 36
36 self.readSettings() 37 self.readSettings()
37 38
38 def readSettings(self): 39 def readSettings(self):
39 """ 40 """
40 Public slot called to reread the configuration parameters. 41 Public slot called to reread the configuration parameters.
41 """ 42 """
42 self.setEnabled(Preferences.getEditorTyping("Ruby/EnabledTypingAids")) 43 self.setEnabled(Preferences.getEditorTyping("Ruby/EnabledTypingAids"))
43 self.__insertClosingBrace = Preferences.getEditorTyping( 44 self.__insertClosingBrace = Preferences.getEditorTyping(
44 "Ruby/InsertClosingBrace") 45 "Ruby/InsertClosingBrace"
45 self.__indentBrace = Preferences.getEditorTyping( 46 )
46 "Ruby/IndentBrace") 47 self.__indentBrace = Preferences.getEditorTyping("Ruby/IndentBrace")
47 self.__skipBrace = Preferences.getEditorTyping( 48 self.__skipBrace = Preferences.getEditorTyping("Ruby/SkipBrace")
48 "Ruby/SkipBrace") 49 self.__insertQuote = Preferences.getEditorTyping("Ruby/InsertQuote")
49 self.__insertQuote = Preferences.getEditorTyping( 50 self.__insertBlank = Preferences.getEditorTyping("Ruby/InsertBlank")
50 "Ruby/InsertQuote") 51 self.__insertHereDoc = Preferences.getEditorTyping("Ruby/InsertHereDoc")
51 self.__insertBlank = Preferences.getEditorTyping( 52 self.__insertInlineDoc = Preferences.getEditorTyping("Ruby/InsertInlineDoc")
52 "Ruby/InsertBlank")
53 self.__insertHereDoc = Preferences.getEditorTyping(
54 "Ruby/InsertHereDoc")
55 self.__insertInlineDoc = Preferences.getEditorTyping(
56 "Ruby/InsertInlineDoc")
57 53
58 def charAdded(self, charNumber): 54 def charAdded(self, charNumber):
59 """ 55 """
60 Public slot called to handle the user entering a character. 56 Public slot called to handle the user entering a character.
61 57
62 @param charNumber value of the character entered (integer) 58 @param charNumber value of the character entered (integer)
63 """ 59 """
64 char = chr(charNumber) 60 char = chr(charNumber)
65 if char not in ['(', ')', '{', '}', '[', ']', ',', "'", '"', 61 if char not in ["(", ")", "{", "}", "[", "]", ",", "'", '"', "\n", " "]:
66 '\n', ' ']:
67 return # take the short route 62 return # take the short route
68 63
69 line, col = self.editor.getCursorPosition() 64 line, col = self.editor.getCursorPosition()
70 65
71 if ( 66 if (
72 self.__inComment(line, col) or 67 self.__inComment(line, col)
73 self.__inDoubleQuotedString() or 68 or self.__inDoubleQuotedString()
74 self.__inSingleQuotedString() or 69 or self.__inSingleQuotedString()
75 self.__inHereDocument() or 70 or self.__inHereDocument()
76 self.__inInlineDocument() 71 or self.__inInlineDocument()
77 ): 72 ):
78 return 73 return
79 74
80 # open parenthesis 75 # open parenthesis
81 # insert closing parenthesis and self 76 # insert closing parenthesis and self
82 if char == '(': 77 if char == "(":
83 txt = self.editor.text(line)[:col] 78 txt = self.editor.text(line)[:col]
84 if self.__insertClosingBrace: 79 if self.__insertClosingBrace:
85 self.editor.insert(')') 80 self.editor.insert(")")
86 81
87 # closing parenthesis 82 # closing parenthesis
88 # skip matching closing parenthesis 83 # skip matching closing parenthesis
89 elif char in [')', '}', ']']: 84 elif char in [")", "}", "]"]:
90 txt = self.editor.text(line) 85 txt = self.editor.text(line)
91 if col < len(txt) and char == txt[col] and self.__skipBrace: 86 if col < len(txt) and char == txt[col] and self.__skipBrace:
92 self.editor.setSelection(line, col, line, col + 1) 87 self.editor.setSelection(line, col, line, col + 1)
93 self.editor.removeSelectedText() 88 self.editor.removeSelectedText()
94 89
95 # space 90 # space
96 # complete inline documentation 91 # complete inline documentation
97 elif char == ' ': 92 elif char == " ":
98 txt = self.editor.text(line)[:col] 93 txt = self.editor.text(line)[:col]
99 if self.__insertInlineDoc and self.__beginRX.fullmatch(txt): 94 if self.__insertInlineDoc and self.__beginRX.fullmatch(txt):
100 self.editor.insert('=end') 95 self.editor.insert("=end")
101 96
102 # comma 97 # comma
103 # insert blank 98 # insert blank
104 elif char == ',' and self.__insertBlank: 99 elif char == "," and self.__insertBlank:
105 self.editor.insert(' ') 100 self.editor.insert(" ")
106 self.editor.setCursorPosition(line, col + 1) 101 self.editor.setCursorPosition(line, col + 1)
107 102
108 # open curly brace 103 # open curly brace
109 # insert closing brace 104 # insert closing brace
110 elif char == '{' and self.__insertClosingBrace: 105 elif char == "{" and self.__insertClosingBrace:
111 self.editor.insert('}') 106 self.editor.insert("}")
112 107
113 # open bracket 108 # open bracket
114 # insert closing bracket 109 # insert closing bracket
115 elif char == '[' and self.__insertClosingBrace: 110 elif char == "[" and self.__insertClosingBrace:
116 self.editor.insert(']') 111 self.editor.insert("]")
117 112
118 # double quote 113 # double quote
119 # insert double quote 114 # insert double quote
120 elif char == '"' and self.__insertQuote: 115 elif char == '"' and self.__insertQuote:
121 self.editor.insert('"') 116 self.editor.insert('"')
122 117
123 # quote 118 # quote
124 # insert quote 119 # insert quote
125 elif char == '\'' and self.__insertQuote: 120 elif char == "'" and self.__insertQuote:
126 self.editor.insert('\'') 121 self.editor.insert("'")
127 122
128 # new line 123 # new line
129 # indent to opening brace, complete inline documentation 124 # indent to opening brace, complete inline documentation
130 elif char == '\n': 125 elif char == "\n":
131 txt = self.editor.text(line - 1) 126 txt = self.editor.text(line - 1)
132 if self.__insertInlineDoc and self.__beginNlRX.fullmatch(txt): 127 if self.__insertInlineDoc and self.__beginNlRX.fullmatch(txt):
133 self.editor.insert('=end') 128 self.editor.insert("=end")
134 elif self.__insertHereDoc and self.__hereRX.fullmatch(txt): 129 elif self.__insertHereDoc and self.__hereRX.fullmatch(txt):
135 self.editor.insert(self.__hereRX.fullmatch(txt).group(1)) 130 self.editor.insert(self.__hereRX.fullmatch(txt).group(1))
136 elif self.__indentBrace and re.search(":\r?\n", txt) is None: 131 elif self.__indentBrace and re.search(":\r?\n", txt) is None:
137 stxt = txt.strip() 132 stxt = txt.strip()
138 if stxt and stxt[-1] in ("(", "[", "{"): 133 if stxt and stxt[-1] in ("(", "[", "{"):
153 txt2 = txt[lastOpenIndex:] 148 txt2 = txt[lastOpenIndex:]
154 openCount = len(re.findall("[({[]", txt2)) 149 openCount = len(re.findall("[({[]", txt2))
155 closeCount = len(re.findall(r"[)}\]]", txt2)) 150 closeCount = len(re.findall(r"[)}\]]", txt2))
156 index -= 1 151 index -= 1
157 if openCount > closeCount and lastOpenIndex > col: 152 if openCount > closeCount and lastOpenIndex > col:
158 self.editor.insert(' ' * (lastOpenIndex - col + 1)) 153 self.editor.insert(" " * (lastOpenIndex - col + 1))
159 self.editor.setCursorPosition(line, 154 self.editor.setCursorPosition(line, lastOpenIndex + 1)
160 lastOpenIndex + 1) 155
161
162 def __inComment(self, line, col): 156 def __inComment(self, line, col):
163 """ 157 """
164 Private method to check, if the cursor is inside a comment. 158 Private method to check, if the cursor is inside a comment.
165 159
166 @param line current line (integer) 160 @param line current line (integer)
167 @param col current position within line (integer) 161 @param col current position within line (integer)
168 @return flag indicating, if the cursor is inside a comment (boolean) 162 @return flag indicating, if the cursor is inside a comment (boolean)
169 """ 163 """
170 txt = self.editor.text(line) 164 txt = self.editor.text(line)
173 while col >= 0: 167 while col >= 0:
174 if txt[col] == "#": 168 if txt[col] == "#":
175 return True 169 return True
176 col -= 1 170 col -= 1
177 return False 171 return False
178 172
179 def __inDoubleQuotedString(self): 173 def __inDoubleQuotedString(self):
180 """ 174 """
181 Private method to check, if the cursor is within a double quoted 175 Private method to check, if the cursor is within a double quoted
182 string. 176 string.
183 177
184 @return flag indicating, if the cursor is inside a double 178 @return flag indicating, if the cursor is inside a double
185 quoted string (boolean) 179 quoted string (boolean)
186 """ 180 """
187 return self.editor.currentStyle() == QsciLexerRuby.DoubleQuotedString 181 return self.editor.currentStyle() == QsciLexerRuby.DoubleQuotedString
188 182
189 def __inSingleQuotedString(self): 183 def __inSingleQuotedString(self):
190 """ 184 """
191 Private method to check, if the cursor is within a single quoted 185 Private method to check, if the cursor is within a single quoted
192 string. 186 string.
193 187
194 @return flag indicating, if the cursor is inside a single 188 @return flag indicating, if the cursor is inside a single
195 quoted string (boolean) 189 quoted string (boolean)
196 """ 190 """
197 return self.editor.currentStyle() == QsciLexerRuby.SingleQuotedString 191 return self.editor.currentStyle() == QsciLexerRuby.SingleQuotedString
198 192
199 def __inHereDocument(self): 193 def __inHereDocument(self):
200 """ 194 """
201 Private method to check, if the cursor is within a here document. 195 Private method to check, if the cursor is within a here document.
202 196
203 @return flag indicating, if the cursor is inside a here document 197 @return flag indicating, if the cursor is inside a here document
204 (boolean) 198 (boolean)
205 """ 199 """
206 return self.editor.currentStyle() == QsciLexerRuby.HereDocument 200 return self.editor.currentStyle() == QsciLexerRuby.HereDocument
207 201
208 def __inInlineDocument(self): 202 def __inInlineDocument(self):
209 """ 203 """
210 Private method to check, if the cursor is within an inline document. 204 Private method to check, if the cursor is within an inline document.
211 205
212 @return flag indicating, if the cursor is inside an inline document 206 @return flag indicating, if the cursor is inside an inline document
213 (boolean) 207 (boolean)
214 """ 208 """
215 return self.editor.currentStyle() == QsciLexerRuby.POD 209 return self.editor.currentStyle() == QsciLexerRuby.POD

eric ide

mercurial