Skip to content

Commit

Permalink
🐛 Pass needs to highlight filter of graphviz needflow (#1274)
Browse files Browse the repository at this point in the history
In a minor oversight from #1235, the `needs` list was not passed to the `highlight` filter, as is the case for the `plantuml` engine.

The opportunity was also taken to improve `_plantuml`, by passing the `NeedsView` to the sub-functions, rather than just the dict values.
  • Loading branch information
chrisjsewell authored Sep 6, 2024
1 parent 03b4892 commit 73f1e1b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
19 changes: 13 additions & 6 deletions sphinx_needs/directives/needflow/_graphviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from sphinx.util.logging import getLogger

from sphinx_needs.config import LinkOptionsType, NeedsSphinxConfig
from sphinx_needs.data import NeedsInfoType, SphinxNeedsData
from sphinx_needs.data import NeedsInfoType, NeedsView, SphinxNeedsData
from sphinx_needs.debug import measure_time
from sphinx_needs.diagrams_common import calculate_link
from sphinx_needs.directives.needflow._directive import NeedflowGraphiz
Expand Down Expand Up @@ -49,7 +49,7 @@ def process_needflow_graphviz(
) -> None:
needs_config = NeedsSphinxConfig(app.config)
env_data = SphinxNeedsData(app.env)
all_needs = env_data.get_needs_view()
needs_view = env_data.get_needs_view()

link_type_names = [link["option"].upper() for link in needs_config.extra_links]
allowed_link_types_options = [link.upper() for link in needs_config.flow_link_types]
Expand Down Expand Up @@ -111,14 +111,14 @@ def process_needflow_graphviz(

init_filtered_needs = (
filter_by_tree(
all_needs,
needs_view,
root_id,
allowed_link_types,
attributes["root_direction"],
attributes["root_depth"],
)
if (root_id := attributes["root_id"])
else all_needs
else needs_view
)
filtered_needs = process_filters(
app,
Expand Down Expand Up @@ -156,6 +156,7 @@ def process_needflow_graphviz(
content += _render_node(
root_need,
node,
needs_view,
needs_config,
lambda n: calculate_link(app, n, fromdocname, relative="."),
id_comp_to_need,
Expand Down Expand Up @@ -202,6 +203,7 @@ def _quote(text: str) -> str:
def _render_node(
need: NeedsInfoType,
node: NeedflowGraphiz,
needs_view: NeedsView,
config: NeedsSphinxConfig,
calc_link: Callable[[NeedsInfoType], str],
id_comp_to_need: dict[str, NeedsInfoType],
Expand All @@ -220,7 +222,7 @@ def _render_node(
# graphviz cannot nest nodes,
# so we have to create a subgraph to represent a need with parts/children
return _render_subgraph(
need, node, config, calc_link, id_comp_to_need, rendered_nodes
need, node, needs_view, config, calc_link, id_comp_to_need, rendered_nodes
)

rendered_nodes[need["id_complete"]] = {"need": need, "cluster_id": None}
Expand Down Expand Up @@ -258,7 +260,9 @@ def _render_node(
params.append(("fillcolor", _quote(need["type_color"])))

# outline color
if node["highlight"] and filter_single_need(need, config, node["highlight"]):
if node["highlight"] and filter_single_need(
need, config, node["highlight"], needs_view.values()
):
params.append(("color", "red"))
elif node["border_color"]:
color = match_variants(
Expand All @@ -278,6 +282,7 @@ def _render_node(
def _render_subgraph(
need: NeedsInfoType,
node: NeedflowGraphiz,
needs_view: NeedsView,
config: NeedsSphinxConfig,
calc_link: Callable[[NeedsInfoType], str],
id_comp_to_need: dict[str, NeedsInfoType],
Expand Down Expand Up @@ -340,6 +345,7 @@ def _render_subgraph(
_render_node(
id_comp_to_need[need_part_id],
node,
needs_view,
config,
calc_link,
id_comp_to_need,
Expand All @@ -356,6 +362,7 @@ def _render_subgraph(
_render_node(
id_comp_to_need[child_need_id],
node,
needs_view,
config,
calc_link,
id_comp_to_need,
Expand Down
28 changes: 14 additions & 14 deletions sphinx_needs/directives/needflow/_plantuml.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import html
import os
from functools import lru_cache
from typing import Iterable

from docutils import nodes
from jinja2 import Template
Expand All @@ -16,6 +15,7 @@
from sphinx_needs.data import (
NeedsFlowType,
NeedsInfoType,
NeedsView,
SphinxNeedsData,
)
from sphinx_needs.debug import measure_time
Expand Down Expand Up @@ -46,7 +46,7 @@ def get_need_node_rep_for_plantuml(
app: Sphinx,
fromdocname: str,
current_needflow: NeedsFlowType,
all_needs: Iterable[NeedsInfoType],
needs_view: NeedsView,
need_info: NeedsInfoType,
) -> str:
"""Calculate need node representation for plantuml."""
Expand All @@ -63,7 +63,7 @@ def get_need_node_rep_for_plantuml(
node_colors.append(need_info["type_color"].replace("#", ""))

if current_needflow["highlight"] and filter_single_need(
need_info, needs_config, current_needflow["highlight"], all_needs
need_info, needs_config, current_needflow["highlight"], needs_view.values()
):
node_colors.append("line:FF0000")

Expand Down Expand Up @@ -98,7 +98,7 @@ def walk_curr_need_tree(
app: Sphinx,
fromdocname: str,
current_needflow: NeedsFlowType,
all_needs: Iterable[NeedsInfoType],
needs_view: NeedsView,
found_needs: list[NeedsInfoType],
need: NeedsInfoType,
) -> str:
Expand All @@ -125,7 +125,7 @@ def walk_curr_need_tree(
if need_part_id == found_need["id_complete"]:
curr_need_tree += (
get_need_node_rep_for_plantuml(
app, fromdocname, current_needflow, all_needs, found_need
app, fromdocname, current_needflow, needs_view, found_need
)
+ "\n"
)
Expand All @@ -140,15 +140,15 @@ def walk_curr_need_tree(
for curr_child_need in found_needs:
if curr_child_need["id_complete"] == curr_child_need_id:
curr_need_tree += get_need_node_rep_for_plantuml(
app, fromdocname, current_needflow, all_needs, curr_child_need
app, fromdocname, current_needflow, needs_view, curr_child_need
)
# check curr need child has children or has parts
if curr_child_need["parent_needs_back"] or curr_child_need["parts"]:
curr_need_tree += walk_curr_need_tree(
app,
fromdocname,
current_needflow,
all_needs,
needs_view,
found_needs,
curr_child_need,
)
Expand All @@ -166,23 +166,23 @@ def cal_needs_node(
app: Sphinx,
fromdocname: str,
current_needflow: NeedsFlowType,
all_needs: Iterable[NeedsInfoType],
needs_view: NeedsView,
found_needs: list[NeedsInfoType],
) -> str:
"""Calculate and get needs node representaion for plantuml including all child needs and need parts."""
top_needs = get_root_needs(found_needs)
curr_need_tree = ""
for top_need in top_needs:
top_need_node = get_need_node_rep_for_plantuml(
app, fromdocname, current_needflow, all_needs, top_need
app, fromdocname, current_needflow, needs_view, top_need
)
curr_need_tree += (
top_need_node
+ walk_curr_need_tree(
app,
fromdocname,
current_needflow,
all_needs,
needs_view,
found_needs,
top_need,
)
Expand All @@ -203,7 +203,7 @@ def process_needflow_plantuml(
env = app.env
needs_config = NeedsSphinxConfig(app.config)
env_data = SphinxNeedsData(env)
all_needs = env_data.get_needs_view()
needs_view = env_data.get_needs_view()

link_type_names = [link["option"].upper() for link in needs_config.extra_links]
allowed_link_types_options = [link.upper() for link in needs_config.flow_link_types]
Expand Down Expand Up @@ -265,14 +265,14 @@ def process_needflow_plantuml(

need_values = (
filter_by_tree(
all_needs,
needs_view,
root_id,
allowed_link_types,
current_needflow["root_direction"],
current_needflow["root_depth"],
)
if (root_id := current_needflow.get("root_id"))
else all_needs
else needs_view
)

found_needs = process_filters(
Expand Down Expand Up @@ -310,7 +310,7 @@ def process_needflow_plantuml(

puml_node["uml"] += "\n' Nodes definition \n\n"
puml_node["uml"] += cal_needs_node(
app, fromdocname, current_needflow, all_needs.values(), found_needs
app, fromdocname, current_needflow, needs_view, found_needs
)

puml_node["uml"] += "\n' Connection definition \n\n"
Expand Down

0 comments on commit 73f1e1b

Please sign in to comment.