GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — pythonVSCode/pythonFiles/completion.py at master · solidrock/pythonVSCode Expand file tree/ Copy pathcompletion.pyMore file actions 342 lines (298 loc) · 12.4 KB / Copy pathcompletion.pyFile metadata and controls342 lines (298 loc) · 12.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 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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 import os import io import re import sys import json import traceback sys.path.append(os.path.dirname(__file__)) import jedi # remove jedi from path after we import it so it will not be completed sys.path.pop(0) WORD_RE = re.compile(r'\w') class JediCompletion(object): basic_types = { 'module': 'import', 'instance': 'variable', 'statement': 'value', 'param': 'variable', } def __init__(self): self.default_sys_path = sys.path self._input = io.open(sys.stdin.fileno(), encoding='utf-8') def _get_definition_type(self, definition): is_built_in = definition.in_builtin_module if definition.type not in ['import', 'keyword'] and is_built_in(): return 'builtin' if definition.type in ['statement'] and definition.name.isupper(): return 'constant' return self.basic_types.get(definition.type, definition.type) def _additional_info(self, completion): """Provide additional information about the completion object.""" if completion._definition is None: return '' if completion.type == 'statement': nodes_to_display = ['InstanceElement', 'String', 'Node', 'Lambda', 'Number'] return ''.join(c.get_code() for c in completion._definition.children if type(c).__name__ in nodes_to_display).replace('\n', '') return '' @classmethod def _get_top_level_module(cls, path): """Recursively walk through directories looking for top level module. Jedi will use current filepath to look for another modules at same path, but it will not be able to see modules **above**, so our goal is to find the higher python module available from filepath. """ _path, _ = os.path.split(path) if os.path.isfile(os.path.join(_path, '__init__.py')): return cls._get_top_level_module(_path) return path def _generate_signature(self, completion): """Generate signature with function arguments. """ if not hasattr(completion, 'params'): return '' return '%s(%s)' % ( completion.name, ', '.join(param.description for param in completion.params)) def _get_call_signatures(self, script): """Extract call signatures from jedi.api.Script object in failsafe way. Returns: Tuple with original signature object, name and value. """ _signatures = [] try: call_signatures = script.call_signatures() except KeyError: call_signatures = [] for signature in call_signatures: for pos, param in enumerate(signature.params): if not param.name: continue if param.name == 'self' and pos == 0: continue if WORD_RE.match(param.name) is None: continue try: name, value = param.description.split('=') except ValueError: name = param.description value = None if name.startswith('*'): continue _signatures.append((signature, name, value)) return _signatures def _get_call_signatures_with_args(self, script): """Extract call signatures from jedi.api.Script object in failsafe way. Returns: Array with dictionary """ _signatures = [] try: call_signatures = script.call_signatures() except KeyError: call_signatures = [] for signature in call_signatures: sig = {"name": "", "description": "", "docstring": "", "paramindex": 0, "params": [], "bracketstart": []} sig["description"] = signature.description sig["docstring"] = signature.docstring() sig["name"] = signature.name sig["paramindex"] = signature.index sig["bracketstart"].append(signature.index) _signatures.append(sig) for pos, param in enumerate(signature.params): if not param.name: continue if param.name == 'self' and pos == 0: continue if WORD_RE.match(param.name) is None: continue try: name, value = param.description.split('=') except ValueError: name = param.description value = None #if name.startswith('*'): # continue #_signatures.append((signature, name, value)) sig["params"].append({"name": name, "value":value, "docstring":param.docstring(), "description":param.description}) return _signatures def _serialize_completions(self, script, identifier=None, prefix=''): """Serialize response to be read from VSCode. Args: script: Instance of jedi.api.Script object. identifier: Unique completion identifier to pass back to VSCode. prefix: String with prefix to filter function arguments. Used only when fuzzy matcher turned off. Returns: Serialized string to send to VSCode. """ _completions = [] for signature, name, value in self._get_call_signatures(script): if not self.fuzzy_matcher and not name.lower().startswith( prefix.lower()): continue _completion = { 'type': 'property', 'rightLabel': self._additional_info(signature) } # we pass 'text' here only for fuzzy matcher if value: _completion['snippet'] = '%s=${1:%s}$0' % (name, value) _completion['text'] = '%s=%s' % (name, value) else: _completion['snippet'] = '%s=$1$0' % name _completion['text'] = name _completion['displayText'] = name if self.show_doc_strings: _completion['description'] = signature.docstring() else: _completion['description'] = self._generate_signature( signature) _completions.append(_completion) try: completions = script.completions() except KeyError: completions = [] for completion in completions: if self.show_doc_strings: description = completion.docstring() else: description = self._generate_signature(completion) _completion = { 'text': completion.name, 'type': self._get_definition_type(completion), 'description': description, 'rightLabel': self._additional_info(completion) } if any([c['text'].split('=')[0] == _completion['text'] for c in _completions]): # ignore function arguments we already have continue _completions.append(_completion) return json.dumps({'id': identifier, 'results': _completions}) def _serialize_arguments(self, script, identifier=None): """Serialize response to be read from VSCode. Args: script: Instance of jedi.api.Script object. identifier: Unique completion identifier to pass back to VSCode. Returns: Serialized string to send to VSCode. """ return json.dumps({"id": identifier, "results" : self._get_call_signatures_with_args(script) }) def _serialize_definitions(self, definitions, identifier=None): """Serialize response to be read from VSCode. Args: definitions: List of jedi.api.classes.Definition objects. identifier: Unique completion identifier to pass back to VSCode. Returns: Serialized string to send to VSCode. """ def _top_definition(definition): for d in definition.goto_assignments(): if d == definition: continue if d.type == 'import': return _top_definition(d) else: return d return definition _definitions = [] for definition in definitions: try: if definition.module_path: if definition.type == 'import': definition = _top_definition(definition) _definition = { 'text': definition.name, 'type': self._get_definition_type(definition), 'fileName': definition.module_path, 'line': definition.line - 1, 'column': definition.column } _definitions.append(_definition) except Exception as e: pass return json.dumps({'id': identifier, 'results': _definitions}) def _serialize_usages(self, usages, identifier=None): _usages = [] for usage in usages: _usages.append({ 'name': usage.name, 'moduleName': usage.module_name, 'fileName': usage.module_path, 'line': usage.line, 'column': usage.column, }) return json.dumps({'id': identifier, 'results': _usages}) def _deserialize(self, request): """Deserialize request from VSCode. Args: request: String with raw request from VSCode. Returns: Python dictionary with request data. """ return json.loads(request) def _set_request_config(self, config): """Sets config values for current request. This includes sys.path modifications which is getting restored to default value on each request so each project should be isolated from each other. Args: config: Dictionary with config values. """ sys.path = self.default_sys_path self.use_snippets = config.get('useSnippets') self.show_doc_strings = config.get('showDescriptions', True) self.fuzzy_matcher = config.get('fuzzyMatcher', False) jedi.settings.case_insensitive_completion = config.get( 'caseInsensitiveCompletion', True) for path in config.get('extraPaths', []): if path and path not in sys.path: sys.path.insert(0, path) def _process_request(self, request): """Accept serialized request from VSCode and write response. """ request = self._deserialize(request) self._set_request_config(request.get('config', {})) path = self._get_top_level_module(request.get('path', '')) if path not in sys.path: sys.path.insert(0, path) lookup = request.get('lookup', 'completions') if lookup == 'names': return self._write_response(self._serialize_definitions( jedi.api.names( source=request['source'], path=request.get('path', ''), all_scopes=True), request['id'])) script = jedi.api.Script( source=request['source'], line=request['line'] + 1, column=request['column'], path=request.get('path', '')) if lookup == 'definitions': return self._write_response(self._serialize_definitions( script.goto_assignments(), request['id'])) elif lookup == 'arguments': return self._write_response(self._serialize_arguments( script, request['id'])) elif lookup == 'usages': return self._write_response(self._serialize_usages( script.usages(), request['id'])) else: return self._write_response( self._serialize_completions(script, request['id'], request.get('prefix', ''))) def _write_response(self, response): sys.stdout.write(response + '\n') sys.stdout.flush() def watch(self): while True: try: self._process_request(self._input.readline()) except Exception: sys.stderr.write(traceback.format_exc() + '\n') sys.stderr.flush() if __name__ == '__main__': JediCompletion().watch() |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |