| Conditions | 10 |
| Total Lines | 66 |
| 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 | # python libs |
||
| 121 | def on_query_completions(self, view, prefix, locations): |
||
| 122 | for location in locations: |
||
| 123 | # ignore non scope |
||
| 124 | if not view.match_selector(location, self.scope): |
||
| 125 | return None |
||
| 126 | |||
| 127 | # ignore on comment lines |
||
| 128 | cursor_line = view.line(location) |
||
| 129 | line_content = view.substr(cursor_line) |
||
| 130 | if self.comment_exp.search(line_content): |
||
| 131 | logger.info(__file__, "on_query_completions", "found comment") |
||
| 132 | return None |
||
| 133 | |||
| 134 | # find last occurance of the [] to determine the ini sections |
||
| 135 | lines = self.get_lines_of_section_on_cursor(view, location) |
||
| 136 | # filter empty lines |
||
| 137 | lines = list(filter(None, lines)) |
||
| 138 | # filter comments |
||
| 139 | lines = list(filter(lambda l: not self.comment_exp.search(l), lines)) |
||
| 140 | |||
| 141 | if not lines: |
||
| 142 | logger.info(__file__, "bootstrap.on_query_completions", "section is empty") |
||
| 143 | size = view.size() |
||
| 144 | content = view.substr(sublime.Region(0, size)) |
||
| 145 | sections = self.bracket_expression.findall(content) |
||
| 146 | |||
| 147 | return self.section.get_key_context_completion(prefix, line_content, sections) |
||
| 148 | |||
| 149 | # extract section |
||
| 150 | first_line = lines[0] |
||
| 151 | match = self.section_expression.search(first_line) |
||
| 152 | |||
| 153 | # no section defined |
||
| 154 | # TODO section suggestion |
||
| 155 | # if not match: |
||
| 156 | # logger.info(__file__, "on_query_completions", "no section found") |
||
| 157 | # size = view.size() |
||
| 158 | # content = view.substr(sublime.Region(0, size)) |
||
| 159 | # sections = self.bracket_expression.findall(content) |
||
| 160 | |||
| 161 | # return self.section.get_key_context_completion(prefix, line_content, sections) |
||
| 162 | section = match.group(1) |
||
| 163 | |||
| 164 | key_match, value_match = self.get_key_value(line_content) |
||
| 165 | key_values = self.get_key_values(lines) |
||
| 166 | |||
| 167 | if value_match == "": |
||
| 168 | logger.info(__file__, "on_query_completions", "after equal trigger in '" + line_content + "'") |
||
| 169 | # value trigger |
||
| 170 | value_result = self.skin_rainmeter_section.get_value_context_completion(view, prefix, location, line_content, section, key_match, key_values) |
||
| 171 | if value_result: |
||
| 172 | return value_result |
||
| 173 | |||
| 174 | # only do key completion if we are in the key are |
||
| 175 | # that means in front of the equal or no equal at all |
||
| 176 | else: |
||
| 177 | logger.info(__file__, "on_query_completions", "before equal trigger in '" + line_content + "'") |
||
| 178 | key_result = self.skin_rainmeter_section.get_key_context_completion(view, prefix, location, line_content, section, key_values) |
||
| 179 | if key_result: |
||
| 180 | return key_result |
||
| 181 | |||
| 182 | key_result = self.skin_metadata_section.get_key_context_completion(prefix, line_content, section, key_values) |
||
| 183 | if key_result: |
||
| 184 | return key_result |
||
| 185 | |||
| 186 | return None |
||
| 187 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.