From f84ac7c77420d310aa97b39e047a34e10115db46 Mon Sep 17 00:00:00 2001 From: paulobressan Date: Tue, 5 Aug 2025 13:43:30 -0300 Subject: [PATCH] chore: updated tx3 lang crate --- Cargo.lock | 16 ++++++++++++++-- Cargo.toml | 2 +- src/ast_to_svg.rs | 8 ++++++-- src/server.rs | 27 ++++++++++++++++++--------- src/visitor.rs | 35 +++++++---------------------------- 5 files changed, 46 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 43b4784..45e0f6f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1211,11 +1211,22 @@ dependencies = [ "once_cell", ] +[[package]] +name = "trait-variant" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70977707304198400eb4835a78f6a9f928bf41bba420deb8fdb175cd965d77a7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tx3-lang" -version = "0.7.2" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b920a74d0ee62f0ff63844333f9e81badfe705b6a01fbc94b52ec410873e716d" +checksum = "4faaad8f112e459a8e06c0ccd07756cf4b71a5d5545d1b95078c304dc74934fe" dependencies = [ "bincode", "hex", @@ -1224,6 +1235,7 @@ dependencies = [ "pest_derive", "serde", "thiserror", + "trait-variant", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 13d2bf4..2c27bb9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ tower-lsp = "0.20.0" tower = { version = "0.4", features = ["util"] } dashmap = "6.1.0" ropey = "1.6.1" -tx3-lang = "0.7.2" +tx3-lang = "0.11.0" pest = "2.7.15" miette = "7.5.0" serde_json = "1.0.140" diff --git a/src/ast_to_svg.rs b/src/ast_to_svg.rs index c0a4474..5f704d4 100644 --- a/src/ast_to_svg.rs +++ b/src/ast_to_svg.rs @@ -1,4 +1,5 @@ use std::fmt::Write; +use tx3_lang::ast::Identifier; use tx3_lang::ast::InputBlockField; use tx3_lang::ast::OutputBlockField; use tx3_lang::ast::Program; @@ -145,9 +146,11 @@ fn get_outputs(tx: &TxDef) -> Vec { .enumerate() .map(|(i, output)| { let name = output - .name .clone() - .unwrap_or_else(|| format!("output {}", i + 1)); + .name + .unwrap_or(Identifier::new(format!("output {}", i + 1))) + .value; + let party = output.fields.iter().find_map(|f| { if let OutputBlockField::To(address_expr) = f { address_expr @@ -158,6 +161,7 @@ fn get_outputs(tx: &TxDef) -> Vec { None } }); + Parameter { name, party } }) .collect() diff --git a/src/server.rs b/src/server.rs index 9ba4d4f..ca91b92 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,5 +1,6 @@ use serde_json::Value; use tower_lsp::{jsonrpc::Result, lsp_types::*, LanguageServer}; +use tx3_lang::ast::Identifier; use crate::{ cmds, position_to_offset, span_contains, span_to_lsp_range, @@ -189,7 +190,7 @@ impl LanguageServer for Context { for output in &tx.outputs { if let Some(output_name) = &output.name { - if output_name == &identifier.value { + if output_name == identifier { return Ok(Some(GotoDefinitionResponse::Scalar(Location { uri: uri.clone(), range: span_to_lsp_range(document.value(), &output.span), @@ -307,14 +308,17 @@ impl LanguageServer for Context { } } - for output in &tx.outputs { + for (i, output) in tx.outputs.iter().enumerate() { if span_contains(&output.span, offset) { - let default_output = "output".to_string(); + let default_output = Identifier::new(format!("output {}", i + 1)); let name = output.name.as_ref().unwrap_or(&default_output); return Ok(Some(Hover { contents: HoverContents::Markup(MarkupContent { kind: MarkupKind::Markdown, - value: format!("**Output**: `{}`\n\nTransaction output.", name), + value: format!( + "**Output**: `{}`\n\nTransaction output.", + name.value + ), }), range: Some(span_to_lsp_range(document.value(), &output.span)), })); @@ -360,10 +364,11 @@ impl LanguageServer for Context { if !tx.outputs.is_empty() { hover_text.push_str("**Outputs**:\n"); - for output in &tx.outputs { - let default_output = "output".to_string(); + for (i, output) in tx.outputs.iter().enumerate() { + let default_output = Identifier::new(format!("output {}", i + 1)); + let name = output.name.as_ref().unwrap_or(&default_output); - hover_text.push_str(&format!("- `{}`\n", name)); + hover_text.push_str(&format!("- `{}`\n", name.value)); } } @@ -456,9 +461,13 @@ impl LanguageServer for Context { )); } - for output in tx.outputs { + for (i, output) in tx.outputs.iter().enumerate() { + let default_output = Identifier::new(format!("output {}", i + 1)); + + let name = output.name.as_ref().unwrap_or(&default_output); + children.push(make_symbol( - output.name.unwrap_or_else(|| { "output" }.to_string()), + name.value.clone(), "Output".to_string(), SymbolKind::OBJECT, span_to_lsp_range(document.value(), &output.span), diff --git a/src/visitor.rs b/src/visitor.rs index e9b3bfa..d1610c1 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -58,6 +58,11 @@ fn visit_tx_def<'a>(tx: &'a tx3_lang::ast::TxDef, offset: usize) -> Option(tx: &'a tx3_lang::ast::TxDef, offset: usize) -> Option( None } -fn visit_burn_block<'a>( - bb: &'a tx3_lang::ast::BurnBlock, - offset: usize, -) -> Option> { - for field in &bb.fields { - match field { - tx3_lang::ast::MintBlockField::Amount(expr) => { - if let Some(sym) = visit_data_expr(expr, offset) { - return Some(sym); - } - } - tx3_lang::ast::MintBlockField::Redeemer(expr) => { - if let Some(sym) = visit_data_expr(expr, offset) { - return Some(sym); - } - } - } - } - None -} - fn visit_metadata_block<'a>( _mb: &'a tx3_lang::ast::MetadataBlock, _offset: usize, @@ -451,11 +430,11 @@ fn visit_policy_field<'a>( } fn visit_address_expr<'a>( - expr: &'a tx3_lang::ast::AddressExpr, + expr: &'a tx3_lang::ast::DataExpr, offset: usize, ) -> Option> { match expr { - tx3_lang::ast::AddressExpr::Identifier(id) => visit_identifier(id, offset), + tx3_lang::ast::DataExpr::Identifier(id) => visit_identifier(id, offset), _ => None, } }