Skip to content

Commit

Permalink
Add cmp between span and byte index, rename extra is_within_comment
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklimmm authored and lpil committed May 15, 2024
1 parent 9b0fc80 commit a85dba1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
11 changes: 11 additions & 0 deletions compiler-core/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::type_::expression::Implementations;
use crate::type_::{
self, Deprecation, ModuleValueConstructor, PatternConstructor, Type, ValueConstructor,
};
use std::cmp::Ordering;
use std::sync::Arc;

use ecow::EcoString;
Expand Down Expand Up @@ -1320,6 +1321,16 @@ impl SrcSpan {
pub fn contains(&self, byte_index: u32) -> bool {
byte_index >= self.start && byte_index < self.end
}

pub fn cmp_byte_index(&self, byte_index: u32) -> Ordering {
if byte_index < self.start {
Ordering::Less
} else if self.end <= byte_index {
Ordering::Greater
} else {
Ordering::Equal
}
}
}

#[derive(Debug, PartialEq, Eq, Clone)]
Expand Down
8 changes: 6 additions & 2 deletions compiler-core/src/language_server/code_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl<'a> RedundantTupleInCaseSubject<'a> {
let (lparen_offset, _) = tuple_code
.match_indices('(')
// Ignore in comments
.find(|(i, _)| !self.extra.contains(location.start + *i as u32))
.find(|(i, _)| !self.extra.is_within_comment(location.start + *i as u32))
.expect("`(` not found in tuple");

edits.push(TextEdit {
Expand All @@ -233,7 +233,11 @@ impl<'a> RedundantTupleInCaseSubject<'a> {
if let Some((trailing_comma_offset, _)) = code_after_last_elem
.rmatch_indices(',')
// Ignore in comments
.find(|(i, _)| !self.extra.contains(last_elem_location.end + *i as u32))
.find(|(i, _)| {
!self
.extra
.is_within_comment(last_elem_location.end + *i as u32)
})
{
edits.push(TextEdit {
range: src_span_to_lsp_range(
Expand Down
20 changes: 11 additions & 9 deletions compiler-core/src/parse/extra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ impl ModuleExtra {
Default::default()
}

pub fn contains(&self, offset: u32) -> bool {
self.module_comments
.iter()
.any(|span| span.contains(offset))
|| self.doc_comments.iter().any(|span| span.contains(offset))
|| self.comments.iter().any(|span| span.contains(offset))
pub fn is_within_comment(&self, byte_index: u32) -> bool {
self.comments
.binary_search_by(|span| span.cmp_byte_index(byte_index))
.is_ok()
|| self
.empty_lines
.iter()
.any(|&empty_line_offset| empty_line_offset == offset)
.doc_comments
.binary_search_by(|span| span.cmp_byte_index(byte_index))
.is_ok()
|| self
.module_comments
.binary_search_by(|span| span.cmp_byte_index(byte_index))
.is_ok()
}
}

Expand Down

0 comments on commit a85dba1

Please sign in to comment.