Handle errors when replacing
This commit is contained in:
@@ -7,6 +7,7 @@ import hashlib
|
||||
import os
|
||||
import re
|
||||
import signal
|
||||
import sre_parse
|
||||
import sys
|
||||
from typing import Generator
|
||||
from typing import NamedTuple
|
||||
@@ -419,7 +420,12 @@ class Screen:
|
||||
'replace with', history='replace', allow_empty=True,
|
||||
)
|
||||
if response is not PromptResult.CANCELLED:
|
||||
self.file.replace(self, search_response, response)
|
||||
try:
|
||||
sre_parse.parse_template(response, search_response)
|
||||
except re.error:
|
||||
self.status.update('invalid replacement string')
|
||||
else:
|
||||
self.file.replace(self, search_response, response)
|
||||
|
||||
def command(self) -> EditResult | None:
|
||||
response = self.prompt('', history='command')
|
||||
|
||||
@@ -22,6 +22,16 @@ def test_replace_invalid_regex(run):
|
||||
h.await_text("invalid regex: '('")
|
||||
|
||||
|
||||
def test_replace_invalid_replacement(run, ten_lines):
|
||||
with run(str(ten_lines)) as h, and_exit(h):
|
||||
h.press('^\\')
|
||||
h.await_text('search (to replace):')
|
||||
h.press_and_enter('line_0')
|
||||
h.await_text('replace with:')
|
||||
h.press_and_enter('\\')
|
||||
h.await_text('invalid replacement string')
|
||||
|
||||
|
||||
def test_replace_cancel_at_replace_string(run):
|
||||
with run() as h, and_exit(h):
|
||||
h.press('^\\')
|
||||
|
||||
Reference in New Issue
Block a user