Skip to content
Closed
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 @@ -12,7 +12,7 @@ djls-templates = { path = "crates/djls-templates" }

anyhow = "1.0"
async-trait = "0.1"
pyo3 = "0.24"
pyo3 = { version = "0.24", features = ["abi3-py39"] }
pyo3-async-runtimes = "0.24"
pyo3-build-config = "0.24"
serde = { version = "1.0", features = ["derive"] }
Expand Down
9 changes: 2 additions & 7 deletions crates/djls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@ name = "djls"
version = "5.2.0-alpha"
edition = "2021"

[lib]
[[bin]]
name = "djls"
crate-type = ["cdylib"]
path = "src/main.rs"

[features]
extension-module = [
"djls-server/extension-module",
"djls-project/extension-module",
"pyo3/extension-module"
]
default = []

[dependencies]
Expand Down
101 changes: 101 additions & 0 deletions crates/djls/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
mod commands;

use crate::commands::Serve;
use anyhow::Result;
use clap::{Parser, Subcommand};
use pyo3;
use std::process::ExitCode;

#[derive(Parser)]
#[command(name = "djls")]
#[command(version, about, long_about = None)]
pub struct Cli {
#[command(subcommand)]
command: Command,

#[command(flatten)]
args: Args,
}

#[derive(Debug, Subcommand)]
enum Command {
/// Start the LSP server
Serve(Serve),
}

#[derive(Parser)]
pub struct Args {
#[command(flatten)]
global: GlobalArgs,
}

#[derive(Parser, Debug, Clone)]
struct GlobalArgs {
/// Do not print any output.
#[arg(global = true, long, short, conflicts_with = "verbose")]
pub quiet: bool,

/// Use verbose output.
#[arg(global = true, action = clap::ArgAction::Count, long, short, conflicts_with = "quiet")]
pub verbose: u8,
}

async fn run() -> Result<ExitCode> {
let cli = Cli::parse();

match cli.command {
Command::Serve(_serve) => djls_server::serve().await?,
}

Ok(ExitCode::SUCCESS)
}

fn main() -> ExitCode {
// Initialize Python interpreter
pyo3::prepare_freethreaded_python();

let runtime = tokio::runtime::Runtime::new().unwrap();
let local = tokio::task::LocalSet::new();
let exit_code = local.block_on(&runtime, async move {
tokio::select! {
// The main CLI program
result = run() => {
match result {
Ok(code) => code,
Err(e) => {
eprintln!("Error: {}", e);
if let Some(source) = e.source() {
eprintln!("Caused by: {}", source);
}
ExitCode::FAILURE
}
}
}
// Ctrl+C handling
_ = tokio::signal::ctrl_c() => {
println!("\nReceived Ctrl+C, shutting down...");
// Cleanup code here if needed
ExitCode::from(130) // Standard Ctrl+C exit code
}
// SIGTERM handling (Unix only)
_ = async {
#[cfg(unix)]
{
use tokio::signal::unix::{signal, SignalKind};
let mut term = signal(SignalKind::terminate()).unwrap();
term.recv().await;
}
#[cfg(not(unix))]
{
// On non-unix platforms, this future never completes
std::future::pending::<()>().await;
}
} => {
println!("\nReceived termination signal, shutting down...");
ExitCode::from(143) // Standard SIGTERM exit code
}
}
});

exit_code
}
4 changes: 1 addition & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ classifiers = [
"Topic :: Text Editors :: Integrated Development Environments (IDE)"
]

[project.scripts]
djls = "djls:entrypoint"

[project.urls]
Documentation = "https://django-language-server.readthedocs.io/"
Issues = "https://github.com/joshuadavidthomas/django-language-server/issues"
Expand Down Expand Up @@ -110,6 +107,7 @@ indent_size = 2

[tool.maturin]
manifest-path = "crates/djls/Cargo.toml"
bindings = "bin"
strip = true
include = [
{ path = "LICENSE", format = "sdist" },
Expand Down
Loading