Skip to content

Commit c715368

Browse files
committed
improve code snippet preview by showing more context around and enabling word wrapping
1 parent 6b00b61 commit c715368

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

cycode/cli/printers/rich_printer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ def _print_violation_card(
111111
detection,
112112
document,
113113
obfuscate=not self.show_secret,
114+
lines_to_display_before=3,
115+
lines_to_display_after=3,
114116
),
115117
title=':computer: Code Snippet',
116118
)

cycode/cli/printers/utils/code_snippet_syntax.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import math
21
from typing import TYPE_CHECKING
32

43
from rich.syntax import Syntax
@@ -13,8 +12,8 @@
1312
from cycode.cyclient.models import Detection
1413

1514

16-
def _get_code_segment_start_line(detection_line: int, lines_to_display: int) -> int:
17-
start_line = detection_line - math.ceil(lines_to_display / 2)
15+
def _get_code_segment_start_line(detection_line: int, lines_to_display_before: int) -> int:
16+
start_line = detection_line - lines_to_display_before
1817
return 0 if start_line < 0 else start_line
1918

2019

@@ -27,17 +26,24 @@ def get_detection_line(scan_type: str, detection: 'Detection') -> int:
2726

2827

2928
def _get_code_snippet_syntax_from_file(
30-
scan_type: str, detection: 'Detection', document: 'Document', lines_to_display: int, obfuscate: bool
29+
scan_type: str,
30+
detection: 'Detection',
31+
document: 'Document',
32+
lines_to_display_before: int,
33+
lines_to_display_after: int,
34+
obfuscate: bool,
3135
) -> Syntax:
3236
detection_details = detection.detection_details
3337
detection_line = get_detection_line(scan_type, detection)
34-
start_line_index = _get_code_segment_start_line(detection_line, lines_to_display)
38+
start_line_index = _get_code_segment_start_line(detection_line, lines_to_display_before)
3539
detection_position = get_position_in_line(document.content, detection_details.get('start_position', -1))
3640
violation_length = detection_details.get('length', -1)
3741

3842
code_lines_to_render = []
3943
document_content_lines = document.content.splitlines()
40-
for line_index in range(lines_to_display):
44+
total_lines_to_display = lines_to_display_before + 1 + lines_to_display_after
45+
46+
for line_index in range(total_lines_to_display):
4147
current_line_index = start_line_index + line_index
4248
if current_line_index >= len(document_content_lines):
4349
break
@@ -57,6 +63,7 @@ def _get_code_snippet_syntax_from_file(
5763
code=code_to_render,
5864
lexer=Syntax.guess_lexer(document.path, code=code_to_render),
5965
line_numbers=True,
66+
word_wrap=True,
6067
dedent=True,
6168
tab_size=2,
6269
start_line=start_line_index + 1,
@@ -97,11 +104,14 @@ def get_code_snippet_syntax(
97104
command_scan_type: str,
98105
detection: 'Detection',
99106
document: 'Document',
100-
lines_to_display: int = 3,
107+
lines_to_display_before: int = 1,
108+
lines_to_display_after: int = 1,
101109
obfuscate: bool = True,
102110
) -> Syntax:
103111
if is_git_diff_based_scan(scan_type, command_scan_type):
104112
# it will return syntax with just one line
105113
return _get_code_snippet_syntax_from_git_diff(scan_type, detection, document, obfuscate)
106114

107-
return _get_code_snippet_syntax_from_file(scan_type, detection, document, lines_to_display, obfuscate)
115+
return _get_code_snippet_syntax_from_file(
116+
scan_type, detection, document, lines_to_display_before, lines_to_display_after, obfuscate
117+
)

0 commit comments

Comments
 (0)