|
30 | 30 |
|
31 | 31 |
|
32 | 32 | def print_contextual_diff_hunk(diff_hunk, comment_position, context_lines_count):
|
33 |
| - if not diff_hunk or not diff_hunk.strip(): # Handle empty or whitespace-only diff_hunk |
| 33 | + if not diff_hunk or not diff_hunk.strip(): |
34 | 34 | print("(No diff hunk available or content is empty)")
|
35 | 35 | return
|
36 | 36 |
|
37 |
| - hunk_lines = diff_hunk.split('\n') |
| 37 | + hunk_lines = diff_hunk.split('\n') # Note: Python's split('\n') is generally fine. |
38 | 38 |
|
39 |
| - # comment_position is 1-indexed from GitHub API. If None, or context is 0, print full hunk. |
40 |
| - if context_lines_count == 0 or comment_position is None or comment_position < 1 or comment_position > len(hunk_lines): |
| 39 | + # Case 1: User explicitly wants the full hunk |
| 40 | + if context_lines_count == 0: |
41 | 41 | print(diff_hunk)
|
42 | 42 | return
|
43 | 43 |
|
44 |
| - comment_line_index = comment_position - 1 # Convert to 0-indexed for list access |
| 44 | + # Case 2: Contextual display is requested (context_lines_count > 0), |
| 45 | + # but comment is not on a specific line or position is invalid for contextual display. |
| 46 | + if comment_position is None or comment_position < 1 or comment_position > len(hunk_lines): |
| 47 | + print("(Comment is not on a specific line in the diff, or position is invalid; full hunk context suppressed by --context-lines setting)") |
| 48 | + # As an alternative to the above message, if the hunk is small, one might choose to print it. |
| 49 | + # However, sticking to the user's feedback of not wanting full hunks when context is specified: |
| 50 | + # print(diff_hunk) # This would be the old behavior for this case. |
| 51 | + return |
| 52 | + |
| 53 | + # Case 3: Contextual display is possible and requested |
| 54 | + comment_line_index = comment_position - 1 # Convert to 0-indexed |
45 | 55 |
|
46 | 56 | start_index = max(0, comment_line_index - context_lines_count)
|
47 | 57 | end_index = min(len(hunk_lines), comment_line_index + context_lines_count + 1)
|
48 | 58 |
|
49 |
| - # Ensure start_index is not greater than comment_line_index, in case of small hunks |
50 |
| - # This also means that if comment_line_index is valid, start_index will be <= comment_line_index |
51 |
| - start_index = min(start_index, comment_line_index if comment_line_index >=0 else 0) |
52 |
| - |
| 59 | + # The following line was identified as redundant and is removed: |
| 60 | + # start_index = min(start_index, comment_line_index if comment_line_index >=0 else 0) |
53 | 61 |
|
54 | 62 | for i in range(start_index, end_index):
|
55 |
| - # Basic safety for i, though start/end logic should make this robust |
| 63 | + # Robust check, though start/end logic should prevent out-of-bounds |
56 | 64 | if i >= 0 and i < len(hunk_lines):
|
57 | 65 | prefix = "> " if i == comment_line_index else " "
|
58 | 66 | print(f"{prefix}{hunk_lines[i]}")
|
59 |
| - # else: # This case should ideally not be reached with correct boundary conditions |
60 |
| - # print(f" Error: Skipped line index {i} in hunk processing due to boundary issue.") |
61 | 67 |
|
62 | 68 |
|
63 | 69 | def main():
|
|
0 commit comments