diff --git a/README.md b/README.md index fa9b447..b10fa21 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,18 @@ SublimeLinter-contrib-codeclimate -================================ +================================= [![Build Status](https://travis-ci.org/codeclimate/SublimeLinter-contrib-codeclimate.svg?branch=master)](https://travis-ci.org/codeclimate/SublimeLinter-contrib-codeclimate) This linter plugin for [SublimeLinter][docs] provides an interface to [codeclimate](https://github.com/codeclimate/codeclimate). Code Climate supports a variety of languages through standardized Docker images known as static analysis engines. ## Installation -SublimeLinter 3 must be installed in order to use this plugin. If SublimeLinter 3 is not installed, please follow the instructions [here][installation]. +SublimeLinter 4 must be installed in order to use this plugin. If SublimeLinter 4 is not installed, please follow the instructions [here][installation]. ### Linter installation Before using this plugin, you must ensure that `codeclimate` is installed on your system. Please see the `codeclimate` documentation, specifically [Prerequisites](https://github.com/codeclimate/codeclimate#prerequisites) and [Installation](https://github.com/codeclimate/codeclimate#installation) ### Linter configuration -In order for `codeclimate` to be executed by SublimeLinter, you must ensure that its path is available to SublimeLinter. Before going any further, please read and follow the steps in [“Finding a linter executable”](http://sublimelinter.readthedocs.org/en/latest/troubleshooting.html#finding-a-linter-executable) through “Validating your PATH” in the documentation. - -__This plugin requires usage within a Sublime project at this time.__ +In order for `codeclimate` to be executed by SublimeLinter, you must ensure that its path is available to SublimeLinter. Before going any further, please read and follow the steps in ["Finding a linter executable"](http://sublimelinter.readthedocs.org/en/latest/troubleshooting.html#finding-a-linter-executable) through "Validating your PATH" in the documentation. Once you have installed and configured `codeclimate`, you can proceed to install the SublimeLinter-contrib-codeclimate plugin if it is not yet installed. @@ -23,21 +21,72 @@ Please use [Package Control][pc] to install the linter plugin. This will ensure To install via Package Control, do the following: -1. Within Sublime Text, bring up the [Command Palette][cmd] and type `install`. Among the commands you should see `Package Control: Install Package`. If that command is not highlighted, use the keyboard or mouse to select it. There will be a pause of a few seconds while Package Control fetches the list of available plugins. +Within Sublime Text, bring up the [Command Palette][cmd] and type `install`. Among the commands you should see `Package Control: Install Package`. If that command is not highlighted, use the keyboard or mouse to select it. There will be a pause of a few seconds while Package Control fetches the list of available plugins. + +When the plugin list appears, type `codeclimate`. Among the entries you should see `SublimeLinter-contrib-codeclimate`. If that entry is not highlighted, use the keyboard or mouse to select it. -1. When the plugin list appears, type `codeclimate`. Among the entries you should see `SublimeLinter-contrib-codeclimate`. If that entry is not highlighted, use the keyboard or mouse to select it. +## How it works +If the opened folder in SublimeText contains a `.codeclimate.yml` configuration file in its root, `codeclimate` will recognize this file's settings. + +Suppose the `codeclimate` CLI finds no configuration file in the folder root: In that case, it will automatically run the default inspections of `structure` and `duplication`. If you have a `.codeclimate.yml` configuration file in a different folder, you can set SublimeLinter's `working_dir` setting (please see examples). ## Settings For general information on how SublimeLinter works with settings, please see [Settings][settings]. For information on generic linter settings, please see [Linter Settings][linter-settings]. +### Examples +You can set the path to the `codeclimate` executable in the global SublimeLinter settings or your project settings: + +```json +{ + "linters": { + "codeclimate": { + "executable": "/usr/local/bin/codeclimate" + } + } +} +``` + +If you want to ignore the configuration of a `.codeclimate.yml`, for instance, to run a subset of codeclimate engines, you can set linter arguments in the global SublimeLinter settings or your project settings: + +```json +{ + "linters": { + "codeclimate": { + "args": [ + "-e", + "structure", + "-e", + "duplication" + ] + } + } +} +``` + +Suppose you use a `.codeclimate.yml`-configuration file. In that case, the `codeclimate` CLI needs to be executed in your configuration file's directory. Otherwise, it can't detect your configuration and runs only the default analyzes. + +SublimeLinter takes the current file's root folder in SublimeText's sidebar as the working directory for executing its linter commands. You can change this behavior by setting the working directory of execution: + +```json +{ + "linters": { + "codeclimate": { + "working_dir": "/path/to/working/dir" + } + } +} +``` + +*Hint: Make sure the working directory is in the path of the file you want to lint!* + ## Contributing If you would like to contribute enhancements or fixes, please do the following: 1. Fork the plugin repository. -1. Hack on a separate topic branch created from the latest `master`. -1. Commit and push the topic branch. -1. Make a pull request. -1. Be patient. ;-) +2. Hack on a separate topic branch created from the latest `master`. +3. Commit and push the topic branch. +4. Make a pull request. +5. Be patient. Please note that modifications should follow these coding guidelines: diff --git a/linter.py b/linter.py index 7276298..ab8a160 100644 --- a/linter.py +++ b/linter.py @@ -1,28 +1,31 @@ # # linter.py -# Linter for SublimeLinter3, a code checking framework for Sublime Text 3 +# Linter for SublimeLinter4, a code checking framework for Sublime Text 3 # # Written by Noah Davis # Copyright (c) 2016 Noah Davis # +# Updated by Schuyler Jager & Andreas for SublimeLinter 4 +# Copyright (c) 2020 Schuyler Jager & Andreas # License: MIT # """This module exports the Codeclimate plugin class.""" +import logging +import os +from SublimeLinter.lint import Linter, PermanentError, util -from SublimeLinter.lint import Linter, util, persist + +logger = logging.getLogger('SublimeLinter.plugin.codeclimate') class Codeclimate(Linter): """Provides an interface to codeclimate.""" - defaults = { - 'chdir': "${project}", - 'executable': 'codeclimate', 'selector': ( 'source.css, ' 'source.go, ' - 'source.haskall, ' + 'source.haskell, ' 'source.java, ' 'source.javascript, ' 'source.js, ' @@ -34,6 +37,7 @@ class Codeclimate(Linter): 'text.html' ) } + regex = r'^(?P\d+)(?:-\d+)?:\s(?P.+)$' multiline = False line_col_base = (1, 1) @@ -42,14 +46,18 @@ class Codeclimate(Linter): word_re = None def cmd(self): - """Construct a cmd to provide a relative path to 'codeclimate analyze'.""" - result = ['codeclimate', 'analyze', '-e', 'structure', '-e', 'duplication'] - relative_file_name = None - for folder in self.view.window().folders(): - if self.filename.find(folder) == 0: - relative_file_name = self.filename.replace(folder + '/', '') - if relative_file_name == None: - return result - result.append(relative_file_name) - persist.debug(result) - return result + """Set command and run 'codeclimate analyze'.""" + abs_path = self.filename + working_dir = self.get_working_dir() + + msg = 'The file \'%s\' is not part of any open folder in ' \ + 'SublimeText. Please see the Linter\'s README.md to ' \ + 'get further instructions.' % (abs_path) + + if not (abs_path.startswith(os.path.abspath(working_dir) + os.sep)): + logger.warning(msg) + self.notify_unassign() + raise PermanentError('File not part of an open folder in SublimeText.') + + file = os.path.relpath(abs_path, working_dir) + return ['codeclimate', 'analyze', '${args}', file, '${xoo}']