Repository navigation
Expand file tree
/
Copy pathclutter.py
More file actions
187 lines (126 loc) · 4.4 KB
/
Copy pathclutter.py
File metadata and controls
187 lines (126 loc) · 4.4 KB
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
from collections import namedtuple
from io import StringIO
import csv
import os
import pathlib
import re
import subprocess
import vim
outpattern = re.compile('^(.*):([0-9]+)\.([0-9]+)-([0-9]+)$')
Entry = namedtuple('Entry', ['name', 'path', 'line', 'col', 'endcol', 'attrs'])
Loc = namedtuple('Loc', ['path', 'row', 'col'])
def check():
def err(s):
print(f'clutter: {s}')
print("to reinstall or upgrade clutter, see https://github.com/cluttercode/clutter#installation.")
try:
proc = subprocess.run(["clutter", "version"], capture_output=True, text=True)
except FileNotFoundError:
err("clutter is not in $PATH.")
return
if proc.returncode != 0:
err("something went wrong with clutter.")
return
lines = proc.stdout.split(" ")
if not lines:
err("clutter gave out weird version output.")
return
if lines[0] != 'dev':
ver = lines[0].split(".")
if len(ver) != 3:
err("clutter gave out weird version output.")
return
if ver[0].startswith('v') or (int(ver[0]) == 0 and int(ver[1]) < 2):
err("incompatible clutter version - at least 0.2.0 required.")
return
def _rel_path(path):
return str(pathlib.PurePath(path).relative_to(os.getcwd()))
def _loc():
row, col = vim.current.window.cursor
path = vim.current.buffer.name
try:
path = _rel_path(path)
except ValueError:
print(f'clutter: buffer must be at path relative to current directory')
return None
return Loc(path=path, row=row, col=col)
def _run(opts=[], buffer_stdin=False):
cmd = ["clutter", "-i", ""] + opts
stdin = None
if buffer_stdin:
stdin = ''.join([f'{l}\n' for l in vim.current.buffer[:]])
proc = subprocess.run(cmd, capture_output=True, text=True, input=stdin)
stderr = proc.stderr.strip()
if stderr:
print(f'clutter: {stderr}')
if proc.returncode != 0:
return None
stdout = proc.stdout
r = csv.reader(StringIO(stdout), delimiter=' ')
vim.command('call setloclist(0, [], "f")')
vim.command('call setloclist(0, [], "r", {"title": "clutter"})')
matches = []
for fs in r:
if len(fs) < 2:
print(f'warning: invalid output line: "{fs}"')
continue
name, loc = fs[0], fs[1]
m = outpattern.match(loc)
if not m:
print(f'warning: invalid output line loc: "{loc}"')
continue
fn, lnum, col, endcol = m.groups()
lnum, col, endcol = int(lnum), int(col), int(endcol)
matches.append(Entry(name=name, path=fn, line=lnum, col=col, endcol=endcol, attrs=fs[2:]))
return matches
def _resolve_opts(loc):
return ["r", "--loc", f'{loc.path}:{loc.row}.{loc.col + 1}', '--loc-from-stdin']
def resolve1(which):
loc = _loc()
if not loc:
return
matches = _run(_resolve_opts(loc) + [f'-{which}', '-c'], True)
if not matches:
return
if len(matches) != 1:
_render_list(matches, loc)
return
first = matches[0]
if _rel_path(vim.current.buffer.name) != first.path:
try:
vim.command(f'keepalt edit {first.path}')
except vim.error as e:
# Check for ATTENTION (maybe opening a file that is already opened).
if e.args and e.args[0].find(':E325:') == -1:
raise
vim.command(f'{first.line}')
vim.command(f'normal! {first.col}|')
vim.command(f'normal! zz')
def resolve_list():
loc = _loc()
if not loc:
return
_render_list(_run(_resolve_opts(loc), True), loc)
def search(mode, args):
cmd = ['s']
if mode:
cmd.append(f'-{mode}')
cmd.extend(args)
_render_list(_run(cmd))
def _render_list(matches, loc=None):
if not matches:
print('clutter: no matches')
return
vim.command('call setloclist(0, [], "f")')
vim.command('call setloclist(0, [], "r", {"title": "clutter"})')
i = 1
for m in matches:
vim.command(
'call setloclist(0, [{"filename": "%s", "lnum": %s, "col": %s, "text": "%s %s"}], "a")' % (
m.path, m.line, m.col, m.name, ' '.join(m.attrs),
)
)
if loc and m.path == loc.path and m.line == loc.row and m.col <= loc.col <= m.endcol:
vim.command('call setloclist(0, [], "a", {"idx": %d})' % i)
i += 1
vim.command('lopen')