Skip to content

Commit

Permalink
Started work generalizing code for Hashmaps
Browse files Browse the repository at this point in the history
  • Loading branch information
WillLillis committed Nov 9, 2023
1 parent 14860c8 commit ad8098a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
52 changes: 50 additions & 2 deletions src/lsp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use crate::types::Column;
use crate::{Arch, Instruction, NameToInstructionMap, NameToRegisterMap, Register, TargetConfig};
use crate::{
Arch, Hoverable, Instruction, NameToInstructionMap, NameToRegisterMap, Register, TargetConfig,
};
use log::{error, info};
use lsp_types::{InitializeParams, TextDocumentPositionParams, Url};
use lsp_types::{
Documentation, Hover, HoverContents, InitializeParams, TextDocumentPositionParams, Url,
};
use std::collections::HashMap;
use std::fmt::Display;
use std::fs::File;
use std::io::BufRead;
use std::path::PathBuf;
Expand Down Expand Up @@ -53,6 +59,48 @@ pub fn get_word_from_file_params(
}
}

pub fn get_hover_resp<T: Hoverable + Display>(
word: &str,
map: &HashMap<(Arch, &str), T>,
) -> Option<Hover> {
let (x86_res, x86_64_res) = search_for_hoverable(word, map);

let hover_res: Option<Hover> = match (x86_res.is_some(), x86_64_res.is_some()) {
(true, _) | (_, true) => {
let mut value = String::new();
if let Some(x86_res) = x86_res {
value += &format!("{}", x86_res);
}
if let Some(x86_64_res) = x86_64_res {
value += &format!(
"{}{}",
if x86_res.is_some() { "\n\n" } else { "" },
x86_64_res
);
}
Some(Hover {
contents: HoverContents::Markup(MarkupContent {
kind: MarkupKind::Markdown,
value,
}),
range: None,
})
}
_ => {
// don't know of this word
None
}
};
None
}

fn search_for_hoverable<'a: 'b, 'b, T: Hoverable>(
word: &str,
map: &'a HashMap<(Arch, &str), T>,
) -> (Option<&'b T>, Option<&'b T>) {
(None, None)
}

// Note: Have to call .cloned() on the results of .get()
// here because of compiler issue regarding entangled lifetimes: https://github.com/rust-lang/rust/issues/80389
pub fn search_for_instr<'a: 'b, 'b>(
Expand Down
8 changes: 8 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use strum_macros::{AsRefStr, Display, EnumString};

// Define a trait for types we display on Hover Requests so we can avoid some
// duplicate code
pub trait Hoverable {}

// Instruction ------------------------------------------------------------------------------------
#[derive(Debug, Clone)]
pub struct Instruction {
Expand All @@ -12,6 +16,8 @@ pub struct Instruction {
pub arch: Option<Arch>,
}

impl Hoverable for Instruction {}

impl Default for Instruction {
fn default() -> Self {
let name = String::new();
Expand Down Expand Up @@ -170,6 +176,8 @@ pub struct Register {
pub url: Option<String>,
}

impl Hoverable for Register {}

impl Default for Register {
fn default() -> Self {
let name = String::new();
Expand Down

0 comments on commit ad8098a

Please sign in to comment.