Skip to content

Commit

Permalink
Clean-up deps, fix vscode ext, refactor linter (#697)
Browse files Browse the repository at this point in the history
* replace `atty` with std::io::IsTerminal; MSRV 1.70

* Hide linter behind feature, enabled by default

- make `lint` subcommand a feature-gated, enabled
  by default; only available to build, it will not
  be distributed by default, if it introduces a
  regression, it will be reverted; closes #694

- clean-up code a bit, make `lsp` subcommand fail
  properly as no subcommand found since the errors
  are swallowed somewhere before reaching stderr

- remove references to `reqwest::Url`, use
  `url::Url` exclusively

* update rustls

* update wasm-bindgen

* update indexmap

* fix wasm panic in vscode extension
  • Loading branch information
panekj authored Nov 4, 2024
1 parent db31b2b commit 9acabdf
Show file tree
Hide file tree
Showing 19 changed files with 881 additions and 731 deletions.
26 changes: 2 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions crates/taplo-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ license = { workspace = true }
repository = { workspace = true }

[features]
default = ["lsp", "rustls-tls", "toml-test"]
lsp = ["async-ctrlc", "taplo-lsp"]
default = ["lint", "lsp", "rustls-tls", "toml-test"]
lint = ["taplo-common/schema", "taplo-common/reqwest", "reqwest"]
lsp = ["async-ctrlc", "taplo-lsp", "lint"]
native-tls = ["taplo-common/native-tls", "taplo-lsp?/native-tls"]
rustls-tls = ["taplo-common/rustls-tls", "taplo-lsp?/rustls-tls"]
toml-test = []
toml-test = ["lint"]

[dependencies]
taplo = { path = "../taplo", features = ["serde"] }
Expand All @@ -32,7 +33,7 @@ hex = { workspace = true }
itertools = { workspace = true }
once_cell = { workspace = true }
regex = { workspace = true }
reqwest = { workspace = true, features = ["json"] }
reqwest = { workspace = true, features = ["json"], optional = true }
schemars = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
Expand All @@ -45,7 +46,6 @@ url = { workspace = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ansi_term = { version = "0.12" }
async-ctrlc = { version = "1.2.0", features = ["stream"], optional = true }
atty = { version = "0.2.14" }
lsp-async-stub = { path = "../lsp-async-stub", features = ["tokio-tcp", "tokio-stdio"] }
# `prettydiff` is also a CLI that pulls in `clap` by default
prettydiff = { version = "0.6.1", default-features = false }
Expand Down
11 changes: 11 additions & 0 deletions crates/taplo-cli/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::{crate_version, Args, Parser, Subcommand, ValueEnum};
use std::path::PathBuf;
#[cfg(feature = "lint")]
use url::Url;

#[derive(Clone, Parser)]
Expand Down Expand Up @@ -48,25 +49,32 @@ pub enum Colors {
pub enum TaploCommand {
/// Lint TOML documents.
#[clap(visible_aliases = &["check", "validate"])]
#[cfg(feature = "lint")]
Lint(LintCommand),

/// Format TOML documents.
///
/// Files are modified in-place unless the input comes from the standard input, in which case the formatted result is printed to the standard output.
#[clap(visible_aliases = &["fmt"])]
Format(FormatCommand),

/// Language server operations.
#[cfg(feature = "lsp")]
Lsp {
#[clap(flatten)]
cmd: LspCommand,
},

/// Operations with the Taplo config file.
#[clap(visible_aliases = &["cfg"])]
Config {
#[clap(subcommand)]
cmd: ConfigCommand,
},

/// Extract a value from the given TOML document.
Get(GetCommand),

/// Start a decoder for `toml-test` (https://github.com/BurntSushi/toml-test).
#[cfg(feature = "toml-test")]
TomlTest {},
Expand Down Expand Up @@ -108,6 +116,7 @@ pub struct FormatCommand {
pub stdin_filepath: Option<String>,
}

#[cfg(feature = "lsp")]
#[derive(Clone, Args)]
pub struct LspCommand {
#[clap(flatten)]
Expand All @@ -117,6 +126,7 @@ pub struct LspCommand {
pub io: LspCommandIo,
}

#[cfg(feature = "lsp")]
#[derive(Clone, Subcommand)]
pub enum LspCommandIo {
/// Run the language server and listen on a TCP address.
Expand All @@ -137,6 +147,7 @@ pub enum ConfigCommand {
Schema,
}

#[cfg(feature = "lint")]
#[derive(Clone, Args)]
pub struct LintCommand {
#[clap(flatten)]
Expand Down
22 changes: 7 additions & 15 deletions crates/taplo-cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{

mod config;
mod format;
#[cfg(feature = "lint")]
mod lint;
#[cfg(feature = "lsp")]
mod lsp;
Expand All @@ -24,24 +25,15 @@ impl<E: Environment> Taplo<E> {
};

match taplo.cmd {
TaploCommand::Config { cmd } => self.execute_config(cmd).await,
TaploCommand::Format(fmt) => self.execute_format(fmt).await,
TaploCommand::Lsp { cmd } => {
#[cfg(feature = "lsp")]
{
self.execute_lsp(cmd).await
}
#[cfg(not(feature = "lsp"))]
{
let _ = cmd;
Err(anyhow::anyhow!("the LSP is not part of this build, please consult the documentation about enabling the functionality"))
}
}

TaploCommand::Get(cmd) => self.execute_get(cmd).await,
#[cfg(feature = "lint")]
TaploCommand::Lint(cmd) => self.execute_lint(cmd).await,
#[cfg(feature = "lsp")]
TaploCommand::Lsp { cmd } => self.execute_lsp(cmd).await,
#[cfg(feature = "toml-test")]
TaploCommand::TomlTest {} => self.execute_toml_test().await,
TaploCommand::Lint(cmd) => self.execute_lint(cmd).await,
TaploCommand::Config { cmd } => self.execute_config(cmd).await,
TaploCommand::Get(cmd) => self.execute_get(cmd).await,
}
}
}
10 changes: 7 additions & 3 deletions crates/taplo-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use std::{
str,
sync::Arc,
};
use taplo_common::{config::Config, environment::Environment, schema::Schemas, util::Normalize};
#[cfg(feature = "lint")]
use taplo_common::schema::Schemas;
use taplo_common::{config::Config, environment::Environment, util::Normalize};

pub mod args;
pub mod commands;
Expand All @@ -15,20 +17,22 @@ pub mod printing;
pub struct Taplo<E: Environment> {
env: E,
colors: bool,
#[cfg(feature = "lint")]
schemas: Schemas<E>,
config: Option<Arc<Config>>,
}

impl<E: Environment> Taplo<E> {
pub fn new(env: E) -> Self {
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(not(target_arch = "wasm32"), feature = "lint"))]
let http =
taplo_common::util::get_reqwest_client(std::time::Duration::from_secs(5)).unwrap();

#[cfg(target_arch = "wasm32")]
#[cfg(all(target_arch = "wasm32", feature = "lint"))]
let http = reqwest::Client::default();

Self {
#[cfg(feature = "lint")]
schemas: Schemas::new(env.clone(), http),
colors: env.atty_stderr(),
config: None,
Expand Down
5 changes: 4 additions & 1 deletion crates/taplo-cli/src/printing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use codespan_reporting::{
use itertools::Itertools;
use std::ops::Range;
use taplo::{dom, parser, rowan::TextRange};
use taplo_common::{environment::Environment, schema::NodeValidationError};
use taplo_common::environment::Environment;
#[cfg(feature = "lint")]
use taplo_common::schema::NodeValidationError;
use tokio::io::AsyncWriteExt;

impl<E: Environment> Taplo<E> {
Expand Down Expand Up @@ -113,6 +115,7 @@ impl<E: Environment> Taplo<E> {
Ok(())
}

#[cfg(feature = "lint")]
pub(crate) async fn print_schema_errors(
&self,
file: &SimpleFile<&str, &str>,
Expand Down
15 changes: 8 additions & 7 deletions crates/taplo-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ repository = { workspace = true }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
# default-tls enables native-tls but without enabling native-tls specific features.
native-tls = ["reqwest/default-tls"]
rustls-tls = ["reqwest/rustls-tls"]
schema = []
reqwest = ["dep:reqwest"]

[dependencies]
taplo = { path = "../taplo", features = ["schema"] }

Expand All @@ -19,7 +26,6 @@ anyhow = { workspace = true, features = ["backtrace"] }
arc-swap = { workspace = true }
async-recursion = { version = "1.0.0" }
async-trait = { workspace = true }
atty = { version = "0.2.14" }
futures = { workspace = true }
glob = { workspace = true }
globset = { workspace = true }
Expand All @@ -32,7 +38,7 @@ lru = { version = "0.11.1" }
parking_lot = { workspace = true }
percent-encoding = { version = "2.1.0" }
regex = { workspace = true }
reqwest = { workspace = true, features = ["json"] }
reqwest = { workspace = true, features = ["json"], optional = true }
schemars = { workspace = true, features = ["url"] }
semver = { version = "1.0.5", features = ["serde"] }
serde = { workspace = true, features = ["derive"] }
Expand All @@ -51,10 +57,5 @@ tokio = { workspace = true, features = ["sync", "fs", "time", "io-std", "parking
[target.'cfg(target_arch = "wasm32")'.dependencies]
tokio = { workspace = true, features = ["sync", "parking_lot", "io-util"] }

[features]
# default-tls enables native-tls but without enabling native-tls specific features.
native-tls = ["reqwest/default-tls"]
rustls-tls = ["reqwest/rustls-tls"]

[package.metadata.auto-tag]
enabled = true
5 changes: 3 additions & 2 deletions crates/taplo-common/src/environment/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ impl Environment for NativeEnvironment {
}

fn atty_stderr(&self) -> bool {
atty::is(atty::Stream::Stderr)
use std::io::IsTerminal;
std::io::stderr().is_terminal()
}

fn stdin(&self) -> Self::Stdin {
Expand Down Expand Up @@ -94,7 +95,7 @@ impl Environment for NativeEnvironment {
Ok(tokio::fs::write(path, bytes).await?)
}

fn to_file_path(&self, url: &reqwest::Url) -> Option<std::path::PathBuf> {
fn to_file_path(&self, url: &url::Url) -> Option<std::path::PathBuf> {
url.to_file_path().ok()
}

Expand Down
1 change: 1 addition & 0 deletions crates/taplo-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod config;
pub mod convert;
pub mod environment;
pub mod log;
#[cfg(feature = "schema")]
pub mod schema;
pub mod util;

Expand Down
2 changes: 1 addition & 1 deletion crates/taplo-common/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ pub mod cache;
pub mod ext;

pub mod builtins {
use reqwest::Url;
use serde_json::Value;
use std::sync::Arc;
use url::Url;

pub const TAPLO_CONFIG_URL: &str = "taplo://taplo.toml";

Expand Down
2 changes: 1 addition & 1 deletion crates/taplo-common/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub(crate) fn normalize_str(s: &str) -> Cow<str> {
}
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(not(target_arch = "wasm32"), feature = "reqwest"))]
#[tracing::instrument]
pub fn get_reqwest_client(timeout: std::time::Duration) -> Result<reqwest::Client, reqwest::Error> {
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
Expand Down
2 changes: 1 addition & 1 deletion crates/taplo-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ rustls-tls = ["taplo-common/rustls-tls"]
[dependencies]
lsp-async-stub = { path = "../lsp-async-stub" }
taplo = { path = "../taplo", features = ["serde"] }
taplo-common = { path = "../taplo-common" }
taplo-common = { path = "../taplo-common", features = ["schema", "reqwest"] }

anyhow = { workspace = true }
arc-swap = { workspace = true }
Expand Down
Loading

0 comments on commit 9acabdf

Please sign in to comment.