Skip to content

Commit

Permalink
Convert matched_snippet spans in CodebaseResourceDetailView to line n…
Browse files Browse the repository at this point in the history
…umbers

Signed-off-by: Jono Yang <[email protected]>
  • Loading branch information
JonoYang committed Jan 24, 2025
1 parent 3d464f0 commit b7d7d21
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
import xlsxwriter
from django_filters.views import FilterView
from packageurl.contrib.django.models import PACKAGE_URL_FIELDS
from licensedcode.spans import Span
from matchcode_toolkit.fingerprinting import get_line_by_pos

from scancodeio.auth import ConditionalLoginRequired
from scancodeio.auth import conditional_login_required
Expand Down Expand Up @@ -1931,6 +1933,28 @@ def get_context_data(self, **kwargs):
annotations = self.get_annotations(getattr(resource, field_name), value_key)
context["detected_values"][field_name] = annotations

# convert qspan from list of ints to Spans
matched_snippet_annotations = []
matched_snippets = resource.extra_data.get("matched_snippets")
if matched_snippets:
# tokenize file content and map tokens to line numbers
line_by_pos = get_line_by_pos(resource.file_content)
last_pos = max(line_by_pos, key=line_by_pos.get)
for matched_snippet in matched_snippets:
qspan = Span(matched_snippet["qspan"])
matched_snippet_annotations.append
matched_snippet["qspan"] = qspan
for span in qspan.subspans():
# Convert qstart and qends to start_line and end_lines
end = min(span.end, last_pos)
matched_snippet_annotations.append(
{
"start_line": line_by_pos[span.start],
"end_line": line_by_pos[end],
}
)

context["detected_values"]["matched_snippets"] = matched_snippet_annotations
return context


Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ install_requires =
# Font Awesome
fontawesomefree==6.6.0
# MatchCode-toolkit
matchcode-toolkit @ git+https://github.com/aboutcode-org/matchcode-toolkit.git@3881f224d4ee56e95b3c15e34673c6844c07f4f5
matchcode-toolkit @ git+https://github.com/aboutcode-org/matchcode-toolkit.git@007ca57509ab73dda391af8631616b59e35eb9ef
# Univers
univers==30.12.1
# Markdown
Expand Down

0 comments on commit b7d7d21

Please sign in to comment.