Skip to content

Commit 29fc409

Browse files
bors[bot]matklad
andauthored
Merge #4131
4131: Switch to text-size r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 27a7718 + e873469 commit 29fc409

File tree

439 files changed

+26954
-26996
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

439 files changed

+26954
-26996
lines changed

Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_assists/src/assist_ctx.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use ra_fmt::{leading_indent, reindent};
55
use ra_ide_db::RootDatabase;
66
use ra_syntax::{
77
algo::{self, find_covering_element, find_node_at_offset},
8-
AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextUnit,
8+
AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextSize,
99
TokenAtOffset,
1010
};
1111
use ra_text_edit::TextEditBuilder;
@@ -178,7 +178,7 @@ impl<'a> AssistGroup<'a> {
178178
#[derive(Default)]
179179
pub(crate) struct ActionBuilder {
180180
edit: TextEditBuilder,
181-
cursor_position: Option<TextUnit>,
181+
cursor_position: Option<TextSize>,
182182
target: Option<TextRange>,
183183
file: AssistFile,
184184
}
@@ -211,12 +211,12 @@ impl ActionBuilder {
211211
}
212212

213213
/// Append specified `text` at the given `offset`
214-
pub(crate) fn insert(&mut self, offset: TextUnit, text: impl Into<String>) {
214+
pub(crate) fn insert(&mut self, offset: TextSize, text: impl Into<String>) {
215215
self.edit.insert(offset, text.into())
216216
}
217217

218218
/// Specify desired position of the cursor after the assist is applied.
219-
pub(crate) fn set_cursor(&mut self, offset: TextUnit) {
219+
pub(crate) fn set_cursor(&mut self, offset: TextSize) {
220220
self.cursor_position = Some(offset)
221221
}
222222

crates/ra_assists/src/handlers/add_custom_impl.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use ra_syntax::{
22
ast::{self, AstNode},
33
Direction, SmolStr,
44
SyntaxKind::{IDENT, WHITESPACE},
5-
TextRange, TextUnit,
5+
TextRange, TextSize,
66
};
77
use stdx::SepBy;
88

@@ -60,7 +60,6 @@ pub(crate) fn add_custom_impl(ctx: AssistCtx) -> Option<Assist> {
6060
.collect::<Vec<SmolStr>>();
6161
let has_more_derives = !new_attr_input.is_empty();
6262
let new_attr_input = new_attr_input.iter().sep_by(", ").surround_with("(", ")").to_string();
63-
let new_attr_input_len = new_attr_input.len();
6463

6564
let mut buf = String::new();
6665
buf.push_str("\n\nimpl ");
@@ -70,8 +69,9 @@ pub(crate) fn add_custom_impl(ctx: AssistCtx) -> Option<Assist> {
7069
buf.push_str(" {\n");
7170

7271
let cursor_delta = if has_more_derives {
72+
let delta = input.syntax().text_range().len() - TextSize::of(&new_attr_input);
7373
edit.replace(input.syntax().text_range(), new_attr_input);
74-
input.syntax().text_range().len() - TextUnit::from_usize(new_attr_input_len)
74+
delta
7575
} else {
7676
let attr_range = attr.syntax().text_range();
7777
edit.delete(attr_range);
@@ -81,13 +81,13 @@ pub(crate) fn add_custom_impl(ctx: AssistCtx) -> Option<Assist> {
8181
.next_sibling_or_token()
8282
.filter(|t| t.kind() == WHITESPACE)
8383
.map(|t| t.text_range())
84-
.unwrap_or_else(|| TextRange::from_to(TextUnit::from(0), TextUnit::from(0)));
84+
.unwrap_or_else(|| TextRange::new(TextSize::from(0), TextSize::from(0)));
8585
edit.delete(line_break_range);
8686

8787
attr_range.len() + line_break_range.len()
8888
};
8989

90-
edit.set_cursor(start_offset + TextUnit::of_str(&buf) - cursor_delta);
90+
edit.set_cursor(start_offset + TextSize::of(&buf) - cursor_delta);
9191
buf.push_str("\n}");
9292
edit.insert(start_offset, buf);
9393
})

crates/ra_assists/src/handlers/add_derive.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use ra_syntax::{
22
ast::{self, AstNode, AttrsOwner},
33
SyntaxKind::{COMMENT, WHITESPACE},
4-
TextUnit,
4+
TextSize,
55
};
66

77
use crate::{Assist, AssistCtx, AssistId};
@@ -37,17 +37,17 @@ pub(crate) fn add_derive(ctx: AssistCtx) -> Option<Assist> {
3737
let offset = match derive_attr {
3838
None => {
3939
edit.insert(node_start, "#[derive()]\n");
40-
node_start + TextUnit::of_str("#[derive(")
40+
node_start + TextSize::of("#[derive(")
4141
}
42-
Some(tt) => tt.syntax().text_range().end() - TextUnit::of_char(')'),
42+
Some(tt) => tt.syntax().text_range().end() - TextSize::of(')'),
4343
};
4444
edit.target(nominal.syntax().text_range());
4545
edit.set_cursor(offset)
4646
})
4747
}
4848

4949
// Insert `derive` after doc comments.
50-
fn derive_insertion_offset(nominal: &ast::NominalDef) -> Option<TextUnit> {
50+
fn derive_insertion_offset(nominal: &ast::NominalDef) -> Option<TextSize> {
5151
let non_ws_child = nominal
5252
.syntax()
5353
.children_with_tokens()

crates/ra_assists/src/handlers/add_explicit_type.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ pub(crate) fn add_explicit_type(ctx: AssistCtx) -> Option<Assist> {
3737
let stmt_range = stmt.syntax().text_range();
3838
let eq_range = stmt.eq_token()?.text_range();
3939
// Assist should only be applicable if cursor is between 'let' and '='
40-
let let_range = TextRange::from_to(stmt_range.start(), eq_range.start());
41-
let cursor_in_range = ctx.frange.range.is_subrange(&let_range);
40+
let let_range = TextRange::new(stmt_range.start(), eq_range.start());
41+
let cursor_in_range = let_range.contains_range(ctx.frange.range);
4242
if !cursor_in_range {
4343
return None;
4444
}

crates/ra_assists/src/handlers/add_from_impl_for_enum.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ra_syntax::{
22
ast::{self, AstNode, NameOwner},
3-
TextUnit,
3+
TextSize,
44
};
55
use stdx::format_to;
66

@@ -65,7 +65,7 @@ impl From<{0}> for {1} {{
6565
variant_name
6666
);
6767
edit.insert(start_offset, buf);
68-
edit.set_cursor(start_offset + TextUnit::of_str("\n\n"));
68+
edit.set_cursor(start_offset + TextSize::of("\n\n"));
6969
},
7070
)
7171
}

crates/ra_assists/src/handlers/add_function.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ra_syntax::{
22
ast::{self, AstNode},
3-
SyntaxKind, SyntaxNode, TextUnit,
3+
SyntaxKind, SyntaxNode, TextSize,
44
};
55

66
use crate::{Assist, AssistCtx, AssistFile, AssistId};
@@ -69,8 +69,8 @@ pub(crate) fn add_function(ctx: AssistCtx) -> Option<Assist> {
6969
}
7070

7171
struct FunctionTemplate {
72-
insert_offset: TextUnit,
73-
cursor_offset: TextUnit,
72+
insert_offset: TextSize,
73+
cursor_offset: TextSize,
7474
fn_def: ast::SourceFile,
7575
file: AssistFile,
7676
}
@@ -129,7 +129,7 @@ impl FunctionBuilder {
129129
let fn_def = indent_once.increase_indent(fn_def);
130130
let fn_def = ast::make::add_trailing_newlines(1, fn_def);
131131
let fn_def = indent.increase_indent(fn_def);
132-
(fn_def, it.syntax().text_range().start() + TextUnit::from_usize(1))
132+
(fn_def, it.syntax().text_range().start() + TextSize::of('{'))
133133
}
134134
};
135135

crates/ra_assists/src/handlers/add_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ra_syntax::{
22
ast::{self, AstNode, NameOwner, TypeParamsOwner},
3-
TextUnit,
3+
TextSize,
44
};
55
use stdx::{format_to, SepBy};
66

@@ -51,7 +51,7 @@ pub(crate) fn add_impl(ctx: AssistCtx) -> Option<Assist> {
5151
format_to!(buf, "<{}>", generic_params)
5252
}
5353
buf.push_str(" {\n");
54-
edit.set_cursor(start_offset + TextUnit::of_str(&buf));
54+
edit.set_cursor(start_offset + TextSize::of(&buf));
5555
buf.push_str("\n}");
5656
edit.insert(start_offset, buf);
5757
})

crates/ra_assists/src/handlers/add_new.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use ra_syntax::{
33
ast::{
44
self, AstNode, NameOwner, StructKind, TypeAscriptionOwner, TypeParamsOwner, VisibilityOwner,
55
},
6-
TextUnit, T,
6+
TextSize, T,
77
};
88
use stdx::{format_to, SepBy};
99

@@ -77,16 +77,16 @@ pub(crate) fn add_new(ctx: AssistCtx) -> Option<Assist> {
7777
.text_range()
7878
.end();
7979

80-
Some((start, TextUnit::from_usize(1)))
80+
Some((start, TextSize::of("\n")))
8181
})
8282
.unwrap_or_else(|| {
8383
buf = generate_impl_text(&strukt, &buf);
8484
let start = strukt.syntax().text_range().end();
8585

86-
(start, TextUnit::from_usize(3))
86+
(start, TextSize::of("\n}\n"))
8787
});
8888

89-
edit.set_cursor(start_offset + TextUnit::of_str(&buf) - end_offset);
89+
edit.set_cursor(start_offset + TextSize::of(&buf) - end_offset);
9090
edit.insert(start_offset, buf);
9191
})
9292
}

crates/ra_assists/src/handlers/apply_demorgan.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub(crate) fn apply_demorgan(ctx: AssistCtx) -> Option<Assist> {
2626
let op = expr.op_kind()?;
2727
let op_range = expr.op_token()?.text_range();
2828
let opposite_op = opposite_logic_op(op)?;
29-
let cursor_in_range = ctx.frange.range.is_subrange(&op_range);
29+
let cursor_in_range = op_range.contains_range(ctx.frange.range);
3030
if !cursor_in_range {
3131
return None;
3232
}

crates/ra_assists/src/handlers/change_visibility.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use ra_syntax::{
55
ATTR, COMMENT, CONST_DEF, ENUM_DEF, FN_DEF, MODULE, STRUCT_DEF, TRAIT_DEF, VISIBILITY,
66
WHITESPACE,
77
},
8-
SyntaxNode, TextUnit, T,
8+
SyntaxNode, TextSize, T,
99
};
1010

1111
use crate::{Assist, AssistCtx, AssistId};
@@ -67,7 +67,7 @@ fn add_vis(ctx: AssistCtx) -> Option<Assist> {
6767
})
6868
}
6969

70-
fn vis_offset(node: &SyntaxNode) -> TextUnit {
70+
fn vis_offset(node: &SyntaxNode) -> TextSize {
7171
node.children_with_tokens()
7272
.skip_while(|it| match it.kind() {
7373
WHITESPACE | COMMENT | ATTR => true,

crates/ra_assists/src/handlers/flip_binexpr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub(crate) fn flip_binexpr(ctx: AssistCtx) -> Option<Assist> {
2323
let rhs = expr.rhs()?.syntax().clone();
2424
let op_range = expr.op_token()?.text_range();
2525
// The assist should be applied only if the cursor is on the operator
26-
let cursor_in_range = ctx.frange.range.is_subrange(&op_range);
26+
let cursor_in_range = op_range.contains_range(ctx.frange.range);
2727
if !cursor_in_range {
2828
return None;
2929
}

crates/ra_assists/src/handlers/inline_local_variable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option<Assist> {
5252
.next_sibling_or_token()
5353
.and_then(|it| ast::Whitespace::cast(it.as_token()?.clone()))
5454
{
55-
TextRange::from_to(
55+
TextRange::new(
5656
let_stmt.syntax().text_range().start(),
5757
whitespace.syntax().text_range().end(),
5858
)

crates/ra_assists/src/handlers/introduce_variable.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ra_syntax::{
44
BLOCK_EXPR, BREAK_EXPR, COMMENT, LAMBDA_EXPR, LOOP_EXPR, MATCH_ARM, PATH_EXPR, RETURN_EXPR,
55
WHITESPACE,
66
},
7-
SyntaxNode, TextUnit,
7+
SyntaxNode, TextSize,
88
};
99
use stdx::format_to;
1010
use test_utils::tested_by;
@@ -47,10 +47,10 @@ pub(crate) fn introduce_variable(ctx: AssistCtx) -> Option<Assist> {
4747

4848
let cursor_offset = if wrap_in_block {
4949
buf.push_str("{ let var_name = ");
50-
TextUnit::of_str("{ let ")
50+
TextSize::of("{ let ")
5151
} else {
5252
buf.push_str("let var_name = ");
53-
TextUnit::of_str("let ")
53+
TextSize::of("let ")
5454
};
5555
format_to!(buf, "{}", expr.syntax());
5656
let full_stmt = ast::ExprStmt::cast(anchor_stmt.clone());

crates/ra_assists/src/handlers/invert_if.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub(crate) fn invert_if(ctx: AssistCtx) -> Option<Assist> {
2828
let if_keyword = ctx.find_token_at_offset(T![if])?;
2929
let expr = ast::IfExpr::cast(if_keyword.parent())?;
3030
let if_range = if_keyword.text_range();
31-
let cursor_in_range = ctx.frange.range.is_subrange(&if_range);
31+
let cursor_in_range = if_range.contains_range(ctx.frange.range);
3232
if !cursor_in_range {
3333
return None;
3434
}

crates/ra_assists/src/handlers/merge_match_arms.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::iter::successors;
33
use ra_syntax::{
44
algo::neighbor,
55
ast::{self, AstNode},
6-
Direction, TextUnit,
6+
Direction, TextSize,
77
};
88

99
use crate::{Assist, AssistCtx, AssistId, TextRange};
@@ -42,8 +42,8 @@ pub(crate) fn merge_match_arms(ctx: AssistCtx) -> Option<Assist> {
4242
let current_text_range = current_arm.syntax().text_range();
4343

4444
enum CursorPos {
45-
InExpr(TextUnit),
46-
InPat(TextUnit),
45+
InExpr(TextSize),
46+
InPat(TextSize),
4747
}
4848
let cursor_pos = ctx.frange.range.start();
4949
let cursor_pos = if current_expr.syntax().text_range().contains(cursor_pos) {
@@ -89,10 +89,10 @@ pub(crate) fn merge_match_arms(ctx: AssistCtx) -> Option<Assist> {
8989

9090
edit.target(current_text_range);
9191
edit.set_cursor(match cursor_pos {
92-
CursorPos::InExpr(back_offset) => start + TextUnit::from_usize(arm.len()) - back_offset,
92+
CursorPos::InExpr(back_offset) => start + TextSize::of(&arm) - back_offset,
9393
CursorPos::InPat(offset) => offset,
9494
});
95-
edit.replace(TextRange::from_to(start, end), arm);
95+
edit.replace(TextRange::new(start, end), arm);
9696
})
9797
}
9898

0 commit comments

Comments
 (0)