• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

basilisp-lang / basilisp / 10692400497

03 Sep 2024 11:46PM CUT coverage: 98.898%. First build
10692400497

Pull #1031

github

web-flow
Merge f373f79c9 into 7f1b75ad7
Pull Request #1031: Fix a bug with using fields with special characters in records

1870 of 1877 branches covered (99.63%)

Branch coverage included in aggregate %.

8542 of 8651 relevant lines covered (98.74%)

0.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.35
/src/basilisp/lang/source.py
1
import itertools
1✔
2
import linecache
1✔
3
import os
1✔
4
from typing import Iterable, List, Optional
1✔
5

6
try:
1✔
7
    import pygments.formatters
1✔
8
    import pygments.lexers
1✔
9
    import pygments.styles
1✔
10
except ImportError:  # pragma: no cover
11

12
    def _format_source(s: str) -> str:
13
        return f"{s}{os.linesep}"
14

15
else:
16

17
    def _get_formatter_name() -> Optional[str]:  # pragma: no cover
18
        """Get the Pygments formatter name for formatting the source code by
19
        inspecting various environment variables set by terminals.
20

21
        If `BASILISP_NO_COLOR` is set to a truthy value, use no formatting."""
22
        if os.environ.get("BASILISP_NO_COLOR", "false").lower() in {"1", "true"}:
23
            return None
24
        elif os.environ.get("COLORTERM", "") in {"truecolor", "24bit"}:
25
            return "terminal16m"
26
        elif "256" in os.environ.get("TERM", ""):
27
            return "terminal256"
28
        else:
29
            return "terminal"
30

31
    def _format_source(s: str) -> str:  # pragma: no cover
32
        """Format source code for terminal output."""
33
        if (formatter_name := _get_formatter_name()) is None:
34
            return f"{s}{os.linesep}"
35
        return pygments.highlight(
36
            s,
37
            lexer=pygments.lexers.get_lexer_by_name("clojure"),
38
            formatter=pygments.formatters.get_formatter_by_name(
39
                formatter_name, style=pygments.styles.get_style_by_name("emacs")
40
            ),
41
        )
42

43

44
def format_source_context(  # pylint: disable=too-many-locals
1✔
45
    filename: str,
46
    line: int,
47
    end_line: Optional[int] = None,
48
    num_context_lines: int = 5,
49
    show_cause_marker: bool = True,
50
) -> List[str]:
51
    """Format source code context with line numbers and identifiers for the affected
52
    line(s)."""
53
    assert num_context_lines >= 0
1✔
54

55
    lines = []
1✔
56

57
    if not filename.startswith("<") and not filename.endswith(">"):
1✔
58
        cause_range: Optional[range]
59
        if not show_cause_marker:
1✔
60
            cause_range = None
×
61
        elif end_line is not None and end_line != line:
1✔
62
            cause_range = range(line, end_line + 1)
1✔
63
        else:
64
            cause_range = range(line, line + 1)
1✔
65

66
        if source_lines := linecache.getlines(filename):
1✔
67
            selected_lines: Iterable[str]
68
            if end_line is None and line > len(source_lines):
1✔
69
                end = len(source_lines) + 1
1✔
70
                start = max(end - num_context_lines, 0)
1✔
71
                selected_lines = itertools.chain(
1✔
72
                    source_lines[start:end], itertools.repeat("\n")
73
                )
74
            else:
75
                start = max(0, line - num_context_lines)
1✔
76
                end = min((end_line or line) + num_context_lines, len(source_lines))
1✔
77
                selected_lines = source_lines[start:end]
1✔
78

79
            num_justify = max(len(str(start)), len(str(end))) + 1
1✔
80
            for n, source_line in zip(range(start, end), selected_lines):
1✔
81
                if cause_range is None:
1✔
82
                    line_marker = " "
×
83
                elif n + 1 in cause_range:
1✔
84
                    line_marker = " > "
1✔
85
                else:
86
                    line_marker = "   "
1✔
87

88
                line_num = str(n + 1).rjust(num_justify)
1✔
89
                lines.append(
1✔
90
                    f"{line_num}{line_marker}| {_format_source(source_line.rstrip())}"
91
                )
92

93
    return lines
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc