Skip to content

Commit e085afb

Browse files
JungoQuentinazjezz
andcommitted
feat(cli): add lsp bases
from: carthage-software#74 Co-Authored-By: Saif Eddin Gmati <[email protected]>
1 parent af74809 commit e085afb

File tree

9 files changed

+602
-11
lines changed

9 files changed

+602
-11
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
/target
33

44
/vendor/
5+
6+
/editors

Cargo.lock

Lines changed: 135 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ memchr = "2.7.4"
8989
parking_lot = "0.12.3"
9090
dialoguer = "0.11.0"
9191
unicode-width = { version = "0.2.0", default-features = false }
92+
tower-lsp = "0.20.0"
9293

9394
[dependencies]
9495
mago-syntax = { workspace = true }
@@ -104,8 +105,9 @@ mago-fixer = { workspace = true }
104105
mago-php-version = { workspace = true }
105106
mago-reference = { workspace = true }
106107
mago-composer = { workspace = true }
108+
mago-span = { workspace = true }
107109
serde = { workspace = true }
108-
tokio = { workspace = true, features = ["rt", "rt-multi-thread", "time"] }
110+
tokio = { workspace = true, features = ["rt", "rt-multi-thread", "time", "io-std"] }
109111
clap = { workspace = true }
110112
ahash = { workspace = true }
111113
termtree = { workspace = true }
@@ -125,6 +127,7 @@ tracing-subscriber = { workspace = true }
125127
indicatif = { workspace = true }
126128
colored = { workspace = true }
127129
dialoguer = { workspace = true }
130+
tower-lsp = { workspace = true }
128131

129132
[target.'cfg(target_os = "linux")'.dependencies]
130133
openssl = { workspace = true }

src/commands/lsp.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::process::ExitCode;
2+
3+
use clap::Parser;
4+
use tower_lsp::LspService;
5+
use tower_lsp::Server;
6+
7+
use mago_interner::ThreadedInterner;
8+
9+
use crate::error::Error;
10+
use crate::lsp::MagoLanguageServer;
11+
12+
/// Represents the `lsp` command, which starts the Language Server Protocol (LSP) server.
13+
#[derive(Parser, Debug)]
14+
#[command(
15+
name = "lsp",
16+
about = "Starts the Language Server Protocol (LSP) server.",
17+
long_about = r#"
18+
The `lsp` command starts the Language Server Protocol (LSP) server, which can be used to provide
19+
editor support for PHP files. The server will listen for incoming connections on stdin and stdout.
20+
"#
21+
)]
22+
pub struct LspCommand {}
23+
24+
/// Executes the LSP command with the provided options.
25+
///
26+
/// # Arguments
27+
///
28+
/// * `command` - The `LspCommand` structure containing user-specified options.
29+
///
30+
/// # Returns
31+
///
32+
/// An `ExitCode` indicating the success or failure of the command.
33+
pub async fn execute(_command: LspCommand) -> Result<ExitCode, Error> {
34+
let interner = ThreadedInterner::new();
35+
36+
let (stdin, stdout) = (tokio::io::stdin(), tokio::io::stdout());
37+
38+
let (service, socket) = LspService::new(|client| MagoLanguageServer::new(interner, client));
39+
Server::new(stdin, stdout, socket).serve(service).await;
40+
41+
Ok(ExitCode::SUCCESS)
42+
}

src/commands/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::commands::find::FindCommand;
1313
use crate::commands::format::FormatCommand;
1414
use crate::commands::init::InitCommand;
1515
use crate::commands::lint::LintCommand;
16+
use crate::commands::lsp::LspCommand;
1617
use crate::commands::self_update::SelfUpdateCommand;
1718
use crate::error::Error;
1819

@@ -21,6 +22,7 @@ pub mod find;
2122
pub mod format;
2223
pub mod init;
2324
pub mod lint;
25+
pub mod lsp;
2426
pub mod self_update;
2527

2628
/// Styling for the Mago CLI.
@@ -51,6 +53,9 @@ pub enum MagoCommand {
5153
/// Find references to symbols in PHP code.
5254
#[command(name = "find")]
5355
Find(FindCommand),
56+
/// Start the language server
57+
#[command(name = "lsp")]
58+
Lsp(LspCommand),
5459
/// Update Mago to the latest version.
5560
#[command(name = "self-update")]
5661
SelfUpdate(SelfUpdateCommand),

0 commit comments

Comments
 (0)