Skip to content

Commit 0156a53

Browse files
bors[bot]vemoo
andcommitted
Merge #276
276: Extract and rename AtomEdit and Edit into ra_text_edit r=matklad a=vemoo As discused in #105 Co-authored-by: Bernardo <[email protected]>
2 parents f655f99 + 0527e3b commit 0156a53

File tree

21 files changed

+156
-110
lines changed

21 files changed

+156
-110
lines changed

Cargo.lock

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_analysis/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ rustc-hash = "1.0"
1414
parking_lot = "0.6.4"
1515
ra_syntax = { path = "../ra_syntax" }
1616
ra_editor = { path = "../ra_editor" }
17+
ra_text_edit = { path = "../ra_text_edit" }
1718
ra_db = { path = "../ra_db" }
1819
hir = { path = "../ra_hir", package = "ra_hir" }
1920
test_utils = { path = "../test_utils" }

crates/ra_analysis/src/completion.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
mod reference_completion;
22

33
use ra_editor::find_node_at_offset;
4+
use ra_text_edit::AtomTextEdit;
45
use ra_syntax::{
56
algo::visit::{visitor_ctx, VisitorCtx},
67
ast,
7-
AstNode, AtomEdit,
8+
AstNode,
89
SyntaxNodeRef,
910
};
1011
use ra_db::SyntaxDatabase;
@@ -33,7 +34,7 @@ pub(crate) fn completions(
3334
let original_file = db.source_file(position.file_id);
3435
// Insert a fake ident to get a valid parse tree
3536
let file = {
36-
let edit = AtomEdit::insert(position.offset, "intellijRulezz".to_string());
37+
let edit = AtomTextEdit::insert(position.offset, "intellijRulezz".to_string());
3738
original_file.reparse(&edit)
3839
};
3940

crates/ra_analysis/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ pub mod mock_analysis;
1818

1919
use std::{fmt, sync::Arc};
2020

21-
use ra_syntax::{AtomEdit, SourceFileNode, TextRange, TextUnit};
21+
use ra_syntax::{SourceFileNode, TextRange, TextUnit};
22+
use ra_text_edit::AtomTextEdit;
2223
use ra_db::FileResolverImp;
2324
use rayon::prelude::*;
2425
use relative_path::RelativePathBuf;
@@ -120,7 +121,7 @@ pub struct SourceChange {
120121
#[derive(Debug)]
121122
pub struct SourceFileNodeEdit {
122123
pub file_id: FileId,
123-
pub edits: Vec<AtomEdit>,
124+
pub edits: Vec<AtomTextEdit>,
124125
}
125126

126127
#[derive(Debug)]

crates/ra_editor/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ join_to_string = "0.1.1"
1212
rustc-hash = "1.0"
1313

1414
ra_syntax = { path = "../ra_syntax" }
15+
ra_text_edit = { path = "../ra_text_edit" }
1516

1617
[dev-dependencies]
1718
test_utils = { path = "../test_utils" }

crates/ra_editor/src/code_actions.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ use ra_syntax::{
88
SyntaxNodeRef, TextRange, TextUnit,
99
};
1010

11-
use crate::{find_node_at_offset, Edit, EditBuilder};
11+
use crate::{find_node_at_offset, TextEdit, TextEditBuilder};
1212

1313
#[derive(Debug)]
1414
pub struct LocalEdit {
15-
pub edit: Edit,
15+
pub edit: TextEdit,
1616
pub cursor_position: Option<TextUnit>,
1717
}
1818

@@ -26,7 +26,7 @@ pub fn flip_comma<'a>(
2626
let prev = non_trivia_sibling(comma, Direction::Prev)?;
2727
let next = non_trivia_sibling(comma, Direction::Next)?;
2828
Some(move || {
29-
let mut edit = EditBuilder::new();
29+
let mut edit = TextEditBuilder::new();
3030
edit.replace(prev.range(), next.text().to_string());
3131
edit.replace(next.range(), prev.text().to_string());
3232
LocalEdit {
@@ -49,7 +49,7 @@ pub fn add_derive<'a>(
4949
.filter(|(name, _arg)| name == "derive")
5050
.map(|(_name, arg)| arg)
5151
.next();
52-
let mut edit = EditBuilder::new();
52+
let mut edit = TextEditBuilder::new();
5353
let offset = match derive_attr {
5454
None => {
5555
edit.insert(node_start, "#[derive()]\n".to_string());
@@ -82,7 +82,7 @@ pub fn add_impl<'a>(
8282

8383
Some(move || {
8484
let type_params = nominal.type_param_list();
85-
let mut edit = EditBuilder::new();
85+
let mut edit = TextEditBuilder::new();
8686
let start_offset = nominal.syntax().range().end();
8787
let mut buf = String::new();
8888
buf.push_str("\n\nimpl");
@@ -129,7 +129,7 @@ pub fn introduce_variable<'a>(
129129
}
130130
return Some(move || {
131131
let mut buf = String::new();
132-
let mut edit = EditBuilder::new();
132+
let mut edit = TextEditBuilder::new();
133133

134134
buf.push_str("let var_name = ");
135135
expr.syntax().text().push_to(&mut buf);

crates/ra_editor/src/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
mod code_actions;
2-
mod edit;
32
mod extend_selection;
43
mod folding_ranges;
54
mod line_index;
@@ -10,14 +9,13 @@ mod typing;
109

1110
pub use self::{
1211
code_actions::{add_derive, add_impl, flip_comma, introduce_variable, LocalEdit},
13-
edit::{Edit, EditBuilder},
1412
extend_selection::extend_selection,
1513
folding_ranges::{folding_ranges, Fold, FoldKind},
1614
line_index::{LineCol, LineIndex},
1715
symbols::{file_structure, file_symbols, FileSymbol, StructureNode},
1816
typing::{join_lines, on_enter, on_eq_typed},
1917
};
20-
pub use ra_syntax::AtomEdit;
18+
use ra_text_edit::{TextEdit, TextEditBuilder};
2119
use ra_syntax::{
2220
algo::find_leaf_at_offset,
2321
ast::{self, AstNode, NameOwner},

crates/ra_editor/src/typing.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ use std::mem;
33
use ra_syntax::{
44
algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset},
55
ast,
6-
text_utils::{contains_offset_nonstrict, intersect},
6+
text_utils::intersect,
77
AstNode, SourceFileNode, SyntaxKind,
88
SyntaxKind::*,
99
SyntaxNodeRef, TextRange, TextUnit,
1010
};
11+
use ra_text_edit::text_utils::contains_offset_nonstrict;
1112

12-
use crate::{find_node_at_offset, EditBuilder, LocalEdit};
13+
use crate::{find_node_at_offset, TextEditBuilder, LocalEdit};
1314

1415
pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit {
1516
let range = if range.is_empty() {
@@ -18,7 +19,7 @@ pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit {
1819
let pos = match text.find('\n') {
1920
None => {
2021
return LocalEdit {
21-
edit: EditBuilder::new().finish(),
22+
edit: TextEditBuilder::new().finish(),
2223
cursor_position: None,
2324
};
2425
}
@@ -30,7 +31,7 @@ pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit {
3031
};
3132

3233
let node = find_covering_node(file.syntax(), range);
33-
let mut edit = EditBuilder::new();
34+
let mut edit = TextEditBuilder::new();
3435
for node in node.descendants() {
3536
let text = match node.leaf_text() {
3637
Some(text) => text,
@@ -72,7 +73,7 @@ pub fn on_enter(file: &SourceFileNode, offset: TextUnit) -> Option<LocalEdit> {
7273
let indent = node_indent(file, comment.syntax())?;
7374
let inserted = format!("\n{}{} ", indent, prefix);
7475
let cursor_position = offset + TextUnit::of_str(&inserted);
75-
let mut edit = EditBuilder::new();
76+
let mut edit = TextEditBuilder::new();
7677
edit.insert(offset, inserted);
7778
Some(LocalEdit {
7879
edit: edit.finish(),
@@ -122,15 +123,20 @@ pub fn on_eq_typed(file: &SourceFileNode, offset: TextUnit) -> Option<LocalEdit>
122123
return None;
123124
}
124125
let offset = let_stmt.syntax().range().end();
125-
let mut edit = EditBuilder::new();
126+
let mut edit = TextEditBuilder::new();
126127
edit.insert(offset, ";".to_string());
127128
Some(LocalEdit {
128129
edit: edit.finish(),
129130
cursor_position: None,
130131
})
131132
}
132133

133-
fn remove_newline(edit: &mut EditBuilder, node: SyntaxNodeRef, node_text: &str, offset: TextUnit) {
134+
fn remove_newline(
135+
edit: &mut TextEditBuilder,
136+
node: SyntaxNodeRef,
137+
node_text: &str,
138+
offset: TextUnit,
139+
) {
134140
if node.kind() != WHITESPACE || node_text.bytes().filter(|&b| b == b'\n').count() != 1 {
135141
// The node is either the first or the last in the file
136142
let suff = &node_text[TextRange::from_to(
@@ -191,7 +197,7 @@ fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
191197
}
192198
}
193199

194-
fn join_single_expr_block(edit: &mut EditBuilder, node: SyntaxNodeRef) -> Option<()> {
200+
fn join_single_expr_block(edit: &mut TextEditBuilder, node: SyntaxNodeRef) -> Option<()> {
195201
let block = ast::Block::cast(node.parent()?)?;
196202
let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?;
197203
let expr = single_expr(block)?;
@@ -269,14 +275,14 @@ fn foo() {
269275
fn test_join_lines_lambda_block() {
270276
check_join_lines(
271277
r"
272-
pub fn reparse(&self, edit: &AtomEdit) -> File {
278+
pub fn reparse(&self, edit: &AtomTextEdit) -> File {
273279
<|>self.incremental_reparse(edit).unwrap_or_else(|| {
274280
self.full_reparse(edit)
275281
})
276282
}
277283
",
278284
r"
279-
pub fn reparse(&self, edit: &AtomEdit) -> File {
285+
pub fn reparse(&self, edit: &AtomTextEdit) -> File {
280286
<|>self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit))
281287
}
282288
",

crates/ra_lsp_server/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ rustc-hash = "1.0"
2828

2929
ra_syntax = { path = "../ra_syntax" }
3030
ra_editor = { path = "../ra_editor" }
31+
ra_text_edit = { path = "../ra_text_edit" }
3132
ra_analysis = { path = "../ra_analysis" }
3233
gen_lsp_server = { path = "../gen_lsp_server" }
3334

crates/ra_lsp_server/src/conv.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use languageserver_types::{
2-
Location, Position, Range, SymbolKind, TextDocumentEdit, TextDocumentIdentifier,
3-
TextDocumentItem, TextDocumentPositionParams, TextEdit, Url, VersionedTextDocumentIdentifier,
2+
self, Location, Position, Range, SymbolKind, TextDocumentEdit, TextDocumentIdentifier,
3+
TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier,
44
};
55
use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileNodeEdit, FilePosition};
6-
use ra_editor::{AtomEdit, Edit, LineCol, LineIndex};
6+
use ra_editor::{LineCol, LineIndex};
7+
use ra_text_edit::{AtomTextEdit, TextEdit};
78
use ra_syntax::{SyntaxKind, TextRange, TextUnit};
89

910
use crate::{req, server_world::ServerWorld, Result};
@@ -91,24 +92,24 @@ impl ConvWith for Range {
9192
}
9293
}
9394

94-
impl ConvWith for Edit {
95+
impl ConvWith for TextEdit {
9596
type Ctx = LineIndex;
96-
type Output = Vec<TextEdit>;
97+
type Output = Vec<languageserver_types::TextEdit>;
9798

98-
fn conv_with(self, line_index: &LineIndex) -> Vec<TextEdit> {
99+
fn conv_with(self, line_index: &LineIndex) -> Vec<languageserver_types::TextEdit> {
99100
self.into_atoms()
100101
.into_iter()
101102
.map_conv_with(line_index)
102103
.collect()
103104
}
104105
}
105106

106-
impl ConvWith for AtomEdit {
107+
impl ConvWith for AtomTextEdit {
107108
type Ctx = LineIndex;
108-
type Output = TextEdit;
109+
type Output = languageserver_types::TextEdit;
109110

110-
fn conv_with(self, line_index: &LineIndex) -> TextEdit {
111-
TextEdit {
111+
fn conv_with(self, line_index: &LineIndex) -> languageserver_types::TextEdit {
112+
languageserver_types::TextEdit {
112113
range: self.delete.conv_with(line_index),
113114
new_text: self.insert,
114115
}
@@ -228,7 +229,7 @@ impl TryConvWith for SourceChange {
228229
fn translate_offset_with_edit(
229230
pre_edit_index: &LineIndex,
230231
offset: TextUnit,
231-
edits: &[AtomEdit],
232+
edits: &[AtomTextEdit],
232233
) -> LineCol {
233234
let fallback = pre_edit_index.line_col(offset);
234235
let edit = match edits.first() {

crates/ra_lsp_server/src/main_loop/handlers.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use languageserver_types::{
99
WorkspaceEdit, ParameterInformation, SignatureInformation, Hover, HoverContents,
1010
};
1111
use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FilePosition};
12-
use ra_syntax::{TextUnit, text_utils::{contains_offset_nonstrict, intersect}};
12+
use ra_syntax::{TextUnit, text_utils::intersect};
13+
use ra_text_edit::text_utils::contains_offset_nonstrict;
1314
use rustc_hash::FxHashMap;
1415
use serde_json::to_value;
1516

crates/ra_syntax/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ drop_bomb = "0.1.4"
1515
parking_lot = "0.6.0"
1616
rowan = "0.1.2"
1717
text_unit = "0.1.5"
18+
ra_text_edit = { path = "../ra_text_edit" }
1819

1920
[dev-dependencies]
2021
test_utils = { path = "../test_utils" }

crates/ra_syntax/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ pub use rowan::{SmolStr, TextRange, TextUnit};
4141
pub use crate::{
4242
ast::AstNode,
4343
lexer::{tokenize, Token},
44-
reparsing::AtomEdit,
4544
syntax_kinds::SyntaxKind,
4645
yellow::{
4746
Direction, OwnedRoot, RefRoot, SyntaxError, SyntaxNode, SyntaxNodeRef, TreeRoot, WalkEvent, Location,
4847
},
4948
};
5049

50+
use ra_text_edit::AtomTextEdit;
5151
use crate::yellow::GreenNode;
5252

5353
/// `SourceFileNode` represents a parse tree for a single Rust file.
@@ -68,15 +68,15 @@ impl SourceFileNode {
6868
parser_impl::parse_with(yellow::GreenBuilder::new(), text, &tokens, grammar::root);
6969
SourceFileNode::new(green, errors)
7070
}
71-
pub fn reparse(&self, edit: &AtomEdit) -> SourceFileNode {
71+
pub fn reparse(&self, edit: &AtomTextEdit) -> SourceFileNode {
7272
self.incremental_reparse(edit)
7373
.unwrap_or_else(|| self.full_reparse(edit))
7474
}
75-
pub fn incremental_reparse(&self, edit: &AtomEdit) -> Option<SourceFileNode> {
75+
pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<SourceFileNode> {
7676
reparsing::incremental_reparse(self.syntax(), edit, self.errors())
7777
.map(|(green_node, errors)| SourceFileNode::new(green_node, errors))
7878
}
79-
fn full_reparse(&self, edit: &AtomEdit) -> SourceFileNode {
79+
fn full_reparse(&self, edit: &AtomTextEdit) -> SourceFileNode {
8080
let text =
8181
text_utils::replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert);
8282
SourceFileNode::parse(&text)

0 commit comments

Comments
 (0)