Skip to content

Commit a270c3c

Browse files
committed
use peeks in jinja
1 parent 66d8e1d commit a270c3c

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

elixir/web.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -635,23 +635,26 @@ def generate_source_page(ctx, q, project, version, path):
635635
# type: type of the symbol
636636
# path: path of the file that contains the symbol
637637
# line: list of LineWithURL
638-
SymbolEntry = namedtuple('SymbolEntry', 'type, path, lines')
638+
# peeks: map of code line previews for this path
639+
SymbolEntry = namedtuple('SymbolEntry', 'type, path, lines, peeks')
639640

640641
# Converts SymbolInstance into SymbolEntry
641642
# path of SymbolInstance will be appended to base_url
642-
def symbol_instance_to_entry(base_url, symbol):
643+
def symbol_instance_to_entry(base_url, symbol, peeks):
643644
# TODO this should be a responsibility of Query
644645
if type(symbol.line) is str:
645646
line_numbers = symbol.line.split(',')
646647
else:
647648
line_numbers = [symbol.line]
648649

649650
lines = [
650-
LineWithURL(l, f'{ base_url }/{ symbol.path }#L{ l }')
651+
LineWithURL(int(l), f'{ base_url }/{ symbol.path }#L{ l }')
651652
for l in line_numbers
652653
]
653654

654-
return SymbolEntry(symbol.type, symbol.path, lines)
655+
current_peeks = peeks.get(symbol.path, {})
656+
657+
return SymbolEntry(symbol.type, symbol.path, lines, current_peeks)
655658

656659
# Generates response (status code and optionally HTML) of the `ident` route
657660
# ctx: RequestContext
@@ -662,15 +665,16 @@ def generate_ident_page(ctx, q, project, version, family, ident):
662665

663666
source_base_url = get_source_base_url(project, version)
664667

665-
symbol_definitions, symbol_references, symbol_doccomments = q.query('ident', version, ident, family)
668+
symbol_definitions, symbol_references, symbol_doccomments, peeks = q.query('ident', version, ident, family)
666669

667670
symbol_sections = []
671+
empty_peeks = {}
668672

669673
if len(symbol_definitions) or len(symbol_references):
670674
if len(symbol_doccomments):
671675
symbol_sections.append({
672676
'title': 'Documented',
673-
'symbols': {'_unknown': [symbol_instance_to_entry(source_base_url, sym) for sym in symbol_doccomments]},
677+
'symbols': {'_unknown': [symbol_instance_to_entry(source_base_url, sym, empty_peeks) for sym in symbol_doccomments]},
674678
})
675679

676680
if len(symbol_definitions):
@@ -679,9 +683,9 @@ def generate_ident_page(ctx, q, project, version, family, ident):
679683
# TODO this should be a responsibility of Query
680684
for sym in symbol_definitions:
681685
if sym.type not in defs_by_type:
682-
defs_by_type[sym.type] = [symbol_instance_to_entry(source_base_url, sym)]
686+
defs_by_type[sym.type] = [symbol_instance_to_entry(source_base_url, sym, peeks)]
683687
else:
684-
defs_by_type[sym.type].append(symbol_instance_to_entry(source_base_url, sym))
688+
defs_by_type[sym.type].append(symbol_instance_to_entry(source_base_url, sym, peeks))
685689

686690
symbol_sections.append({
687691
'title': 'Defined',
@@ -695,7 +699,7 @@ def generate_ident_page(ctx, q, project, version, family, ident):
695699
if len(symbol_references):
696700
symbol_sections.append({
697701
'title': 'Referenced',
698-
'symbols': {'_unknown': [symbol_instance_to_entry(source_base_url, sym) for sym in symbol_references]},
702+
'symbols': {'_unknown': [symbol_instance_to_entry(source_base_url, sym, peeks) for sym in symbol_references]},
699703
})
700704
else:
701705
symbol_sections.append({

templates/ident.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ <h2>{{ section['title'] }} in {{ symbols|length }} files
3333
<li>
3434
<a href="{{ symbol.lines[0].url }}">
3535
<strong>{{ symbol.path }}</strong>
36-
{%- if symbol.lines|length == 1 %},
36+
{%- if symbol.lines|length == 1 and symbol.peeks|length == 0 %},
3737
line {{ symbol.lines[0].lineno }}
3838
{%- elif symbols|length > 100 %},
3939
<em>{{ symbol.lines|length }} times</em>
@@ -42,10 +42,14 @@ <h2>{{ section['title'] }} in {{ symbols|length }} files
4242
<em>(as a {{ symbol.type }})</em>
4343
{% endif %}
4444
</a>
45-
{% if symbol.lines|length > 1 and symbols|length <= 100 %}
45+
{% if (symbol.lines|length > 1 or symbol.peeks|length > 0) and symbols|length <= 100 %}
4646
<ul>
4747
{% for line, url in symbol.lines %}
48-
<li><a href="{{ url }}">line {{ line }}</a>
48+
<li><a href="{{ url }}"><span>line {{ line }}</span>
49+
{% if line in symbol.peeks %}
50+
<pre>{{ symbol.peeks[line] }}</pre>
51+
{% endif %}
52+
</a></li>
4953
{% endfor %}
5054
</ul>
5155
{% endif %}

0 commit comments

Comments
 (0)