summaryrefslogtreecommitdiffstats
path: root/python/mozlint/mozlint/editor.py
blob: 1738892f93c33dc04cbb263b890a0243d9294c5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import subprocess
import tempfile

from mozlint import formatters


def get_editor():
    return os.environ.get("EDITOR")


def edit_issues(result):
    if not result.issues:
        return

    editor = get_editor()
    if not editor:
        print("warning: could not find a default editor")
        return

    name = os.path.basename(editor)
    if name in ("vim", "nvim", "gvim"):
        cmd = [
            editor,
            # need errorformat to match both Error and Warning, with or without a column
            "--cmd",
            "set errorformat+=%f:\\ line\\ %l\\\\,\\ col\\ %c\\\\,\\ %trror\\ -\\ %m",
            "--cmd",
            "set errorformat+=%f:\\ line\\ %l\\\\,\\ col\\ %c\\\\,\\ %tarning\\ -\\ %m",
            "--cmd",
            "set errorformat+=%f:\\ line\\ %l\\\\,\\ %trror\\ -\\ %m",
            "--cmd",
            "set errorformat+=%f:\\ line\\ %l\\\\,\\ %tarning\\ -\\ %m",
            # start with quickfix window opened
            "-c",
            "copen",
            # running with -q seems to open an empty buffer in addition to the
            # first file, this removes that empty buffer
            "-c",
            "1bd",
        ]

        with tempfile.NamedTemporaryFile(mode="w") as fh:
            s = formatters.get("compact", summary=False)(result)
            fh.write(s)
            fh.flush()

            cmd.extend(["-q", fh.name])
            subprocess.call(cmd)

    else:
        for path, errors in result.issues.items():
            subprocess.call([editor, path])