Skip to content

Commit

Permalink
feat: Add supress unused rule
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielHauge committed Jul 26, 2024
1 parent 09aada6 commit c05202b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ And: <https://www.youtube.com/watch?v=YsdlcQoHqPY&t=348s>
- [x] Go to definition
- [x] Multi definition hover information
- [x] Document symbols
- [x] supress unused diagnostics rule
- [ ] Code actions : (Merge multi defs, rename unused def with underscore, add ignore rule)
- [ ] Semantic token terminal byte
- [ ] Workspace support - multiple files
68 changes: 53 additions & 15 deletions src/lsp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{BTreeMap, HashMap};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::fmt::Debug;
use std::ops::Bound::Included;

Expand Down Expand Up @@ -26,6 +26,7 @@ pub struct AnalysisContext {
line_to_offset: HashMap<usize, usize>,
symbols: RangeMap<usize, String>,
syntax_error: Option<SyntaxError>,
supress_unused_rule: HashSet<String>,
}

#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -58,6 +59,7 @@ impl AnalysisContext {

let mut lsp_context = Self {
src: None,
supress_unused_rule: HashSet::new(),
definitions: HashMap::new(),
alternative_definitions: HashMap::new(),
hover: HashMap::new(),
Expand All @@ -82,7 +84,7 @@ impl AnalysisContext {
}

fn compute_lsp_context(&mut self, doc_content: &str, parse_results: &ParseResult) {
self.compute_symbols(parse_results);
self.compute_symbols_comments(parse_results);
for rule in &parse_results.syntax.rules {
let loc = self.location_at_offset(rule.span.start);
let old_def = self.definitions.insert(rule.name.to_string(), loc.clone());
Expand Down Expand Up @@ -135,21 +137,36 @@ impl AnalysisContext {
}
}

fn compute_symbols(&mut self, parse_results: &ParseResult) {
fn compute_symbols_comments(&mut self, parse_results: &ParseResult) {
let mut symbol_supress_rule_tree = BTreeSet::new();
for token in &parse_results.tokens {
let range = token.span.start..token.span.end;
match &token.kind {
ebnf_parser::TokenKind::Identifier(x) => {
self.symbols.insert(range, x.to_string());
symbol_supress_rule_tree.insert(token.span.start);
}
_ => continue,
};
}
parse_results
.tokens
.iter()
.filter_map(|t| {
if let ebnf_parser::TokenKind::Identifier(x) = t.kind {
Some((x, t.span))
} else {
None
.comments
.values()
.flatten()
.for_each(|comment| {
if comment.text.contains("#[allow(unused)]") {
symbol_supress_rule_tree
.range((Included(comment.span.end), Included(usize::MAX)))
.next()
.map(|x| {
self.symbols
.get(x)
.expect("Symbol should exist as it was found in btree")
.clone()
})
.map(|x| self.supress_unused_rule.insert(x));
}
})
.for_each(|(x, span)| {
let range = span.start..span.end;
self.symbols.insert(range, x.to_string());
})
});
}

fn location_at_offset(&self, offset: usize) -> Location {
Expand Down Expand Up @@ -190,6 +207,9 @@ impl AnalysisContext {
.keys()
.filter(|k| !self.references.contains_key(*k))
.flat_map(|k| {
if self.supress_unused_rule.contains(k) {
return vec![];
}
let start = self.definitions.get(k).unwrap();
let end = Location {
line: start.line,
Expand Down Expand Up @@ -467,4 +487,22 @@ mod tests {
"Expected '=', was 'w'"
);
}

#[test]
fn test_supress_unused() {
let ebnf = "hello = \"hello\"; (* #[allow(unused)] *)\nworld = hello;";
let lsp_context = super::AnalysisContext::from_src(ebnf.to_string());

assert_eq!(lsp_context.supress_unused_rule.len(), 1);
assert_eq!(lsp_context.supress_unused_rule.contains("world"), true);
}

#[test]
fn test_unused_w_rule() {
let ebnf = "hello = \"hello\";\n(* #[allow(unused)] *)\nworld = hello;\ntest = hello;";
let lsp_context = super::AnalysisContext::from_src(ebnf.to_string());
let mut diagnostics = lsp_context.diagnostics();
diagnostics.sort_by(|a, b| a.start.line.cmp(&b.start.line));
assert_eq!(diagnostics.len(), 0);
}
}

0 comments on commit c05202b

Please sign in to comment.