| Conditions | 10 |
| Total Lines | 96 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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.""" |
||
| 137 | def on_query_completions(self, view, prefix, locations): |
||
| 138 | """"Execute if a auto completion is requested. |
||
| 139 | |||
| 140 | can be either via typing or manual invoked with ctrl+space. |
||
| 141 | |||
| 142 | This provides general variable extractions which are then |
||
| 143 | passed to the domain specific worker completions. |
||
| 144 | """ |
||
| 145 | for location in locations: |
||
| 146 | # ignore non scope |
||
| 147 | if not view.match_selector(location, self.scope): |
||
| 148 | return None |
||
| 149 | |||
| 150 | # ignore on comment lines |
||
| 151 | cursor_line = view.line(location) |
||
| 152 | line_content = view.substr(cursor_line) |
||
| 153 | if self.comment_exp.search(line_content): |
||
| 154 | logger.info(__file__, "on_query_completions", "found comment") |
||
| 155 | return None |
||
| 156 | |||
| 157 | # find last occurance of the [] to determine the ini sections |
||
| 158 | lines = self.get_lines_of_section_on_cursor(view, location) |
||
| 159 | # filter empty lines |
||
| 160 | lines = list(filter(None, lines)) |
||
| 161 | # filter comments |
||
| 162 | lines = list(filter(lambda l: not self.comment_exp.search(l), lines)) |
||
| 163 | |||
| 164 | if not lines: |
||
| 165 | logger.info(__file__, "bootstrap.on_query_completions", "section is empty") |
||
| 166 | size = view.size() |
||
| 167 | content = view.substr(sublime.Region(0, size)) |
||
| 168 | sections = self.bracket_expression.findall(content) |
||
| 169 | |||
| 170 | return self.section.get_key_context_completion(prefix, line_content, sections) |
||
| 171 | |||
| 172 | # extract section |
||
| 173 | first_line = lines[0] |
||
| 174 | match = self.section_expression.search(first_line) |
||
| 175 | |||
| 176 | # no section defined |
||
| 177 | # TODO section suggestion |
||
| 178 | # if not match: |
||
| 179 | # logger.info(__file__, "on_query_completions", "no section found") |
||
| 180 | # size = view.size() |
||
| 181 | # content = view.substr(sublime.Region(0, size)) |
||
| 182 | # sections = self.bracket_expression.findall(content) |
||
| 183 | |||
| 184 | # return self.section.get_key_context_completion(prefix, line_content, sections) |
||
| 185 | section = match.group(1) |
||
| 186 | |||
| 187 | key_match, value_match = self.get_key_value(line_content) |
||
| 188 | key_values = self.get_key_values(lines) |
||
| 189 | |||
| 190 | if value_match == "": |
||
| 191 | logger.info( |
||
| 192 | __file__, |
||
| 193 | "on_query_completions", |
||
| 194 | "after equal trigger in '" + line_content + "'" |
||
| 195 | ) |
||
| 196 | # value trigger |
||
| 197 | value_result = self.skin_rainmeter_section.get_value_context_completion( |
||
| 198 | section, |
||
| 199 | key_match |
||
| 200 | ) |
||
| 201 | |||
| 202 | if value_result: |
||
| 203 | return value_result |
||
| 204 | |||
| 205 | # only do key completion if we are in the key are |
||
| 206 | # that means in front of the equal or no equal at all |
||
| 207 | else: |
||
| 208 | logger.info( |
||
| 209 | __file__, |
||
| 210 | "on_query_completions", |
||
| 211 | "before equal trigger in '" + line_content + "'" |
||
| 212 | ) |
||
| 213 | key_result = self.skin_rainmeter_section.get_key_context_completion( |
||
| 214 | prefix, |
||
| 215 | line_content, |
||
| 216 | section, |
||
| 217 | key_values |
||
| 218 | ) |
||
| 219 | |||
| 220 | if key_result: |
||
| 221 | return key_result |
||
| 222 | |||
| 223 | key_result = self.skin_metadata_section.get_key_context_completion( |
||
| 224 | prefix, |
||
| 225 | line_content, |
||
| 226 | section, |
||
| 227 | key_values |
||
| 228 | ) |
||
| 229 | if key_result: |
||
| 230 | return key_result |
||
| 231 | |||
| 232 | return None |
||
| 233 |