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