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 d1a9949
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 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
61 changes: 35 additions & 26 deletions crates/typos-lsp/src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,33 @@ impl<'s> BackendState<'s> {
.map_err(|_| anyhow!("Cannot convert uri {} to file path", folder.uri))?;
let path_wildcard = format!(
"{}{}",
path.to_str()
.ok_or_else(|| anyhow!("Invalid unicode in path {:?}", path))?,
normalise_windows_path(
path.to_str()
.ok_or_else(|| anyhow!("Invalid unicode in path {:?}", path))?
),
"/*p"
);
tracing::debug!("{}", &path_wildcard);
let config = TyposCli::try_from(&path)?;
self.router.insert(path_wildcard, config)?;
}
Ok(())
}
}

// normalise windows path to make matchit happy
fn normalise_windows_path(path: &str) -> Cow<str> {
if path.starts_with('\\') {
// unix path
path.into()
} else {
// windows path
// strip out : from windows paths so matchit doesn't use it as a wildcard
// and normalise forward slashes to backslashes, else matchit complains
path.replace(':', "").replace('\\', "/").into()
}
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct DiagnosticData<'c> {
corrections: Vec<Cow<'c, str>>,
Expand Down Expand Up @@ -284,37 +300,30 @@ 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))?;

let path_str = path
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 = normalise_windows_path(path
.to_str()
.ok_or_else(|| anyhow!("Invalid unicode in path {:?}", path))?;
.unwrap_or_else(|| {
tracing::warn!("check_text: Invalid unicode in path {:?}", path);
"/"
}));

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 @@ -334,15 +343,15 @@ impl<'s, 'p> Backend<'s, 'p> {

// skip file if matches extend-exclude
if let Some(overrides) = overrides {
if overrides.matched(path_str, false).is_ignore() {
if overrides.matched(path, 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 +383,7 @@ impl<'s, 'p> Backend<'s, 'p> {
..Diagnostic::default()
}
})
.collect())
.collect()
}
}

Expand Down

0 comments on commit d1a9949

Please sign in to comment.