Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 60 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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:

Expand Down
42 changes: 25 additions & 17 deletions linter.py
Original file line number Diff line number Diff line change
@@ -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, '
Expand All @@ -34,6 +37,7 @@ class Codeclimate(Linter):
'text.html'
)
}

regex = r'^(?P<line>\d+)(?:-\d+)?:\s(?P<message>.+)$'
multiline = False
line_col_base = (1, 1)
Expand All @@ -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']
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My fork is purpose-built to only run the structure and duplication engines.

Maybe it would be beneficial to accept some user settings that can set these flags instead?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why the flags are defined here is because CodeClimate will run extra engines if they're defined in .codeclimate.yml - which I don't need them to in my local environment because I have other linters for them (e.g. Rubocop)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @schuylr

Thank you very much for your response.

As far as I can see in my tests, and I experienced the behavior of SublimeLinter, arguments take place in the SublimeLinter settings. They will be applied either of global settings or project-specific settings. I've tested both. If you set for instance

"linters": {
  "codeclimate": {
    "args": [
        "-e",
        "structure",
        "-e",
        "duplication"
      ]
  }
}

in the global settings, codeclimate will ignore the .codeclimate.yml in the project root because it will be executed with arguments ($ codeclimate analyze path/to/file.ext -e structure -e duplication)

I suggest, to take this way of using the SublimeLinter settings as it is designed, instead of "hard coding" the arguments.

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}']