Skip to content

Commit

Permalink
fix: config files are now used on windows
Browse files Browse the repository at this point in the history
correctly handle windows file paths that contain a colon, eg: file:///D:/a/typos-vscode/typos-vscode/crates/typos-lsp/tests/diagnostics.txt

also still check text with an invalid windows path, eg: file:///diagnostics.txt, so that tests pass on windows
  • Loading branch information
tekumara committed Aug 14, 2023
1 parent afa3ac7 commit 3e96eb7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

env:
CARGO_TERM_COLOR: always
RUST_LOG: debug
RUST_LOG: typos_cli,typos_lsp

jobs:
build-linux:
Expand Down
44 changes: 21 additions & 23 deletions crates/typos-lsp/src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,12 @@ impl<'s> BackendState<'s> {
let path_wildcard = format!(
"{}{}",
path.to_str()
.ok_or_else(|| anyhow!("Invalid unicode in path {:?}", path))?,
"/*p"
.ok_or_else(|| anyhow!("Invalid unicode in path {:?}", path))?
// strip out : from windows paths so matchit doesn't use it as a wildcard
.replace(':', ""),
"\\*p"
);
tracing::debug!("{}", &path_wildcard);
let config = TyposCli::try_from(&path)?;
self.router.insert(path_wildcard, config)?;
}
Expand Down Expand Up @@ -284,37 +287,32 @@ impl<'s, 'p> Backend<'s, 'p> {
}

async fn report_diagnostics(&self, params: TextDocumentItem) {
let diagnostics = match self.check_text(&params.text, &params.uri) {
Err(e) => {
tracing::warn!("{}", e);
Vec::new()
}
Ok(diagnostics) => diagnostics,
};

let diagnostics = self.check_text(&params.text, &params.uri);
self.client
.publish_diagnostics(params.uri, diagnostics, Some(params.version))
.await;
}

// mimics typos_cli::file::FileChecker::check_file
fn check_text(
&self,
buffer: &str,
uri: &Url,
) -> anyhow::Result<Vec<Diagnostic>, anyhow::Error> {
let path = uri
.to_file_path()
.map_err(|_| anyhow!("Cannot convert uri {} to file path", uri))?;
fn check_text(&self, buffer: &str, uri: &Url) -> Vec<Diagnostic> {
let path = uri.to_file_path().unwrap_or_else(|_| {
tracing::warn!("check_text: Cannot convert uri {} to file path", uri);
PathBuf::default()
});

let path_str = path
.to_str()
.ok_or_else(|| anyhow!("Invalid unicode in path {:?}", path))?;
.unwrap_or_else(|| {
tracing::warn!("check_text: Invalid unicode in path {:?}", path);
"/"
})
// strip out : from windows paths to match what we do at insert time
.replace(':', "");

let state = self.state.lock().unwrap();

// find relevant overrides and engine for the workspace folder
let (overrides, tokenizer, dict) = match state.router.at(path_str) {
let (overrides, tokenizer, dict) = match state.router.at(&path_str) {
Err(_) => {
tracing::debug!(
"Using default policy because no workspace folder found for {}",
Expand All @@ -336,13 +334,13 @@ impl<'s, 'p> Backend<'s, 'p> {
if let Some(overrides) = overrides {
if overrides.matched(path_str, false).is_ignore() {
tracing::debug!("Ignoring {} because it matches extend-exclude.", uri);
return Ok(Vec::default());
return Vec::default();
}
}

let mut accum = AccumulatePosition::new();

Ok(typos::check_str(buffer, tokenizer, dict)
typos::check_str(buffer, tokenizer, dict)
.map(|typo| {
tracing::debug!("typo: {:?}", typo);

Expand Down Expand Up @@ -374,7 +372,7 @@ impl<'s, 'p> Backend<'s, 'p> {
..Diagnostic::default()
}
})
.collect())
.collect()
}
}

Expand Down

0 comments on commit 3e96eb7

Please sign in to comment.