Skip to content

Commit

Permalink
bugfix: fix the cli args for startup of lsp (#740)
Browse files Browse the repository at this point in the history
* bugfix: fix the cli args for normal startup of lsp to be consistent with client of vscode

Signed-off-by: He1pa <[email protected]>

* test: add invalid subcommand unit test

Signed-off-by: He1pa <[email protected]>

* chore: fmt code

Signed-off-by: He1pa <[email protected]>

---------

Signed-off-by: He1pa <[email protected]>
  • Loading branch information
He1pa committed Oct 8, 2023
1 parent 7c585c5 commit e5f0482
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 36 deletions.
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)]
/// 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

0 comments on commit e5f0482

Please sign in to comment.