|
1 |
| -import vim, urllib2, urllib, urlparse, logging, json, os, os.path, cgi, types, threading |
2 |
| -import asyncrequest |
| 1 | +import json, logging, os.path, platform, re, urllib2, urlparse, vim |
3 | 2 |
|
4 | 3 | logger = logging.getLogger('omnisharp')
|
5 | 4 | logger.setLevel(logging.WARNING)
|
|
13 | 12 | formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
|
14 | 13 | hdlr.setFormatter(formatter)
|
15 | 14 |
|
| 15 | +translate_unix_win = bool(int(vim.eval('g:OmniSharp_translate_cygwin_wsl'))) |
| 16 | +is_cygwin = 'cygwin' in platform.system().lower() |
| 17 | +is_wsl = 'linux' in platform.system().lower() and 'microsoft' in platform.release().lower() |
| 18 | + |
| 19 | +# When working in Windows Subsystem for Linux (WSL) or Cygwin, vim uses |
| 20 | +# unix-style paths but OmniSharp (with a Windows binary) uses Windows |
| 21 | +# paths. This means that filenames returned FROM OmniSharp must be |
| 22 | +# translated from e.g. "C:\path\to\file" to "/mnt/c/path/to/file", and |
| 23 | +# filenames sent TO OmniSharp must be translated in the other direction. |
| 24 | +def formatPathForServer(filepath): |
| 25 | + if translate_unix_win and (is_cygwin or is_wsl): |
| 26 | + pattern = r'^/cygdrive/([a-zA-Z])/' if is_cygwin else r'^/mnt/([a-zA-Z])/' |
| 27 | + return re.sub(pattern, r'\1:\\', filepath).replace('/', '\\') |
| 28 | + return filepath |
| 29 | +def formatPathForClient(filepath): |
| 30 | + if translate_unix_win and (is_cygwin or is_wsl): |
| 31 | + def path_replace(matchobj): |
| 32 | + prefix = '/cygdrive/{0}/' if is_cygwin else '/mnt/{0}/' |
| 33 | + return prefix.format(matchobj.group(1).lower()) |
| 34 | + return re.sub(r'^([a-zA-Z]):\\', path_replace, filepath).replace('\\', '/') |
| 35 | + return filepath |
16 | 36 |
|
17 | 37 | def getResponse(endPoint, additional_parameters=None, timeout=None):
|
18 | 38 | parameters = {}
|
19 | 39 | parameters['line'] = vim.eval('line(".")')
|
20 | 40 | parameters['column'] = vim.eval('col(".")')
|
21 | 41 | parameters['buffer'] = '\r\n'.join(vim.eval("getline(1,'$')")[:])
|
22 |
| - parameters['filename'] = vim.current.buffer.name |
| 42 | + parameters['filename'] = formatPathForServer(vim.current.buffer.name) |
23 | 43 | if additional_parameters != None:
|
24 | 44 | parameters.update(additional_parameters)
|
25 | 45 |
|
@@ -67,14 +87,47 @@ def findImplementations():
|
67 | 87 | js = getResponse('/findimplementations', parameters)
|
68 | 88 | return get_quickfix_list(js, 'QuickFixes')
|
69 | 89 |
|
| 90 | +def getCompletions(column, partialWord): |
| 91 | + parameters = {} |
| 92 | + parameters['column'] = vim.eval(column) |
| 93 | + parameters['wordToComplete'] = vim.eval(partialWord) |
| 94 | + |
| 95 | + parameters['WantDocumentationForEveryCompletionResult'] = \ |
| 96 | + bool(int(vim.eval('g:omnicomplete_fetch_full_documentation'))) |
| 97 | + |
| 98 | + want_snippet = \ |
| 99 | + bool(int(vim.eval('g:OmniSharp_want_snippet'))) |
| 100 | + |
| 101 | + parameters['WantSnippet'] = want_snippet |
| 102 | + parameters['WantMethodHeader'] = want_snippet |
| 103 | + parameters['WantReturnType'] = want_snippet |
| 104 | + |
| 105 | + parameters['buffer'] = '\r\n'.join(vim.eval('s:textBuffer')[:]) |
| 106 | + |
| 107 | + response = json.loads(getResponse('/autocomplete', parameters)) |
| 108 | + |
| 109 | + vim_completions = [] |
| 110 | + if response != None: |
| 111 | + for completion in response: |
| 112 | + vim_completions.append({ |
| 113 | + 'snip': completion['Snippet'] or '', |
| 114 | + 'word': completion['MethodHeader'] or completion['CompletionText'], |
| 115 | + 'menu': completion['ReturnType'] or completion['DisplayText'], |
| 116 | + 'info': (completion['Description'] or '').replace('\r\n', '\n'), |
| 117 | + 'icase': 1, |
| 118 | + 'dup': 1 |
| 119 | + }) |
| 120 | + return vim_completions |
| 121 | + |
70 | 122 | def gotoDefinition():
|
71 | 123 | js = getResponse('/gotodefinition');
|
72 |
| - if(js != ''): |
| 124 | + if js != '': |
73 | 125 | definition = json.loads(js)
|
74 | 126 | if(definition['FileName'] != None):
|
75 |
| - openFile(definition['FileName'].replace("'","''"), definition['Line'], definition['Column']) |
| 127 | + filename = formatPathForClient(definition['FileName'].replace("'","''")) |
| 128 | + openFile(filename, definition['Line'], definition['Column']) |
76 | 129 | else:
|
77 |
| - print "Not found" |
| 130 | + print("Not found") |
78 | 131 |
|
79 | 132 | def openFile(filename, line, column):
|
80 | 133 | vim.command("call OmniSharp#JumpToLocation('%(filename)s', %(line)s, %(column)s)" % locals())
|
@@ -159,9 +212,9 @@ def build():
|
159 | 212 |
|
160 | 213 | success = js["Success"]
|
161 | 214 | if success:
|
162 |
| - print "Build succeeded" |
| 215 | + print("Build succeeded") |
163 | 216 | else:
|
164 |
| - print "Build failed" |
| 217 | + print("Build failed") |
165 | 218 |
|
166 | 219 | return quickfixes_from_js(js, 'QuickFixes')
|
167 | 220 |
|
@@ -194,7 +247,7 @@ def addReference():
|
194 | 247 | js = getResponse("/addreference", parameters)
|
195 | 248 | if js != '':
|
196 | 249 | message = json.loads(js)['Message']
|
197 |
| - print message |
| 250 | + print(message) |
198 | 251 |
|
199 | 252 | def findSyntaxErrors():
|
200 | 253 | js = getResponse('/syntaxerrors')
|
@@ -235,6 +288,8 @@ def quickfixes_from_response(response):
|
235 | 288 | filename = quickfix['FileName']
|
236 | 289 | if filename == None:
|
237 | 290 | filename = vim.current.buffer.name
|
| 291 | + else: |
| 292 | + filename = formatPathForClient(filename) |
238 | 293 |
|
239 | 294 | item = {
|
240 | 295 | 'filename': filename,
|
|
0 commit comments