Conditions | 12 |
Total Lines | 64 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like ContextSensAutoCompletion.on_query_completions() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """This module handles the whole auto smart completion sublime-rainmeter has to offer.""" |
||
159 | def on_query_completions(self, view, prefix, locations): |
||
160 | """ |
||
161 | Execute if a auto completion is requested. |
||
162 | |||
163 | can be either via typing or manual invoked with ctrl+space. |
||
164 | |||
165 | This provides general variable extractions which are then |
||
166 | passed to the domain specific worker completions. |
||
167 | """ |
||
168 | for location in locations: |
||
169 | # ignore non scope |
||
170 | if not view.match_selector(location, self.scope): |
||
171 | return None |
||
172 | |||
173 | # ignore on comment lines |
||
174 | cursor_line = view.line(location) |
||
175 | line_content = view.substr(cursor_line) |
||
176 | if self.comment_exp.search(line_content): |
||
177 | logger.info("found comment") |
||
178 | return None |
||
179 | |||
180 | # find last occurance of the [] to determine the ini sections |
||
181 | section_start_index, section_lines = self.get_lines_of_section_on_cursor(view, location) |
||
182 | |||
183 | # determine auto completions of sections on double newline |
||
184 | line_start_coord = cursor_line.begin() |
||
185 | row, _ = view.rowcol(line_start_coord) |
||
186 | section_start = view.rowcol(section_start_index) |
||
187 | section_start_row, _ = section_start |
||
188 | |||
189 | if row > 2: |
||
190 | line_above_empty = section_lines[row - section_start_row - 1] == '' |
||
191 | line_aabove_empty = section_lines[row - section_start_row - 2] == '' |
||
192 | |||
193 | if line_above_empty and line_aabove_empty: |
||
194 | size = view.size() |
||
195 | content = view.substr(sublime.Region(0, size)) |
||
196 | sections = self.bracket_expression.findall(content) |
||
197 | |||
198 | return self.section.get_key_context_completion(prefix, line_content, sections) |
||
199 | |||
200 | # filter empty lines |
||
201 | section_lines = [section_line for section_line in section_lines if section_line] |
||
202 | # filter comments |
||
203 | section_lines = [line for line in section_lines if not self.comment_exp.search(line)] |
||
204 | |||
205 | if not section_lines: |
||
206 | logger.info("section is empty") |
||
207 | size = view.size() |
||
208 | content = view.substr(sublime.Region(0, size)) |
||
209 | sections = self.bracket_expression.findall(content) |
||
210 | |||
211 | return self.section.get_key_context_completion(prefix, line_content, sections) |
||
212 | |||
213 | # extract section |
||
214 | first_line = section_lines[0] |
||
215 | match = self.section_expression.search(first_line) |
||
216 | |||
217 | section = match.group(1) |
||
218 | key_values = self.get_key_values(section_lines) |
||
219 | |||
220 | return self.__autocomplete_value(line_content, section) or \ |
||
221 | self.__autocomplete_key(line_content, prefix, section, key_values) or \ |
||
222 | self.__autocomplete_default() |
||
223 |