tmcode 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- termcode/__init__.py +3 -0
- termcode/app.py +2236 -0
- termcode/buffers.py +168 -0
- termcode/cli.py +69 -0
- termcode/commands.py +234 -0
- termcode/completions.py +205 -0
- termcode/findbar.py +258 -0
- termcode/formatting.py +365 -0
- termcode/highlighting.py +61 -0
- termcode/modals.py +431 -0
- termcode/runner.py +725 -0
- termcode/search.py +105 -0
- termcode/styles.tcss +796 -0
- termcode/theme.py +99 -0
- termcode/workspace.py +271 -0
- tmcode-0.2.0.dist-info/METADATA +128 -0
- tmcode-0.2.0.dist-info/RECORD +21 -0
- tmcode-0.2.0.dist-info/WHEEL +5 -0
- tmcode-0.2.0.dist-info/entry_points.txt +3 -0
- tmcode-0.2.0.dist-info/licenses/LICENSE +21 -0
- tmcode-0.2.0.dist-info/top_level.txt +1 -0
termcode/search.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"""Pure helpers for find/replace and go-to-line (no UI, easy to test)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import bisect
|
|
6
|
+
import re
|
|
7
|
+
|
|
8
|
+
Match = tuple[int, int] # (start, end) string offsets into the document text
|
|
9
|
+
|
|
10
|
+
MAX_MATCHES = 10_000
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class SearchError(ValueError):
|
|
14
|
+
"""Raised for an invalid regular expression."""
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _compile(query: str, *, case_sensitive: bool, regex: bool) -> re.Pattern[str]:
|
|
18
|
+
flags = 0 if case_sensitive else re.IGNORECASE
|
|
19
|
+
try:
|
|
20
|
+
return re.compile(query if regex else re.escape(query), flags)
|
|
21
|
+
except re.error as exc:
|
|
22
|
+
raise SearchError(f"Invalid regex: {exc}") from exc
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def find_matches(
|
|
26
|
+
text: str,
|
|
27
|
+
query: str,
|
|
28
|
+
*,
|
|
29
|
+
case_sensitive: bool = False,
|
|
30
|
+
regex: bool = False,
|
|
31
|
+
) -> list[Match]:
|
|
32
|
+
"""Return non-empty, non-overlapping matches of query in text."""
|
|
33
|
+
if not query:
|
|
34
|
+
return []
|
|
35
|
+
pattern = _compile(query, case_sensitive=case_sensitive, regex=regex)
|
|
36
|
+
matches: list[Match] = []
|
|
37
|
+
for m in pattern.finditer(text):
|
|
38
|
+
if m.end() > m.start():
|
|
39
|
+
matches.append((m.start(), m.end()))
|
|
40
|
+
if len(matches) >= MAX_MATCHES:
|
|
41
|
+
break
|
|
42
|
+
return matches
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def next_match_index(matches: list[Match], cursor: int, *, backwards: bool = False) -> int | None:
|
|
46
|
+
"""Index of the match at/after cursor (or before it when backwards), wrapping around."""
|
|
47
|
+
if not matches:
|
|
48
|
+
return None
|
|
49
|
+
starts = [s for s, _ in matches]
|
|
50
|
+
if backwards:
|
|
51
|
+
idx = bisect.bisect_left(starts, cursor) - 1
|
|
52
|
+
return idx % len(matches)
|
|
53
|
+
idx = bisect.bisect_left(starts, cursor)
|
|
54
|
+
return idx % len(matches)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def replace_one(
|
|
58
|
+
matched_text: str,
|
|
59
|
+
query: str,
|
|
60
|
+
replacement: str,
|
|
61
|
+
*,
|
|
62
|
+
case_sensitive: bool = False,
|
|
63
|
+
regex: bool = False,
|
|
64
|
+
) -> str:
|
|
65
|
+
"""Replacement for a single match; expands \\1 groups only in regex mode."""
|
|
66
|
+
if not regex:
|
|
67
|
+
return replacement
|
|
68
|
+
pattern = _compile(query, case_sensitive=case_sensitive, regex=True)
|
|
69
|
+
try:
|
|
70
|
+
return pattern.sub(replacement, matched_text, count=1)
|
|
71
|
+
except re.error as exc:
|
|
72
|
+
raise SearchError(f"Invalid replacement: {exc}") from exc
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def replace_all(
|
|
76
|
+
text: str,
|
|
77
|
+
query: str,
|
|
78
|
+
replacement: str,
|
|
79
|
+
*,
|
|
80
|
+
case_sensitive: bool = False,
|
|
81
|
+
regex: bool = False,
|
|
82
|
+
) -> tuple[str, int]:
|
|
83
|
+
"""Replace every match; returns (new_text, count)."""
|
|
84
|
+
if not query:
|
|
85
|
+
return text, 0
|
|
86
|
+
pattern = _compile(query, case_sensitive=case_sensitive, regex=regex)
|
|
87
|
+
repl = replacement if regex else (lambda _m: replacement)
|
|
88
|
+
try:
|
|
89
|
+
return pattern.subn(repl, text)
|
|
90
|
+
except re.error as exc:
|
|
91
|
+
raise SearchError(f"Invalid replacement: {exc}") from exc
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def parse_goto(raw: str) -> tuple[int, int] | None:
|
|
95
|
+
"""Parse '42' or '42:7' (1-based) into a 0-based (row, col)."""
|
|
96
|
+
parts = raw.strip().split(":")
|
|
97
|
+
if not parts[0].isdigit() or len(parts) > 2:
|
|
98
|
+
return None
|
|
99
|
+
row = max(int(parts[0]) - 1, 0)
|
|
100
|
+
col = 0
|
|
101
|
+
if len(parts) == 2:
|
|
102
|
+
if not parts[1].isdigit():
|
|
103
|
+
return None
|
|
104
|
+
col = max(int(parts[1]) - 1, 0)
|
|
105
|
+
return row, col
|