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

feat: Config builder #163

Merged
merged 4 commits into from
Nov 5, 2024
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
66 changes: 66 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ license = "BSD-2-Clause"
[workspace.dependencies]
anyhow = "1.0.86"
bincode = "1.3.3"
dirs = "5.0.1"
toml = "0.8.1"
clap = { version = "4.5.8", features = ["derive", "cargo"] }
serde = { version = "1.0.204", features = ["derive"] }

[workspace.lints.clippy]
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ created for different sub-directories or files within your project as `project`s
Source files not contained within any `project` configs will use the default configuration
if provided.

#### Config Builder

Creating a `.asm-lsp.toml` file manually is fine, but can be error-prone as projects
grow in complexity. Running `asm-lsp gen-config` will walk you through the creation
of a config interactively, with informative prompts and extra validation checks
along the way.

```
$ asm-lsp gen-config --help
Generate a .asm-lsp.toml config file

Usage: asm-lsp gen-config [OPTIONS]

Options:
-o, --output-dir <OUTPUT_DIR> Directory to place .asm-lsp.toml into. (Default is the current directory)
-g, --global-cfg Place the config in the global config directory
-p, --project-path <PROJECT_PATH> Path to the project this config is being generated for. (Default is the current directory)
-w, --overwrite Overwrite any existing .asm-lsp.toml in the target directory
-q, --quiet Don't display the generated config file after generation
-h, --help Print help
```

#### NOTE

If the server reads in an invalid configuration file, it will display an error
Expand Down
6 changes: 4 additions & 2 deletions asm-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ path = "bin/main.rs"
anyhow.workspace = true
bincode.workspace = true
serde.workspace = true
dirs.workspace = true
toml.workspace = true
clap.workspace = true
# write to stderr instead of stdout
flexi_logger = "0.29.3"
log = { version = "0.4.17" }
Expand All @@ -35,10 +38,8 @@ reqwest = { version = "0.12.8", features = ["blocking"] }
strum = "0.26.3"
strum_macros = "0.26.4"
serde_json = "1.0.94"
toml = "0.8.1"
home = "0.5.5"
once_cell = "1.18.0"
dirs = "5.0.1"
symbolic = { version = "12.8.0", features = ["demangle"] }
symbolic-demangle = "12.8.0"
url-escape = "0.1.1"
Expand All @@ -47,6 +48,7 @@ lsp-textdocument = "0.4.0"
tree-sitter = "0.22.6"
tree-sitter-asm = "0.22.6"
compile_commands = "0.3.0"
dialoguer = { version = "0.11.0", features = ["fuzzy-select"] }

[dev-dependencies]
mockito = "1.2.0"
Expand Down
61 changes: 59 additions & 2 deletions asm-lsp/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::thread::sleep;
use std::time::Duration;

use asm_lsp::config_builder::{gen_config, GenerateArgs, GenerateOpts};
use asm_lsp::run_info;
use asm_lsp::types::LspClient;

use asm_lsp::handle::{handle_notification, handle_request};
Expand All @@ -9,6 +11,7 @@ use asm_lsp::{
send_notification, DocumentStore, RootConfig, ServerStore,
};

use clap::{Command, FromArgMatches as _, Subcommand};
use lsp_types::{
CompletionItemKind, CompletionOptions, CompletionOptionsCompletionItem, DiagnosticOptions,
DiagnosticServerCapabilities, HoverProviderCapability, InitializeParams, MessageType, OneOf,
Expand All @@ -20,7 +23,61 @@ use anyhow::Result;
use log::{info, warn};
use lsp_server::{Connection, Message};

/// Entry point of the server. Connects to the client, loads documentation resources,
#[derive(Subcommand)]
enum Commands {
GenConfig(GenerateArgs),
/// Print information about asm-lsp
Info,
#[clap(hide(true))]
Run,
}

/// Entry point of the lsp. Runs a subcommand is specified, otherwise starts the
/// lsp server
///
/// # Errors
///
/// Returns `Err` on failure to parse arguments, if a sub command fails, or if the
/// server returns an error
pub fn main() -> Result<()> {
let cli = Command::new("asm-lsp").subcommand_required(false);
let cli = Commands::augment_subcommands(cli);

let command = match Commands::from_arg_matches(&cli.get_matches()) {
Ok(cmd) => cmd,
Err(e) => {
// If no subcommand is provided, run the server as normal
if e.kind() == clap::error::ErrorKind::MissingSubcommand {
Commands::Run
} else {
eprintln!("{e}");
std::process::exit(1);
}
}
};
match command {
Commands::GenConfig(args) => {
let opts: GenerateOpts = match args.try_into() {
Ok(opts) => opts,
Err(e) => {
eprintln!("{e}");
std::process::exit(1);
}
};

if let Err(e) = gen_config(&opts) {
eprintln!("Error: {e}");
std::process::exit(1);
}
}
Commands::Info => run_info(),
Commands::Run => run_lsp()?,
}

Ok(())
}

/// Entry point of the lsp server. Connects to the client, loads documentation resources,
/// and then enters the main loop
///
/// # Errors
Expand All @@ -30,7 +87,7 @@ use lsp_server::{Connection, Message};
/// # Panics
///
/// Panics if JSON serialization of the server capabilities fails
pub fn main() -> Result<()> {
pub fn run_lsp() -> Result<()> {
// initialisation
// Set up logging. Because `stdio_transport` gets a lock on stdout and stdin, we must have our
// logging only write out to stderr.
Expand Down
Loading
Loading