Skip to content
Open
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
582 changes: 236 additions & 346 deletions Cargo.lock

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,26 @@ authors = [
license = "MIT"
version = "0.0.5"
edition = "2024"
build = "build.rs"

[dependencies]
clap = { version = "4.0.23", features = ["derive"] }
datex-core = { version = "0.0.6", features = ["default", "debug"] }
datex-core = { version = "0.0.7", git = "https://github.com/unyt-org/datex-core", branch = "feat/lsp", features = [
"default",
"debug",
] }

tokio = { version = "1.17.0", features = ["full"] }
tower-lsp = { version = "0.20.0", features = ["proposed"] }
#tower-lsp = { version = "0.20.0", features = ["proposed"]}
# using fork realhydroper-lsp because tower-lsp does not support local threading (single-threaded tokio runtime), which is required by the DATEX runtime
realhydroper-lsp = { version = "0.22.0", features = ["proposed"] }
serde = { version = "1.0", features = ["derive"] }
rustyline = "15.0.0"
ratatui = "0.29.0"
crossterm = "0.28.1"
home = "0.5.11"
serde_json = "1.0.145"


[build-dependencies]
serde_json = "1.0.145"
24 changes: 24 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::process::Command;

fn main() {
// Run `cargo metadata` to get dependency information
let output = Command::new("cargo")
.args(["metadata", "--format-version", "1"])
.output()
.expect("failed to run cargo metadata");

let metadata: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();

// Find the `serde` package version
let serde_version = metadata["packages"]
.as_array()
.unwrap()
.iter()
.find(|pkg| pkg["name"] == "datex-core")
.and_then(|pkg| pkg["version"].as_str())
.unwrap()
.to_string();

// Set environment variable for compile-time use
println!("cargo:rustc-env=DEP_DATEX_CORE_VERSION={}", serde_version);
}
15 changes: 10 additions & 5 deletions src/command_line_args.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::path::PathBuf;
use clap::{Args, Parser, Subcommand};
use std::path::PathBuf;

#[derive(Parser)]
#[command(author, version, about, long_about = None, bin_name = "datex")]
#[command(propagate_version = true)]
#[command(disable_version_flag = true)]
pub struct Cli {
#[command(subcommand)]
command: Option<Subcommands>,
pub command: Option<Subcommands>,
#[arg(short = 'V', long, help = "Print version")]
pub version: bool,
}

#[derive(Subcommand)]
Expand All @@ -20,6 +22,9 @@ pub enum Subcommands {
#[derive(Args)]
pub struct Run {
pub file: Option<String>,
/// optional path to dx config file
#[arg(short, long)]
pub config: Option<PathBuf>,
}

#[derive(Args)]
Expand All @@ -38,6 +43,6 @@ pub struct Repl {
#[derive(Args)]
pub struct Workbench {}

pub fn get_command() -> Option<Subcommands> {
Cli::parse().command
pub fn get_command() -> Cli {
Cli::parse()
}
55 changes: 0 additions & 55 deletions src/lsp/mod.rs

This file was deleted.

64 changes: 51 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,53 @@
use datex_core::compiler::workspace::CompilerWorkspace;
use datex_core::crypto::crypto_native::CryptoNative;
use datex_core::decompiler::{DecompileOptions, decompile_value};
use datex_core::lsp::create_lsp;
use datex_core::run_async;
use datex_core::runtime::global_context::{DebugFlags, GlobalContext, set_global_context};
use datex_core::runtime::{Runtime, RuntimeConfig};
use datex_core::runtime::{AsyncContext, Runtime, RuntimeConfig};
use datex_core::utils::time_native::TimeNative;
use datex_core::values::core_values::endpoint::Endpoint;
use std::path::PathBuf;
use std::sync::Arc;

mod command_line_args;
mod lsp;
mod repl;
mod utils;
mod workbench;

use crate::command_line_args::Repl;
use crate::lsp::Backend;
use crate::repl::{ReplOptions, repl};
use crate::utils::config::{ConfigError, create_runtime_with_config};
use command_line_args::{Subcommands, get_command};
use tower_lsp::{LspService, Server};
use realhydroper_lsp::{LspService, Server};

#[tokio::main]
async fn main() {
let command = get_command();

// print version
let command = if command.version {
println!("datex-cli {}", env!("CARGO_PKG_VERSION"));
println!("datex {}", env!("DEP_DATEX_CORE_VERSION"));
return;
} else {
command.command
};

if let Some(cmd) = command {
match cmd {
Subcommands::Lsp(lsp) => {
// println!("Running LSP");
Subcommands::Lsp(_) => {
let stdin = tokio::io::stdin();
let stdout = tokio::io::stdout();

let (service, socket) = LspService::new(|client| Backend { client });
Server::new(stdin, stdout, socket).serve(service).await;
let runtime = Runtime::new(
RuntimeConfig::new_with_endpoint(Endpoint::default()),
AsyncContext::new(),
);
create_lsp(runtime, stdin, stdout).await;
}
Subcommands::Run(run) => {
if run.file.is_some() {
println!("File: {}", run.file.unwrap())
}
let runtime = Runtime::new(RuntimeConfig::default());
execute_file(run).await;
}
Subcommands::Repl(Repl { verbose, config }) => {
let options = ReplOptions {
Expand All @@ -57,6 +67,34 @@ async fn main() {
}
}

async fn execute_file(run: command_line_args::Run) {
run_async! {
if let Some(file) = run.file {
let runtime = create_runtime_with_config(run.config, false, false).await.unwrap();
// yield to wait for connect. TODO: better way
tokio::task::yield_now().await;
let file_contents = std::fs::read_to_string(file).expect("Could not read file");
let _result = runtime.execute(&file_contents, &[], None).await;
if let Err(e) = _result {
eprintln!("{}", e);
}
else {
let result = _result.unwrap();
if let Some(output) = result {
let formatted_output = decompile_value(
&output,
DecompileOptions::colorized()
);
println!("{}", formatted_output);
}
}
}
else {
eprintln!("No file provided to run.");
}
}
}

async fn workbench(config_path: Option<PathBuf>, debug: bool) -> Result<(), ConfigError> {
set_global_context(GlobalContext {
crypto: Arc::new(CryptoNative),
Expand All @@ -65,7 +103,7 @@ async fn workbench(config_path: Option<PathBuf>, debug: bool) -> Result<(), Conf
});

run_async! {
let runtime = create_runtime_with_config(config_path, debug).await?;
let runtime = create_runtime_with_config(config_path, debug, false).await?;
workbench::start_workbench(runtime).await?;

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions src/repl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::config::{create_runtime_with_config, ConfigError};
use crate::utils::config::{ConfigError, create_runtime_with_config};
use datex_core::crypto::crypto_native::CryptoNative;
use datex_core::decompiler::{DecompileOptions, apply_syntax_highlighting, decompile_value};
use datex_core::run_async;
Expand Down Expand Up @@ -108,7 +108,7 @@ pub async fn repl(options: ReplOptions) -> Result<(), ReplError> {
let (response_sender, response_receiver) = tokio::sync::mpsc::channel::<ReplResponse>(100);

run_async! {
let runtime = create_runtime_with_config(options.config_path, options.verbose).await?;
let runtime = create_runtime_with_config(options.config_path, options.verbose, true).await?;

repl_loop(cmd_sender, response_receiver)?;

Expand Down
17 changes: 10 additions & 7 deletions src/utils/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ pub fn get_config(custom_config_path: Option<PathBuf>) -> Result<RuntimeConfig,
pub async fn create_runtime_with_config(
custom_config_path: Option<PathBuf>,
force_debug: bool,
print_header: bool,
) -> Result<Runtime, ConfigError> {
let mut config = get_config(custom_config_path)?;
// overwrite debug mode if force_debug is true
Expand All @@ -137,14 +138,16 @@ pub async fn create_runtime_with_config(
}
let runtime = Runtime::create_native(config).await;

let cli_version = env!("CARGO_PKG_VERSION");
if print_header {
let cli_version = env!("CARGO_PKG_VERSION");

println!("================================================");
println!("DATEX REPL v{cli_version}");
println!("DATEX Core version: {}", runtime.version);
println!("Endpoint: {}", runtime.endpoint());
println!("\nexit using [CTRL + C]");
println!("================================================\n");
println!("================================================");
println!("DATEX REPL v{cli_version}");
println!("DATEX Core version: {}", runtime.version);
println!("Endpoint: {}", runtime.endpoint());
println!("\nexit using [CTRL + C]");
println!("================================================\n");
}

Ok(runtime)
}
2 changes: 1 addition & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod config;
pub mod config;
4 changes: 3 additions & 1 deletion src/workbench/views/comhub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ impl Widget for &ComHub {
(match &socket.endpoint {
Some(endpoint) => endpoint.to_string(),
None => "unknown".to_string(),
}).to_string().into(),
})
.to_string()
.into(),
]));
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/workbench/views/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ impl Widget for &Metadata {
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::White));


let lines = vec![
Line::from(vec![
"Endpoint: ".into(),
self.runtime.endpoint().to_string().bold(),
]),
Line::from(vec!["Version: ".into(), self.runtime.version.clone().bold()]),
Line::from(vec![
"Version: ".into(),
self.runtime.version.clone().bold(),
]),
// Line::from(vec![
// "Allocated pointers: ".into(),
// self.runtime
Expand Down
4 changes: 2 additions & 2 deletions src/workbench/workbench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Workbench {
runtime: runtime.clone(),
};
let comhub = ComHub {
runtime: runtime.clone()
runtime: runtime.clone(),
};

Workbench {
Expand All @@ -40,7 +40,7 @@ impl Workbench {
while !self.exit {
terminal.draw(|frame| self.draw(frame))?;
self.handle_events()?;

// // add ptr to the runtime
// let id = random_bytes_slice::<26>();
// runtime
Expand Down
Loading