File: vim_interface.py

package info (click to toggle)
vim-ultisnips 3.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,924 kB
  • sloc: python: 8,353; sh: 64; makefile: 38
file content (277 lines) | stat: -rw-r--r-- 8,823 bytes parent folder | download
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# encoding: utf-8

import os
import re
import shutil
import signal
import subprocess
import tempfile
import textwrap
import time

from test.constant import ARR_D, ARR_L, ARR_R, ARR_U, BS, ESC, PYTHON3, SEQUENCES


def wait_until_file_exists(file_path, times=None, interval=0.01):
    while times is None or times:
        if os.path.exists(file_path):
            return True
        time.sleep(interval)
        if times is not None:
            times -= 1
    return False


def read_text_file(filename):
    """Reads the content of a text file."""
    if PYTHON3:
        return open(filename, "r", encoding="utf-8").read()
    else:
        return open(filename, "r").read()


def is_process_running(pid):
    """Returns true if a process with pid is running, false otherwise."""
    # from
    # http://stackoverflow.com/questions/568271/how-to-check-if-there-exists-a-process-with-a-given-pid
    try:
        os.kill(pid, 0)
    except OSError:
        return False
    else:
        return True


def create_directory(dirname):
    """Creates 'dirname' and its parents if it does not exist."""
    try:
        os.makedirs(dirname)
    except OSError:
        pass


class TempFileManager(object):
    def __init__(self, name=""):
        self._temp_dir = tempfile.mkdtemp(prefix="UltiSnipsTest_" + name)

    def name_temp(self, file_path):
        return os.path.join(self._temp_dir, file_path)

    def write_temp(self, file_path, content):
        abs_path = self.name_temp(file_path)
        create_directory(os.path.dirname(abs_path))
        if PYTHON3:
            with open(abs_path, "w", encoding="utf-8") as f:
                f.write(content)
        else:
            with open(abs_path, "w") as f:
                f.write(content)
        return abs_path

    def unique_name_temp(self, suffix="", prefix=""):
        file_handler, abspath = tempfile.mkstemp(suffix, prefix, self._temp_dir)
        os.close(file_handler)
        os.remove(abspath)
        return abspath

    def clear_temp(self):
        shutil.rmtree(self._temp_dir)
        create_directory(self._temp_dir)


class VimInterface(TempFileManager):
    def __init__(self, vim_executable, name):
        TempFileManager.__init__(self, name)
        self._vim_executable = vim_executable

    @property
    def vim_executable(self):
        return self._vim_executable

    def has_version(self, major, minor, patchlevel):
        cmd = [
            self._vim_executable, "-e", "-c",
            "if has('patch-%d.%d.%d') | quit | else | cquit | endif"
                % (major, minor, patchlevel),
        ]
        return not subprocess.call(cmd, stdout=subprocess.DEVNULL)

    def get_buffer_data(self):
        buffer_path = self.unique_name_temp(prefix="buffer_")
        self.send_to_vim(ESC + ":w! %s\n" % buffer_path)
        if wait_until_file_exists(buffer_path, 50):
            return read_text_file(buffer_path)[:-1]

    def send_to_terminal(self, s):
        """Types 's' into the terminal."""
        raise NotImplementedError()

    def send_to_vim(self, s):
        """Types 's' into the vim instance under test."""
        raise NotImplementedError()

    def launch(self, config=[]):
        """Returns the python version in Vim as a string, e.g. '2.7'"""
        pid_file = self.name_temp("vim.pid")
        done_file = self.name_temp("loading_done")
        if os.path.exists(done_file):
            os.remove(done_file)

        post_config = []
        post_config.append("%s << EOF" % ("py3" if PYTHON3 else "py"))
        post_config.append("import vim, sys")
        post_config.append(
            "with open('%s', 'w') as pid_file: pid_file.write(vim.eval('getpid()'))"
            % pid_file
        )
        post_config.append("with open('%s', 'w') as done_file:" % done_file)
        post_config.append("    done_file.write('%i.%i.%i' % sys.version_info[:3])")
        post_config.append("EOF")

        config_path = self.write_temp(
            "vim_config.vim",
            textwrap.dedent(os.linesep.join(config + post_config) + "\n"),
        )

        # Note the space to exclude it from shell history. Also we always set
        # NVIM_LISTEN_ADDRESS, even when running vanilla Vim, because it will
        # just not care.
        self.send_to_terminal(
            """ NVIM_LISTEN_ADDRESS=%s %s -u %s\r\n"""
            % (self.name_temp("nvim.sock"), self._vim_executable, config_path)
        )
        if not wait_until_file_exists(done_file, 500):
            raise TimeoutError("Unable to load Vim within 5 seconds")
        self._vim_pid = int(read_text_file(pid_file))
        return read_text_file(done_file).strip()

    def leave_with_wait(self):
        self.send_to_vim(3 * ESC + ":qa!\n")
        for _ in range(25):
            if not is_process_running(self._vim_pid):
                return
            time.sleep(0.2)
        os.kill(self._vim_pid, signal.SIGKILL)


class VimInterfaceTmux(VimInterface):
    def __init__(self, vim_executable, session):
        VimInterface.__init__(self, vim_executable, "Tmux")
        self.session = session
        self._check_version()

    def _send(self, s):
        # I did not find any documentation on what needs escaping when sending
        # to tmux, but it seems like this is all that is needed for now.
        s = s.replace(";", r"\;")

        if len(s) == 1:
            subprocess.check_call(["tmux", "send-keys", "-t", self.session, hex(ord(s))])
        else:
            subprocess.check_call(["tmux", "send-keys", "-t", self.session, "-l", s])

    def send_to_terminal(self, s):
        return self._send(s)

    def send_to_vim(self, s):
        return self._send(s)

    def _check_version(self):
        stdout, _ = subprocess.Popen(
            ["tmux", "-V"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
        ).communicate()
        if PYTHON3:
            stdout = stdout.decode("utf-8")
        m = re.match(r"tmux (?:next-)?(\d+).(\d+)", stdout)
        if not m or not (int(m.group(1)), int(m.group(2))) >= (1, 8):
            raise RuntimeError("Need at least tmux 1.8, you have %s." % stdout.strip())


class VimInterfaceTmuxNeovim(VimInterfaceTmux):
    def __init__(self, vim_executable, session):
        VimInterfaceTmux.__init__(self, vim_executable, session)
        self._nvim = None

    def send_to_vim(self, s):
        if s == ARR_L:
            s = "<Left>"
        elif s == ARR_R:
            s = "<Right>"
        elif s == ARR_U:
            s = "<Up>"
        elif s == ARR_D:
            s = "<Down>"
        elif s == BS:
            s = "<bs>"
        elif s == ESC:
            s = "<esc>"
        elif s == "<":
            s = "<lt>"
        self._nvim.input(s)

    def launch(self, config=[]):
        import neovim

        rv = VimInterfaceTmux.launch(self, config)
        self._nvim = neovim.attach("socket", path=self.name_temp("nvim.sock"))
        return rv


class VimInterfaceWindows(VimInterface):
    BRACES = re.compile("([}{])")
    WIN_ESCAPES = ["+", "^", "%", "~", "[", "]", "<", ">", "(", ")"]
    WIN_REPLACES = [
        (BS, "{BS}"),
        (ARR_L, "{LEFT}"),
        (ARR_R, "{RIGHT}"),
        (ARR_U, "{UP}"),
        (ARR_D, "{DOWN}"),
        ("\t", "{TAB}"),
        ("\n", "~"),
        (ESC, "{ESC}"),
        # On my system ` waits for a second keystroke, so `+SPACE = "`".  On
        # most systems, `+Space = "` ". I work around this, by sending the host
        # ` as `+_+BS. Awkward, but the only way I found to get this working.
        ("`", "`_{BS}"),
        ("´", "´_{BS}"),
        ("{^}", "{^}_{BS}"),
    ]

    def __init__(self):
        # import windows specific modules
        import win32com.client
        import win32gui

        self.win32gui = win32gui
        self.shell = win32com.client.Dispatch("WScript.Shell")

    def is_focused(self, title=None):
        cur_title = self.win32gui.GetWindowText(self.win32gui.GetForegroundWindow())
        if (title or "- GVIM") in cur_title:
            return True
        return False

    def focus(self, title=None):
        if not self.shell.AppActivate(title or "- GVIM"):
            raise Exception("Failed to switch to GVim window")
        time.sleep(1)

    def convert_keys(self, keys):
        keys = self.BRACES.sub(r"{\1}", keys)
        for k in self.WIN_ESCAPES:
            keys = keys.replace(k, "{%s}" % k)
        for f, r in self.WIN_REPLACES:
            keys = keys.replace(f, r)
        return keys

    def send(self, keys):
        keys = self.convert_keys(keys)

        if not self.is_focused():
            time.sleep(2)
            self.focus()
        if not self.is_focused():
            # This is the only way I can find to stop test execution
            raise KeyboardInterrupt("Failed to focus GVIM")

        self.shell.SendKeys(keys)