Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #361: support liberal space by \ #364

Merged
merged 1 commit into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/engine/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,18 @@ impl AndOrEngineFactory {
}
}

// we want to treat `\ ` as plain white space
// regex crate doesn't support look around, so I use a lazy workaround
// that replace `\ ` with `\0` ahead of split and replace it back afterwards
fn parse_or(&self, query: &str, case: CaseMatching) -> Box<dyn MatchEngine> {
if query.trim().is_empty() {
self.inner.create_engine_with_case(query, case)
} else {
Box::new(
OrEngine::builder()
.engines(RE_OR.split(query).map(|q| self.parse_and(q, case)).collect())
.build(),
)
let engines = RE_OR
.split(&self.mask_escape_space(query))
.map(|q| self.parse_and(q, case))
.collect();
Box::new(OrEngine::builder().engines(engines).build())
}
}

Expand All @@ -161,8 +164,9 @@ impl AndOrEngineFactory {
for mat in RE_AND.find_iter(query_trim) {
let (start, end) = (mat.start(), mat.end());
let term = query_trim[last..start].trim_matches(|c| c == ' ' || c == '|');
let term = self.unmask_escape_space(term);
if !term.is_empty() {
engines.push(self.inner.create_engine_with_case(term, case));
engines.push(self.inner.create_engine_with_case(&term, case));
}

if !mat.as_str().trim().is_empty() {
Expand All @@ -172,11 +176,20 @@ impl AndOrEngineFactory {
}

let term = query_trim[last..].trim_matches(|c| c == ' ' || c == '|');
let term = self.unmask_escape_space(term);
if !term.is_empty() {
engines.push(self.inner.create_engine_with_case(term, case));
engines.push(self.inner.create_engine_with_case(&term, case));
}
Box::new(AndEngine::builder().engines(engines).build())
}

fn mask_escape_space(&self, string: &str) -> String {
string.replace("\\ ", "\0")
}

fn unmask_escape_space(&self, string: &str) -> String {
string.replace("\0", " ")
}
}

impl MatchEngineFactory for AndOrEngineFactory {
Expand Down
14 changes: 14 additions & 0 deletions test/test_skim.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,20 @@ def test_issue_359_multi_byte_and_regex(self):
self.tmux.send_keys(f"""echo 'ああa' | {self.sk("--regex -q 'a'")}""", Key('Enter'))
self.tmux.until(lambda lines: lines[-3].startswith('> ああa'))

def test_issue_361_literal_space(self):
args = '''-q "'foo\\ bar"'''
self.tmux.send_keys(f"""echo 'foo bar\nfoo bar' | {self.sk(args)}""", Key('Enter'))
self.tmux.until(lambda lines: lines.ready_with_matches(1))
self.tmux.until(lambda lines: lines[-3].startswith('> foo bar'))
self.tmux.send_keys(Key('Enter'))

args = '''-q "!foo\\ bar"'''
self.tmux.send_keys(f"""echo 'foo bar\nfoo bar' | {self.sk(args)}""", Key('Enter'))
self.tmux.until(lambda lines: lines.ready_with_matches(1))
self.tmux.until(lambda lines: lines[-3].startswith('> foo bar'))
self.tmux.send_keys(Key('Enter'))


def find_prompt(lines, interactive=False, reverse=False):
linen = -1
prompt = ">"
Expand Down