Skip to content

Commit

Permalink
chore: fmt code
Browse files Browse the repository at this point in the history
Signed-off-by: He1pa <[email protected]>
  • Loading branch information
He1pa committed Oct 8, 2023
1 parent 64b62c8 commit 2c8420f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 73 deletions.
2 changes: 1 addition & 1 deletion kclvm/tools/src/LSP/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod analysis;
mod completion;
mod capabilities;
mod completion;
mod config;
mod db;
mod dispatcher;
Expand Down
69 changes: 66 additions & 3 deletions kclvm/tools/src/LSP/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use main_loop::_main;

use crate::main_loop::main_loop;
use config::Config;
use main_loop::app;
mod analysis;
mod capabilities;
mod completion;
Expand All @@ -25,5 +26,67 @@ mod tests;
/// Main entry point for the `kcl-language-server` executable.
fn main() -> Result<(), anyhow::Error> {
let args: Vec<String> = std::env::args().collect();
_main(args)
let matches = app()
.arg_required_else_help(false)
.try_get_matches_from(args);
match matches {
Ok(arg_matches) => match arg_matches.subcommand() {
Some(("version", _)) => {
println!("{}", kclvm_version::get_version_info());
Ok(())
}
Some((subcommand, _)) => Err(anyhow::anyhow!("unknown subcommand: {}", subcommand)),
None => {
let status: Result<ExitStatus, anyhow::Error> = {
run_server().map_err(|e| anyhow::anyhow!("{}", e))?;
Ok(ExitStatus::Success)
};
match status.unwrap() {
ExitStatus::Success => {}
ExitStatus::Error => std::process::exit(1),
};
Ok(())
}
},
Err(e) => Err(e.into()),
}
}

#[allow(dead_code)]
/// Main entry point for the language server
fn run_server() -> anyhow::Result<()> {
// Setup IO connections
let (connection, io_threads) = lsp_server::Connection::stdio();
// Wait for a client to connect
let (initialize_id, initialize_params) = connection.initialize_start()?;

let initialize_params =
util::from_json::<lsp_types::InitializeParams>("InitializeParams", initialize_params)?;

let server_capabilities = capabilities::server_capabilities(&initialize_params.capabilities);

let initialize_result = lsp_types::InitializeResult {
capabilities: server_capabilities,
server_info: Some(lsp_types::ServerInfo {
name: String::from("kcl-language-server"),
version: None,
}),
};

let initialize_result = serde_json::to_value(initialize_result)
.map_err(|_| anyhow::anyhow!("Initialize result error"))?;

connection.initialize_finish(initialize_id, initialize_result)?;

let config = Config::default();
main_loop(connection, config)?;
io_threads.join()?;
Ok(())
}

#[allow(dead_code)]
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
enum ExitStatus {
Success,
Error,
}
67 changes: 0 additions & 67 deletions kclvm/tools/src/LSP/src/main_loop.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::config::Config;
use crate::state::LanguageServerState;
use crate::{capabilities, util};
use clap::Command;
use lsp_server::Connection;

Expand All @@ -10,72 +9,6 @@ pub(crate) fn main_loop(connection: Connection, config: Config) -> anyhow::Resul
LanguageServerState::new(connection.sender, config).run(connection.receiver)
}

#[allow(dead_code)]
/// Main entry point for the language server
fn run_server() -> anyhow::Result<()> {
// Setup IO connections
let (connection, io_threads) = lsp_server::Connection::stdio();
// Wait for a client to connect
let (initialize_id, initialize_params) = connection.initialize_start()?;

let initialize_params =
util::from_json::<lsp_types::InitializeParams>("InitializeParams", initialize_params)?;

let server_capabilities = capabilities::server_capabilities(&initialize_params.capabilities);

let initialize_result = lsp_types::InitializeResult {
capabilities: server_capabilities,
server_info: Some(lsp_types::ServerInfo {
name: String::from("kcl-language-server"),
version: None,
}),
};

let initialize_result = serde_json::to_value(initialize_result)
.map_err(|_| anyhow::anyhow!("Initialize result error"))?;

connection.initialize_finish(initialize_id, initialize_result)?;

let config = Config::default();
main_loop(connection, config)?;
io_threads.join()?;
Ok(())
}

#[allow(dead_code)]
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
enum ExitStatus {
Success,
Error,
}

pub(crate) fn _main(args: Vec<String>) -> Result<(), anyhow::Error> {
let matches = app()
.arg_required_else_help(false)
.try_get_matches_from(args);
match matches {
Ok(arg_matches) => match arg_matches.subcommand() {
Some(("version", _)) => {
println!("{}", kclvm_version::get_version_info());
Ok(())
}
Some((subcommand, _)) => Err(anyhow::anyhow!("unknown subcommand: {}", subcommand)),
None => {
let status: Result<ExitStatus, anyhow::Error> = {
run_server().map_err(|e| anyhow::anyhow!("{}", e))?;
Ok(ExitStatus::Success)
};
match status.unwrap() {
ExitStatus::Success => {}
ExitStatus::Error => std::process::exit(1),
};
Ok(())
}
},
Err(e) => Err(e.into()),
}
}

#[allow(dead_code)]
/// Get the kcl language server CLI application.
pub(crate) fn app() -> Command {
Expand Down
3 changes: 1 addition & 2 deletions kclvm/tools/src/LSP/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ use crate::config::Config;
use crate::from_lsp::file_path_from_url;

use crate::hover::hover;
use crate::main_loop::_main;
use crate::main_loop::main_loop;
use crate::to_lsp::kcl_diag_to_lsp_diags;
use crate::util::to_json;
Expand Down Expand Up @@ -1244,7 +1243,7 @@ fn lsp_invalid_subcommand_test() {
.arg_required_else_help(false)
.try_get_matches_from(args);
match matches {
Ok(arg_match) => panic!("test failed"),
Ok(_) => panic!("test failed"),
Err(e) => match e.kind() {
clap::error::ErrorKind::InvalidSubcommand => {}
_ => panic!("test failed"),
Expand Down

0 comments on commit 2c8420f

Please sign in to comment.