Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 20 additions & 25 deletions dxr/plugins/rust/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def needles_by_line(self):
def refs(self):
classes_and_tables = [(refs.FunctionRef, 'functions'),
(refs.FunctionRefRef, 'function_refs'),
(refs.MacroRef, "macros"),
(refs.MacroRefRef, "macro_refs"),
(refs.VariableRef, 'variables'),
(refs.VariableRefRef, 'variable_refs'),
(refs.TypeRef, 'types'),
Expand Down Expand Up @@ -105,6 +107,8 @@ def all_needles(self):
return iterable_per_line(with_start_and_end(split_into_lines(chain(
self.file_needles('function', 'functions'),
self.file_needles('function_ref', 'function_refs'),
self.file_needles('macro', 'macros'),
self.file_needles('macro_ref', 'macro_refs'),
self.file_needles('var', 'variables'),
self.file_needles('var_ref', 'variable_refs'),
self.file_needles('type', 'types'),
Expand Down Expand Up @@ -262,6 +266,8 @@ def __init__(self):
self.variable_refs = []
self.functions = {}
self.function_refs = []
self.macros = {}
self.macro_refs = []
self.types = {}
self.type_refs = []
self.impl_defs = {}
Expand Down Expand Up @@ -402,7 +408,6 @@ def process_csv_second_pass(self, path):

# We need to do this once per crate whilst the current crate is still current
self.generate_scopes()
self.std_hack()

def process_csv(self, file_name, header_only):
try:
Expand Down Expand Up @@ -630,30 +635,6 @@ def generate_scopes(self):

self.mod_parents = {}

def std_hack(self):
# This is nasty - Rust implicitly includes the standard library,
# crate `std`, but without generating an `extern crate` item, so we need
# to do that. However, it is possible the project includes some other crate
# called `std` (by building without the standard lib, we can't tell from
# the indexing data which is the case), so we need to check in case there
# is one already.
# We probably wouldn't need this if we dealt with generated code properly
# in the compiler indexing.
if 'std' not in self.data.index('module_aliases', 'name').keys():
id = next_id()
scopeid = self.find_id_cur('0')
args = {
'name': 'std',
'location': 'std',
'id': id,
'scopeid': scopeid,
# Jesus, this is fragile
'crate': '1',
'qualname': str(scopeid) + '$std',
'refid': self.crate_map[1][1]['id']
}
self.data.module_aliases[id] = args


def closure(self, input):
""" Compute the (non-refexive) transitive closure of a list."""
Expand Down Expand Up @@ -857,6 +838,11 @@ def process_function(args, tree):
process_function_impl(args, tree)


def process_macro(args, tree):
tree.data.macros[args['qualname']] = args
tree.add_to_lines(args, ('macros', args))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Just a nit: 1 skipped line between methods of a class)


def process_method_decl(args, tree):
process_function_impl(args, tree)

Expand Down Expand Up @@ -909,6 +895,13 @@ def process_fn_call(args, tree):
tree.add_to_lines(args, ('function_refs', args))


def process_macro_use(args, tree):
# Need to insert a name field for use in the line needle
args['name'] = args['callee_name']
tree.data.macro_refs.append(args)
tree.add_to_lines(args, ('macro_refs', args))


def process_var_ref(args, tree):
if tree.add_external_item(args):
return;
Expand Down Expand Up @@ -1054,6 +1047,8 @@ def process_end_external_crates(args, tree):
'properties': {
'rust_function': QUALIFIED_LINE_NEEDLE,
'rust_function_ref': QUALIFIED_LINE_NEEDLE,
'rust_macro': QUALIFIED_LINE_NEEDLE,
'rust_macro_ref': QUALIFIED_LINE_NEEDLE,
'rust_var': QUALIFIED_LINE_NEEDLE,
'rust_var_ref': QUALIFIED_LINE_NEEDLE,
'rust_type': QUALIFIED_LINE_NEEDLE,
Expand Down
10 changes: 10 additions & 0 deletions dxr/plugins/rust/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ class FunctionRefFilter(_QualifiedNameFilter):
is_reference = True
description = 'Function or method references'

class MacroFilter(_QualifiedNameFilter):
name = 'macro'
is_identifier = True
description = Markup('Macro definition: <code>macro rules! foo</code>')

class MacroRefFilter(_QualifiedNameFilter):
name = 'macro-ref'
is_reference = True
description = Markup('Macro callers: <code>foo!</code>')

class CallersFilter(_QualifiedNameFilter):
name = 'callers'
is_reference = True
Expand Down
1 change: 1 addition & 0 deletions dxr/plugins/rust/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def jump_to_target_from_decl(menu_maker, tree, decl):
jump_to_type_declaration_menu_item = partial(jump_to_target_menu_item, target_name='type declaration')
jump_to_variable_declaration_menu_item = partial(jump_to_target_menu_item, target_name='variable declaration')
jump_to_function_declaration_menu_item = partial(jump_to_target_menu_item, target_name='function declaration')
jump_to_macro_definition_menu_item = partial(jump_to_target_menu_item, target_name='macro definition')


def trait_impl_menu_item(tree_config, qualname, count):
Expand Down
25 changes: 24 additions & 1 deletion dxr/plugins/rust/refs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
jump_to_alias_definition_menu_item, jump_to_crate_menu_item, find_references_menu_item,
std_lib_links_menu, jump_to_module_declaration_menu_item, jump_to_type_declaration_menu_item,
jump_to_variable_declaration_menu_item, jump_to_function_declaration_menu_item,
trait_impl_menu_item)
trait_impl_menu_item, jump_to_macro_definition_menu_item)


def trim_dict(dictionary, keys):
Expand Down Expand Up @@ -126,6 +126,29 @@ def menu_items(self):
return menu


class MacroRef(_RustRef):
def prepare_menu_data(self, tree_index, datum):
self.hover = datum['qualname']
return trim_dict(datum, ['qualname'])

def menu_items(self):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here and below we also want to add the generic_variable_menu items

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite following - that shouldn't have any impact on macros.

Unless you mean you want to be able to jump to macro references from a macro reference?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, sorry, should have looked that up, it wasn't what I was expecting. Anyway, yeah, I guess you should add find_references_... to RefRef

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return [find_references_menu_item(self.tree, self.menu_data["qualname"], "macro-ref", "macro")]

class MacroRefRef(_RustRef):
def prepare_menu_data(self, tree_index, datum):
if datum['qualname'] and datum['qualname'] in tree_index.data.macros:
self.hover = datum['qualname']
mac = tree_index.data.macros[datum['qualname']]
return trim_dict(mac, ['file_name', 'file_line', 'qualname'])

def menu_items(self):
if self.menu_data:
menu = [jump_to_target_from_decl(jump_to_macro_definition_menu_item, self.tree, self.menu_data)]
menu.append(find_references_menu_item(self.tree, self.menu_data["qualname"], "macro-ref", "macro"))
return menu
return []


class VariableRef(_KeysFromDatum):
keys = ['type', 'qualname']

Expand Down