Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ time = { version = "0.3.47" }
tokio = { version = "1.45.0", default-features = false }
tombi-accessor = { path = "crates/tombi-accessor" }
tombi-ast = { path = "crates/tombi-ast" }
tombi-ast-syntax = { path = "crates/tombi-ast-syntax" }
tombi-cache = { path = "crates/tombi-cache" }
tombi-cli-options = { path = "crates/tombi-cli-options" }
tombi-comment-directive = { path = "crates/tombi-comment-directive" }
Expand Down Expand Up @@ -112,7 +113,6 @@ tombi-regex = { path = "crates/tombi-regex" }
tombi-schema-store = { path = "crates/tombi-schema-store", default-features = false }
tombi-schema-type = { path = "crates/tombi-schema-type" }
tombi-severity-level = { path = "crates/tombi-severity-level" }
tombi-ast-syntax = { path = "crates/tombi-ast-syntax" }
tombi-test-lib = { path = "crates/tombi-test-lib" }
tombi-text = { path = "crates/tombi-text", default-features = false, features = ["serde"] }
tombi-toml-text = { path = "crates/tombi-toml-text" }
Expand Down
2 changes: 1 addition & 1 deletion crates/tombi-document-tree-syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ chrono.workspace = true
itertools.workspace = true
thiserror.workspace = true
tombi-accessor.workspace = true
tombi-ast-syntax.workspace = true
tombi-date-time.workspace = true
tombi-diagnostic = { workspace = true, optional = true }
tombi-document-tree.workspace = true
tombi-hashmap.workspace = true
tombi-ast-syntax.workspace = true
tombi-text.workspace = true
tombi-toml-text.workspace = true
tombi-toml-version.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/tombi-formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ log.workspace = true
schemars = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
thiserror.workspace = true
tombi-ast-syntax.workspace = true
tombi-comment-directive.workspace = true
tombi-comment-directive-serde.workspace = true
tombi-config.workspace = true
Expand All @@ -21,7 +22,6 @@ tombi-hashmap.workspace = true
tombi-parser.workspace = true
tombi-regex.workspace = true
tombi-schema-store = { workspace = true, features = ["ast-syntax"] }
tombi-ast-syntax.workspace = true
tombi-text.workspace = true
tombi-uri.workspace = true
tombi-validator.workspace = true
Expand Down
11 changes: 11 additions & 0 deletions crates/tombi-formatter/src/editor/rule/array_values_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ pub(in crate::editor) async fn array_values_order<'a>(
return Vec::new();
};

let old_order = values_with_comma
.iter()
.map(|(value, _)| value.syntax().range())
.collect_vec();
let mut changes = vec![];

let is_last_comma = values_with_comma
Expand Down Expand Up @@ -126,6 +130,13 @@ pub(in crate::editor) async fn array_values_order<'a>(
return Vec::new();
};

if old_order.into_iter().eq(sorted_values_with_comma
.iter()
.map(|(value, _)| value.syntax().range()))
{
return Vec::new();
}

if let Some((_, comma)) = sorted_values_with_comma.last_mut()
&& !is_last_comma
&& let Some(new_last_comma) = comma
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ pub(in crate::editor) async fn inline_table_keys_order<'a>(
.as_ref()
.and_then(|comment_directive| comment_directive.table_keys_order().map(Into::into));

let schema_override = schema_context.table_order_override(current_schema, accessors);
if schema_override.is_some_and(|override_item| override_item.disabled) {
return Vec::new();
}

let schema_order_enabled = schema_override.is_some_and(|override_item| !override_item.disabled)
|| schema_context.schema_table_keys_order_enabled(current_schema);
if order.is_none() && !schema_order_enabled {
return Vec::new();
}

let old_order = key_values_with_comma
.iter()
.map(|(key_value, _)| key_value.syntax().range())
.collect_vec();
let mut changes = vec![];

let is_last_comma = key_values_with_comma
Expand Down Expand Up @@ -70,6 +85,13 @@ pub(in crate::editor) async fn inline_table_keys_order<'a>(
return Vec::new();
};

if old_order.into_iter().eq(sorted_key_values_with_comma
.iter()
.map(|(key_value, _)| key_value.syntax().range()))
{
return Vec::new();
}

if let Some((_, comma)) = sorted_key_values_with_comma.last_mut()
&& !is_last_comma
&& let Some(new_last_comma) = comma
Expand Down
11 changes: 11 additions & 0 deletions crates/tombi-formatter/src/editor/rule/root_table_keys_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ pub(in crate::editor) async fn root_table_keys_order<'a>(
return changes;
}

let old_order = table_or_array_of_tables
.iter()
.map(|table| table.syntax().range())
.collect_vec();
let old_first = table_or_array_of_tables.first().unwrap().syntax().clone();
let old_last = table_or_array_of_tables.last().unwrap().syntax().clone();

Expand Down Expand Up @@ -108,6 +112,13 @@ pub(in crate::editor) async fn root_table_keys_order<'a>(
return changes;
};

if old_order
.into_iter()
.eq(sorted_table.iter().map(|table| table.syntax().range()))
{
return changes;
}

let new = sorted_table.iter().map(SourcePart::node).collect_vec();

changes.push(crate::editor::Change::replace_range(
Expand Down
17 changes: 17 additions & 0 deletions crates/tombi-formatter/src/editor/rule/table_keys_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ pub(in crate::editor) async fn table_keys_order<'a>(
return Vec::new();
}

let schema_order_enabled = schema_override.is_some_and(|override_item| !override_item.disabled)
|| schema_context.schema_table_keys_order_enabled(current_schema);
if comment_directive_order.is_none() && !schema_order_enabled {
return Vec::new();
}

let old_order = key_values_with_comma
.iter()
.map(|(key_value, _)| key_value.syntax().range())
.collect_vec();
let old_first = key_values_with_comma.first().unwrap().0.syntax().clone();
let (last_key_value, last_comma) = key_values_with_comma.last().unwrap();
let old_last = last_comma.as_ref().map_or_else(
Expand Down Expand Up @@ -77,6 +87,13 @@ pub(in crate::editor) async fn table_keys_order<'a>(
return Vec::new();
};

if old_order.into_iter().eq(sorted_key_values_with_comma
.iter()
.map(|(key_value, _)| key_value.syntax().range()))
{
return Vec::new();
}

let mut new = Vec::with_capacity(sorted_key_values_with_comma.len() * 2);
for (value, comma) in &sorted_key_values_with_comma {
new.push(SourcePart::node(value));
Expand Down
2 changes: 1 addition & 1 deletion crates/tombi-lexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ license.workspace = true

[dependencies]
memchr.workspace = true
tombi-regex.workspace = true
tombi-ast-syntax.workspace = true
tombi-regex.workspace = true
tombi-text.workspace = true

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/tombi-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ serde.workspace = true
serde_json.workspace = true
serde_tombi.workspace = true
tokio = { workspace = true, features = ["rt", "sync"] }
tombi-ast-syntax.workspace = true
tombi-cache.workspace = true
tombi-comment-directive.workspace = true
tombi-comment-directive-serde.workspace = true
Expand All @@ -36,7 +37,6 @@ tombi-parser.workspace = true
tombi-regex.workspace = true
tombi-schema-store = { workspace = true, features = ["ast-syntax", "document-tree"] }
tombi-schema-type.workspace = true
tombi-ast-syntax.workspace = true
tombi-text = { workspace = true, features = ["lsp"] }
tombi-toml-text.workspace = true
tombi-uri.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/tombi-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ drop_bomb.workspace = true
itertools.workspace = true
log.workspace = true
thiserror.workspace = true
tombi-ast-syntax.workspace = true
tombi-diagnostic = { workspace = true, optional = true }
tombi-lexer.workspace = true
tombi-ast-syntax.workspace = true
tombi-text.workspace = true

[dev-dependencies]
Expand Down
42 changes: 26 additions & 16 deletions crates/tombi-schema-store/src/http_client/reqwest_client.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::http_client::{FetchError, HttpClient, HttpFuture, http_timeout_secs};
use bytes::Bytes;
use std::sync::{Arc, OnceLock};
use tombi_future::Boxable;

#[derive(Debug, Clone)]
pub struct ReqwestHttpClient(reqwest::Client);
pub struct ReqwestHttpClient(Arc<OnceLock<Result<reqwest::Client, String>>>);

impl Default for ReqwestHttpClient {
fn default() -> Self {
Expand All @@ -13,27 +14,36 @@ impl Default for ReqwestHttpClient {

impl ReqwestHttpClient {
pub fn new() -> Self {
Self(
reqwest::Client::builder()
.user_agent("tombi-language-server")
.timeout(std::time::Duration::from_secs(http_timeout_secs()))
.build()
.expect("Failed to create reqwest client"),
)
Self(Arc::new(OnceLock::new()))
}

fn client(&self) -> Result<&reqwest::Client, FetchError> {
self.0
.get_or_init(|| {
reqwest::Client::builder()
.user_agent("tombi-language-server")
.timeout(std::time::Duration::from_secs(http_timeout_secs()))
.build()
.map_err(|err| err.to_string())
})
.as_ref()
.map_err(|reason| FetchError::FetchFailed {
reason: reason.clone(),
})
}
}

impl HttpClient for ReqwestHttpClient {
fn get_bytes<'a>(&'a self, url: &'a str) -> HttpFuture<'a, Result<Bytes, FetchError>> {
async move {
let response = self
.0
.get(url)
.send()
.await
.map_err(|err| FetchError::FetchFailed {
reason: err.to_string(),
})?;
let response =
self.client()?
.get(url)
.send()
.await
.map_err(|err| FetchError::FetchFailed {
reason: err.to_string(),
})?;

if !response.status().is_success() {
return Err(FetchError::StatusNotOk {
Expand Down
Loading
Loading