Skip to content

Commit

Permalink
Cleanup, finished statics
Browse files Browse the repository at this point in the history
  • Loading branch information
WillLillis committed Nov 17, 2023
1 parent cec9cbd commit 1ed7e55
Showing 1 changed file with 69 additions and 52 deletions.
121 changes: 69 additions & 52 deletions src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn tree_sitter_logger(log_type: tree_sitter::LogType, message: &str) {
}
}

/// Given a NameTo<item> map, returns a Vec<CompletionItem> for the items
/// Given a NameTo_SomeItem_ map, returns a `Vec<CompletionItem>` for the items
/// contained within the map
pub fn get_completes<T: Completable>(
map: &HashMap<(Arch, &str), T>,
Expand Down Expand Up @@ -144,10 +144,9 @@ pub fn get_comp_resp(
}
}

// TODO: filter by width allowed by corresponding instruction
// TODO: filter register completions by width allowed by corresponding instruction
// TODO: Would like to do incremental parsing but don't see a straightforward
// way to map the LSP's edit info the the format/ info tree-sitter is
// expecting...
// way to map the LSP's edit info the the format/ info tree-sitter is expecting...
let curr_tree = parser.parse(curr_doc, None);
if let Some(tree) = curr_tree {
let mut cursor = tree_sitter::QueryCursor::new();
Expand All @@ -161,6 +160,7 @@ pub fn get_comp_resp(
column: usize::MAX,
},
});
let curr_doc = curr_doc.as_bytes();

// Instruction and two register arguments
static QUERY_INSTR_REG_REG: Lazy<tree_sitter::Query> = Lazy::new(|| {
Expand All @@ -172,7 +172,7 @@ pub fn get_comp_resp(
});

let matches: Vec<tree_sitter::QueryMatch<'_, '_>> = cursor
.matches(&QUERY_INSTR_REG_REG, tree.root_node(), curr_doc.as_bytes())
.matches(&QUERY_INSTR_REG_REG, tree.root_node(), curr_doc)
.collect();
if let Some(match_) = matches.first() {
let caps = match_.captures;
Expand Down Expand Up @@ -212,19 +212,16 @@ pub fn get_comp_resp(
}

// Instruction and one register argument, one non-register argument
let query = match tree_sitter::Query::new(
tree_sitter_asm::language(),
"(instruction kind: (word) @instr_name (ident (reg) @r1) (ident))",
) {
Ok(query_) => query_,
Err(e) => {
error!("Error creating tree-sitter query - Error: {e}");
return None;
}
};
static QUERY_INSTR_REG_ARG: Lazy<tree_sitter::Query> = Lazy::new(|| {
tree_sitter::Query::new(
tree_sitter_asm::language(),
"(instruction kind: (word) @instr_name (ident (reg) @r1) (ident))",
)
.unwrap()
});

let matches: Vec<tree_sitter::QueryMatch<'_, '_>> = cursor
.matches(&query, tree.root_node(), curr_doc.as_bytes())
.matches(&QUERY_INSTR_REG_ARG, tree.root_node(), curr_doc)
.collect();
if let Some(match_) = matches.first() {
let caps = match_.captures;
Expand Down Expand Up @@ -256,19 +253,15 @@ pub fn get_comp_resp(
}

// Instruction and one non-register argument, one register argument
let query = match tree_sitter::Query::new(
tree_sitter_asm::language(),
"(instruction kind: (word) @instr_name (ident) (ident (reg) @r1))",
) {
Ok(query_) => query_,
Err(e) => {
error!("Error creating tree-sitter query - Error: {e}");
return None;
}
};

static QUERY_INSTR_ARG_REG: Lazy<tree_sitter::Query> = Lazy::new(|| {
tree_sitter::Query::new(
tree_sitter_asm::language(),
"(instruction kind: (word) @instr_name (ident) (ident (reg) @r1))",
)
.unwrap()
});
let matches: Vec<tree_sitter::QueryMatch<'_, '_>> = cursor
.matches(&query, tree.root_node(), curr_doc.as_bytes())
.matches(&QUERY_INSTR_ARG_REG, tree.root_node(), curr_doc)
.collect();
if let Some(match_) = matches.first() {
let caps = match_.captures;
Expand Down Expand Up @@ -301,20 +294,15 @@ pub fn get_comp_resp(
}

// Instruction and one register argument
let query = match tree_sitter::Query::new(
tree_sitter_asm::language(),
"(instruction kind: (word) @instr_name (ident (reg) @r1))",
) {
Ok(query_) => query_,

Err(e) => {
error!("Error creating tree-sitter query - Error: {e}");
return None;
}
};

static QUERY_INSTR_REG: Lazy<tree_sitter::Query> = Lazy::new(|| {
tree_sitter::Query::new(
tree_sitter_asm::language(),
"(instruction kind: (word) @instr_name (ident (reg) @r1))",
)
.unwrap()
});
let matches: Vec<tree_sitter::QueryMatch<'_, '_>> = cursor
.matches(&query, tree.root_node(), curr_doc.as_bytes())
.matches(&QUERY_INSTR_REG, tree.root_node(), curr_doc)
.collect();
if let Some(match_) = matches.first() {
let caps = match_.captures;
Expand Down Expand Up @@ -346,20 +334,49 @@ pub fn get_comp_resp(
}
}

// Just an instruction
let query = match tree_sitter::Query::new(
tree_sitter_asm::language(),
"(instruction kind: (word) @instr_name)",
) {
Ok(query_) => query_,
Err(e) => {
error!("Error creating tree-sitter query - Error: {e}");
return None;
// Instruction and one non-register argument
static QUERY_INSTR_ARG: Lazy<tree_sitter::Query> = Lazy::new(|| {
tree_sitter::Query::new(
tree_sitter_asm::language(),
"(instruction kind: (word) @instr_name (ident))",
)
.unwrap()
});
let matches: Vec<tree_sitter::QueryMatch<'_, '_>> = cursor
.matches(&QUERY_INSTR_ARG, tree.root_node(), curr_doc)
.collect();
if let Some(match_) = matches.first() {
let caps = match_.captures;
if caps.len() == 1 {
let instr_start = caps[0].node.range().start_point;
let instr_end = caps[0].node.range().end_point;
if instr_start.row == cursor_line
&& instr_end.row == cursor_line
&& instr_start.column <= cursor_char
&& instr_end.column >= cursor_char
{
comp_items = Some(instr_comps.to_owned());
}

if let Some(items) = comp_items {
return Some(CompletionList {
is_incomplete: true,
items,
});
}
}
};
}

// Just an instruction
static QUERY_INSTR: Lazy<tree_sitter::Query> = Lazy::new(|| {
tree_sitter::Query::new(
tree_sitter_asm::language(),
"(instruction kind: (word) @instr_name)",
)
.unwrap()
});
let matches: Vec<tree_sitter::QueryMatch<'_, '_>> = cursor
.matches(&query, tree.root_node(), curr_doc.as_bytes())
.matches(&QUERY_INSTR, tree.root_node(), curr_doc)
.collect();
if let Some(match_) = matches.first() {
let caps = match_.captures;
Expand Down

0 comments on commit 1ed7e55

Please sign in to comment.