Skip to content

Commit

Permalink
WIP: multi-config
Browse files Browse the repository at this point in the history
  • Loading branch information
WillLillis committed Oct 17, 2024
1 parent 7342997 commit 25dd4c9
Show file tree
Hide file tree
Showing 5 changed files with 664 additions and 241 deletions.
87 changes: 34 additions & 53 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use asm_lsp::handle::{
handle_references_request, handle_signature_help_request,
};
use asm_lsp::{
get_compile_cmds, get_completes, get_config, get_include_dirs, instr_filter_targets,
populate_name_to_directive_map, populate_name_to_instruction_map,
populate_name_to_register_map, Arch, Assembler, Config, Instruction, NameToInfoMaps, TreeStore,
get_compile_cmds, get_completes, get_config, get_include_dirs, populate_name_to_directive_map,
populate_name_to_instruction_map, populate_name_to_register_map, Arch, Assembler, Instruction,
NameToInfoMaps, RootConfig, TreeStore,
};

use compile_commands::{CompilationDatabase, SourceFile};
Expand Down Expand Up @@ -53,7 +53,7 @@ pub fn main() -> Result<()> {
flexi_logger::Logger::try_with_str("info")?.start()?;

// LSP server initialisation ------------------------------------------------------------------
info!("Starting asm_lsp...");
info!("Starting asm_lsp-{}", env!("CARGO_PKG_VERSION"));

// Create the transport
let (connection, _io_threads) = Connection::stdio();
Expand Down Expand Up @@ -119,25 +119,18 @@ pub fn main() -> Result<()> {
if let Some(ref client_info) = params.client_info {
if client_info.name.eq("helix") {
info!("Helix LSP client detected");
config.client = Some(LspClient::Helix);
config.set_client(LspClient::Helix);
}
}

let mut names_to_info = NameToInfoMaps::default();
// create a map of &Instruction_name -> &Instruction - Use that in user queries
// The Instruction(s) themselves are stored in a vector and we only keep references to the
// former map
let x86_instructions = if config.instruction_sets.x86.unwrap_or(false) {
let x86_instructions = if config.is_isa_enabled(Arch::X86) {
let start = std::time::Instant::now();
let x86_instrs = include_bytes!("../../docs_store/opcodes/serialized/x86");
let instrs = bincode::deserialize::<Vec<Instruction>>(x86_instrs)?
.into_iter()
.map(|instruction| {
// filter out assemblers by user config
instr_filter_targets(&instruction, &config)
})
.filter(|instruction| !instruction.forms.is_empty())
.collect();
let instrs = bincode::deserialize::<Vec<Instruction>>(x86_instrs)?;
info!(
"x86 instruction set loaded in {}ms",
start.elapsed().as_millis()
Expand All @@ -147,17 +140,10 @@ pub fn main() -> Result<()> {
Vec::new()
};

let x86_64_instructions = if config.instruction_sets.x86_64.unwrap_or(false) {
let x86_64_instructions = if config.is_isa_enabled(Arch::X86_64) {
let start = std::time::Instant::now();
let x86_64_instrs = include_bytes!("../../docs_store/opcodes/serialized/x86_64");
let instrs = bincode::deserialize::<Vec<Instruction>>(x86_64_instrs)?
.into_iter()
.map(|instruction| {
// filter out assemblers by user config
instr_filter_targets(&instruction, &config)
})
.filter(|instruction| !instruction.forms.is_empty())
.collect();
let instrs = bincode::deserialize::<Vec<Instruction>>(x86_64_instrs)?;
info!(
"x86-64 instruction set loaded in {}ms",
start.elapsed().as_millis()
Expand All @@ -167,17 +153,10 @@ pub fn main() -> Result<()> {
Vec::new()
};

let z80_instructions = if config.instruction_sets.z80.unwrap_or(false) {
let z80_instructions = if config.is_isa_enabled(Arch::Z80) {
let start = std::time::Instant::now();
let z80_instrs = include_bytes!("../../docs_store/opcodes/serialized/z80");
let instrs = bincode::deserialize::<Vec<Instruction>>(z80_instrs)?
.into_iter()
.map(|instruction| {
// filter out assemblers by user config
instr_filter_targets(&instruction, &config)
})
.filter(|instruction| !instruction.forms.is_empty())
.collect();
let instrs = bincode::deserialize::<Vec<Instruction>>(z80_instrs)?;
info!(
"z80 instruction set loaded in {}ms",
start.elapsed().as_millis()
Expand All @@ -187,7 +166,7 @@ pub fn main() -> Result<()> {
Vec::new()
};

let arm_instructions = if config.instruction_sets.arm.unwrap_or(false) {
let arm_instructions = if config.is_isa_enabled(Arch::ARM) {
let start = std::time::Instant::now();
let arm_instrs = include_bytes!("../../docs_store/opcodes/serialized/arm");
// NOTE: No need to filter these instructions by assembler like we do for
Expand All @@ -202,7 +181,7 @@ pub fn main() -> Result<()> {
Vec::new()
};

let riscv_instructions = if config.instruction_sets.riscv.unwrap_or(false) {
let riscv_instructions = if config.is_isa_enabled(Arch::RISCV) {
let start = std::time::Instant::now();
let riscv_instrs = include_bytes!("../../docs_store/opcodes/serialized/riscv");
// NOTE: No need to filter these instructions by assembler like we do for
Expand Down Expand Up @@ -246,7 +225,7 @@ pub fn main() -> Result<()> {
// create a map of &Register_name -> &Register - Use that in user queries
// The Register(s) themselves are stored in a vector and we only keep references to the
// former map
let x86_registers = if config.instruction_sets.x86.unwrap_or(false) {
let x86_registers = if config.is_isa_enabled(Arch::X86) {
let start = std::time::Instant::now();
let regs_x86 = include_bytes!("../../docs_store/registers/serialized/x86");
let regs = bincode::deserialize(regs_x86)?;
Expand All @@ -259,7 +238,7 @@ pub fn main() -> Result<()> {
Vec::new()
};

let x86_64_registers = if config.instruction_sets.x86_64.unwrap_or(false) {
let x86_64_registers = if config.is_isa_enabled(Arch::X86_64) {
let start = std::time::Instant::now();
let regs_x86_64 = include_bytes!("../../docs_store/registers/serialized/x86_64");
let regs = bincode::deserialize(regs_x86_64)?;
Expand All @@ -272,7 +251,7 @@ pub fn main() -> Result<()> {
Vec::new()
};

let z80_registers = if config.instruction_sets.z80.unwrap_or(false) {
let z80_registers = if config.is_isa_enabled(Arch::Z80) {
let start = std::time::Instant::now();
let regs_z80 = include_bytes!("../../docs_store/registers/serialized/z80");
let regs = bincode::deserialize(regs_z80)?;
Expand All @@ -285,7 +264,7 @@ pub fn main() -> Result<()> {
Vec::new()
};

let arm_registers = if config.instruction_sets.arm.unwrap_or(false) {
let arm_registers = if config.is_isa_enabled(Arch::ARM) {
let start = std::time::Instant::now();
let regs_arm = include_bytes!("../../docs_store/registers/serialized/arm");
let regs = bincode::deserialize(regs_arm)?;
Expand All @@ -298,7 +277,7 @@ pub fn main() -> Result<()> {
Vec::new()
};

let riscv_registers = if config.instruction_sets.riscv.unwrap_or(false) {
let riscv_registers = if config.is_isa_enabled(Arch::RISCV) {
let start = std::time::Instant::now();
let regs_riscv = include_bytes!("../../docs_store/registers/serialized/riscv");
let regs = bincode::deserialize(regs_riscv)?;
Expand All @@ -321,7 +300,7 @@ pub fn main() -> Result<()> {
populate_name_to_register_map(Arch::ARM, &arm_registers, &mut names_to_info.registers);
populate_name_to_register_map(Arch::RISCV, &riscv_registers, &mut names_to_info.registers);

let gas_directives = if config.assemblers.gas.unwrap_or(false) {
let gas_directives = if config.is_assembler_enabled(Assembler::Gas) {
let start = std::time::Instant::now();
let gas_dirs = include_bytes!("../../docs_store/directives/serialized/gas");
let dirs = bincode::deserialize(gas_dirs)?;
Expand All @@ -334,7 +313,7 @@ pub fn main() -> Result<()> {
Vec::new()
};

let masm_directives = if config.assemblers.masm.unwrap_or(false) {
let masm_directives = if config.is_assembler_enabled(Assembler::Masm) {
let start = std::time::Instant::now();
let masm_dirs = include_bytes!("../../docs_store/directives/serialized/masm");
let dirs = bincode::deserialize(masm_dirs)?;
Expand All @@ -347,7 +326,7 @@ pub fn main() -> Result<()> {
Vec::new()
};

let nasm_directives = if config.assemblers.nasm.unwrap_or(false) {
let nasm_directives = if config.is_assembler_enabled(Assembler::Nasm) {
let start = std::time::Instant::now();
let nasm_dirs = include_bytes!("../../docs_store/directives/serialized/nasm");
let dirs = bincode::deserialize(nasm_dirs)?;
Expand Down Expand Up @@ -414,7 +393,7 @@ pub fn main() -> Result<()> {
#[allow(clippy::too_many_arguments, clippy::too_many_lines)]
fn main_loop(
connection: &Connection,
config: &Config,
config: &RootConfig,
names_to_info: &NameToInfoMaps,
instruction_completion_items: &[CompletionItem],
directive_completion_items: &[CompletionItem],
Expand All @@ -437,7 +416,7 @@ fn main_loop(
handle_hover_request(
connection,
id,
config,
config.get_config(&params.text_document_position_params.text_document.uri),
&params,
&text_store,
&mut tree_store,
Expand All @@ -453,7 +432,7 @@ fn main_loop(
connection,
id,
&params,
config,
config.get_config(&params.text_document_position.text_document.uri),
&text_store,
&mut tree_store,
instruction_completion_items,
Expand All @@ -469,7 +448,7 @@ fn main_loop(
connection,
id,
&params,
config,
config.get_config(&params.text_document_position_params.text_document.uri),
&text_store,
&mut tree_store,
)?;
Expand All @@ -482,7 +461,7 @@ fn main_loop(
connection,
id,
&params,
config,
config.get_config(&params.text_document.uri),
&text_store,
&mut tree_store,
)?;
Expand All @@ -495,7 +474,7 @@ fn main_loop(
connection,
id,
&params,
config,
config.get_config(&params.text_document_position_params.text_document.uri),
&text_store,
&mut tree_store,
&names_to_info.instructions,
Expand All @@ -509,7 +488,7 @@ fn main_loop(
connection,
id,
&params,
config,
config.get_config(&params.text_document_position.text_document.uri),
&text_store,
&mut tree_store,
)?;
Expand All @@ -519,12 +498,13 @@ fn main_loop(
);
} else if let Ok((_id, params)) = cast_req::<DocumentDiagnosticRequest>(req.clone())
{
let project_config = config.get_config(&params.text_document.uri);
// Ok to unwrap, this should never be `None`
if config.opts.diagnostics.unwrap() {
if project_config.opts.diagnostics.unwrap() {
handle_diagnostics(
connection,
&params.text_document.uri,
config,
project_config,
compile_cmds,
)?;
info!(
Expand Down Expand Up @@ -568,12 +548,13 @@ fn main_loop(
start.elapsed().as_millis()
);
} else if let Ok(params) = cast_notif::<DidSaveTextDocument>(notif.clone()) {
let project_config = config.get_config(&params.text_document.uri);
// Ok to unwrap, this should never be `None`
if config.opts.diagnostics.unwrap() {
if project_config.opts.diagnostics.unwrap() {
handle_diagnostics(
connection,
&params.text_document.uri,
config,
project_config,
compile_cmds,
)?;
info!(
Expand Down
1 change: 1 addition & 0 deletions src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ pub fn handle_signature_help_request(
let sig_resp = get_sig_help_resp(
doc.get_content(None),
params,
config,
tree_entry,
names_to_instructions,
);
Expand Down
Loading

0 comments on commit 25dd4c9

Please sign in to comment.