-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathcommands.py
More file actions
295 lines (230 loc) · 8.82 KB
/
Copy pathcommands.py
File metadata and controls
295 lines (230 loc) · 8.82 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import vim # pylint: disable=import-error
from .util import VimUtilCtx
from .util import find_free_port as util_find_free_port
from .util import (formatPathForClient, formatPathForServer, getResponse,
quickfixes_from_response, doRequest)
from .vimcmd import vimcmd
logger = logging.getLogger('omnisharp')
ctx = VimUtilCtx(vim)
def openFile(filename, line=0, column=0, noautocmd=0):
vim.command("let l:loc = {{ 'filename': '{0}', 'lnum': {1}, 'col': {2} }}"
.format(filename, line, column))
vim.command("call OmniSharp#locations#Navigate(l:loc, {0})".format(noautocmd))
def setBuffer(text):
if text is None:
return False
pos = vim.current.window.cursor
lines = text.splitlines()
lines = [line.encode('utf-8') for line in lines]
vim.current.buffer[:] = lines
try:
vim.current.window.cursor = pos
except vim.error:
vim.current.window.cursor = (len(vim.current.buffer), pos[1])
return True
@vimcmd
def findUsages():
response = getResponse(ctx, '/findusages', json=True)
return quickfixes_from_response(ctx, response['QuickFixes'])
@vimcmd
def findMembers():
response = getResponse(ctx, '/currentfilemembersasflat', json=True)
return quickfixes_from_response(ctx, response)
@vimcmd
def findImplementations():
response = getResponse(ctx, '/findimplementations', json=True)
return quickfixes_from_response(ctx, response['QuickFixes'])
@vimcmd
def getCompletions(partialWord):
parameters = {}
parameters['WordToComplete'] = partialWord
parameters['WantDocumentationForEveryCompletionResult'] = \
bool(int(vim.eval('g:OmniSharp_complete_documentation')))
want_snippet = \
bool(int(vim.eval('g:OmniSharp_want_snippet')))
parameters['WantSnippet'] = want_snippet
parameters['WantMethodHeader'] = True
parameters['WantReturnType'] = True
response = getResponse(ctx, '/autocomplete', parameters, json=True)
vim_completions = []
if response is not None:
for cmp in response:
if want_snippet:
word = cmp['MethodHeader'] or cmp['CompletionText']
menu = cmp['ReturnType'] or cmp['DisplayText']
else:
word = cmp['CompletionText'] or cmp['MethodHeader']
menu = cmp['DisplayText'] or cmp['MethodHeader']
menu = ' '.join(filter(None, [cmp['ReturnType'], menu]))
vim_completions.append({
'snip': cmp['Snippet'] or '',
'word': word,
'menu': menu,
'info': ((cmp['Description'] or ' ')
.replace('\r\n', '\n')),
'icase': 1,
'dup': 1
})
return vim_completions
@vimcmd
def gotoDefinition():
definition = getResponse(ctx, '/gotodefinition', json=True)
if definition.get('FileName'):
return quickfixes_from_response(ctx, [definition])[0]
else:
return None
@vimcmd
def getCodeActions(mode):
parameters = codeActionParameters(mode)
response = getResponse(ctx, '/v2/getcodeactions', parameters, json=True)
return response['CodeActions']
@vimcmd
def runCodeAction(mode, action):
parameters = codeActionParameters(mode)
parameters['identifier'] = action
response = getResponse(ctx, '/v2/runcodeaction', parameters, json=True)
changes = response.get('Changes')
if changes:
bufname = vim.current.buffer.name
bufnum = vim.current.buffer.number
pos = vim.current.window.cursor
vim.command('let l:hidden_bak = &hidden | set hidden')
for changeDefinition in changes:
filename = formatPathForClient(ctx, changeDefinition['FileName'])
openFile(filename, noautocmd=1)
if not setBuffer(changeDefinition.get('Buffer')):
for change in changeDefinition.get('Changes', []):
setBuffer(change.get('NewText'))
if vim.current.buffer.number != bufnum:
vim.command('silent write | silent edit')
openFile(bufname, pos[0], pos[1], 1)
vim.command('let &hidden = l:hidden_bak | unlet l:hidden_bak')
return True
return False
def codeActionParameters(mode):
parameters = {}
if mode == 'visual':
start = vim.eval('getpos("\'<")')
end = vim.eval('getpos("\'>")')
# In visual line mode, getpos("'>")[2] is a large number (2147483647).
# In python this is represented as a string, so when the length of this
# string is greater than 5 it means the position is greater than 99999.
# In this case, use the length of the line as the column position.
if len(end[2]) > 5:
end[2] = vim.eval('len(getline(%s))' % end[1])
logger.error(end)
parameters['Selection'] = {
'Start': {'Line': start[1], 'Column': start[2]},
'End': {'Line': end[1], 'Column': end[2]}
}
return parameters
@vimcmd
def codeCheck():
response = getResponse(ctx, '/codecheck', json=True)
return quickfixes_from_response(ctx, response['QuickFixes'])
@vimcmd
def globalCodeCheck():
parameters = {}
response = doRequest(ctx.host, '/codecheck', parameters, json=True)
return quickfixes_from_response(ctx, response['QuickFixes'])
@vimcmd
def signatureHelp():
return getResponse(ctx, '/signatureHelp', json=True)
@vimcmd
def typeLookup(include_documentation):
parameters = {
'includeDocumentation': bool(include_documentation),
}
response = getResponse(ctx, '/typelookup', parameters, json=True)
return {
'Type': response.get('Type', '') or '',
'Documentation': response.get('Documentation', '') or ''
}
@vimcmd
def renameTo(name):
parameters = {
'renameto': name,
}
response = getResponse(ctx, '/rename', parameters, json=True)
changes = response['Changes']
ret = []
for change in changes:
ret.append({
'FileName': formatPathForClient(ctx, change['FileName']),
'Buffer': change['Buffer']
})
return ret
@vimcmd
def codeFormat():
parameters = {}
parameters['ExpandTab'] = bool(int(vim.eval('&expandtab')))
response = getResponse(ctx, '/codeformat', parameters, json=True)
setBuffer(response.get("Buffer"))
@vimcmd
def fixUsings():
response = getResponse(ctx, '/fixusings', json=True)
setBuffer(response.get("Buffer"))
return quickfixes_from_response(ctx, response['AmbiguousResults'])
@vimcmd
def findSymbols(filter='', symbolfilter=''):
parameters = {}
parameters["filter"] = filter
parameters["symbolfilter"] = symbolfilter
response = getResponse(ctx, '/findsymbols', parameters, json=True)
return quickfixes_from_response(ctx, response['QuickFixes'])
@vimcmd
def findHighlightTypes():
# Original buffer lines
bufferLines = ctx.buffer.split('\r\n')
response = getResponse(ctx, '/highlight', json=True)
highlights = response.get('Highlights', [])
identifierKinds = ['constant name', 'enum member name', 'field name',
'identifier', 'local name', 'parameter name',
'property name', 'static symbol']
interfaceKinds = ['interface name']
methodKinds = ['extension method name', 'method name']
typeKinds = ['class name', 'enum name', 'namespace name', 'struct name']
types = []
for hi in highlights:
lnum = hi['StartLine'] - 1
if lnum >= len(bufferLines):
# An error has occurred with invalid line endings - perhaps a
# combination of unix and dos line endings?
return {'error': 'Invalid buffer - check line endings'}
line = bufferLines[lnum]
types.append({
'kind': hi['Kind'],
'name': line[hi['StartColumn'] - 1:hi['EndColumn'] - 1]
})
return {
'identifiers': [t['name'] for t in types if t['kind'] in identifierKinds],
'interfaces': [t['name'] for t in types if t['kind'] in interfaceKinds],
'methods': [t['name'] for t in types if t['kind'] in methodKinds],
'types': [t['name'] for t in types if t['kind'] in typeKinds]
}
@vimcmd
def navigateUp():
get_navigate_response('/navigateup')
@vimcmd
def navigateDown():
get_navigate_response('/navigatedown')
def get_navigate_response(endpoint):
response = getResponse(ctx, endpoint, json=True)
vim.current.window.cursor = (response['Line'], response['Column'] - 1)
@vimcmd
def find_free_port():
return util_find_free_port()
@vimcmd
def checkAliveStatus():
try:
return getResponse(ctx, "/checkalivestatus", timeout=0.2) == 'true'
except Exception:
return 0
@vimcmd
def updateBuffer():
getResponse(ctx, "/updatebuffer")
__all__ = [name for name, fxn in locals().items()
if getattr(fxn, 'is_cmd', False)]