LoginSignup
8
8

More than 5 years have passed since last update.

SublimeText2とSublimeLinter - Python3の構文チェック -

Posted at

1. 新しくLinterを定義

SublimeLinter/sublimelinter/modules/python3.py

python3.py
import re
from base_linter import BaseLinter, INPUT_METHOD_FILE


CONFIG = {
    'language': 'Python3',
    'executable': 'flake8',
    'test_existence_args': ['--version'],
    'lint_args': '{filename}',
    'input_method': INPUT_METHOD_FILE # 保存時のみ。リアルタイムチェックしたい場合はINPUT_METHOD_TEMP_FILEにする
}


class Linter(BaseLinter):
    def parse_errors(self, view, errors, lines, errorUnderlines, violationUnderlines, warningUnderlines, errorMessages, violationMessages, warningMessages):
        for line in errors.splitlines():
            match = re.match(r'^.+:(?P<line>\d+):(?P<offset>\d*):?\s+(?P<error>.+)', line)
            if match:
                error, line, offset = match.group('error'), match.group('line'), match.group('offset')
                if not error.startswith('E501'):
                    self.add_message(int(line), lines, '[{0}: {1}]'.format(offset, error), errorMessages)

好みでE501は除いています。

2. Python3というsyntaxを作る

cd ~/Library/Application Support/Sublime Text 2/Packages/Python
cp Python.tmLanguage Python3.tmLanguage

よくわからないのでnameとscopeNameだけPython3とsource.python3に変更した
Set SyntaxでPython3が現れればOK

3. SublimeLinterの設定ファイル

{
    "sublimelinter_executable_map": {
        "python3": "python3版flake8へのパス"
    }
}

4. fleke8

utf-8なファイルで非ASCIIだと死ぬ

回避法は環境変数PYTHONIOENCODINGをutf-8にし、かつLC_CTYPEをen_US.utf-8にする事。Macな人は/etc/launchd.confに書く。
影響デカくていやだという人は、スタートアップスクリプト(flake8コマンドに相当するもの)を自分で書いて、中にエンコードの設定を書けばいいでしょう。
sys.getdefaultencoding() == 'utf-8'、locale.getpreferredencoding(False) == 'utf-8'になるように内部の環境を設定すること。

bytesがあると死ぬ

これはflake8のバグっぽいです。flake8/pyflakes.py:319あたりの

NUM = STR = ELLIPSIS = ignore

にBYTESを追加してやったらうまくいきました。

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8