Skip to content

Commit

Permalink
fix: properly use schema directives
Browse files Browse the repository at this point in the history
  • Loading branch information
tamasfe committed May 13, 2022
1 parent 3cf64ab commit c79096a
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 121 deletions.
4 changes: 2 additions & 2 deletions crates/taplo-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "A CLI for Taplo TOML toolkit"
license = "MIT"
edition = "2021"
name = "taplo-cli"
version = "0.6.2"
version = "0.6.3"
homepage = "https://taplo.tamasfe.dev"
repository = "https://github.com/tamasfe/taplo"
categories = ["development-tools", "command-line-utilities"]
Expand Down Expand Up @@ -37,7 +37,7 @@ schemars = "0.8"
serde = "1"
serde_json = "1"
taplo = { version = "0.8.0", path = "../taplo", features = ["serde"] }
taplo-common = { version = "0.1.0", path = "../taplo-common" }
taplo-common = { version = "0.1.2", path = "../taplo-common" }
taplo-lsp = { version = "0.3.0", path = "../taplo-lsp", optional = true }
time = { version = "0.3", features = ["parsing"] }
toml = "0.5"
Expand Down
6 changes: 4 additions & 2 deletions crates/taplo-cli/src/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,13 @@ impl<E: Environment> Taplo<E> {
return Err(anyhow!("semantic errors found"));
}

let file_uri: Url = format!("file://{file_path}").parse().unwrap();

self.schemas
.associations()
.add_from_document(&format!("file://{file_path}").parse().unwrap(), &dom);
.add_from_document(&file_uri, &dom);

if let Some(schema_association) = self.schemas.associations().association_for(file_path) {
if let Some(schema_association) = self.schemas.associations().association_for(&file_uri) {
tracing::debug!(
schema.url = %schema_association.url,
schema.name = schema_association.meta["name"].as_str().unwrap_or(""),
Expand Down
2 changes: 1 addition & 1 deletion crates/taplo-common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "taplo-common"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
description = "Shared code for taplo utilities."
license = "MIT"
Expand Down
4 changes: 2 additions & 2 deletions crates/taplo-common/src/schema/associations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,12 @@ impl<E: Environment> SchemaAssociations<E> {
}
}

pub fn association_for(&self, file: &str) -> Option<SchemaAssociation> {
pub fn association_for(&self, file: &Url) -> Option<SchemaAssociation> {
self.associations
.read()
.iter()
.filter_map(|(rule, assoc)| {
if rule.is_match(file) {
if rule.is_match(file.as_str()) {
Some(assoc.clone())
} else {
None
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 @@ -193,7 +193,7 @@ impl<E: Environment> Schemas<E> {
};

if let Err(error) = self.cache.store(schema_url.clone(), schema.clone()).await {
tracing::warn!(%error, "failed to cache schema");
tracing::debug!(%error, "failed to cache schema");
}

Ok(schema)
Expand Down
4 changes: 2 additions & 2 deletions crates/taplo-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ authors = ["tamasf97 <tamasf97@outlook.com>"]
description = "Language server for Taplo"
edition = "2021"
name = "taplo-lsp"
version = "0.3.0"
version = "0.3.1"
license = "MIT"
homepage = "https://taplo.tamasfe.dev"
repository = "https://github.com/tamasfe/taplo"
Expand Down Expand Up @@ -34,7 +34,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["preserve_order"] }
tap = "1.0.1"
taplo = { version = "0.8.0", path = "../taplo", features = ["serde"] }
taplo-common = { version = "0.1.0", path = "../taplo-common" }
taplo-common = { version = "0.1.2", path = "../taplo-common" }
time = { version = "0.3", features = ["formatting", "parsing"] }
toml = "0.5"
tracing = "0.1.29"
Expand Down
2 changes: 1 addition & 1 deletion crates/taplo-lsp/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ async fn collect_schema_errors<E: Environment>(
if let Some(schema_association) = ws
.schemas
.associations()
.association_for(document_url.as_str())
.association_for(document_url)
{
tracing::debug!(
schema.url = %schema_association.url,
Expand Down
202 changes: 99 additions & 103 deletions crates/taplo-lsp/src/handlers/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ pub async fn completion<E: Environment>(

let doc = ws.document(&document_uri)?;

let schema_association = match ws
.schemas
.associations()
.association_for(document_uri.as_str())
{
let schema_association = match ws.schemas.associations().association_for(&document_uri) {
Some(ass) => ass,
None => return Ok(None),
};
Expand Down Expand Up @@ -568,108 +564,108 @@ fn add_value_completions(
});
}

let types = match schema["type"].clone() {
Value::Null => Vec::from([Value::String("object".into())]),
Value::String(s) => Vec::from([Value::String(s)]),
Value::Array(tys) => tys,
_ => Vec::new(),
};
let types = match schema["type"].clone() {
Value::Null => Vec::from([Value::String("object".into())]),
Value::String(s) => Vec::from([Value::String(s)]),
Value::Array(tys) => tys,
_ => Vec::new(),
};

for ty in types {
if let Some(s) = ty.as_str() {
match s {
"string" => {
completions.push(CompletionItem {
label: r#""""#.into(),
kind: Some(CompletionItemKind::VALUE),
documentation: Some(Documentation::MarkupContent(MarkupContent {
kind: lsp_types::MarkupKind::Markdown,
value: schema_docs.clone().unwrap_or_else(|| "string".into()),
})),
insert_text_format: Some(InsertTextFormat::SNIPPET),
text_edit: range.map(|range| {
CompletionTextEdit::Edit(TextEdit {
range,
new_text: r#""$0""#.into(),
})
}),
..Default::default()
});
}
"boolean" => {
completions.push(CompletionItem {
label: r#"true"#.into(),
kind: Some(CompletionItemKind::VALUE),
documentation: Some(Documentation::MarkupContent(MarkupContent {
kind: lsp_types::MarkupKind::Markdown,
value: schema_docs.clone().unwrap_or_else(|| "true value".into()),
})),
insert_text_format: Some(InsertTextFormat::SNIPPET),
text_edit: range.map(|range| {
CompletionTextEdit::Edit(TextEdit {
range,
new_text: r#"true$0"#.into(),
})
}),
..Default::default()
});
completions.push(CompletionItem {
label: r#"false"#.into(),
kind: Some(CompletionItemKind::VALUE),
documentation: Some(Documentation::MarkupContent(MarkupContent {
kind: lsp_types::MarkupKind::Markdown,
value: schema_docs.clone().unwrap_or_else(|| "false value".into()),
})),
insert_text_format: Some(InsertTextFormat::SNIPPET),
text_edit: range.map(|range| {
CompletionTextEdit::Edit(TextEdit {
range,
new_text: r#"false$0"#.into(),
})
}),
..Default::default()
});
}
"array" => {
completions.push(CompletionItem {
label: r#"[]"#.into(),
kind: Some(CompletionItemKind::VALUE),
documentation: Some(Documentation::MarkupContent(MarkupContent {
kind: lsp_types::MarkupKind::Markdown,
value: schema_docs.clone().unwrap_or_else(|| "array".into()),
})),
insert_text_format: Some(InsertTextFormat::SNIPPET),
text_edit: range.map(|range| {
CompletionTextEdit::Edit(TextEdit {
range,
new_text: r#"[$0]"#.into(),
})
}),
..Default::default()
});
}
"object" => {
completions.push(CompletionItem {
label: r#"{ }"#.into(),
kind: Some(CompletionItemKind::VALUE),
documentation: Some(Documentation::MarkupContent(MarkupContent {
kind: lsp_types::MarkupKind::Markdown,
value: schema_docs.clone().unwrap_or_else(|| "object".into()),
})),
insert_text_format: Some(InsertTextFormat::SNIPPET),
text_edit: range.map(|range| {
CompletionTextEdit::Edit(TextEdit {
range,
new_text: r#"{ $0 }"#.into(),
})
}),
..Default::default()
});
}
_ => {}
for ty in types {
if let Some(s) = ty.as_str() {
match s {
"string" => {
completions.push(CompletionItem {
label: r#""""#.into(),
kind: Some(CompletionItemKind::VALUE),
documentation: Some(Documentation::MarkupContent(MarkupContent {
kind: lsp_types::MarkupKind::Markdown,
value: schema_docs.clone().unwrap_or_else(|| "string".into()),
})),
insert_text_format: Some(InsertTextFormat::SNIPPET),
text_edit: range.map(|range| {
CompletionTextEdit::Edit(TextEdit {
range,
new_text: r#""$0""#.into(),
})
}),
..Default::default()
});
}
"boolean" => {
completions.push(CompletionItem {
label: r#"true"#.into(),
kind: Some(CompletionItemKind::VALUE),
documentation: Some(Documentation::MarkupContent(MarkupContent {
kind: lsp_types::MarkupKind::Markdown,
value: schema_docs.clone().unwrap_or_else(|| "true value".into()),
})),
insert_text_format: Some(InsertTextFormat::SNIPPET),
text_edit: range.map(|range| {
CompletionTextEdit::Edit(TextEdit {
range,
new_text: r#"true$0"#.into(),
})
}),
..Default::default()
});
completions.push(CompletionItem {
label: r#"false"#.into(),
kind: Some(CompletionItemKind::VALUE),
documentation: Some(Documentation::MarkupContent(MarkupContent {
kind: lsp_types::MarkupKind::Markdown,
value: schema_docs.clone().unwrap_or_else(|| "false value".into()),
})),
insert_text_format: Some(InsertTextFormat::SNIPPET),
text_edit: range.map(|range| {
CompletionTextEdit::Edit(TextEdit {
range,
new_text: r#"false$0"#.into(),
})
}),
..Default::default()
});
}
"array" => {
completions.push(CompletionItem {
label: r#"[]"#.into(),
kind: Some(CompletionItemKind::VALUE),
documentation: Some(Documentation::MarkupContent(MarkupContent {
kind: lsp_types::MarkupKind::Markdown,
value: schema_docs.clone().unwrap_or_else(|| "array".into()),
})),
insert_text_format: Some(InsertTextFormat::SNIPPET),
text_edit: range.map(|range| {
CompletionTextEdit::Edit(TextEdit {
range,
new_text: r#"[$0]"#.into(),
})
}),
..Default::default()
});
}
"object" => {
completions.push(CompletionItem {
label: r#"{ }"#.into(),
kind: Some(CompletionItemKind::VALUE),
documentation: Some(Documentation::MarkupContent(MarkupContent {
kind: lsp_types::MarkupKind::Markdown,
value: schema_docs.clone().unwrap_or_else(|| "object".into()),
})),
insert_text_format: Some(InsertTextFormat::SNIPPET),
text_edit: range.map(|range| {
CompletionTextEdit::Edit(TextEdit {
range,
new_text: r#"{ $0 }"#.into(),
})
}),
..Default::default()
});
}
_ => {}
}
}
}
}

fn new_entry_snippet(keys: &Keys, schema: &Value) -> String {
Expand Down
2 changes: 1 addition & 1 deletion crates/taplo-lsp/src/handlers/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub(crate) async fn hover<E: Environment>(
if let Some(schema_association) = ws
.schemas
.associations()
.association_for(document_uri.as_str())
.association_for(&document_uri)
{
tracing::debug!(
schema.url = %schema_association.url,
Expand Down
2 changes: 1 addition & 1 deletion crates/taplo-lsp/src/handlers/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub async fn links<E: Environment>(
if let Some(schema_association) = ws
.schemas
.associations()
.association_for(p.text_document.uri.as_str())
.association_for(&p.text_document.uri)
{
tracing::debug!(
schema.url = %schema_association.url,
Expand Down
2 changes: 1 addition & 1 deletion crates/taplo-lsp/src/handlers/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub async fn associated_schema<E: Environment>(
schema: ws
.schemas
.associations()
.association_for(p.document_uri.as_str())
.association_for(&p.document_uri)
.map(|s| SchemaInfo {
url: s.url,
meta: s.meta,
Expand Down
2 changes: 1 addition & 1 deletion crates/taplo-lsp/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ impl<E: Environment> WorkspaceState<E> {
if let Some(assoc) = self
.schemas
.associations()
.association_for(document_url.as_str())
.association_for(document_url)
{
if let Err(error) = context
.write_notification::<DidChangeSchemaAssociation, _>(Some(
Expand Down
2 changes: 1 addition & 1 deletion crates/taplo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "MIT"
name = "taplo"
readme = false
repository = "https://github.com/tamasfe/taplo"
version = "0.8.0"
version = "0.8.1"

[features]
default = ["serde"]
Expand Down
4 changes: 2 additions & 2 deletions crates/taplo/src/dom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ impl Keys {
}

pub fn skip_left(&self, n: usize) -> Self {
Self::new(self.keys.iter().cloned().skip(n))
Self::new(self.keys.iter().skip(n).cloned())
}

pub fn skip_right(&self, n: usize) -> Self {
Self::new(self.keys.iter().rev().cloned().skip(n).rev())
Self::new(self.keys.iter().rev().skip(n).cloned().rev())
}

pub fn all_text_range(&self) -> TextRange {
Expand Down

0 comments on commit c79096a

Please sign in to comment.