mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-17 05:32:41 +00:00
hints kitten: Use Go's stdlib regexp for the builtin patterns
regexp2 backtracks on the path pattern, retrying it at every position of a long line that has no spaces, so the path and linenum types took seconds on a screen full of such text (about 36s for a 380x100 window of hex). The builtin patterns dont need lookarounds, so use the linear time stdlib engine for them. User supplied --regex patterns stay on regexp2 so lookarounds keep working (#6265).
This commit is contained in:
parent
b8c48043b6
commit
0ceca31ef1
3 changed files with 43 additions and 6 deletions
|
|
@ -233,6 +233,9 @@ Detailed list of changes
|
|||
- macOS: Fix a custom dock icon reverting to the stock icon after a progress
|
||||
bar is cleared (:iss:`10545`)
|
||||
|
||||
- hints kitten: Fix delays of many seconds when selecting paths or line
|
||||
numbers on a screen with long lines that contain no spaces
|
||||
|
||||
0.49.1 [2026-09-24]
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
|
|
|||
|
|
@ -490,9 +490,25 @@ func find_all_matches(re *regexp2.Regexp, text string) (ans []Match, err error)
|
|||
return
|
||||
}
|
||||
|
||||
func mark(r *regexp2.Regexp, post_processors []PostProcessorFunc, group_processors []GroupProcessorFunc, text string, opts *Options) (ans []Mark) {
|
||||
func find_all_matches_stdlib(re *regexp.Regexp, text string) (ans []Match) {
|
||||
names := re.SubexpNames()
|
||||
for _, loc := range re.FindAllStringSubmatchIndex(text, -1) {
|
||||
match := Match{Groups: make([]Group, len(names))}
|
||||
for i, name := range names {
|
||||
match.Groups[i] = Group{Name: name, IsNamed: name != ""}
|
||||
if s, e := loc[2*i], loc[2*i+1]; s > -1 {
|
||||
c := Capture{Text: text[s:e]}
|
||||
c.Byte_Offsets.Start, c.Byte_Offsets.End = s, e
|
||||
match.Groups[i].Captures = []Capture{c}
|
||||
}
|
||||
}
|
||||
ans = append(ans, match)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func mark(all_matches []Match, post_processors []PostProcessorFunc, group_processors []GroupProcessorFunc, text string, opts *Options) (ans []Mark) {
|
||||
sanitize_pat := regexp.MustCompile("[\r\n\x00]")
|
||||
all_matches, _ := find_all_matches(r, text)
|
||||
for i, m := range all_matches {
|
||||
full_capture := m.Groups[0].LastCapture()
|
||||
match_start, match_end := full_capture.Byte_Offsets.Start, full_capture.Byte_Offsets.End
|
||||
|
|
@ -654,11 +670,22 @@ func find_marks(text string, opts *Options, cli_args ...string) (sanitized_text
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r, err := regexp2.Compile(pattern, regexp2.RE2)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to compile the regex pattern: %#v with error: %w", pattern, err)
|
||||
var matches []Match
|
||||
// the builtin patterns dont need lookarounds, so use the linear time stdlib engine for them
|
||||
if pattern != opts.Regex {
|
||||
r, err := regexp.Compile(pattern)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to compile the regex pattern: %#v with error: %w", pattern, err)
|
||||
}
|
||||
matches = find_all_matches_stdlib(r, sanitized_text)
|
||||
} else {
|
||||
r, err := regexp2.Compile(pattern, regexp2.RE2)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to compile the regex pattern: %#v with error: %w", pattern, err)
|
||||
}
|
||||
matches, _ = find_all_matches(r, sanitized_text)
|
||||
}
|
||||
ans = mark(r, post_processors, group_processors, sanitized_text, opts)
|
||||
ans = mark(matches, post_processors, group_processors, sanitized_text, opts)
|
||||
used_pattern = pattern
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -98,6 +98,13 @@ func TestHintMarking(t *testing.T) {
|
|||
r("file.epub.", "file.epub")
|
||||
r("(file.epub)", "file.epub")
|
||||
r("some/path", "some/path")
|
||||
// long lines without spaces must not cause quadratic backtracking
|
||||
long_line := strings.Repeat("1,", 20000)
|
||||
r(long_line)
|
||||
r("漢字 "+long_line+" some/path", "some/path")
|
||||
opts.Type = "linenum"
|
||||
r(long_line)
|
||||
m(long_line+" a/file.c:23", "a/file.c", 23)
|
||||
|
||||
reset()
|
||||
cols = 60
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue