Conditions | 11 |
Total Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Tests | 1 |
CRAP Score | 118.5556 |
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 start_host() 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 client for Nvim. |
||
27 | 6 | def start_host(session=None): |
|
28 | """Promote the current process into python plugin host for Nvim. |
||
29 | |||
30 | Start msgpack-rpc event loop for `session`, listening for Nvim requests |
||
31 | and notifications. It registers Nvim commands for loading/unloading |
||
32 | python plugins. |
||
33 | |||
34 | The sys.stdout and sys.stderr streams are redirected to Nvim through |
||
35 | `session`. That means print statements probably won't work as expected |
||
36 | while this function doesn't return. |
||
37 | |||
38 | This function is normally called at program startup and could have been |
||
39 | defined as a separate executable. It is exposed as a library function for |
||
40 | testing purposes only. |
||
41 | """ |
||
42 | plugins = [] |
||
43 | for arg in sys.argv: |
||
44 | _, ext = os.path.splitext(arg) |
||
45 | if ext == '.py': |
||
46 | plugins.append(arg) |
||
47 | elif os.path.isdir(arg): |
||
48 | init = os.path.join(arg, '__init__.py') |
||
49 | if os.path.isfile(init): |
||
50 | plugins.append(arg) |
||
51 | |||
52 | # This is a special case to support the old workaround of |
||
53 | # adding an empty .py file to make a package directory |
||
54 | # visible, and it should be removed soon. |
||
55 | for path in list(plugins): |
||
56 | dup = path + ".py" |
||
57 | if os.path.isdir(path) and dup in plugins: |
||
58 | plugins.remove(dup) |
||
59 | |||
60 | # Special case: the legacy scripthost receives a single relative filename |
||
61 | # while the rplugin host will receive absolute paths. |
||
62 | if plugins == ["script_host.py"]: |
||
63 | name = "script" |
||
64 | else: |
||
65 | name = "rplugin" |
||
66 | |||
67 | setup_logging(name) |
||
68 | |||
69 | if not session: |
||
70 | session = stdio_session() |
||
71 | nvim = Nvim.from_session(session) |
||
72 | |||
73 | if nvim.version.api_level < 1: |
||
74 | sys.stderr.write("This version of the neovim python package " |
||
75 | "requires nvim 0.1.6 or later") |
||
76 | sys.exit(1) |
||
77 | |||
78 | host = Host(nvim) |
||
79 | host.start(plugins) |
||
80 | |||
145 |