Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
BinderDavid committed Jan 8, 2025
1 parent 1d2d31d commit eb52c3b
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 150 deletions.
4 changes: 2 additions & 2 deletions lang/driver/src/codespan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl File {

match line_index.cmp(&self.last_line_index()) {
Ordering::Less => Ok(self.line_starts[line_index.to_usize()]),
Ordering::Equal => Ok(self.source_span().end()),
Ordering::Equal => Ok(self.source_span().end),
Ordering::Greater => Err(Error::LineTooLarge {
given: line_index.to_usize(),
max: self.last_line_index().to_usize(),
Expand All @@ -50,7 +50,7 @@ impl File {
let line_start = self.line_start(line_index)?;
let next_line_start = self.line_start(line_index + LineOffset(1))?;

Check warning on line 51 in lang/driver/src/codespan.rs

View check run for this annotation

Codecov / codecov/patch

lang/driver/src/codespan.rs#L49-L51

Added lines #L49 - L51 were not covered by tests

Ok(Span::new(line_start, next_line_start))
Ok(Span { start: line_start, end: next_line_start })
}

Check warning on line 54 in lang/driver/src/codespan.rs

View check run for this annotation

Codecov / codecov/patch

lang/driver/src/codespan.rs#L53-L54

Added lines #L53 - L54 were not covered by tests

fn line_index(&self, byte_index: ByteIndex) -> LineIndex {
Expand Down
4 changes: 2 additions & 2 deletions lang/driver/src/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Database {
let char_range =
rope.byte_to_char(remove_range.start)..rope.byte_to_char(remove_range.end);
rope.remove(char_range);
let char_idx = rope.byte_to_char(edit.span.start().0 as usize);
let char_idx = rope.byte_to_char(edit.span.start.0 as usize);
rope.insert(char_idx, &edit.text);
}

Expand All @@ -39,6 +39,6 @@ trait SpanAsRange {

impl SpanAsRange for Span {
fn as_range(&self) -> Range<usize> {
self.start().0 as usize..self.end().0 as usize
self.start.0 as usize..self.end.0 as usize
}
}
10 changes: 5 additions & 5 deletions lang/driver/src/info/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub async fn collect_info(
content: InfoContent::UseInfo(UseInfo { uri: dep_uri, path: use_decl.path.clone() }),
};
collector.info_spans.push(Interval {
start: use_decl.span.start().0,
stop: use_decl.span.end().0,
start: use_decl.span.start.0,
stop: use_decl.span.end.0,

Check warning on line 37 in lang/driver/src/info/collect.rs

View check run for this annotation

Codecov / codecov/patch

lang/driver/src/info/collect.rs#L36-L37

Added lines #L36 - L37 were not covered by tests
val: info,
})
}
Expand All @@ -61,15 +61,15 @@ impl InfoCollector {

fn add_info<T: Into<InfoContent>>(&mut self, span: Span, info: T) {
let info = Interval {
start: span.start().0,
stop: span.end().0,
start: span.start.0,
stop: span.end.0,

Check warning on line 65 in lang/driver/src/info/collect.rs

View check run for this annotation

Codecov / codecov/patch

lang/driver/src/info/collect.rs#L64-L65

Added lines #L64 - L65 were not covered by tests
val: Info { span, content: info.into() },
};
self.info_spans.push(info)
}

fn add_item(&mut self, span: Span, item: Item) {
let item = Interval { start: span.start().0, stop: span.end().0, val: item };
let item = Interval { start: span.start.0, stop: span.end.0, val: item };

Check warning on line 72 in lang/driver/src/info/collect.rs

View check run for this annotation

Codecov / codecov/patch

lang/driver/src/info/collect.rs#L72

Added line #L72 was not covered by tests
self.item_spans.push(item)
}
}
Expand Down
12 changes: 6 additions & 6 deletions lang/driver/src/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl Database {
pub fn location_to_index(&self, uri: &Url, location: Position) -> Option<ByteIndex> {

Check warning on line 10 in lang/driver/src/spans.rs

View check run for this annotation

Codecov / codecov/patch

lang/driver/src/spans.rs#L10

Added line #L10 was not covered by tests
let file = self.files.get_even_if_stale(uri).unwrap();
let line_span = file.line_span(LineIndex(location.line)).ok()?;
let index: usize = line_span.start().to_usize() + LineIndex(location.character).to_usize();
let index: usize = line_span.start.to_usize() + LineIndex(location.character).to_usize();
Some(ByteIndex(index as u32))

Check warning on line 14 in lang/driver/src/spans.rs

View check run for this annotation

Codecov / codecov/patch

lang/driver/src/spans.rs#L12-L14

Added lines #L12 - L14 were not covered by tests
}

Expand All @@ -20,26 +20,26 @@ impl Database {
}

pub fn span_to_locations(&self, uri: &Url, span: Span) -> Option<Range> {
let start = self.index_to_location(uri, span.start())?;
let end = self.index_to_location(uri, span.end())?;
let start = self.index_to_location(uri, span.start)?;
let end = self.index_to_location(uri, span.end)?;
Some(Range { start, end })

Check warning on line 25 in lang/driver/src/spans.rs

View check run for this annotation

Codecov / codecov/patch

lang/driver/src/spans.rs#L22-L25

Added lines #L22 - L25 were not covered by tests
}

pub async fn hoverinfo_at_index(&mut self, uri: &Url, idx: ByteIndex) -> Option<Info> {
self.hoverinfo_at_span(uri, Span::new(idx, ByteIndex(idx.0 + 1))).await
self.hoverinfo_at_span(uri, Span { start: idx, end: ByteIndex(idx.0 + 1) }).await

Check warning on line 29 in lang/driver/src/spans.rs

View check run for this annotation

Codecov / codecov/patch

lang/driver/src/spans.rs#L29

Added line #L29 was not covered by tests
}

pub async fn hoverinfo_at_span(&mut self, uri: &Url, span: Span) -> Option<Info> {
let lapper = self.info_by_id(uri).await.ok()?;
let intervals = lapper.find(span.start().0, span.end().0);
let intervals = lapper.find(span.start.0, span.end.0);

Check warning on line 34 in lang/driver/src/spans.rs

View check run for this annotation

Codecov / codecov/patch

lang/driver/src/spans.rs#L34

Added line #L34 was not covered by tests
let smallest_interval =
intervals.min_by(|i1, i2| (i1.stop - i1.start).cmp(&(i2.stop - i2.start)));
smallest_interval.map(|interval| interval.val.clone())
}

pub async fn item_at_span(&mut self, uri: &Url, span: Span) -> Option<Item> {
let lapper = self.item_by_id(uri).await.ok()?;
let intervals = lapper.find(span.start().0, span.end().0);
let intervals = lapper.find(span.start.0, span.end.0);

Check warning on line 42 in lang/driver/src/spans.rs

View check run for this annotation

Codecov / codecov/patch

lang/driver/src/spans.rs#L42

Added line #L42 was not covered by tests
let largest_interval =
intervals.max_by(|i1, i2| (i1.stop - i1.start).cmp(&(i2.stop - i1.start)));
largest_interval.map(|interval| interval.val.clone())
Expand Down
4 changes: 2 additions & 2 deletions lang/lsp/src/codeactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub async fn code_action(
let mut db = server.database.write().await;
let span_start = db.location_to_index(&text_document.uri.from_lsp(), range.start);
let span_end = db.location_to_index(&text_document.uri.from_lsp(), range.end);
let span = span_start
.and_then(|start| span_end.map(|end| miette_util::codespan::Span::new(start, end)));
let span =
span_start.and_then(|start| span_end.map(|end| miette_util::codespan::Span { start, end }));

Check warning on line 29 in lang/lsp/src/codeactions.rs

View check run for this annotation

Codecov / codecov/patch

lang/lsp/src/codeactions.rs#L26-L29

Added lines #L26 - L29 were not covered by tests
let item = if let Some(span) = span {
db.item_at_span(&text_document.uri.from_lsp(), span).await
} else {
Expand Down
136 changes: 7 additions & 129 deletions lang/miette_util/src/codespan.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::fmt;
use std::ops::{Add, Sub};

/// A byte position in a source file.
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ByteIndex(pub u32);

impl ByteIndex {
Expand All @@ -12,19 +11,6 @@ impl ByteIndex {
}
}

impl fmt::Debug for ByteIndex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ByteIndex(")?;
self.0.fmt(f)?;
write!(f, ")")
}
}

impl fmt::Display for ByteIndex {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl Sub for ByteIndex {
type Output = ByteOffset;

Expand All @@ -44,7 +30,7 @@ impl Add<ByteOffset> for ByteIndex {
}

/// A byte offset in a source file
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ByteOffset(pub i64);

impl ByteOffset {
Expand All @@ -54,76 +40,21 @@ impl ByteOffset {
}
}

impl fmt::Debug for ByteOffset {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ByteOffset(")?;
self.0.fmt(f)?;
write!(f, ")")
}
}

impl fmt::Display for ByteOffset {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Span {
start: ByteIndex,
end: ByteIndex,
pub start: ByteIndex,
pub end: ByteIndex,
}

impl Span {
/// Create a new span from a starting and ending span.
pub fn new(start: ByteIndex, end: ByteIndex) -> Span {
assert!(end >= start);

Span { start, end }
}

/// Gives an empty span at the start of a source.
pub const fn initial() -> Span {
Span { start: ByteIndex(0), end: ByteIndex(0) }
}

/// Measure the span of a string.
///
/// ```rust
/// use miette_util::codespan::{ByteIndex, Span};
///
/// let span = Span::from_string("hello");
///
/// assert_eq!(span, Span::new(ByteIndex(0), ByteIndex(5)));
/// ```
pub fn from_string(s: &str) -> Span {
Span::new(ByteIndex(0), ByteIndex(s.len() as u32))
}

/// Get the starting byte index.
///
/// ```rust
/// use miette_util::codespan::{ByteIndex, Span};
///
/// let span = Span::new(ByteIndex(0), ByteIndex(4));
///
/// assert_eq!(span.start(), ByteIndex(0));
/// ```
pub fn start(self) -> ByteIndex {
self.start
}

/// Get the ending byte index.
///
/// ```rust
/// use miette_util::codespan::{ByteIndex, Span};
///
/// let span = Span::new(ByteIndex(0), ByteIndex(4));
///
/// assert_eq!(span.end(), ByteIndex(4));
/// ```
pub fn end(self) -> ByteIndex {
self.end
Span { start: ByteIndex(0), end: ByteIndex(s.len() as u32) }
}

Check warning on line 58 in lang/miette_util/src/codespan.rs

View check run for this annotation

Codecov / codecov/patch

lang/miette_util/src/codespan.rs#L56-L58

Added lines #L56 - L58 were not covered by tests
}

Expand All @@ -133,24 +64,12 @@ impl Default for Span {
}
}

impl fmt::Display for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{start}, {end})", start = self.start(), end = self.end(),)
}
}

/// A 1-indexed line number. Useful for pretty printing source locations.
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct LineNumber(u32);

impl fmt::Display for LineNumber {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

/// A zero-indexed line offset into a source file
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct LineIndex(pub u32);

/// A line offset in a source file
Expand All @@ -159,13 +78,6 @@ pub struct LineOffset(pub i64);

impl LineIndex {
/// The 1-indexed line number. Useful for pretty printing source locations.
///
/// ```rust
/// use miette_util::codespan::{LineIndex, LineNumber};
///
/// assert_eq!(format!("{}", LineIndex(0).number()), "1");
/// assert_eq!(format!("{}", LineIndex(3).number()), "4");
/// ```
pub const fn number(self) -> LineNumber {
LineNumber(self.0 + 1)
}

Check warning on line 83 in lang/miette_util/src/codespan.rs

View check run for this annotation

Codecov / codecov/patch

lang/miette_util/src/codespan.rs#L81-L83

Added lines #L81 - L83 were not covered by tests
Expand All @@ -185,32 +97,12 @@ impl Add<LineOffset> for LineIndex {
}

Check warning on line 97 in lang/miette_util/src/codespan.rs

View check run for this annotation

Codecov / codecov/patch

lang/miette_util/src/codespan.rs#L95-L97

Added lines #L95 - L97 were not covered by tests
}

impl fmt::Debug for LineIndex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "LineIndex(")?;
self.0.fmt(f)?;
write!(f, ")")
}
}

impl fmt::Display for LineIndex {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

/// A 1-indexed column number. Useful for pretty printing source locations.
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ColumnNumber(u32);

impl fmt::Display for ColumnNumber {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

/// A zero-indexed column offset into a source file
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ColumnIndex(pub u32);

/// A column offset in a source file
Expand All @@ -223,17 +115,3 @@ impl ColumnIndex {
self.0 as usize
}

Check warning on line 116 in lang/miette_util/src/codespan.rs

View check run for this annotation

Codecov / codecov/patch

lang/miette_util/src/codespan.rs#L114-L116

Added lines #L114 - L116 were not covered by tests
}

impl fmt::Debug for ColumnIndex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ColumnIndex(")?;
self.0.fmt(f)?;
write!(f, ")")
}
}

impl fmt::Display for ColumnIndex {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
6 changes: 3 additions & 3 deletions lang/miette_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ impl ToMiette for codespan::Span {
type Target = miette::SourceSpan;

fn to_miette(self) -> Self::Target {
let length = self.end() - self.start();
miette::SourceSpan::new(self.start().to_miette(), length.to_usize())
let length = self.end - self.start;
miette::SourceSpan::new(self.start.to_miette(), length.to_usize())
}
}

Expand All @@ -60,6 +60,6 @@ impl FromMiette for miette::SourceSpan {
fn from_miette(self) -> Self::Target {
let start = codespan::ByteIndex(self.offset() as u32);
let end = codespan::ByteIndex((self.offset() + self.len()) as u32);
codespan::Span::new(start, end)
codespan::Span { start, end }

Check warning on line 63 in lang/miette_util/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lang/miette_util/src/lib.rs#L63

Added line #L63 was not covered by tests
}
}
2 changes: 1 addition & 1 deletion lang/parser/src/grammar/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use miette_util::codespan::{ByteIndex, Span};

pub fn span(l: usize, r: usize) -> Span {
Span::new(ByteIndex(l as u32), ByteIndex(r as u32))
Span { start: ByteIndex(l as u32), end: ByteIndex(r as u32) }
}

0 comments on commit eb52c3b

Please sign in to comment.