From a1cd8c3a2819586159dfb33559777959f087f028 Mon Sep 17 00:00:00 2001 From: Jacob Pratt Date: Sun, 14 May 2023 18:30:18 -0400 Subject: [PATCH] Add `Span::{line, column}` --- compiler/rustc_expand/src/proc_macro_server.rs | 10 ++++++++++ library/proc_macro/src/bridge/mod.rs | 2 ++ library/proc_macro/src/lib.rs | 16 ++++++++++++++++ .../crates/proc-macro-srv/src/server.rs | 10 ++++++++++ 4 files changed, 38 insertions(+) diff --git a/compiler/rustc_expand/src/proc_macro_server.rs b/compiler/rustc_expand/src/proc_macro_server.rs index 9400c501aa5..ecd2315112a 100644 --- a/compiler/rustc_expand/src/proc_macro_server.rs +++ b/compiler/rustc_expand/src/proc_macro_server.rs @@ -656,6 +656,16 @@ impl server::Span for Rustc<'_, '_> { span.shrink_to_hi() } + fn line(&mut self, span: Self::Span) -> usize { + let loc = self.sess().source_map().lookup_char_pos(span.lo()); + loc.line + } + + fn column(&mut self, span: Self::Span) -> usize { + let loc = self.sess().source_map().lookup_char_pos(span.lo()); + loc.col.to_usize() + 1 + } + fn join(&mut self, first: Self::Span, second: Self::Span) -> Option { let self_loc = self.sess().source_map().lookup_char_pos(first.lo()); let other_loc = self.sess().source_map().lookup_char_pos(second.lo()); diff --git a/library/proc_macro/src/bridge/mod.rs b/library/proc_macro/src/bridge/mod.rs index c7c7e974ae9..86ce5d9c6d5 100644 --- a/library/proc_macro/src/bridge/mod.rs +++ b/library/proc_macro/src/bridge/mod.rs @@ -97,6 +97,8 @@ macro_rules! with_api { fn byte_range($self: $S::Span) -> Range; fn start($self: $S::Span) -> $S::Span; fn end($self: $S::Span) -> $S::Span; + fn line($self: $S::Span) -> usize; + fn column($self: $S::Span) -> usize; fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>; fn subspan($self: $S::Span, start: Bound, end: Bound) -> Option<$S::Span>; fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span; diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index 89b5aef1bcd..cb8b7ec70e0 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -505,6 +505,22 @@ impl Span { Span(self.0.end()) } + /// The one-indexed line of the source file where the span starts. + /// + /// To obtain the line of the span's end, use `span.end().line()`. + #[unstable(feature = "proc_macro_span", issue = "54725")] + pub fn line(&self) -> usize { + self.0.line() + } + + /// The one-indexed column of the source file where the span starts. + /// + /// To obtain the column of the span's end, use `span.end().column()`. + #[unstable(feature = "proc_macro_span", issue = "54725")] + pub fn column(&self) -> usize { + self.0.column() + } + /// Creates a new span encompassing `self` and `other`. /// /// Returns `None` if `self` and `other` are from different files. diff --git a/src/tools/rust-analyzer/crates/proc-macro-srv/src/server.rs b/src/tools/rust-analyzer/crates/proc-macro-srv/src/server.rs index 410f07f9b84..e67f1e798e8 100644 --- a/src/tools/rust-analyzer/crates/proc-macro-srv/src/server.rs +++ b/src/tools/rust-analyzer/crates/proc-macro-srv/src/server.rs @@ -329,6 +329,16 @@ impl server::Span for RustAnalyzer { fn start(&mut self, _self_: Self::Span) -> Self::Span { tt::TokenId::unspecified() } + + fn line(&mut self, _span: Self::Span) -> usize { + // FIXME handle line + 0 + } + + fn column(&mut self, _span: Self::Span) -> usize { + // FIXME handle column + 0 + } } impl server::Symbol for RustAnalyzer {