-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract typos & state modules
- Loading branch information
Showing
5 changed files
with
218 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
pub mod lsp; | ||
pub mod windows; | ||
mod state; | ||
mod typos; | ||
mod windows; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use anyhow::anyhow; | ||
use matchit::Router; | ||
use std::path::{Path, PathBuf}; | ||
use tower_lsp::lsp_types::{DiagnosticSeverity, Url, WorkspaceFolder}; | ||
|
||
use crate::typos::Instance; | ||
|
||
#[derive(Default)] | ||
pub(crate) struct BackendState<'s> { | ||
pub severity: Option<DiagnosticSeverity>, | ||
pub config: Option<PathBuf>, | ||
pub workspace_folders: Vec<WorkspaceFolder>, | ||
pub router: Router<crate::typos::Instance<'s>>, | ||
} | ||
|
||
impl<'s> BackendState<'s> { | ||
pub(crate) fn set_workspace_folders( | ||
&mut self, | ||
workspace_folders: Vec<WorkspaceFolder>, | ||
) -> anyhow::Result<(), anyhow::Error> { | ||
self.workspace_folders = workspace_folders; | ||
self.update_router()?; | ||
Ok(()) | ||
} | ||
|
||
pub(crate) fn update_workspace_folders( | ||
&mut self, | ||
added: Vec<WorkspaceFolder>, | ||
removed: Vec<WorkspaceFolder>, | ||
) -> anyhow::Result<(), anyhow::Error> { | ||
self.workspace_folders.extend(added); | ||
if !removed.is_empty() { | ||
self.workspace_folders.retain(|x| !removed.contains(x)); | ||
} | ||
self.update_router()?; | ||
Ok(()) | ||
} | ||
|
||
pub(crate) fn update_router(&mut self) -> anyhow::Result<(), anyhow::Error> { | ||
self.router = Router::new(); | ||
for folder in self.workspace_folders.iter() { | ||
let path = folder | ||
.uri | ||
.to_file_path() | ||
.map_err(|_| anyhow!("Cannot convert uri {} to file path", folder.uri))?; | ||
let route = format!("{}{}", url_path_sanitised(&folder.uri), "/*p"); | ||
self.router | ||
.insert_instance(&route, &path, self.config.as_deref())?; | ||
} | ||
|
||
// add low priority catch all route used for files outside the workspace, or | ||
// when there is no workspace folder | ||
#[cfg(windows)] | ||
for drive in crate::windows::get_drives() { | ||
// file:///c%3A/Users/oliver/typos-vscode/src/test/fixture | ||
let route = format!("/{}%3A/*p", &drive); | ||
self.router.insert_new_typos_cli( | ||
&route, | ||
&PathBuf::from(format!("{}:\\", &drive)), | ||
self.config.as_deref(), | ||
)?; | ||
} | ||
|
||
#[cfg(not(windows))] | ||
{ | ||
let route = "/*p"; | ||
self.router | ||
.insert_instance(route, &PathBuf::from("/"), self.config.as_deref())?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
trait RouterExt { | ||
fn insert_instance( | ||
&mut self, | ||
route: &str, | ||
path: &Path, | ||
config: Option<&Path>, | ||
) -> anyhow::Result<(), anyhow::Error>; | ||
} | ||
|
||
impl RouterExt for Router<Instance<'_>> { | ||
// convenience method to insert a new TyposCli into the router | ||
// implemented as an extension trait to avoid interprocedural conflicts | ||
fn insert_instance( | ||
&mut self, | ||
route: &str, | ||
path: &Path, | ||
config: Option<&Path>, | ||
) -> anyhow::Result<(), anyhow::Error> { | ||
tracing::debug!("Adding route {} for path {}", route, path.display()); | ||
let instance = Instance::new(path, config)?; | ||
self.insert(route, instance)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn url_path_sanitised(url: &Url) -> String { | ||
// windows paths (eg: /C:/Users/..) may not be percent-encoded by some clients | ||
// and therefore contain colons, see | ||
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#uri | ||
// | ||
// and because matchit treats colons as a wildcard we need to strip them | ||
url.path().replace(':', "%3A") | ||
} |
Oops, something went wrong.