Skip to content

Commit

Permalink
feat: Add command line arguments support
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-prusov committed Oct 19, 2024
1 parent 85a11c6 commit 6b3bf3c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 12 deletions.
35 changes: 35 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use clap::Parser;
use std::sync::OnceLock;

#[derive(clap::Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Enable experimental features
#[arg(long)]
experimental: bool,
}

#[derive(Debug)]
pub struct Config {
#[allow(dead_code)]
pub experimental: bool,
pub process_neighbours: bool,
}

impl Default for Config {
fn default() -> Self {
Self {
experimental: false,
process_neighbours: true,
}
}
}

pub static CONFIG: OnceLock<Config> = OnceLock::new();

pub fn get() -> &'static Config {
CONFIG.get_or_init(|| Config {
experimental: Args::parse().experimental,
..Default::default()
})
}
13 changes: 11 additions & 2 deletions src/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ use logger::LogProcessor;
use std::fs::read_to_string;
use std::sync::mpsc;
use utils::current_url;
use utils::Leakable;

impl Leakable for Config {}

async fn make_backend_ext(path: &str, process_neighbours: bool) -> Backend {
// Go to test directory, each test directory emulates a workspace
let handle = tokio::runtime::Handle::current();
LogProcessor::local_set(LogProcessor::Strict);
let be = Backend {
data: Workspace::new(handle, None),
let config = Config {
process_neighbours,
..Default::default()
}
.leak();

let be = Backend {
data: Workspace::new(handle, None, config),
client: None,
config,
};
let mut root = current_url().unwrap();
let root_path = root.path().to_string() + "/" + path;
Expand Down
14 changes: 8 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use config::Config;
use logger::log_message;
use logger::Logger;
use std::collections::HashMap;
Expand All @@ -12,6 +13,7 @@ use tree_sitter::Parser;
use tree_sitter::Point;
use utils::convert_range;

mod config;
mod diagnostics;
mod file_depot;
mod includes_depot;
Expand All @@ -29,15 +31,15 @@ use workspace::Workspace;
struct Backend {
data: Workspace,
client: Option<Client>,
process_neighbours: bool,
config: &'static Config,
}

impl Backend {
fn new(handle: Handle, client: Client) -> Self {
fn new(handle: Handle, client: Client, config: &'static Config) -> Self {
Backend {
data: Workspace::new(handle, Some(client.clone())),
process_neighbours: true,
data: Workspace::new(handle, Some(client.clone()), config),
client: Some(client),
config,
}
}

Expand Down Expand Up @@ -118,7 +120,7 @@ impl LanguageServer for Backend {
let text = params.text_document.text.as_str();
self.data.handle_file(uri, Some(text.to_string()));

if self.process_neighbours {
if self.config.process_neighbours {
self.data.open_neighbours(uri);
}
}
Expand Down Expand Up @@ -353,7 +355,7 @@ async fn main() {
let (service, socket) = LspService::new(|client| {
let handle = tokio::runtime::Handle::current();
Logger::set(Logger::Lsp(handle.clone(), client.clone()));
Backend::new(handle, client)
Backend::new(handle, client, config::get())
});
Server::new(stdin, stdout, socket).serve(service).await;
}
19 changes: 19 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::borrow::Cow;
#[cfg(test)]
use std::sync::Mutex;
use tower_lsp::jsonrpc::Error;
use tower_lsp::jsonrpc::Result;
use tower_lsp::lsp_types::{Position, Range, Url};
Expand Down Expand Up @@ -63,3 +65,20 @@ pub fn current_url() -> Result<Url> {
};
Ok(uri)
}

// Leak object, but keep reference in static array to avoid valgrind errors
#[cfg(test)]
pub trait Leakable {
fn leak(self) -> &'static Self
where
Self: Sized,
Self: Sync,
Self: Send,
{
static LEAKED: Mutex<Vec<&(dyn Leakable + Sync + Send)>> = Mutex::new(Vec::new());
let leak = Box::leak(Box::new(self));
let mut v = LEAKED.lock().unwrap();
v.push(leak);
leak
}
}
11 changes: 7 additions & 4 deletions src/workspace.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config::Config;
use crate::file_depot;
use crate::file_depot::FileDepot;
use crate::includes_depot::IncludesDepot;
Expand All @@ -22,6 +23,7 @@ use tree_sitter::Tree;
use crate::diagnostics;

pub struct Workspace {
config: &'static Config,
handle: Handle,
client: Option<Client>,
pub fd: FileDepot,
Expand All @@ -31,9 +33,10 @@ pub struct Workspace {
}

impl Workspace {
pub fn new(handle: Handle, client: Option<Client>) -> Workspace {
pub fn new(handle: Handle, client: Option<Client>, config: &'static Config) -> Workspace {
let fd = FileDepot::new();
Workspace {
config,
ld: LabelsDepot::new(&fd),
rd: ReferencesDepot::new(&fd),
id: IncludesDepot::new(&fd),
Expand All @@ -43,8 +46,6 @@ impl Workspace {
}
}

// TODO: Temporarily disabled due to false positives
#[allow(dead_code)]
fn process_diagnostics(&self, tree: &Tree, uri: &Url) {
let diagnostics = diagnostics::gather(tree);
let u = uri.clone();
Expand Down Expand Up @@ -204,7 +205,9 @@ impl Workspace {
}

self.process_labels(&tree, uri, &text);
//self.process_diagnostics(&tree, uri);
if self.config.experimental {
self.process_diagnostics(&tree, uri);
}
self.process_references(&tree, uri, &text);
self.process_includes(&tree, uri, &text)
}
Expand Down

0 comments on commit 6b3bf3c

Please sign in to comment.