def pyls_definitions(config, document, position): settings = config.plugin_settings('jedi_definition') definitions = document.jedi_script(position).goto_assignments( follow_imports=settings.get('follow_imports', False), follow_builtin_imports=settings.get('follow_builtin_imports', False)) definitions = [ d for d in definitions if d.is_definition() and d.line is not None and d.column is not None and d.module_path is not None ] return [{ 'uri': uris.translate_to_client_uri( uris.uri_with(document.uri, path=d.module_path)), 'range': { 'start': { 'line': d.line - 1, 'character': d.column }, 'end': { 'line': d.line - 1, 'character': d.column + len(d.name) } } } for d in definitions]
def pyls_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name) kwargs = _utils.position_to_jedi_linecolumn(document, position) kwargs['new_name'] = new_name try: refactoring = document.jedi_script().rename(**kwargs) except NotImplementedError: raise Exception('No support for renaming in Python 2/3.5 with Jedi. ' 'Consider using the rope_rename plugin instead') log.debug('Finished rename: %s', refactoring.get_diff()) return { 'documentChanges': [ { 'textDocument': { 'uri': uris.uri_with(document.uri, path=file_path), 'version': workspace.get_document(document.uri).version, }, 'edits': [ { 'range': { 'start': {'line': 0, 'character': 0}, 'end': { 'line': _num_lines(changed_file.get_new_code()), 'character': 0, }, }, 'newText': changed_file.get_new_code(), } ], } for file_path, changed_file in refactoring.get_changed_files().items() ], }
def pyls_references(document, position, exclude_declaration=False): # Note that usages is not that great in a lot of cases: https://github.com/davidhalter/jedi/issues/744 usages = document.jedi_script(position).usages() if exclude_declaration: # Filter out if the usage is the actual declaration of the thing usages = [d for d in usages if not d.is_definition()] # Filter out builtin modules usages = [d for d in usages if not d.in_builtin_module()] return [{ 'uri': uris.uri_with(document.uri, path=d.module_path) if d.module_path else document.uri, 'range': { 'start': { 'line': d.line - 1, 'character': d.column }, 'end': { 'line': d.line - 1, 'character': d.column + len(d.name) } } } for d in usages]
def pyls_rename(config, workspace, document, position, new_name): rope_config = config.settings(document_path=document.path).get('rope', {}) rope_project = workspace._rope_project_builder(rope_config) rename = Rename(rope_project, libutils.path_to_resource(rope_project, document.path), document.offset_at_position(position)) log.debug("Executing rename of %s to %s", document.word_at_position(position), new_name) changeset = rename.get_changes(new_name, in_hierarchy=True, docs=True) log.debug("Finished rename: %s", changeset.changes) return { 'documentChanges': [{ 'textDocument': { 'uri': uris.uri_with(document.uri, path=os.path.join(workspace.root_path, change.resource.path)), }, 'edits': [{ 'range': { 'start': { 'line': 0, 'character': 0 }, 'end': { 'line': sys.maxsize, 'character': 0 }, }, 'newText': change.new_contents }] } for change in changeset.changes] }
def conv_loc(config, document, loc): if not loc: return path = make_abs_bess_filename(config, document, loc['file']) return { 'uri': uris.uri_with(document.uri, path=path), 'range': { 'start': {'line': loc['line'] - 1, 'character': 0}, 'end': {'line': loc['line'] - 1, 'character': 0} } }
def pyls_definitions(document, position): definitions = document.jedi_script(position).goto_assignments(follow_imports=True) definitions = [ d for d in definitions if d.is_definition() and d.line is not None and d.column is not None and d.module_path is not None ] return [{ 'uri': uris.uri_with(document.uri, path=d.module_path), 'range': { 'start': {'line': d.line - 1, 'character': d.column}, 'end': {'line': d.line - 1, 'character': d.column + len(d.name)} } } for d in definitions]
def pyls_definitions(config, document, position): params = {k: v for k, v in config.plugin_settings('jedi_definition').items() if v is not None} definitions = document.jedi_script(position).goto_assignments(**params) definitions = [ d for d in definitions if d.is_definition() and d.line is not None and d.column is not None and d.module_path is not None ] return [{ 'uri': uris.uri_with(document.uri, path=d.module_path), 'range': { 'start': {'line': d.line - 1, 'character': d.column}, 'end': {'line': d.line - 1, 'character': d.column + len(d.name)} } } for d in definitions]
def pyls_definitions(config, document, position): settings = config.plugin_settings('jedi_definition') definitions = document.jedi_script(position).goto_assignments( follow_imports=settings.get('follow_imports', True), follow_builtin_imports=settings.get('follow_builtin_imports', True)) return [{ 'uri': uris.uri_with(document.uri, path=d.module_path), 'range': { 'start': { 'line': d.line - 1, 'character': d.column }, 'end': { 'line': d.line - 1, 'character': d.column + len(d.name) }, } } for d in definitions if d.is_definition() and _not_internal_definition(d)]
def insert_bess_refs(config, document, goto_kind, refs): ref_groups = collections.defaultdict(list) mclass_uri = uris.uri_with(document.uri, path=get_mpath('mclass.py')) db = get_mclass_db() for ref in refs: if not (ref['uri'] == mclass_uri): ref_groups['project'].append(ref) continue ref_groups['mclass'].append(ref) mline = ref['range']['start']['line'] for mclass in db['mclass']: for cmd in [mclass] + mclass['cmds']: if mline == cmd.get('line', 0) - 1: break else: continue ref = conv_loc(config, document, cmd['definition']) ref_groups['cpp_definition'].append(ref) loc = db['msg'].get(cmd['arg'], {}) ref = conv_loc(config, document, loc) ref_groups['protobuf'].append(ref) loc = db['msg_full'].get(cmd.get('return')) ref = conv_loc(config, document, loc) if ref and ref not in ref_groups['protobuf']: ref_groups['protobuf'].append(ref) for loc in cmd.get('examples', []): ref = conv_loc(config, document, loc) ref_groups['examples'].append(ref) refs = [] for ref_type in get_ref_types(config, document, goto_kind): refs += ref_groups[ref_type] return refs
def pyls_references(document, position, exclude_declaration=False): code_position = _utils.position_to_jedi_linecolumn(document, position) usages = document.jedi_script().get_references(**code_position) if exclude_declaration: # Filter out if the usage is the actual declaration of the thing usages = [d for d in usages if not d.is_definition()] # Filter out builtin modules return [{ 'uri': uris.uri_with(document.uri, path=str(d.module_path)) if d.module_path else document.uri, 'range': { 'start': { 'line': d.line - 1, 'character': d.column }, 'end': { 'line': d.line - 1, 'character': d.column + len(d.name) } } } for d in usages if not d.in_builtin_module()]
def test_uri_with(uri, kwargs, new_uri): assert uris.uri_with(uri, **kwargs) == new_uri
def local_to_document(definition): return not definition.module_path or uris.uri_with( document.uri, path=definition.module_path) == document.uri
def pyls_used_document_symbols(config, document): all_scopes = config.plugin_settings('jedi_symbols').get('all_scopes', True) references = document.jedi_names(all_scopes=all_scopes, definitions=False, references=True) out = [] for ref in references: if not _include_ref(ref): continue # log.debug(f"{pformat(ref._name)}, {pformat(dir(ref._name))}, {pformat(vars(ref._name))}") # log.debug(f"{pformat(ref._name.tree_name)}") # log.debug(f"{pformat(ref._name.tree_name.start_pos)}") # log.debug(f"{pformat(ref._name.tree_name.end_pos)}") # log.debug(f"{pformat(ref._name.tree_name.parent)}") definitions = ref.goto_assignments() first_def = definitions[0] if (len(definitions) > 0) else None def_type = first_def.type if first_def else None def_module = first_def.module_name if first_def else None def_range = None try: def_range = _def_range(first_def) except: # TODO: fix this. first_def could be None, or the def range could be in another file. log.debug( f"{pformat(ref._name)}, {pformat(dir(ref._name))}, {pformat(vars(ref._name))}" ) log.debug(f"{pformat(ref._name.tree_name)}") log.debug(f"{pformat(ref._name.tree_name.start_pos)}") log.debug(f"{pformat(ref._name.tree_name.end_pos)}") log.debug(f"{pformat(ref._name.tree_name.parent)}") continue def_uri = uris.uri_with( document.uri, path=first_def.module_path) if first_def else None out.append({ 'name': first_def.name if first_def else ref.name, 'containerName': _container(first_def), 'location': { 'uri': def_uri, 'range': def_range, }, 'kind': _SYMBOL_KIND_MAP.get(def_type), 'rayBensModule': def_module, 'rayBensBuiltin': _is_internal_definition(first_def) if first_def else False, # TODO: rayBensUsageLocation instead? 'rayBensUsageRange': _ref_range(ref), }) return out