Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
50 changes: 39 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
SublimeLinter-contrib-codeclimate
================================
=================================

> Please note: This was originally a fork of [codeclimate/SublimeLinter-contrib-codeclimate][codeclimate-origin] to make it compatible for SublimeLinter 4.

[![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 +23,48 @@ 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.

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.
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.

## 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].

## How it works

The linter is first looking for a SublimeText Project. If a `*.sublime-project` is given, it executes `codeclimate` in the project root directory. If no project is given, it tries to execute `codeclimate` in the first opened folder of the folder pane.

If the SublimeText project folder or the first opened folder in the folder pane contains a `.codeclimate.yml` configuration file, `codeclimate` will recognise its settings. If no configuration file can be evaluated, `codeclimate` will automatically run the defaults of `structure` and `duplication` at this time.

## Linter Configuration

To specify the path to executable, please use the SublimeLinter settings:

```json
{
"linters": {
"executable": "/usr/local/bin/codeclimate"
}
}
```


If your `.codeclimate.yml` does not live in the project root folder, you can set also the `working_dir` of SublimeLinter:

```json
{
"working_dir": "${folder:$file_path}"
}
```

## 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 All @@ -48,6 +75,7 @@ Please note that modifications should follow these coding guidelines:

Thank you for helping out!

[codeclimate-origin]: https://github.com/codeclimate/SublimeLinter-contrib-codeclimate
[docs]: http://sublimelinter.readthedocs.org
[installation]: http://sublimelinter.readthedocs.org/en/latest/installation.html
[locating-executables]: http://sublimelinter.readthedocs.org/en/latest/usage.html#how-linter-executables-are-located
Expand Down
30 changes: 17 additions & 13 deletions linter.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#
# 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."""

from SublimeLinter.lint import Linter, util, persist
import re
from SublimeLinter.lint import Linter, util


class Codeclimate(Linter):
Expand Down Expand Up @@ -41,15 +44,16 @@ class Codeclimate(Linter):
error_stream = util.STREAM_BOTH
word_re = None

root = None
path = 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 working directory and run 'codeclimate analyze'."""
if self.context.get('project_root') is None:
Copy link
Owner

Choose a reason for hiding this comment

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

Does this new code properly read the .codeclimate.yml file found at the project root?

Copy link
Author

@meengit meengit Jun 18, 2020

Choose a reason for hiding this comment

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

Yes, of course.

The defaults are initially still set to ${project} (linter.py#L23).

If the project has the type of None and no project is configured, I try to find the path of the first opened folder in the folder pane (linter.py#L52-#L53). If no folder is open in the folder pane, the folder of the currently open file taking place (linter.py#L54-#L55).

As far as I know, the Codeclimate CLI has no argument to set a path to a .codeclimate.yml file. Instead, it expects such one in the working directory of execution.

Copy link
Author

Choose a reason for hiding this comment

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

I understand you did your changes for your personal/individual use. However, your changes helped me a lot. I do not "expect" that you will use my changes. Instead, I hope you can give it a try/test it. It would enable me to collect outside feedback to prevent mistakes I didn't/can't discover in my development environments.

Copy link
Author

Choose a reason for hiding this comment

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

@schuylr

Before I go to package control directly, I will try bring my changes back to the codeclimate repository in a pull request. Is it okey for you, when I mention you in the copyrights?

# Updated by Schuyler Jager & Andreas for SublimeLinter 4
# Copyright (c) 2020 Schuyler Jager & Andreas
# License: MIT

linter.py#L8-#L10

self.defaults['chdir'] = '${folder}' \
if self.context.get('folder') is not None \
else '${file_path}'

root = re.search(r"\{(\w+)\}", self.defaults['chdir']).group(1)
path = self.filename.replace(self.context.get(root) + '/', '')
return ['codeclimate', 'analyze', path]