extract AtomEdit and Edit into new ra_text_edit crate

This commit is contained in:
Bernardo 2018-12-10 22:09:12 +01:00
parent f655f993fe
commit 7344d28768
19 changed files with 81 additions and 40 deletions

12
Cargo.lock generated
View File

@ -610,6 +610,7 @@ dependencies = [
"ra_editor 0.1.0", "ra_editor 0.1.0",
"ra_hir 0.1.0", "ra_hir 0.1.0",
"ra_syntax 0.1.0", "ra_syntax 0.1.0",
"ra_text_edit 0.1.0",
"rayon 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
"relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -649,6 +650,7 @@ dependencies = [
"itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)",
"join_to_string 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "join_to_string 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"ra_syntax 0.1.0", "ra_syntax 0.1.0",
"ra_text_edit 0.1.0",
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"superslice 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "superslice 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"test_utils 0.1.0", "test_utils 0.1.0",
@ -687,6 +689,7 @@ dependencies = [
"ra_analysis 0.1.0", "ra_analysis 0.1.0",
"ra_editor 0.1.0", "ra_editor 0.1.0",
"ra_syntax 0.1.0", "ra_syntax 0.1.0",
"ra_text_edit 0.1.0",
"rayon 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
"relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -710,6 +713,7 @@ dependencies = [
"drop_bomb 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "drop_bomb 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
"ra_text_edit 0.1.0",
"rowan 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "rowan 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"test_utils 0.1.0", "test_utils 0.1.0",
"text_unit 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "text_unit 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
@ -717,6 +721,14 @@ dependencies = [
"walkdir 2.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 2.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "ra_text_edit"
version = "0.1.0"
dependencies = [
"test_utils 0.1.0",
"text_unit 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "rand" name = "rand"
version = "0.4.3" version = "0.4.3"

View File

@ -14,6 +14,7 @@ rustc-hash = "1.0"
parking_lot = "0.6.4" parking_lot = "0.6.4"
ra_syntax = { path = "../ra_syntax" } ra_syntax = { path = "../ra_syntax" }
ra_editor = { path = "../ra_editor" } ra_editor = { path = "../ra_editor" }
ra_text_edit = { path = "../ra_text_edit" }
ra_db = { path = "../ra_db" } ra_db = { path = "../ra_db" }
hir = { path = "../ra_hir", package = "ra_hir" } hir = { path = "../ra_hir", package = "ra_hir" }
test_utils = { path = "../test_utils" } test_utils = { path = "../test_utils" }

View File

@ -1,10 +1,11 @@
mod reference_completion; mod reference_completion;
use ra_editor::find_node_at_offset; use ra_editor::find_node_at_offset;
use ra_text_edit::AtomEdit;
use ra_syntax::{ use ra_syntax::{
algo::visit::{visitor_ctx, VisitorCtx}, algo::visit::{visitor_ctx, VisitorCtx},
ast, ast,
AstNode, AtomEdit, AstNode,
SyntaxNodeRef, SyntaxNodeRef,
}; };
use ra_db::SyntaxDatabase; use ra_db::SyntaxDatabase;

View File

@ -18,7 +18,8 @@ macro_rules! ctry {
use std::{fmt, sync::Arc}; use std::{fmt, sync::Arc};
use ra_syntax::{AtomEdit, SourceFileNode, TextRange, TextUnit}; use ra_syntax::{SourceFileNode, TextRange, TextUnit};
use ra_text_edit::AtomEdit;
use ra_db::FileResolverImp; use ra_db::FileResolverImp;
use rayon::prelude::*; use rayon::prelude::*;
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;

View File

@ -12,6 +12,7 @@ join_to_string = "0.1.1"
rustc-hash = "1.0" rustc-hash = "1.0"
ra_syntax = { path = "../ra_syntax" } ra_syntax = { path = "../ra_syntax" }
ra_text_edit = { path = "../ra_text_edit" }
[dev-dependencies] [dev-dependencies]
test_utils = { path = "../test_utils" } test_utils = { path = "../test_utils" }

View File

@ -1,5 +1,4 @@
mod code_actions; mod code_actions;
mod edit;
mod extend_selection; mod extend_selection;
mod folding_ranges; mod folding_ranges;
mod line_index; mod line_index;
@ -10,14 +9,13 @@
pub use self::{ pub use self::{
code_actions::{add_derive, add_impl, flip_comma, introduce_variable, LocalEdit}, code_actions::{add_derive, add_impl, flip_comma, introduce_variable, LocalEdit},
edit::{Edit, EditBuilder},
extend_selection::extend_selection, extend_selection::extend_selection,
folding_ranges::{folding_ranges, Fold, FoldKind}, folding_ranges::{folding_ranges, Fold, FoldKind},
line_index::{LineCol, LineIndex}, line_index::{LineCol, LineIndex},
symbols::{file_structure, file_symbols, FileSymbol, StructureNode}, symbols::{file_structure, file_symbols, FileSymbol, StructureNode},
typing::{join_lines, on_enter, on_eq_typed}, typing::{join_lines, on_enter, on_eq_typed},
}; };
pub use ra_syntax::AtomEdit; use ra_text_edit::{Edit, EditBuilder};
use ra_syntax::{ use ra_syntax::{
algo::find_leaf_at_offset, algo::find_leaf_at_offset,
ast::{self, AstNode, NameOwner}, ast::{self, AstNode, NameOwner},

View File

@ -3,11 +3,12 @@
use ra_syntax::{ use ra_syntax::{
algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset}, algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset},
ast, ast,
text_utils::{contains_offset_nonstrict, intersect}, text_utils::intersect,
AstNode, SourceFileNode, SyntaxKind, AstNode, SourceFileNode, SyntaxKind,
SyntaxKind::*, SyntaxKind::*,
SyntaxNodeRef, TextRange, TextUnit, SyntaxNodeRef, TextRange, TextUnit,
}; };
use ra_text_edit::text_utils::contains_offset_nonstrict;
use crate::{find_node_at_offset, EditBuilder, LocalEdit}; use crate::{find_node_at_offset, EditBuilder, LocalEdit};

View File

@ -28,6 +28,7 @@ rustc-hash = "1.0"
ra_syntax = { path = "../ra_syntax" } ra_syntax = { path = "../ra_syntax" }
ra_editor = { path = "../ra_editor" } ra_editor = { path = "../ra_editor" }
ra_text_edit = { path = "../ra_text_edit" }
ra_analysis = { path = "../ra_analysis" } ra_analysis = { path = "../ra_analysis" }
gen_lsp_server = { path = "../gen_lsp_server" } gen_lsp_server = { path = "../gen_lsp_server" }

View File

@ -3,7 +3,8 @@
TextDocumentItem, TextDocumentPositionParams, TextEdit, Url, VersionedTextDocumentIdentifier, TextDocumentItem, TextDocumentPositionParams, TextEdit, Url, VersionedTextDocumentIdentifier,
}; };
use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileNodeEdit, FilePosition}; use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileNodeEdit, FilePosition};
use ra_editor::{AtomEdit, Edit, LineCol, LineIndex}; use ra_editor::{LineCol, LineIndex};
use ra_text_edit::{AtomEdit, Edit};
use ra_syntax::{SyntaxKind, TextRange, TextUnit}; use ra_syntax::{SyntaxKind, TextRange, TextUnit};
use crate::{req, server_world::ServerWorld, Result}; use crate::{req, server_world::ServerWorld, Result};

View File

@ -9,7 +9,8 @@
WorkspaceEdit, ParameterInformation, SignatureInformation, Hover, HoverContents, WorkspaceEdit, ParameterInformation, SignatureInformation, Hover, HoverContents,
}; };
use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FilePosition}; use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FilePosition};
use ra_syntax::{TextUnit, text_utils::{contains_offset_nonstrict, intersect}}; use ra_syntax::{TextUnit, text_utils::intersect};
use ra_text_edit::text_utils::contains_offset_nonstrict;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use serde_json::to_value; use serde_json::to_value;

View File

@ -15,6 +15,7 @@ drop_bomb = "0.1.4"
parking_lot = "0.6.0" parking_lot = "0.6.0"
rowan = "0.1.2" rowan = "0.1.2"
text_unit = "0.1.5" text_unit = "0.1.5"
ra_text_edit = { path = "../ra_text_edit" }
[dev-dependencies] [dev-dependencies]
test_utils = { path = "../test_utils" } test_utils = { path = "../test_utils" }

View File

@ -41,13 +41,13 @@
pub use crate::{ pub use crate::{
ast::AstNode, ast::AstNode,
lexer::{tokenize, Token}, lexer::{tokenize, Token},
reparsing::AtomEdit,
syntax_kinds::SyntaxKind, syntax_kinds::SyntaxKind,
yellow::{ yellow::{
Direction, OwnedRoot, RefRoot, SyntaxError, SyntaxNode, SyntaxNodeRef, TreeRoot, WalkEvent, Location, Direction, OwnedRoot, RefRoot, SyntaxError, SyntaxNode, SyntaxNodeRef, TreeRoot, WalkEvent, Location,
}, },
}; };
use ra_text_edit::AtomEdit;
use crate::yellow::GreenNode; use crate::yellow::GreenNode;
/// `SourceFileNode` represents a parse tree for a single Rust file. /// `SourceFileNode` represents a parse tree for a single Rust file.

View File

@ -6,29 +6,7 @@
use crate::text_utils::replace_range; use crate::text_utils::replace_range;
use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNodeRef}; use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNodeRef};
use crate::{SyntaxKind::*, TextRange, TextUnit}; use crate::{SyntaxKind::*, TextRange, TextUnit};
use ra_text_edit::AtomEdit;
#[derive(Debug, Clone)]
pub struct AtomEdit {
pub delete: TextRange,
pub insert: String,
}
impl AtomEdit {
pub fn replace(range: TextRange, replace_with: String) -> AtomEdit {
AtomEdit {
delete: range,
insert: replace_with,
}
}
pub fn delete(range: TextRange) -> AtomEdit {
AtomEdit::replace(range, String::new())
}
pub fn insert(offset: TextUnit, text: String) -> AtomEdit {
AtomEdit::replace(TextRange::offset_len(offset, 0.into()), text)
}
}
pub(crate) fn incremental_reparse( pub(crate) fn incremental_reparse(
node: SyntaxNodeRef, node: SyntaxNodeRef,

View File

@ -1,8 +1,4 @@
use crate::{TextRange, TextUnit}; use crate::TextRange;
pub fn contains_offset_nonstrict(range: TextRange, offset: TextUnit) -> bool {
range.start() <= offset && offset <= range.end()
}
pub fn intersect(r1: TextRange, r2: TextRange) -> Option<TextRange> { pub fn intersect(r1: TextRange, r2: TextRange) -> Option<TextRange> {
let start = r1.start().max(r2.start()); let start = r1.start().max(r2.start());

View File

@ -1,7 +1,8 @@
use std::{fmt, ops}; use std::{fmt, ops};
use ra_text_edit::text_utils::contains_offset_nonstrict;
use crate::{ use crate::{
text_utils::{contains_offset_nonstrict, intersect}, text_utils::intersect,
SyntaxNodeRef, TextRange, TextUnit, SyntaxNodeRef, TextRange, TextUnit,
}; };

View File

@ -0,0 +1,12 @@
[package]
edition = "2018"
name = "ra_text_edit"
version = "0.1.0"
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
publish = false
[dependencies]
text_unit = "0.1.5"
[dev-dependencies]
test_utils = { path = "../test_utils" }

View File

@ -1,5 +1,6 @@
use crate::{TextRange, TextUnit}; use crate::AtomEdit;
use ra_syntax::{text_utils::contains_offset_nonstrict, AtomEdit}; use crate::text_utils::contains_offset_nonstrict;
use text_unit::{TextRange, TextUnit};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Edit { pub struct Edit {

View File

@ -0,0 +1,29 @@
mod edit;
pub mod text_utils;
pub use crate::edit::{Edit, EditBuilder};
use text_unit::{TextRange, TextUnit};
#[derive(Debug, Clone)]
pub struct AtomEdit {
pub delete: TextRange,
pub insert: String,
}
impl AtomEdit {
pub fn replace(range: TextRange, replace_with: String) -> AtomEdit {
AtomEdit {
delete: range,
insert: replace_with,
}
}
pub fn delete(range: TextRange) -> AtomEdit {
AtomEdit::replace(range, String::new())
}
pub fn insert(offset: TextUnit, text: String) -> AtomEdit {
AtomEdit::replace(TextRange::offset_len(offset, 0.into()), text)
}
}

View File

@ -0,0 +1,5 @@
use text_unit::{TextRange, TextUnit};
pub fn contains_offset_nonstrict(range: TextRange, offset: TextUnit) -> bool {
range.start() <= offset && offset <= range.end()
}