Skip to content

Commit

Permalink
allow dev comments to be enabled from cli, opt in (#138)
Browse files Browse the repository at this point in the history
Closes #132
  • Loading branch information
drahnr authored Jan 16, 2021
1 parent 1257f3c commit d430b36
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 104 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ return code if mistakes are found instead of `0`.
# Project settings where a Cargo.toml exists and is passed
# ${CARGO_MANIFEST_DIR}/.config/spellcheck.toml

# Also take into account developer comments
dev_comments = false

# Skip the README.md file as defined in the cargo manifest
skip_readme = false

# Fallback to per use configuration files:
# Linux: /home/alice/.config/cargo_spellcheck/config.toml
# Windows: C:\Users\Alice\AppData\Roaming\cargo_spellcheck\config.toml
Expand All @@ -104,7 +110,7 @@ lang = "en_US"
# os specific search dirs, searched in order, defaults last
# search_dirs = []

# Adds additional dictionaries, can be specified as
# Adds additional dictionaries, can be specified as
# absolute paths or relative in the search dirs (in this order).
# Relative paths are resolved relative to the configuration file
# which is used.
Expand Down
25 changes: 23 additions & 2 deletions src/action/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,33 @@ where
Ok(())
}

/// Mode in which `cargo-spellcheck` operates
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
/// Mode in which `cargo-spellcheck` operates.
///
/// Eventually to be used directly in parsing args.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Deserialize)]
pub enum Action {
/// Only show errors
#[serde(alias = "check")]
Check,

/// Interactively choose from checker provided suggestions.
#[serde(alias = "fix")]
Fix,
/// Reflow doc comments so they adhere to a given maximum column width.
#[serde(alias = "reflow")]
Reflow,

/// Print help and exit.
#[serde(alias = "help")]
Help,

/// Print the version info and exit.
#[serde(alias = "version")]
Version,

/// Print the config being in use, default config if none.
#[serde(alias = "config")]
Config,
}

impl Action {
Expand Down Expand Up @@ -356,6 +374,9 @@ impl Action {
Ok(Finish::MistakeCount(n))
}
}
Self::Config | Self::Version | Self::Help => {
unreachable!("Should have been handled way earlier")
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/checker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ pub mod tests {
.filter(None, log::LevelFilter::Trace)
.is_test(true)
.try_init();
let d = Documentation::from((ContentOrigin::TestEntityRust, content));
let dev_comments = false;
let d = Documentation::load_from_str(ContentOrigin::TestEntityRust, content, dev_comments);
let suggestion_set =
dummy::DummyChecker::check(&d, &()).expect("Dummy extraction must never fail");

Expand Down
16 changes: 16 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ use std::path::{Path, PathBuf};
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct Config {
// Options that modify the inputs being picked up.
#[serde(default)]
#[serde(alias = "dev-comments")]
#[serde(alias = "devcomments")]
pub dev_comments: bool,

#[serde(default)]
#[serde(alias = "skip-readme")]
#[serde(alias = "skipreadme")]
pub skip_readme: bool,

#[serde(alias = "Hunspell")]
pub hunspell: Option<HunspellConfig>,
#[serde(alias = "LanguageTool")]
Expand Down Expand Up @@ -419,6 +430,8 @@ impl Config {
impl Default for Config {
fn default() -> Self {
Self {
dev_comments: false,
skip_readme: false,
hunspell: Some(HunspellConfig {
lang: Some("en_US".to_owned()),
search_dirs: SearchDirs::default(),
Expand Down Expand Up @@ -452,6 +465,9 @@ mod tests {
fn all() {
let _ = Config::parse(
r#"
dev_comments = true
skip-readme = true
[LanguageTool]
url = "http://127.0.0.1:8010/"
Expand Down
12 changes: 6 additions & 6 deletions src/documentation/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{trace, LiteralSet, Spacing, TokenTree, TrimmedLiteral, TryInto};
use crate::documentation::developer::extract_developer_comments;
use crate::documentation::Range;
use crate::Span;
use anyhow::{anyhow, Error, Result};
use anyhow::{anyhow, Result};
use std::convert::TryFrom;

/// Cluster literals for one file
Expand Down Expand Up @@ -109,18 +109,18 @@ impl Clusters {
fn ensure_sorted(&mut self) {
self.set.sort_by(|ls1, ls2| ls1.coverage.cmp(&ls2.coverage));
}
}

impl TryFrom<&str> for Clusters {
type Error = Error;
fn try_from(source: &str) -> Result<Self> {
/// Load clusters from a `&str`. Optionally loads developer comments as well.
pub(crate) fn load_from_str(source: &str, dev_comments: bool) -> Result<Self> {
let mut chunk = Self {
set: Vec::with_capacity(64),
};
let stream = syn::parse_str::<proc_macro2::TokenStream>(source)
.map_err(|e| anyhow!("Failed to parse content to stream").context(e))?;
chunk.parse_token_tree(source, stream)?;
chunk.parse_developer_comments(source);
if dev_comments {
chunk.parse_developer_comments(source);
}
chunk.ensure_sorted();
Ok(chunk)
}
Expand Down
23 changes: 13 additions & 10 deletions src/documentation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use log::trace;
pub use proc_macro2::LineColumn;
use proc_macro2::{Spacing, TokenTree};
use rayon::prelude::*;
use std::convert::{TryFrom, TryInto};
use std::convert::TryInto;
use std::path::PathBuf;

/// Range based on `usize`, simplification.
Expand Down Expand Up @@ -117,8 +117,13 @@ impl Documentation {
}

/// Adds a rust content str to the documentation.
pub fn add_rust(&mut self, origin: ContentOrigin, content: &str) -> Result<()> {
let cluster = Clusters::try_from(content)?;
pub fn add_rust(
&mut self,
origin: ContentOrigin,
content: &str,
dev_comments: bool,
) -> Result<()> {
let cluster = Clusters::load_from_str(content, dev_comments)?;

let chunks = Vec::<CheckableChunk>::from(cluster);
self.add_inner(origin, chunks);
Expand Down Expand Up @@ -166,26 +171,24 @@ impl Documentation {
pub fn entry_count(&self) -> usize {
self.index.len()
}
}

/// only a shortcut to avoid duplicate code
impl From<(ContentOrigin, &str)> for Documentation {
fn from((origin, content): (ContentOrigin, &str)) -> Self {
/// Load a document from a single string with a defined origin.
pub fn load_from_str(origin: ContentOrigin, content: &str, dev_comments: bool) -> Self {
let mut docs = Documentation::new();

match &origin {
ContentOrigin::RustDocTest(_path, span) => {
if let Ok(excerpt) = load_span_from(&mut content.as_bytes(), span.clone()) {
docs.add_rust(origin.clone(), excerpt.as_str())
docs.add_rust(origin.clone(), excerpt.as_str(), dev_comments)
} else {
// TODO
Ok(())
}
}
ContentOrigin::RustSourceFile(_path) => docs.add_rust(origin, content),
ContentOrigin::RustSourceFile(_path) => docs.add_rust(origin, content, dev_comments),
ContentOrigin::CommonMarkFile(_path) => docs.add_commonmark(origin, content),
#[cfg(test)]
ContentOrigin::TestEntityRust => docs.add_rust(origin, content),
ContentOrigin::TestEntityRust => docs.add_rust(origin, content, dev_comments),
#[cfg(test)]
ContentOrigin::TestEntityCommonMark => docs.add_commonmark(origin, content),
}
Expand Down
9 changes: 5 additions & 4 deletions src/documentation/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn parse_and_construct() {
const TEST_PLAIN: &str = r#"A very good test."#;

let origin = ContentOrigin::TestEntityRust;
let docs = Documentation::from((origin.clone(), TEST_SOURCE));
let docs = Documentation::load_from_str(origin.clone(), TEST_SOURCE, false);
assert_eq!(docs.index.len(), 1);
let chunks = docs.index.get(&origin).expect("Must contain dummy path");
assert_eq!(dbg!(chunks).len(), 1);
Expand Down Expand Up @@ -80,7 +80,7 @@ macro_rules! end2end {
.try_init();

let origin: ContentOrigin = $origin;
let docs = Documentation::from((origin.clone(), $test));
let docs = Documentation::load_from_str(origin.clone(), $test, false);
assert_eq!(docs.index.len(), 1);
let chunks = docs.index.get(&origin).expect("Must contain dummy path");
assert_eq!(dbg!(chunks).len(), 1);
Expand Down Expand Up @@ -175,8 +175,7 @@ mod e2e {

let origin: ContentOrigin = $origin;

let docs = Documentation::from((origin.clone(), $source));

let docs = Documentation::load_from_str(origin.clone(), $source, false);
let suggestion_set =
dbg!(DummyChecker::check(&docs, &())).expect("Dummy checker never fails. qed");

Expand Down Expand Up @@ -1111,6 +1110,8 @@ ff"#,
}

pub(crate) fn annotated_literals(source: &str) -> Vec<TrimmedLiteral> {
use std::convert::TryFrom;

let stream = syn::parse_str::<proc_macro2::TokenStream>(source).expect("Must be valid rust");
stream
.into_iter()
Expand Down
Loading

0 comments on commit d430b36

Please sign in to comment.