Remove carriage returns

This commit is contained in:
Nathan Sobo 2022-02-27 07:47:46 -07:00
parent 28b71cbc03
commit dd6f8d20a3
3 changed files with 9721 additions and 9721 deletions

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -1,227 +1,227 @@
use aho_corasick::{AhoCorasick, AhoCorasickBuilder}; use aho_corasick::{AhoCorasick, AhoCorasickBuilder};
use anyhow::Result; use anyhow::Result;
use client::proto; use client::proto;
use language::{char_kind, Rope}; use language::{char_kind, Rope};
use regex::{Regex, RegexBuilder}; use regex::{Regex, RegexBuilder};
use smol::future::yield_now; use smol::future::yield_now;
use std::{ use std::{
io::{BufRead, BufReader, Read}, io::{BufRead, BufReader, Read},
ops::Range, ops::Range,
sync::Arc, sync::Arc,
}; };
#[derive(Clone)] #[derive(Clone)]
pub enum SearchQuery { pub enum SearchQuery {
Text { Text {
search: Arc<AhoCorasick<usize>>, search: Arc<AhoCorasick<usize>>,
query: Arc<str>, query: Arc<str>,
whole_word: bool, whole_word: bool,
case_sensitive: bool, case_sensitive: bool,
}, },
Regex { Regex {
regex: Regex, regex: Regex,
query: Arc<str>, query: Arc<str>,
multiline: bool, multiline: bool,
whole_word: bool, whole_word: bool,
case_sensitive: bool, case_sensitive: bool,
}, },
} }
impl SearchQuery { impl SearchQuery {
pub fn text(query: impl ToString, whole_word: bool, case_sensitive: bool) -> Self { pub fn text(query: impl ToString, whole_word: bool, case_sensitive: bool) -> Self {
let query = query.to_string(); let query = query.to_string();
let search = AhoCorasickBuilder::new() let search = AhoCorasickBuilder::new()
.auto_configure(&[&query]) .auto_configure(&[&query])
.ascii_case_insensitive(!case_sensitive) .ascii_case_insensitive(!case_sensitive)
.build(&[&query]); .build(&[&query]);
Self::Text { Self::Text {
search: Arc::new(search), search: Arc::new(search),
query: Arc::from(query), query: Arc::from(query),
whole_word, whole_word,
case_sensitive, case_sensitive,
} }
} }
pub fn regex(query: impl ToString, whole_word: bool, case_sensitive: bool) -> Result<Self> { pub fn regex(query: impl ToString, whole_word: bool, case_sensitive: bool) -> Result<Self> {
let mut query = query.to_string(); let mut query = query.to_string();
let initial_query = Arc::from(query.as_str()); let initial_query = Arc::from(query.as_str());
if whole_word { if whole_word {
let mut word_query = String::new(); let mut word_query = String::new();
word_query.push_str("\\b"); word_query.push_str("\\b");
word_query.push_str(&query); word_query.push_str(&query);
word_query.push_str("\\b"); word_query.push_str("\\b");
query = word_query query = word_query
} }
let multiline = query.contains("\n") || query.contains("\\n"); let multiline = query.contains("\n") || query.contains("\\n");
let regex = RegexBuilder::new(&query) let regex = RegexBuilder::new(&query)
.case_insensitive(!case_sensitive) .case_insensitive(!case_sensitive)
.multi_line(multiline) .multi_line(multiline)
.build()?; .build()?;
Ok(Self::Regex { Ok(Self::Regex {
regex, regex,
query: initial_query, query: initial_query,
multiline, multiline,
whole_word, whole_word,
case_sensitive, case_sensitive,
}) })
} }
pub fn from_proto(message: proto::SearchProject) -> Result<Self> { pub fn from_proto(message: proto::SearchProject) -> Result<Self> {
if message.regex { if message.regex {
Self::regex(message.query, message.whole_word, message.case_sensitive) Self::regex(message.query, message.whole_word, message.case_sensitive)
} else { } else {
Ok(Self::text( Ok(Self::text(
message.query, message.query,
message.whole_word, message.whole_word,
message.case_sensitive, message.case_sensitive,
)) ))
} }
} }
pub fn to_proto(&self, project_id: u64) -> proto::SearchProject { pub fn to_proto(&self, project_id: u64) -> proto::SearchProject {
proto::SearchProject { proto::SearchProject {
project_id, project_id,
query: self.as_str().to_string(), query: self.as_str().to_string(),
regex: self.is_regex(), regex: self.is_regex(),
whole_word: self.whole_word(), whole_word: self.whole_word(),
case_sensitive: self.case_sensitive(), case_sensitive: self.case_sensitive(),
} }
} }
pub fn detect<T: Read>(&self, stream: T) -> Result<bool> { pub fn detect<T: Read>(&self, stream: T) -> Result<bool> {
if self.as_str().is_empty() { if self.as_str().is_empty() {
return Ok(false); return Ok(false);
} }
match self { match self {
Self::Text { search, .. } => { Self::Text { search, .. } => {
let mat = search.stream_find_iter(stream).next(); let mat = search.stream_find_iter(stream).next();
match mat { match mat {
Some(Ok(_)) => Ok(true), Some(Ok(_)) => Ok(true),
Some(Err(err)) => Err(err.into()), Some(Err(err)) => Err(err.into()),
None => Ok(false), None => Ok(false),
} }
} }
Self::Regex { Self::Regex {
regex, multiline, .. regex, multiline, ..
} => { } => {
let mut reader = BufReader::new(stream); let mut reader = BufReader::new(stream);
if *multiline { if *multiline {
let mut text = String::new(); let mut text = String::new();
if let Err(err) = reader.read_to_string(&mut text) { if let Err(err) = reader.read_to_string(&mut text) {
Err(err.into()) Err(err.into())
} else { } else {
Ok(regex.find(&text).is_some()) Ok(regex.find(&text).is_some())
} }
} else { } else {
for line in reader.lines() { for line in reader.lines() {
let line = line?; let line = line?;
if regex.find(&line).is_some() { if regex.find(&line).is_some() {
return Ok(true); return Ok(true);
} }
} }
Ok(false) Ok(false)
} }
} }
} }
} }
pub async fn search(&self, rope: &Rope) -> Vec<Range<usize>> { pub async fn search(&self, rope: &Rope) -> Vec<Range<usize>> {
const YIELD_INTERVAL: usize = 20000; const YIELD_INTERVAL: usize = 20000;
if self.as_str().is_empty() { if self.as_str().is_empty() {
return Default::default(); return Default::default();
} }
let mut matches = Vec::new(); let mut matches = Vec::new();
match self { match self {
Self::Text { Self::Text {
search, whole_word, .. search, whole_word, ..
} => { } => {
for (ix, mat) in search for (ix, mat) in search
.stream_find_iter(rope.bytes_in_range(0..rope.len())) .stream_find_iter(rope.bytes_in_range(0..rope.len()))
.enumerate() .enumerate()
{ {
if (ix + 1) % YIELD_INTERVAL == 0 { if (ix + 1) % YIELD_INTERVAL == 0 {
yield_now().await; yield_now().await;
} }
let mat = mat.unwrap(); let mat = mat.unwrap();
if *whole_word { if *whole_word {
let prev_kind = rope.reversed_chars_at(mat.start()).next().map(char_kind); let prev_kind = rope.reversed_chars_at(mat.start()).next().map(char_kind);
let start_kind = char_kind(rope.chars_at(mat.start()).next().unwrap()); let start_kind = char_kind(rope.chars_at(mat.start()).next().unwrap());
let end_kind = char_kind(rope.reversed_chars_at(mat.end()).next().unwrap()); let end_kind = char_kind(rope.reversed_chars_at(mat.end()).next().unwrap());
let next_kind = rope.chars_at(mat.end()).next().map(char_kind); let next_kind = rope.chars_at(mat.end()).next().map(char_kind);
if Some(start_kind) == prev_kind || Some(end_kind) == next_kind { if Some(start_kind) == prev_kind || Some(end_kind) == next_kind {
continue; continue;
} }
} }
matches.push(mat.start()..mat.end()) matches.push(mat.start()..mat.end())
} }
} }
Self::Regex { Self::Regex {
regex, multiline, .. regex, multiline, ..
} => { } => {
if *multiline { if *multiline {
let text = rope.to_string(); let text = rope.to_string();
for (ix, mat) in regex.find_iter(&text).enumerate() { for (ix, mat) in regex.find_iter(&text).enumerate() {
if (ix + 1) % YIELD_INTERVAL == 0 { if (ix + 1) % YIELD_INTERVAL == 0 {
yield_now().await; yield_now().await;
} }
matches.push(mat.start()..mat.end()); matches.push(mat.start()..mat.end());
} }
} else { } else {
let mut line = String::new(); let mut line = String::new();
let mut line_offset = 0; let mut line_offset = 0;
for (chunk_ix, chunk) in rope.chunks().chain(["\n"]).enumerate() { for (chunk_ix, chunk) in rope.chunks().chain(["\n"]).enumerate() {
if (chunk_ix + 1) % YIELD_INTERVAL == 0 { if (chunk_ix + 1) % YIELD_INTERVAL == 0 {
yield_now().await; yield_now().await;
} }
for (newline_ix, text) in chunk.split('\n').enumerate() { for (newline_ix, text) in chunk.split('\n').enumerate() {
if newline_ix > 0 { if newline_ix > 0 {
for mat in regex.find_iter(&line) { for mat in regex.find_iter(&line) {
let start = line_offset + mat.start(); let start = line_offset + mat.start();
let end = line_offset + mat.end(); let end = line_offset + mat.end();
matches.push(start..end); matches.push(start..end);
} }
line_offset += line.len() + 1; line_offset += line.len() + 1;
line.clear(); line.clear();
} }
line.push_str(text); line.push_str(text);
} }
} }
} }
} }
} }
matches matches
} }
pub fn as_str(&self) -> &str { pub fn as_str(&self) -> &str {
match self { match self {
Self::Text { query, .. } => query.as_ref(), Self::Text { query, .. } => query.as_ref(),
Self::Regex { query, .. } => query.as_ref(), Self::Regex { query, .. } => query.as_ref(),
} }
} }
pub fn whole_word(&self) -> bool { pub fn whole_word(&self) -> bool {
match self { match self {
Self::Text { whole_word, .. } => *whole_word, Self::Text { whole_word, .. } => *whole_word,
Self::Regex { whole_word, .. } => *whole_word, Self::Regex { whole_word, .. } => *whole_word,
} }
} }
pub fn case_sensitive(&self) -> bool { pub fn case_sensitive(&self) -> bool {
match self { match self {
Self::Text { case_sensitive, .. } => *case_sensitive, Self::Text { case_sensitive, .. } => *case_sensitive,
Self::Regex { case_sensitive, .. } => *case_sensitive, Self::Regex { case_sensitive, .. } => *case_sensitive,
} }
} }
pub fn is_regex(&self) -> bool { pub fn is_regex(&self) -> bool {
matches!(self, Self::Regex { .. }) matches!(self, Self::Regex { .. })
} }
} }