├── .gitignore ├── messages.json ├── .travis.yml ├── .sublimelinterrc ├── messages └── install.txt ├── LICENSE ├── linter.py └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /messages.json: -------------------------------------------------------------------------------- 1 | { 2 | "install": "messages/install.txt" 3 | } 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "3.3" 4 | # command to install dependencies 5 | install: 6 | - pip install flake8 7 | - pip install pep257 8 | # command to run tests 9 | script: 10 | - flake8 *.py --max-line-length=120 11 | - pep257 . --ignore=D202 12 | -------------------------------------------------------------------------------- /.sublimelinterrc: -------------------------------------------------------------------------------- 1 | { 2 | "@python": 3, 3 | "linters": { 4 | "flake8": { 5 | "max-line-length": 120 6 | }, 7 | "pep257": { 8 | "ignore": ["D202"] 9 | }, 10 | "pep8": { 11 | "max-line-length": 120 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /messages/install.txt: -------------------------------------------------------------------------------- 1 | SublimeLinter-contrib-gometalinter 2 | ------------------------------- 3 | This linter plugin for SublimeLinter provides an interface to gometalinter. 4 | 5 | ** IMPORTANT! ** 6 | 7 | Before this plugin will activate, you *must* 8 | follow the installation instructions here: 9 | 10 | https://github.com/alecthomas/SublimeLinter-contrib-gometalinter 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Permission is hereby granted, free of charge, to any person obtaining a copy 2 | of this software and associated documentation files (the "Software"), to deal 3 | in the Software without restriction, including without limitation the rights 4 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 5 | copies of the Software, and to permit persons to whom the Software is 6 | furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in 9 | all copies or substantial portions of the Software. 10 | 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 12 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 14 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 15 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 16 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 17 | THE SOFTWARE. 18 | -------------------------------------------------------------------------------- /linter.py: -------------------------------------------------------------------------------- 1 | # 2 | # linter.py 3 | # Linter for SublimeLinter3, a code checking framework for Sublime Text 3 4 | # 5 | # Written by Alec Thomas 6 | # Copyright (c) 2014 Alec Thomas 7 | # 8 | # License: MIT 9 | # 10 | 11 | """This module exports the Gometalinter plugin class.""" 12 | 13 | import os 14 | import shlex 15 | import tempfile 16 | 17 | from SublimeLinter.lint import Linter, highlight, util 18 | from SublimeLinter.lint.persist import settings 19 | 20 | 21 | class Gometalinter(Linter): 22 | """Provides an interface to gometalinter.""" 23 | 24 | cmd = 'gometalinter --fast .' 25 | regex = r'(?:[^:]+):(?P\d+):(?P\d+)?:(?:(?Pwarning)|(?Perror)):\s*(?P.*)' 26 | error_stream = util.STREAM_BOTH 27 | default_type = highlight.ERROR 28 | defaults = { 29 | 'selector': 'source.go' 30 | } 31 | 32 | def run(self, cmd, code): 33 | if settings.get('lint_mode') == 'background': 34 | return self._live_lint(cmd, code) 35 | else: 36 | return self._in_place_lint(cmd) 37 | 38 | def _live_lint(self, cmd, code): 39 | dir = os.path.dirname(self.filename) 40 | if not dir: 41 | print('gometalinter: skipped linting of unsaved file') 42 | return 43 | filename = os.path.basename(self.filename) 44 | cmd = cmd + ['-I', '^'+filename] 45 | print('gometalinter: live linting {} in {}: {}'.format(filename, dir, ' '.join(map(shlex.quote, cmd)))) 46 | files = [f for f in os.listdir(dir) if f.endswith('.go')] 47 | if len(files) > 40: 48 | print("gometalinter: too many files ({}), live linting skipped".format(len(files))) 49 | return '' 50 | return self.tmpdir(cmd, dir, files, self.filename, code) 51 | 52 | def _in_place_lint(self, cmd): 53 | dir = os.path.dirname(self.filename) 54 | if not dir: 55 | print('gometalinter: skipped linting of unsaved file') 56 | return 57 | filename = os.path.basename(self.filename) 58 | cmd = cmd + ['-I', '^'+filename] 59 | print('gometalinter: in-place linting {}: {}'.format(filename, ' '.join(map(shlex.quote, cmd)))) 60 | out = self.communicate(cmd) 61 | return out or '' 62 | 63 | def tmpdir(self, cmd, dir, files, filename, code): 64 | """Run an external executable using a temp dir filled with files and return its output.""" 65 | with tempfile.TemporaryDirectory(dir=dir, prefix=".gometalinter-") as tmpdir: 66 | for f in files: 67 | target = os.path.join(tmpdir, f) 68 | f = os.path.join(dir, f) 69 | 70 | if os.path.basename(target) == os.path.basename(filename): 71 | # source file hasn't been saved since change, so update it from our live buffer 72 | with open(target, 'wb') as f: 73 | if isinstance(code, str): 74 | code = code.encode('utf8') 75 | 76 | f.write(code) 77 | else: 78 | os.link(f, target) 79 | 80 | out = self.communicate(cmd) 81 | return out or '' 82 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SublimeLinter-contrib-gometalinter 2 | ================================ 3 | 4 | This linter plugin for [SublimeLinter][docs] provides an interface to [gometalinter](https://github.com/alecthomas/gometalinter). It will be used with files that have the “Go” syntax. 5 | 6 | ## Installation 7 | SublimeLinter 3 must be installed in order to use this plugin. If SublimeLinter 3 is not installed, please follow the instructions [here][installation]. 8 | 9 | ### Linter installation 10 | Before using this plugin, you must ensure that `gometalinter` is installed on your system. To install `gometalinter`, do the following: 11 | 12 | 1. Install [Go](http://golang.org/doc/install). 13 | 14 | 1. Install `gometalinter` by typing the following in a terminal: 15 | ``` 16 | go get github.com/alecthomas/gometalinter 17 | gometalinter --install --update 18 | ``` 19 | 20 | ### Linter configuration 21 | In order for `gometalinter` 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. 22 | 23 | Once you have installed `gometalinter`, you can proceed to install the SublimeLinter-contrib-gometalinter plugin if it is not yet installed. 24 | 25 | ### Plugin installation 26 | 27 | Please use [Package Control][pc] to install the linter plugin. This will ensure that the plugin will be updated when new versions are available. If you want to install from source so you can modify the source code, you probably know what you are doing so we won’t cover that here. 28 | 29 | To install via Package Control, do the following: 30 | 31 | 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. 32 | 33 | 1. When the plugin list appears, type `gometalinter`. Among the entries you should see `SublimeLinter-contrib-gometalinter`. If that entry is not highlighted, use the keyboard or mouse to select it. 34 | 35 | ## Usage 36 | 37 | If you are launching Sublime from a window manager, it's possible your GOPATH will not be set correctly. There are two ways around that: 38 | 39 | 1. Launch Sublime from a shell with GOPATH already set correctly. 40 | 2. Set the `gopath` setting in the linter configuration for SublimeLinter-contrib-gometalinter. 41 | 42 | ## Settings 43 | 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]. 44 | 45 | Go vendoring will not work with SublimeLinter when `lint_mode=background`. Use `save`, `load/save` or `manual`. 46 | 47 | ## Contributing 48 | If you would like to contribute enhancements or fixes, please do the following: 49 | 50 | 1. Fork the plugin repository. 51 | 1. Hack on a separate topic branch created from the latest `master`. 52 | 1. Commit and push the topic branch. 53 | 1. Make a pull request. 54 | 1. Be patient. ;-) 55 | 56 | Please note that modications should follow these coding guidelines: 57 | 58 | - Indent is 4 spaces. 59 | - Code should pass flake8 and pep257 linters. 60 | - Vertical whitespace helps readability, don’t be afraid to use it. 61 | - Please use descriptive variable names, no abbrevations unless they are very well known. 62 | 63 | Thank you for helping out! 64 | 65 | [docs]: http://sublimelinter.readthedocs.org 66 | [installation]: http://sublimelinter.readthedocs.org/en/latest/installation.html 67 | [locating-executables]: http://sublimelinter.readthedocs.org/en/latest/usage.html#how-linter-executables-are-located 68 | [pc]: https://sublime.wbond.net/installation 69 | [cmd]: http://docs.sublimetext.info/en/sublime-text-3/extensibility/command_palette.html 70 | [settings]: http://sublimelinter.readthedocs.org/en/latest/settings.html 71 | [linter-settings]: http://sublimelinter.readthedocs.org/en/latest/linter_settings.html 72 | [inline-settings]: http://sublimelinter.readthedocs.org/en/latest/settings.html#inline-settings 73 | --------------------------------------------------------------------------------