18 |
18 |
19 class CompleterYaml(CompleterBase): |
19 class CompleterYaml(CompleterBase): |
20 """ |
20 """ |
21 Class implementing typing completer for Python. |
21 Class implementing typing completer for Python. |
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 |
28 @param editor reference to the editor object |
28 @type QScintilla.Editor |
29 @type QScintilla.Editor |
29 @param parent reference to the parent object |
30 @param parent reference to the parent object |
30 @type QObject |
31 @type QObject |
31 """ |
32 """ |
32 super().__init__(editor, parent) |
33 super().__init__(editor, parent) |
33 |
34 |
34 self.readSettings() |
35 self.readSettings() |
35 |
36 |
36 def readSettings(self): |
37 def readSettings(self): |
37 """ |
38 """ |
38 Public slot called to reread the configuration parameters. |
39 Public slot called to reread the configuration parameters. |
39 """ |
40 """ |
40 self.setEnabled( |
41 self.setEnabled(Preferences.getEditorTyping("Yaml/EnabledTypingAids")) |
41 Preferences.getEditorTyping("Yaml/EnabledTypingAids")) |
|
42 self.__insertClosingBrace = Preferences.getEditorTyping( |
42 self.__insertClosingBrace = Preferences.getEditorTyping( |
43 "Yaml/InsertClosingBrace") |
43 "Yaml/InsertClosingBrace" |
44 self.__skipBrace = Preferences.getEditorTyping( |
44 ) |
45 "Yaml/SkipBrace") |
45 self.__skipBrace = Preferences.getEditorTyping("Yaml/SkipBrace") |
46 self.__insertQuote = Preferences.getEditorTyping( |
46 self.__insertQuote = Preferences.getEditorTyping("Yaml/InsertQuote") |
47 "Yaml/InsertQuote") |
47 self.__autoIndentation = Preferences.getEditorTyping("Yaml/AutoIndentation") |
48 self.__autoIndentation = Preferences.getEditorTyping( |
48 self.__colonDetection = Preferences.getEditorTyping("Yaml/ColonDetection") |
49 "Yaml/AutoIndentation") |
49 self.__insertBlankDash = Preferences.getEditorTyping("Yaml/InsertBlankDash") |
50 self.__colonDetection = Preferences.getEditorTyping( |
50 self.__insertBlankColon = Preferences.getEditorTyping("Yaml/InsertBlankColon") |
51 "Yaml/ColonDetection") |
|
52 self.__insertBlankDash = Preferences.getEditorTyping( |
|
53 "Yaml/InsertBlankDash") |
|
54 self.__insertBlankColon = Preferences.getEditorTyping( |
|
55 "Yaml/InsertBlankColon") |
|
56 self.__insertBlankQuestion = Preferences.getEditorTyping( |
51 self.__insertBlankQuestion = Preferences.getEditorTyping( |
57 "Yaml/InsertBlankQuestion") |
52 "Yaml/InsertBlankQuestion" |
58 self.__insertBlankComma = Preferences.getEditorTyping( |
53 ) |
59 "Yaml/InsertBlankComma") |
54 self.__insertBlankComma = Preferences.getEditorTyping("Yaml/InsertBlankComma") |
60 |
55 |
61 def charAdded(self, charNumber): |
56 def charAdded(self, charNumber): |
62 """ |
57 """ |
63 Public slot called to handle the user entering a character. |
58 Public slot called to handle the user entering a character. |
64 |
59 |
65 @param charNumber value of the character entered |
60 @param charNumber value of the character entered |
66 @type int |
61 @type int |
67 """ |
62 """ |
68 char = chr(charNumber) |
63 char = chr(charNumber) |
69 if char not in ['{', '}', '[', ']', "'", '"', '-', ':', '?', ',', |
64 if char not in ["{", "}", "[", "]", "'", '"', "-", ":", "?", ",", "\n"]: |
70 '\n']: |
|
71 return # take the short route |
65 return # take the short route |
72 |
66 |
73 line, col = self.editor.getCursorPosition() |
67 line, col = self.editor.getCursorPosition() |
74 |
68 |
75 if self.__inComment(line, col): |
69 if self.__inComment(line, col): |
76 return |
70 return |
77 |
71 |
78 # open curly brace |
72 # open curly brace |
79 # insert closing brace |
73 # insert closing brace |
80 if char == '{' and self.__insertClosingBrace: |
74 if char == "{" and self.__insertClosingBrace: |
81 self.editor.insert('}') |
75 self.editor.insert("}") |
82 |
76 |
83 # open bracket |
77 # open bracket |
84 # insert closing bracket |
78 # insert closing bracket |
85 elif char == '[' and self.__insertClosingBrace: |
79 elif char == "[" and self.__insertClosingBrace: |
86 self.editor.insert(']') |
80 self.editor.insert("]") |
87 |
81 |
88 # closing parenthesis |
82 # closing parenthesis |
89 # skip matching closing parenthesis |
83 # skip matching closing parenthesis |
90 elif char in ['}', ']']: |
84 elif char in ["}", "]"]: |
91 txt = self.editor.text(line) |
85 txt = self.editor.text(line) |
92 if col < len(txt) and char == txt[col] and self.__skipBrace: |
86 if col < len(txt) and char == txt[col] and self.__skipBrace: |
93 self.editor.setSelection(line, col, line, col + 1) |
87 self.editor.setSelection(line, col, line, col + 1) |
94 self.editor.removeSelectedText() |
88 self.editor.removeSelectedText() |
95 |
89 |
96 # colon |
90 # colon |
97 # 1. skip colon if not last character |
91 # 1. skip colon if not last character |
98 # 2. insert blank if last character |
92 # 2. insert blank if last character |
99 elif char == ':': |
93 elif char == ":": |
100 text = self.editor.text(line) |
94 text = self.editor.text(line) |
101 if col < len(text) and char == text[col]: |
95 if col < len(text) and char == text[col]: |
102 if self.__colonDetection: |
96 if self.__colonDetection: |
103 self.editor.setSelection(line, col, line, col + 1) |
97 self.editor.setSelection(line, col, line, col + 1) |
104 self.editor.removeSelectedText() |
98 self.editor.removeSelectedText() |
105 elif self.__insertBlankColon and col == len(text.rstrip()): |
99 elif self.__insertBlankColon and col == len(text.rstrip()): |
106 self.editor.insert(' ') |
100 self.editor.insert(" ") |
107 self.editor.setCursorPosition(line, col + 1) |
101 self.editor.setCursorPosition(line, col + 1) |
108 |
102 |
109 # dash, question mark or comma |
103 # dash, question mark or comma |
110 # insert blank |
104 # insert blank |
111 elif ( |
105 elif ( |
112 (char == '-' and self.__insertBlankDash) or |
106 (char == "-" and self.__insertBlankDash) |
113 (char == '?' and self.__insertBlankQuestion) or |
107 or (char == "?" and self.__insertBlankQuestion) |
114 (char == ',' and self.__insertBlankComma) |
108 or (char == "," and self.__insertBlankComma) |
115 ): |
109 ): |
116 self.editor.insert(' ') |
110 self.editor.insert(" ") |
117 self.editor.setCursorPosition(line, col + 1) |
111 self.editor.setCursorPosition(line, col + 1) |
118 |
112 |
119 # double quote |
113 # double quote |
120 # insert double quote |
114 # insert double quote |
121 elif char == '"' and self.__insertQuote: |
115 elif char == '"' and self.__insertQuote: |
122 self.editor.insert('"') |
116 self.editor.insert('"') |
123 |
117 |
124 # quote |
118 # quote |
125 # insert quote |
119 # insert quote |
126 elif char == "'" and self.__insertQuote: |
120 elif char == "'" and self.__insertQuote: |
127 self.editor.insert("'") |
121 self.editor.insert("'") |
128 |
122 |
129 # new line |
123 # new line |
130 # indent after line ending with ':' |
124 # indent after line ending with ':' |
131 elif char == '\n' and self.__autoIndentation: |
125 elif char == "\n" and self.__autoIndentation: |
132 txt = self.editor.text(line - 1) |
126 txt = self.editor.text(line - 1) |
133 match = re.search( |
127 match = re.search( |
134 "(?:\||\|-|\|\+|>|>-|>\+|-|:)(\s*)\r?\n", |
128 "(?:\||\|-|\|\+|>|>-|>\+|-|:)(\s*)\r?\n", |
135 # __IGNORE_WARNING_W605__ |
129 # __IGNORE_WARNING_W605__ |
136 txt) |
130 txt, |
|
131 ) |
137 if match is not None: |
132 if match is not None: |
138 startBlanks = match.start(1) |
133 startBlanks = match.start(1) |
139 endBlanks = match.end(1) |
134 endBlanks = match.end(1) |
140 if startBlanks != -1 and startBlanks != endBlanks: |
135 if startBlanks != -1 and startBlanks != endBlanks: |
141 # previous line ends with whitespace, e.g. caused by |
136 # previous line ends with whitespace, e.g. caused by |
142 # blank insertion above |
137 # blank insertion above |
143 self.editor.setSelection(line - 1, startBlanks, |
138 self.editor.setSelection(line - 1, startBlanks, line - 1, endBlanks) |
144 line - 1, endBlanks) |
|
145 self.editor.removeSelectedText() |
139 self.editor.removeSelectedText() |
146 |
140 |
147 self.editor.indent(line) |
141 self.editor.indent(line) |
148 self.editor.setCursorPosition(line, 0) |
142 self.editor.setCursorPosition(line, 0) |
149 self.editor.editorCommand(QsciScintilla.SCI_VCHOME) |
143 self.editor.editorCommand(QsciScintilla.SCI_VCHOME) |
150 |
144 |
151 def __inComment(self, line, col): |
145 def __inComment(self, line, col): |
152 """ |
146 """ |
153 Private method to check, if the cursor is inside a comment. |
147 Private method to check, if the cursor is inside a comment. |
154 |
148 |
155 @param line current line |
149 @param line current line |
156 @type int |
150 @type int |
157 @param col current position within line |
151 @param col current position within line |
158 @type int |
152 @type int |
159 @return flag indicating, if the cursor is inside a comment |
153 @return flag indicating, if the cursor is inside a comment |