Skip to content

Commit 341b15c

Browse files
committed
Replace local edit helpers with imported equivalents from pyskills
1 parent 8278078 commit 341b15c

3 files changed

Lines changed: 96 additions & 255 deletions

File tree

dialoghelper/_modidx.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,16 @@
1313
'dialoghelper.core': { 'dialoghelper.core._add_msg': ('core.html#_add_msg', 'dialoghelper/core.py'),
1414
'dialoghelper.core._add_msg_unsafe': ('core.html#_add_msg_unsafe', 'dialoghelper/core.py'),
1515
'dialoghelper.core._check_res': ('core.html#_check_res', 'dialoghelper/core.py'),
16-
'dialoghelper.core._del_lines': ('core.html#_del_lines', 'dialoghelper/core.py'),
1716
'dialoghelper.core._diff_dialog': ('core.html#_diff_dialog', 'dialoghelper/core.py'),
1817
'dialoghelper.core._event_prep': ('core.html#_event_prep', 'dialoghelper/core.py'),
1918
'dialoghelper.core._fire_event_scr': ('core.html#_fire_event_scr', 'dialoghelper/core.py'),
2019
'dialoghelper.core._handle_resp': ('core.html#_handle_resp', 'dialoghelper/core.py'),
2120
'dialoghelper.core._iife_scr': ('core.html#_iife_scr', 'dialoghelper/core.py'),
22-
'dialoghelper.core._insert_line': ('core.html#_insert_line', 'dialoghelper/core.py'),
2321
'dialoghelper.core._maybe_xml': ('core.html#_maybe_xml', 'dialoghelper/core.py'),
2422
'dialoghelper.core._msg_edit': ('core.html#_msg_edit', 'dialoghelper/core.py'),
2523
'dialoghelper.core._norm_lines': ('core.html#_norm_lines', 'dialoghelper/core.py'),
2624
'dialoghelper.core._prep_endp': ('core.html#_prep_endp', 'dialoghelper/core.py'),
2725
'dialoghelper.core._python_edit': ('core.html#_python_edit', 'dialoghelper/core.py'),
28-
'dialoghelper.core._replace_lines': ('core.html#_replace_lines', 'dialoghelper/core.py'),
29-
'dialoghelper.core._str_replace': ('core.html#_str_replace', 'dialoghelper/core.py'),
30-
'dialoghelper.core._strs_replace': ('core.html#_strs_replace', 'dialoghelper/core.py'),
3126
'dialoghelper.core._umsg': ('core.html#_umsg', 'dialoghelper/core.py'),
3227
'dialoghelper.core.add_html': ('core.html#add_html', 'dialoghelper/core.py'),
3328
'dialoghelper.core.add_html_a': ('core.html#add_html_a', 'dialoghelper/core.py'),

dialoghelper/core.py

Lines changed: 7 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -732,68 +732,16 @@ async def _one(mid):
732732
return res
733733

734734
# %% ../nbs/00_core.ipynb #ceb1ad3b
735-
def _insert_line(
736-
text:str,
737-
insert_line:int, # The 1-based line number after which to insert the text (0: before 1st line, 1: after 1st line, 2: after 2nd, etc.)
738-
new_str:str, # The text to insert
739-
):
740-
"Insert new_str at specified line number"
741-
lines = text.splitlines()
742-
if not (0 <= insert_line <= len(lines)): raise ValueError(f'Invalid line {insert_line}. Valid range: 0-{len(lines)}')
743-
lines.insert(insert_line, new_str)
744-
return '\n'.join(lines)
735+
msg_insert_line = _msg_edit (insert_line, 'msg_insert_line')
745736

746-
msg_insert_line = _msg_edit (_insert_line, 'msg_insert_line')
747737

748738
# %% ../nbs/00_core.ipynb #e6b2a19b
749-
def _str_replace(
750-
text:str,
751-
old_str:str, # Text to find and replace
752-
new_str:str, # Text to replace with
753-
start_line:int=None, # Optional 1-based start line to limit search
754-
end_line:int=None, # Optional 1-based end line to limit search
755-
n_matches:int=None, # Max replacements (None=all)
756-
re_filter:str=None, # If provided, only process lines matching this regex (like g// in ex)
757-
invert_filter:bool=False, # Invert the filter (like g!// in ex)
758-
):
759-
"Replace occurrence(s) of old_str with new_str"
760-
def _repl(s, label=''):
761-
if s.count(old_str) == 0: raise ValueError(f"Text not found{label}: {repr(old_str)}")
762-
return s.replace(old_str, new_str) if n_matches is None else s.replace(old_str, new_str, n_matches)
763-
if re_filter or start_line or end_line:
764-
lines = text.splitlines(True)
765-
s,e = (start_line or 1)-1, end_line or len(lines)
766-
if not re_filter: return ''.join(lines[:s]) + _repl(''.join(lines[s:e]), f' in lines {s+1}-{e}') + ''.join(lines[e:])
767-
pat = re.compile(re_filter)
768-
matched = [i for i in range(s,e) if bool(pat.search(lines[i])) != invert_filter]
769-
if not matched: return text
770-
for i in matched: lines[i] = _repl(lines[i], f' on line {i+1}')
771-
return ''.join(lines)
772-
return _repl(text)
773-
774-
msg_str_replace = _msg_edit (_str_replace, 'msg_str_replace')
739+
msg_str_replace = _msg_edit (str_replace, 'msg_str_replace')
740+
775741

776742
# %% ../nbs/00_core.ipynb #983ce14a
777-
def _strs_replace(
778-
text:str,
779-
old_strs:list[str], # List of strings to find and replace
780-
new_strs:list[str], # List of replacement strings (must match length of old_strs)
781-
start_line:int=None, # Optional 1-based start line to limit search
782-
end_line:int=None, # Optional 1-based end line to limit search
783-
n_matches:int=None, # Max replacements per string (None=all)
784-
re_filter:str=None, # If provided, only process lines matching this regex (like g// in ex)
785-
invert_filter:bool=False, # Invert the filter (like g!// in ex)
786-
):
787-
"Replace multiple strings simultaneously"
788-
if not isinstance(old_strs, list): raise ValueError(f"`old_strs` should be a list[str] but got {type(old_strs)}")
789-
if not isinstance(new_strs, list): raise ValueError(f"`new_strs` should be a list[str] but got {type(new_strs)}")
790-
if len(old_strs) != len(new_strs): raise ValueError(f"Length mismatch: {len(old_strs)} old_strs vs {len(new_strs)} new_strs")
791-
for idx,(old_str,new_str) in enumerate(zip(old_strs, new_strs)):
792-
text = _str_replace(text, old_str, new_str, start_line=start_line, end_line=end_line,
793-
n_matches=n_matches, re_filter=re_filter, invert_filter=invert_filter)
794-
return text
743+
msg_strs_replace = _msg_edit (strs_replace, 'msg_strs_replace')
795744

796-
msg_strs_replace = _msg_edit (_strs_replace, 'msg_strs_replace')
797745

798746
# %% ../nbs/00_core.ipynb #7b11e714
799747
def _norm_lines(n:int, start:int, end:int=None):
@@ -805,41 +753,12 @@ def _norm_lines(n:int, start:int, end:int=None):
805753
return start, end
806754

807755
# %% ../nbs/00_core.ipynb #1002423f
808-
def _replace_lines(
809-
text:str,
810-
start_line:int, # Starting line number to replace (1-based indexing)
811-
end_line:int=None, # Ending line number to replace (1-based, inclusive, negative counts from end, None for single line)
812-
new_content:str='', # New content to replace the specified lines
813-
):
814-
"Replace line range with new content"
815-
lines = text.splitlines(keepends=True)
816-
s,e = _norm_lines(len(lines), start_line, end_line)
817-
if lines and new_content and not new_content.endswith('\n'): new_content += '\n'
818-
lines[s-1:e] = [new_content] if new_content else []
819-
return ''.join(lines)
756+
msg_replace_lines = _msg_edit (replace_lines, 'msg_replace_lines')
820757

821-
msg_replace_lines = _msg_edit (_replace_lines, 'msg_replace_lines')
822758

823759
# %% ../nbs/00_core.ipynb #cbd87701
824-
def _del_lines(
825-
text:str,
826-
start_line:int, # Starting line number to delete (1-based indexing)
827-
end_line:int=None, # Ending line number to delete (1-based, inclusive, negative counts from end, None for single line)
828-
re_filter:str=None, # If provided, only delete lines matching this regex (like g// in ex)
829-
invert_filter:bool=False, # Invert the filter (like g!// in ex)
830-
):
831-
"Delete line range"
832-
lines = text.splitlines(keepends=True)
833-
s,e = _norm_lines(len(lines), start_line, end_line)
834-
if re_filter:
835-
pat = re.compile(re_filter)
836-
matched = {i for i in range(s-1,e) if bool(pat.search(lines[i])) != invert_filter}
837-
if not matched: return text
838-
lines = [l for i,l in enumerate(lines) if i not in matched]
839-
else: del lines[s-1:e]
840-
return ''.join(lines)
841-
842-
msg_del_lines = _msg_edit (_del_lines, 'msg_del_lines')
760+
msg_del_lines = _msg_edit (del_lines, 'msg_del_lines')
761+
843762

844763
# %% ../nbs/00_core.ipynb #73cb7c93
845764
async def _python_edit(

0 commit comments

Comments
 (0)