Skip to content

Commit

Permalink
rename for clarity, #346
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyuzhai committed Nov 1, 2024
1 parent d7e8f0f commit c9be034
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 69 deletions.
4 changes: 2 additions & 2 deletions crates/hir/husky-hir-eager-expr/src/helpers/codespan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use husky_sem_expr::{
SemExprIdx, SemExprRegionData,
};
use husky_text::{HasText, Text};
use husky_text_protocol::offset::OffsetRange;
use husky_text_protocol::offset::TextOffsetRange;
use husky_token::{RangedTokenSheet, TokenDb};
use husky_vfs::path::module_path::ModulePath;

Expand Down Expand Up @@ -146,7 +146,7 @@ impl<'a> HirEagerExprCodespanEmitter<'a> {

/// # getters
impl<'a> HirEagerExprCodespanEmitter<'a> {
fn expr_offset_range(&self, expr: HirEagerExprIdx) -> OffsetRange {
fn expr_offset_range(&self, expr: HirEagerExprIdx) -> TextOffsetRange {
let expr: SemExprIdx = self
.hir_eager_expr_source_map_data
.hir_eager_to_sem_expr_idx(expr);
Expand Down
4 changes: 2 additions & 2 deletions crates/lex/husky-text/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod tests;
use self::jar::*;
#[cfg(test)]
use self::tests::*;
use husky_text_protocol::{line_map::*, offset::OffsetRange, position::TextLine, range::*};
use husky_text_protocol::{line_map::*, offset::TextOffsetRange, position::TextLine, range::*};
use husky_vfs::path::module_path::ModulePath;
use line_map::module_text_line_map;

Expand Down Expand Up @@ -50,7 +50,7 @@ impl<'a> std::fmt::Debug for Text<'a> {
}

impl<'a> Text<'a> {
pub fn offset_range(self, range: TextRange) -> OffsetRange {
pub fn offset_range(self, range: TextRange) -> TextOffsetRange {
self.line_map.offset_range(range)
}
}
Expand Down
39 changes: 21 additions & 18 deletions crates/protocols/husky-text-protocol/src/line_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ mod lsp;
mod text_bytes_len;
mod wide_char;

use offset::{OffsetRange, OffsetRangeFrom, OffsetRangeTo};
use offset::{TextOffsetRange, TextOffsetRangeFrom, TextOffsetRangeTo};
pub use text_bytes_len::*;

use crate::offset::Offset;
use crate::offset::TextOffset;
use crate::*;
use rustc_hash::FxHashMap;
use std::{
Expand All @@ -19,7 +19,7 @@ use wide_char::*;
pub struct LineMap {
text_len_in_bytes: usize,
/// Offset the the beginning of each line, zero-based
pub(crate) newlines: Vec<Offset>,
pub(crate) newlines: Vec<TextOffset>,
/// List of non-ASCII characters on each line
pub(crate) wide_chars_line_map: FxHashMap<TextLine, Vec<WideCharColRange>>,
}
Expand All @@ -38,15 +38,15 @@ impl LineMap {
let mut wide_chars_line_map = FxHashMap::default();
let mut wide_chars_buffer: Vec<WideCharColRange> = vec![];

let mut newlines: Vec<Offset> = vec![0.into()];
let mut newlines: Vec<TextOffset> = vec![0.into()];
let mut curr_row: usize = 0;
let mut curr_col: u32 = 0;
let mut line = 0;
for c in text.chars() {
let c_len = c.text_bytes_len();
curr_row += c_len as usize;
if c == '\n' {
newlines.push(Offset::from(curr_row));
newlines.push(TextOffset::from(curr_row));

// Save any utf-16 characters seen in the previous line
if !wide_chars_buffer.is_empty() {
Expand Down Expand Up @@ -86,7 +86,7 @@ impl LineMap {
(0.into())..(self.newlines.len().into())
}

pub fn position_from_offset(&self, offset: Offset) -> TextPosition {
pub fn position_from_offset(&self, offset: TextOffset) -> TextPosition {
let row = self.newlines.partition_point(|&it| it <= offset) - 1;
let line_start_offset = self.newlines[row];
let col = offset - line_start_offset;
Expand All @@ -97,7 +97,7 @@ impl LineMap {
}

/// the byte index
pub fn offset(&self, pos: TextPosition) -> Offset {
pub fn offset(&self, pos: TextPosition) -> TextOffset {
if pos.line.index() == self.newlines.len() {
if pos.col.index32() == 0 {
return self.text_len_in_bytes.into();
Expand All @@ -108,16 +108,16 @@ impl LineMap {
self.newlines[pos.line.index()] + usize::from(pos.col.index())
}

pub fn offset_range(&self, range: impl Into<TextRange>) -> OffsetRange {
pub fn offset_range(&self, range: impl Into<TextRange>) -> TextOffsetRange {
let range = range.into();
(self.offset(range.start)..self.offset(range.end)).into()
}

pub fn offset_range_from(&self, range_from: RangeFrom<TextPosition>) -> OffsetRangeFrom {
pub fn offset_range_from(&self, range_from: RangeFrom<TextPosition>) -> TextOffsetRangeFrom {
(self.offset(range_from.start)..).into()
}

pub fn offset_range_to(&self, range_to: RangeTo<TextPosition>) -> OffsetRangeTo {
pub fn offset_range_to(&self, range_to: RangeTo<TextPosition>) -> TextOffsetRangeTo {
(..self.offset(range_to.end)).into()
}

Expand All @@ -135,7 +135,7 @@ impl LineMap {
// (..=self.offset(range_to_inclusive.end)).into()
// }

pub fn text_line_offset_range(&self, line: TextLine) -> OffsetRange {
pub fn text_line_offset_range(&self, line: TextLine) -> TextOffsetRange {
let end = if (line.0 as usize) < self.newlines.len() {
self.offset(TextPosition {
line: line + 1,
Expand All @@ -151,7 +151,10 @@ impl LineMap {
.into()
}

pub fn text_line_range_offset_range(&self, text_line_range: Range<TextLine>) -> OffsetRange {
pub fn text_line_range_offset_range(
&self,
text_line_range: Range<TextLine>,
) -> TextOffsetRange {
(self.offset(TextPosition {
line: text_line_range.start,
col: 0.into(),
Expand All @@ -165,7 +168,7 @@ impl LineMap {
pub fn offset_range_from_text_line(
&self,
range_from_text_line: RangeFrom<TextLine>,
) -> OffsetRangeFrom {
) -> TextOffsetRangeFrom {
(self.offset(TextPosition {
line: range_from_text_line.start,
col: 0.into(),
Expand All @@ -176,7 +179,7 @@ impl LineMap {
pub fn offset_range_to_text_line(
&self,
range_from_text_line: RangeTo<TextLine>,
) -> OffsetRangeTo {
) -> TextOffsetRangeTo {
(..self.offset(TextPosition {
line: range_from_text_line.end,
col: 0.into(),
Expand All @@ -187,7 +190,7 @@ impl LineMap {
pub fn offset_range_inclusive_text_line(
&self,
range_from_text_line: RangeInclusive<TextLine>,
) -> OffsetRange {
) -> TextOffsetRange {
(self.offset(TextPosition {
line: *range_from_text_line.start(),
col: 0.into(),
Expand All @@ -201,7 +204,7 @@ impl LineMap {
pub fn offset_range_to_inclusive_text_line(
&self,
range_from_text_line: RangeToInclusive<TextLine>,
) -> OffsetRangeTo {
) -> TextOffsetRangeTo {
(..self.offset(TextPosition {
line: range_from_text_line.end + 1,
col: 0.into(),
Expand All @@ -225,7 +228,7 @@ impl LineMap {
}
}

pub fn lines(&self, range: OffsetRange) -> impl Iterator<Item = OffsetRange> + '_ {
pub fn lines(&self, range: TextOffsetRange) -> impl Iterator<Item = TextOffsetRange> + '_ {
let lo = self.newlines.partition_point(|&it| it < range.start());
let hi = self.newlines.partition_point(|&it| it <= range.end());
let all = iter::once(range.start())
Expand All @@ -234,7 +237,7 @@ impl LineMap {

all.clone()
.zip(all.skip(1))
.map(|(lo, hi)| -> OffsetRange { (lo..hi).into() })
.map(|(lo, hi)| -> TextOffsetRange { (lo..hi).into() })
.filter(|it| !it.is_empty())
}

Expand Down
4 changes: 2 additions & 2 deletions crates/protocols/husky-text-protocol/src/line_map/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use super::*;
#[cfg(feature = "lsp_support")]
impl LineMap {
#[cfg(feature = "lsp_support")]
pub fn string_range(line_map: &LineMap, range: lsp_types::Range) -> OffsetRange {
pub fn string_range(line_map: &LineMap, range: lsp_types::Range) -> TextOffsetRange {
let start = line_map.lsp_offset(range.start);
let end = line_map.lsp_offset(range.end);
(start..end).into()
}

pub(crate) fn lsp_offset(&self, position: lsp_types::Position) -> Offset {
pub(crate) fn lsp_offset(&self, position: lsp_types::Position) -> TextOffset {
let line_col = TextPosition {
line: position.line.into(),
col: position.character.into(),
Expand Down
80 changes: 40 additions & 40 deletions crates/protocols/husky-text-protocol/src/offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,47 @@ use shifted_unsigned_int::ShiftedU32;
Debug, PartialEq, Default, Eq, PartialOrd, Ord, Clone, Copy, Hash, Serialize, Deserialize,
)]
#[serde(transparent)]
pub struct Offset(ShiftedU32);
pub struct TextOffset(ShiftedU32);

impl Offset {
impl TextOffset {
pub fn index(self) -> usize {
self.0.index()
}
}

impl From<i32> for Offset {
impl From<i32> for TextOffset {
fn from(raw: i32) -> Self {
Offset(ShiftedU32::from(raw))
TextOffset(ShiftedU32::from(raw))
}
}

impl From<usize> for Offset {
impl From<usize> for TextOffset {
fn from(raw: usize) -> Self {
Offset(ShiftedU32::from(raw))
TextOffset(ShiftedU32::from(raw))
}
}

impl From<u32> for Offset {
impl From<u32> for TextOffset {
fn from(raw: u32) -> Self {
Offset(ShiftedU32::from(raw))
TextOffset(ShiftedU32::from(raw))
}
}

impl std::ops::Add<usize> for Offset {
type Output = Offset;
impl std::ops::Add<usize> for TextOffset {
type Output = TextOffset;

fn add(self, rhs: usize) -> Self::Output {
Offset(self.0 + rhs)
TextOffset(self.0 + rhs)
}
}

impl std::ops::AddAssign<usize> for Offset {
impl std::ops::AddAssign<usize> for TextOffset {
fn add_assign(&mut self, rhs: usize) {
self.0 += rhs;
}
}

impl std::ops::Sub<Self> for Offset {
impl std::ops::Sub<Self> for TextOffset {
type Output = usize;

fn sub(self, rhs: Self) -> Self::Output {
Expand All @@ -62,28 +62,28 @@ impl std::ops::Sub<Self> for Offset {
#[derive(
Debug, PartialEq, Default, Eq, PartialOrd, Ord, Clone, Copy, Hash, Serialize, Deserialize,
)]
pub struct OffsetRange {
start: Offset,
end: Offset,
pub struct TextOffsetRange {
start: TextOffset,
end: TextOffset,
}

impl From<Range<Offset>> for OffsetRange {
fn from(range: Range<Offset>) -> Self {
impl From<Range<TextOffset>> for TextOffsetRange {
fn from(range: Range<TextOffset>) -> Self {
Self {
start: range.start,
end: range.end,
}
}
}

impl Into<Range<usize>> for OffsetRange {
impl Into<Range<usize>> for TextOffsetRange {
fn into(self) -> Range<usize> {
self.raw()
}
}

impl OffsetRange {
pub fn new(start: Offset, end: Offset) -> Self {
impl TextOffsetRange {
pub fn new(start: TextOffset, end: TextOffset) -> Self {
Self { start, end }
}

Expand All @@ -92,12 +92,12 @@ impl OffsetRange {
}
}

impl OffsetRange {
pub fn start(self) -> Offset {
impl TextOffsetRange {
pub fn start(self) -> TextOffset {
self.start
}

pub fn end(self) -> Offset {
pub fn end(self) -> TextOffset {
self.end
}

Expand All @@ -106,30 +106,30 @@ impl OffsetRange {
}
}

impl std::ops::Index<OffsetRange> for str {
impl std::ops::Index<TextOffsetRange> for str {
type Output = str;

fn index(&self, range: OffsetRange) -> &Self::Output {
fn index(&self, range: TextOffsetRange) -> &Self::Output {
&self[range.start.index()..range.end.index()]
}
}

#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct OffsetRangeFrom {
start: Offset,
pub struct TextOffsetRangeFrom {
start: TextOffset,
}

impl From<RangeFrom<Offset>> for OffsetRangeFrom {
fn from(range: RangeFrom<Offset>) -> Self {
impl From<RangeFrom<TextOffset>> for TextOffsetRangeFrom {
fn from(range: RangeFrom<TextOffset>) -> Self {
Self { start: range.start }
}
}

impl std::ops::Index<OffsetRangeFrom> for str {
impl std::ops::Index<TextOffsetRangeFrom> for str {
type Output = str;

fn index(&self, range: OffsetRangeFrom) -> &Self::Output {
fn index(&self, range: TextOffsetRangeFrom) -> &Self::Output {
&self[range.start.index()..]
}
}
Expand All @@ -138,18 +138,18 @@ impl std::ops::Index<OffsetRangeFrom> for str {
Debug, PartialEq, Default, Eq, PartialOrd, Ord, Clone, Copy, Hash, Serialize, Deserialize,
)]
#[serde(transparent)]
pub struct OffsetRangeTo {
end: Offset,
pub struct TextOffsetRangeTo {
end: TextOffset,
}

impl From<RangeTo<Offset>> for OffsetRangeTo {
fn from(range: RangeTo<Offset>) -> Self {
impl From<RangeTo<TextOffset>> for TextOffsetRangeTo {
fn from(range: RangeTo<TextOffset>) -> Self {
Self { end: range.end }
}
}

impl OffsetRangeTo {
pub fn end(self) -> Offset {
impl TextOffsetRangeTo {
pub fn end(self) -> TextOffset {
self.end
}

Expand All @@ -158,10 +158,10 @@ impl OffsetRangeTo {
}
}

impl std::ops::Index<OffsetRangeTo> for str {
impl std::ops::Index<TextOffsetRangeTo> for str {
type Output = str;

fn index(&self, range: OffsetRangeTo) -> &Self::Output {
fn index(&self, range: TextOffsetRangeTo) -> &Self::Output {
&self[range.raw()]
}
}
Loading

0 comments on commit c9be034

Please sign in to comment.