Skip to content

Commit

Permalink
refactor: extract Ignores into typos.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
tekumara committed Apr 8, 2024
1 parent 1a03b6f commit bd2c54e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
35 changes: 3 additions & 32 deletions crates/typos-lsp/src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use tower_lsp::{Client, LanguageServer};
use typos_cli::policy;

use crate::state::{url_path_sanitised, BackendState};
use crate::typos::AccumulatePosition;
use crate::typos::{AccumulatePosition, Ignores};
pub struct Backend<'s, 'p> {
client: Client,
state: Mutex<crate::state::BackendState<'s>>,
Expand Down Expand Up @@ -228,34 +228,6 @@ impl LanguageServer for Backend<'static, 'static> {
}
}

// copied from https://github.com/crate-ci/typos/blob/c15b28fff9a814f9c12bd24cb1cfc114037e9187/crates/typos-cli/src/file.rs#L741
#[derive(Clone, Debug)]
struct Ignores {
blocks: Vec<std::ops::Range<usize>>,
}

impl Ignores {
fn new(content: &[u8], ignores: &[regex::Regex]) -> Self {
let mut blocks = Vec::new();
if let Ok(content) = std::str::from_utf8(content) {
for ignore in ignores {
for mat in ignore.find_iter(content) {
blocks.push(mat.range());
}
}
}
Self { blocks }
}

fn is_ignored(&self, span: std::ops::Range<usize>) -> bool {
let start = span.start;
let end = span.end.saturating_sub(1);
self.blocks
.iter()
.any(|block| block.contains(&start) || block.contains(&end))
}
}

impl<'s, 'p> Backend<'s, 'p> {
pub fn new(client: Client) -> Self {
Self {
Expand All @@ -273,6 +245,8 @@ impl<'s, 'p> Backend<'s, 'p> {
}

// mimics typos_cli::file::FileChecker::check_file
// see https://github.com/crate-ci/typos/blob/c15b28fff9a814f9c12bd24cb1cfc114037e9187/crates/typos-cli/src/file.rs#L43
// but using check_str instead of check_bytes
fn check_text(&self, buffer: &str, uri: &Url) -> Vec<Diagnostic> {
let state = self.state.lock().unwrap();

Expand Down Expand Up @@ -327,9 +301,6 @@ impl<'s, 'p> Backend<'s, 'p> {

let mut accum = AccumulatePosition::new();

// mimics https://github.com/crate-ci/typos/blob/c15b28fff9a814f9c12bd24cb1cfc114037e9187/crates/typos-cli/src/file.rs#L43
// but using check_str instead of check_bytes

let mut ignores: Option<Ignores> = None;

typos::check_str(buffer, tokenizer, dict)
Expand Down
29 changes: 29 additions & 0 deletions crates/typos-lsp/src/typos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,35 @@ impl Instance<'_> {
}
}


// copied from https://github.com/crate-ci/typos/blob/c15b28fff9a814f9c12bd24cb1cfc114037e9187/crates/typos-cli/src/file.rs#L741
#[derive(Clone, Debug)]
pub(crate) struct Ignores {
blocks: Vec<std::ops::Range<usize>>,
}

impl Ignores {
pub(crate) fn new(content: &[u8], ignores: &[regex::Regex]) -> Self {
let mut blocks = Vec::new();
if let Ok(content) = std::str::from_utf8(content) {
for ignore in ignores {
for mat in ignore.find_iter(content) {
blocks.push(mat.range());
}
}
}
Self { blocks }
}

pub(crate) fn is_ignored(&self, span: std::ops::Range<usize>) -> bool {
let start = span.start;
let end = span.end.saturating_sub(1);
self.blocks
.iter()
.any(|block| block.contains(&start) || block.contains(&end))
}
}

pub struct AccumulatePosition {
line_num: usize,
line_pos: usize,
Expand Down

0 comments on commit bd2c54e

Please sign in to comment.