Skip to content

Commit 6be355e

Browse files
committed
Add Span::{line, column}
1 parent 65a3ea6 commit 6be355e

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

+10
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,16 @@ impl server::Span for Rustc<'_, '_> {
656656
span.shrink_to_hi()
657657
}
658658

659+
fn line(&mut self, span: Self::Span) -> usize {
660+
let loc = self.sess().source_map().lookup_char_pos(span.lo());
661+
loc.line
662+
}
663+
664+
fn column(&mut self, span: Self::Span) -> usize {
665+
let loc = self.sess().source_map().lookup_char_pos(span.lo());
666+
loc.col.to_usize() + 1
667+
}
668+
659669
fn join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {
660670
let self_loc = self.sess().source_map().lookup_char_pos(first.lo());
661671
let other_loc = self.sess().source_map().lookup_char_pos(second.lo());

library/proc_macro/src/bridge/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ macro_rules! with_api {
9797
fn byte_range($self: $S::Span) -> Range<usize>;
9898
fn start($self: $S::Span) -> $S::Span;
9999
fn end($self: $S::Span) -> $S::Span;
100+
fn line($self: $S::Span) -> usize;
101+
fn column($self: $S::Span) -> usize;
100102
fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>;
101103
fn subspan($self: $S::Span, start: Bound<usize>, end: Bound<usize>) -> Option<$S::Span>;
102104
fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span;

library/proc_macro/src/lib.rs

+16
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,22 @@ impl Span {
505505
Span(self.0.end())
506506
}
507507

508+
/// The one-indexed line of the source file where the span starts.
509+
///
510+
/// To obtain the line of the span's end, use `span.end().line()`.
511+
#[unstable(feature = "proc_macro_span", issue = "54725")]
512+
pub fn line(&self) -> usize {
513+
self.0.line()
514+
}
515+
516+
/// The one-indexed column of the source file where the span starts.
517+
///
518+
/// To obtain the column of the span's end, use `span.end().column()`.
519+
#[unstable(feature = "proc_macro_span", issue = "54725")]
520+
pub fn column(&self) -> usize {
521+
self.0.column()
522+
}
523+
508524
/// Creates a new span encompassing `self` and `other`.
509525
///
510526
/// Returns `None` if `self` and `other` are from different files.

src/tools/rust-analyzer/crates/proc-macro-srv/src/server.rs

+10
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,16 @@ impl server::Span for RustAnalyzer {
329329
fn start(&mut self, _self_: Self::Span) -> Self::Span {
330330
tt::TokenId::unspecified()
331331
}
332+
333+
fn line(&mut self, _span: Self::Span) -> usize {
334+
// FIXME handle line
335+
0
336+
}
337+
338+
fn column(&mut self, _span: Self::Span) -> usize {
339+
// FIXME handle column
340+
0
341+
}
332342
}
333343

334344
impl server::Symbol for RustAnalyzer {

0 commit comments

Comments
 (0)