|
1 | | -from typing import List, Optional, TypedDict |
| 1 | +from typing import Callable, List, Optional, TypedDict |
2 | 2 |
|
3 | 3 |
|
4 | 4 | class SymbolLoc(TypedDict): |
@@ -47,3 +47,43 @@ def truncate_for_llm(text: str, char_limit: int): |
47 | 47 | if len(text) > char_limit: |
48 | 48 | return text[:char_limit] + "[...too long...]" |
49 | 49 | return text |
| 50 | + |
| 51 | + |
| 52 | +def custom_number_group_of_lines( |
| 53 | + group: List[str], |
| 54 | + first: int, |
| 55 | + strip: bool = True, |
| 56 | + with_note: Optional[Callable[[int], str]] = None, |
| 57 | +) -> str: |
| 58 | + """ |
| 59 | + Add line numbers for each line in the input list. |
| 60 | +
|
| 61 | + Args: |
| 62 | + group (List[str]): The lines to number. |
| 63 | + first (int): The number for the first line. |
| 64 | + strip (bool): Whether to strip leading and trailing blank lines. |
| 65 | +
|
| 66 | + Returns: |
| 67 | + A string concatenation of the numbered lines. |
| 68 | + """ |
| 69 | + if strip: |
| 70 | + while group and not group[0].strip(): |
| 71 | + group = group[1:] |
| 72 | + first += 1 |
| 73 | + while group and not group[-1].strip(): |
| 74 | + group = group[:-1] |
| 75 | + |
| 76 | + last = first + len(group) - 1 |
| 77 | + max_line_number_length = len(str(last)) |
| 78 | + result = "\n".join( |
| 79 | + [ |
| 80 | + "{0:>{1}} {2}{3}".format( |
| 81 | + first + i, |
| 82 | + max_line_number_length, |
| 83 | + line, |
| 84 | + with_note(first + i) if with_note else "", |
| 85 | + ) |
| 86 | + for i, line in enumerate(group) |
| 87 | + ] |
| 88 | + ) |
| 89 | + return result |
0 commit comments