-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathutil.py
246 lines (195 loc) · 7.33 KB
/
util.py
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
# -*- coding: utf-8 -*-
""" Utilities """
import json as jsonlib
import logging
import os.path
import platform
import re
import socket
import sys
from contextlib import closing
from .exceptions import BadResponseError, ServerConnectionError
try:
from urllib import parse as urlparse
from urllib import request
except ImportError:
import urllib2 as request
import urlparse
logger = logging.getLogger('omnisharp.util')
class BaseCtx(object):
"""
Provides properties that will be shared by all implementations of UtilCtx
"""
def __init__(self):
self.is_msys = 'msys_nt' in platform.system().lower()
self.is_cygwin = 'cygwin' in platform.system().lower()
self.is_wsl = ('linux' in platform.system().lower()
and 'microsoft' in platform.release().lower())
class UtilCtx(BaseCtx):
"""
Simple class that holds data that is needed by util functions
Most of the util methods require this object (or an equivalent replacement)
when they are called. The indirection here is to make two things easier:
testing, and running outside of a vim context.
Tests become easier because instead of mocking vim API functions, you can
just pass in an object with whatever test data you wish. As a bonus, as
long as the interface to the UtilCtx doesn't change, you can refactor the
vim implementation all you like without breaking tests.
Running outside a vim context is easier too, for the same reason as tests.
This is useful for such purposes as an ALE linter script.
"""
def __init__(
self,
buffer_name='',
translate_cygwin_wsl=False,
cwd='',
timeout=1,
host='',
line=1,
column=1,
buffer='',
):
super(UtilCtx, self).__init__()
self.buffer_name = buffer_name
self.translate_cygwin_wsl = translate_cygwin_wsl
self.cwd = cwd
self.timeout = timeout
self.host = host
self.line = line
self.column = column
self.buffer = buffer
class VimUtilCtx(BaseCtx):
""" Implementation of a UtilCtx that gets data from vim API """
def __init__(self, vim):
super(VimUtilCtx, self).__init__()
self._vim = vim
@property
def buffer_name(self):
# We can't use self._vim.current.buffer.name because it returns the real
# path. expand('%') will preserve the symlinked path, if one exists.
return self._vim.eval("expand('%:p')")
@property
def translate_cygwin_wsl(self):
return bool(int(self._vim.eval('g:OmniSharp_translate_cygwin_wsl')))
@property
def cwd(self):
return self._vim.eval('getcwd()')
@property
def timeout(self):
return int(self._vim.eval('g:OmniSharp_timeout'))
@property
def host(self):
return self._vim.eval('OmniSharp#GetHost()')
@property
def line(self):
return self._vim.current.window.cursor[0]
@property
def column(self):
return self._vim.current.window.cursor[1] + 1
@property
def buffer(self):
return '\r\n'.join(self._vim.eval("getline(1,'$')")[:])
def quickfixes_from_response(ctx, response):
items = []
for quickfix in response:
# syntax errors returns 'Message' instead of 'Text'.
# I need to sort this out.
text = quickfix.get('Text') or quickfix.get('Message', '')
filename = quickfix.get('FileName')
if filename is None:
filename = ctx.buffer_name
else:
filename = formatPathForClient(ctx, filename)
item = {
'filename': filename,
'text': text,
'lnum': quickfix['Line'],
'col': quickfix['Column'],
'vcol': 0
}
if 'LogLevel' in quickfix:
item['type'] = 'E' if quickfix['LogLevel'] == 'Error' else 'W'
if quickfix['LogLevel'] == 'Hidden':
item['subtype'] = 'Style'
items.append(item)
return items
# When working in Windows Subsystem for Linux (WSL) or Cygwin, vim uses
# unix-style paths but OmniSharp (with a Windows binary) uses Windows
# paths. This means that filenames returned FROM OmniSharp must be
# translated from e.g. "C:\path\to\file" to "/mnt/c/path/to/file", and
# filenames sent TO OmniSharp must be translated in the other direction.
def formatPathForServer(ctx, filepath):
if ctx.translate_cygwin_wsl and (ctx.is_msys or ctx.is_cygwin or ctx.is_wsl):
if ctx.is_msys:
pattern = r'^/([a-zA-Z])/'
elif ctx.is_cygwin:
pattern = r'^/cygdrive/([a-zA-Z])/'
else:
pattern = r'^/mnt/([a-zA-Z])/'
def drive_replace(match):
return match.group(1).upper() + ':\\'
return re.sub(pattern, drive_replace, filepath).replace('/', '\\')
return filepath
def formatPathForClient(ctx, filepath):
if ctx.translate_cygwin_wsl and (ctx.is_msys or ctx.is_cygwin or ctx.is_wsl):
def path_replace(matchobj):
if ctx.is_msys:
prefix = '/{0}/'
elif ctx.is_cygwin:
prefix = '/cygdrive/{0}/'
else:
prefix = '/mnt/{0}/'
return prefix.format(matchobj.group(1).lower())
return re.sub(r'^([a-zA-Z]):\\', path_replace, filepath).replace('\\', '/')
# Shorten path names by checking if we can make them relative
cwd = ctx.cwd
if cwd and os.path.commonprefix([cwd, filepath]) == cwd:
filepath = filepath[len(cwd):].lstrip('/\\')
return filepath
def getResponse(ctx, path, additional_parameters=None, timeout=None, json=False):
parameters = {}
parameters['line'] = ctx.line
parameters['column'] = ctx.column
parameters['buffer'] = ctx.buffer
parameters['filename'] = formatPathForServer(ctx, ctx.buffer_name)
if additional_parameters is not None:
parameters.update(additional_parameters)
if timeout is None:
timeout = ctx.timeout
return doRequest(ctx.host, path, parameters, timeout=timeout,
json=json)
def doRequest(host, path, parameters, timeout=1, json=False):
target = urlparse.urljoin(host, path)
proxy = request.ProxyHandler({})
opener = request.build_opener(proxy)
req = request.Request(target)
req.add_header('Content-Type', 'application/json')
body = jsonlib.dumps(parameters)
if sys.version_info >= (3, 0):
body = body.encode('utf-8')
logger.info("Request: %s", target)
logger.debug(body)
try:
response = opener.open(req, body, timeout)
res = response.read()
except Exception as e:
logger.exception("Could not connect to OmniSharp server: %s", target)
raise ServerConnectionError(str(e))
if sys.version_info >= (3, 0):
res = res.decode('utf-8')
if res.startswith("\xef\xbb\xbf"): # Drop UTF-8 BOM
res = res[3:]
logger.info("Received Response: %s", target)
logger.debug(res)
if json:
try:
return jsonlib.loads(res)
except Exception as e:
logger.error("Error parsing response as json: \n%s", res)
raise BadResponseError(str(e))
return res
def find_free_port():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
with closing(sock):
sock.bind(('', 0))
return sock.getsockname()[1]