diff --git a/linter.py b/linter.py index 4c2af06..841dda1 100644 --- a/linter.py +++ b/linter.py @@ -1,10 +1,51 @@ import logging import re +import os +from itertools import chain, takewhile from SublimeLinter.lint import PythonLinter logger = logging.getLogger('SublimeLinter.plugins.pylint') +HOME = os.path.expanduser('~') + + +def paths_upwards(path): + while True: + yield path + + next_path = os.path.dirname(path) + # Stop just before root in *nix systems + if next_path == '/': + return + + if next_path == path: + return + + path = next_path + + +def paths_upwards_until_home(path): + return chain(takewhile(lambda p: p != HOME, paths_upwards(path)), [HOME]) + + +def find_project_root(src): + """Attempt to get the project root.""" + for src in paths_upwards_until_home(src): + if os.path.exists(os.path.join(src, ".pylintrc")): + return src + + if os.path.exists(os.path.join(src, "pyproject.toml")): + return src + + if os.path.exists(os.path.join(src, ".git")): + return src + + if os.path.exists(os.path.join(src, ".hg")): + return src + + return src + class Pylint(PythonLinter): regex = ( @@ -44,6 +85,8 @@ def cmd(self): ] settings['init-hook'] = commands + self.context['project_root'] = find_project_root(self.view.file_name()) + return ( 'pylint', '--msg-template=\'{line}:{column}:{msg_id}: {msg} ({symbol})\'',