-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: feat: code action to add a misspelling to the config file #72
Draft
mikavilpas
wants to merge
2
commits into
tekumara:main
Choose a base branch
from
mikavilpas:add-misspelling-to-config-file
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,111 @@ | ||
use std::{ | ||
fs::read_to_string, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
use anyhow::{anyhow, Context}; | ||
use toml_edit::DocumentMut; | ||
|
||
pub(super) const IGNORE_IN_PROJECT: &str = "ignore-in-project"; | ||
|
||
pub(super) fn ignore_typo_in_config_file(config_file: PathBuf, typo: String) -> anyhow::Result<()> { | ||
let input = read_to_string(&config_file) | ||
.with_context(|| anyhow!("Cannot read config file at {}", config_file.display())) | ||
.unwrap_or("".to_string()); | ||
|
||
let document = add_typo(input, typo, &config_file)?; | ||
|
||
std::fs::write(&config_file, document.to_string()) | ||
.with_context(|| anyhow!("Cannot write config file to {}", config_file.display()))?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn add_typo( | ||
input: String, | ||
typo: String, | ||
config_file_path: &Path, | ||
) -> Result<DocumentMut, anyhow::Error> { | ||
// preserve comments and formatting | ||
let mut document = input | ||
.parse::<DocumentMut>() | ||
.with_context(|| anyhow!("Cannot parse config file at {}", config_file_path.display()))?; | ||
let extend_words = document | ||
.entry("default") | ||
.or_insert(toml_edit::table()) | ||
.as_table_mut() | ||
.context("Cannot get 'default' table")? | ||
.entry("extend-words") | ||
.or_insert(toml_edit::table()) | ||
.as_table_mut() | ||
.context("Cannot get 'extend-words' table")?; | ||
extend_words[typo.as_str()] = toml_edit::value(typo.clone()); | ||
Ok(document) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_add_typo_to_empty_file() { | ||
let empty_file = ""; | ||
let document = add_typo( | ||
empty_file.to_string(), | ||
"typo".to_string(), | ||
PathBuf::from("test.toml").as_path(), | ||
) | ||
.unwrap(); | ||
|
||
similar_asserts::assert_eq!( | ||
document.to_string(), | ||
[ | ||
"[default]", | ||
"", | ||
"[default.extend-words]", | ||
"typo = \"typo\"", | ||
"" | ||
] | ||
.join("\n") | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_add_typo_to_existing_file() -> anyhow::Result<()> { | ||
// should preserve comments and formatting | ||
|
||
let existing_file = [ | ||
"[files] # comment", | ||
"# comment", | ||
"extend-exclude = [\"CHANGELOG.md\", \"crates/typos-lsp/tests/integration_test.rs\"]", | ||
] | ||
.join("\n"); | ||
|
||
// make sure the config is valid (so the test makes sense) | ||
let _ = typos_cli::config::Config::from_toml(&existing_file)?; | ||
|
||
let document = add_typo( | ||
existing_file.to_string(), | ||
"typo".to_string(), | ||
PathBuf::from("test.toml").as_path(), | ||
)?; | ||
|
||
similar_asserts::assert_eq!( | ||
document.to_string(), | ||
[ | ||
"[files] # comment", | ||
"# comment", | ||
"extend-exclude = [\"CHANGELOG.md\", \"crates/typos-lsp/tests/integration_test.rs\"]", | ||
"", | ||
"[default]", | ||
"", | ||
"[default.extend-words]", | ||
"typo = \"typo\"", | ||
"" | ||
] | ||
.join("\n") | ||
); | ||
|
||
Ok(()) | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI probably the easiest way to trigger reloading the config file would be to call update_router which resets the router with new Instances (this approach does have a memory leak but that could be solved independently see #11)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks really good, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reloading seems to work for me now!