Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: fix the cli args for startup of lsp #740

Merged
merged 3 commits into from
Oct 8, 2023
Merged
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
1 change: 1 addition & 0 deletions kclvm/tools/src/LSP/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod analysis;
mod capabilities;
mod completion;
mod config;
mod db;
Expand Down
70 changes: 35 additions & 35 deletions kclvm/tools/src/LSP/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::main_loop::main_loop;
use config::Config;
use main_loop::main_loop;
use main_loop::app;
mod analysis;
mod capabilities;
mod completion;
Expand All @@ -17,14 +18,43 @@ mod request;
mod state;
mod to_lsp;
mod util;
use clap::Command;

mod formatting;
#[cfg(test)]
mod tests;

/// Main entry point for the `kcl-language-server` executable.
fn main() -> Result<(), anyhow::Error> {
let args: Vec<String> = std::env::args().collect();
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
pub fn run_server() -> anyhow::Result<()> {
fn run_server() -> anyhow::Result<()> {
// Setup IO connections
let (connection, io_threads) = lsp_server::Connection::stdio();
// Wait for a client to connect
Expand Down Expand Up @@ -54,39 +84,9 @@ pub fn run_server() -> anyhow::Result<()> {
Ok(())
}

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

/// Main entry point for the `kcl-language-server` executable.
fn main() -> Result<(), anyhow::Error> {
let args: Vec<String> = std::env::args().collect();
let matches = app().arg_required_else_help(true).get_matches_from(args);
match matches.subcommand() {
Some(("version", _)) => {
println!("{}", kclvm_version::get_version_info());
Ok(())
}
_ => {
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(())
}
}
}

/// Get the kcl language server CLI application.
pub fn app() -> Command {
Command::new("kcl-language-server")
.version(kclvm_version::VERSION)
.about("KCL language server CLI.")
.subcommand(Command::new("version").about("Show the KCL language server version"))
}
11 changes: 11 additions & 0 deletions kclvm/tools/src/LSP/src/main_loop.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
use crate::config::Config;
use crate::state::LanguageServerState;
use clap::Command;
use lsp_server::Connection;

#[allow(dead_code)]
He1pa marked this conversation as resolved.
Show resolved Hide resolved
/// Runs the main loop of the language server. This will receive requests and handle them.
pub(crate) fn main_loop(connection: Connection, config: Config) -> anyhow::Result<()> {
LanguageServerState::new(connection.sender, config).run(connection.receiver)
}

#[allow(dead_code)]
/// Get the kcl language server CLI application.
pub(crate) fn app() -> Command {
Command::new("kcl-language-server")
.version(kclvm_version::VERSION)
.about("KCL language server CLI.")
.subcommand(Command::new("version").about("Show the KCL language server version"))
}
45 changes: 45 additions & 0 deletions kclvm/tools/src/LSP/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,3 +1205,48 @@ fn konfig_hover_test_main() {
_ => unreachable!("test error"),
}
}

#[test]
fn lsp_version_test() {
let args = vec!["kcl-language-server".to_string(), "version".to_string()];
let matches = crate::main_loop::app()
.arg_required_else_help(false)
.try_get_matches_from(args);
match matches {
Ok(arg_match) => match arg_match.subcommand() {
Some(("version", _)) => {}
_ => panic!("test failed"),
},
Err(_) => panic!("test failed"),
}
}

#[test]
fn lsp_run_test() {
let args = vec!["kcl-language-server".to_string()];
let matches = crate::main_loop::app()
.arg_required_else_help(false)
.try_get_matches_from(args);
match matches {
Ok(arg_match) => match arg_match.subcommand() {
None => {}
_ => panic!("test failed"),
},
Err(_) => panic!("test failed"),
}
}

#[test]
fn lsp_invalid_subcommand_test() {
let args = vec!["kcl-language-server".to_string(), "invalid".to_string()];
let matches = crate::main_loop::app()
.arg_required_else_help(false)
.try_get_matches_from(args);
match matches {
Ok(_) => panic!("test failed"),
Err(e) => match e.kind() {
clap::error::ErrorKind::InvalidSubcommand => {}
_ => panic!("test failed"),
},
}
}
2 changes: 1 addition & 1 deletion kclvm/tools/src/LSP/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use kclvm_error::Position as KCLPos;
use kclvm_parser::entry::get_dir_files;
use kclvm_parser::{load_program, ParseSession};
use kclvm_sema::resolver::resolve_program_with_opts;
use kclvm_sema::resolver::scope::ProgramScope;
use kclvm_sema::resolver::scope::Scope;
use kclvm_sema::resolver::{resolve_program, scope::ProgramScope};
use kclvm_utils::pkgpath::rm_external_pkg_name;
use lsp_types::Url;
use parking_lot::{RwLock, RwLockReadGuard};
Expand Down
Loading