upstream text-utils to text_unit

This commit is contained in:
Aleksey Kladov 2019-01-08 21:50:04 +03:00
parent c9e42fcf24
commit f553837c1c
10 changed files with 20 additions and 44 deletions

14
Cargo.lock generated
View file

@ -745,7 +745,7 @@ dependencies = [
"smol_str 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "smol_str 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
"tempfile 3.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.0.5 (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.6 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_worker 0.1.0", "thread_worker 0.1.0",
"threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"tools 0.1.0", "tools 0.1.0",
@ -764,7 +764,7 @@ dependencies = [
"ra_text_edit 0.1.0", "ra_text_edit 0.1.0",
"rowan 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "rowan 0.2.0 (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.6 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -775,7 +775,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"proptest 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)", "proptest 0.8.7 (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.6 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@ -972,7 +972,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"smol_str 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "smol_str 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
"text_unit 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "text_unit 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@ -1218,12 +1218,12 @@ dependencies = [
"difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.34 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.34 (registry+https://github.com/rust-lang/crates.io-index)",
"text_unit 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "text_unit 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "text_unit" name = "text_unit"
version = "0.1.5" version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1597,7 +1597,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum tera 0.11.20 (registry+https://github.com/rust-lang/crates.io-index)" = "4b505279e19d8f7d24b1a9dc58327c9c36174b1a2c7ebdeac70792d017cb64f3" "checksum tera 0.11.20 (registry+https://github.com/rust-lang/crates.io-index)" = "4b505279e19d8f7d24b1a9dc58327c9c36174b1a2c7ebdeac70792d017cb64f3"
"checksum teraron 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0d89ad4617d1dec55331067fadaa041e813479e1779616f3d3ce9308bf46184e" "checksum teraron 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0d89ad4617d1dec55331067fadaa041e813479e1779616f3d3ce9308bf46184e"
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
"checksum text_unit 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8009d7bdbd896a7e09b595f8f9325a19047fc708653e60d0895202b82135048f" "checksum text_unit 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "158bb1c22b638b1da3c95a8ad9f061ea40d4d39fd0301be3a520f92efeeb189e"
"checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" "checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
"checksum threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e2f0c90a5f3459330ac8bc0d2f879c693bb7a2f59689c1083fc4ef83834da865" "checksum threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e2f0c90a5f3459330ac8bc0d2f879c693bb7a2f59689c1083fc4ef83834da865"

View file

@ -4,12 +4,10 @@
use ra_syntax::{ use ra_syntax::{
algo::{find_node_at_offset, find_covering_node, find_leaf_at_offset, LeafAtOffset}, algo::{find_node_at_offset, find_covering_node, find_leaf_at_offset, LeafAtOffset},
ast, ast,
text_utils::intersect,
AstNode, Direction, SourceFile, SyntaxKind, AstNode, Direction, SourceFile, SyntaxKind,
SyntaxKind::*, SyntaxKind::*,
SyntaxNode, TextRange, TextUnit, SyntaxNode, TextRange, TextUnit,
}; };
use ra_text_edit::text_utils::contains_offset_nonstrict;
use crate::{LocalEdit, TextEditBuilder}; use crate::{LocalEdit, TextEditBuilder};
@ -39,7 +37,7 @@ pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit {
Some(text) => text, Some(text) => text,
None => continue, None => continue,
}; };
let range = match intersect(range, node.range()) { let range = match range.intersection(&node.range()) {
Some(range) => range, Some(range) => range,
None => continue, None => continue,
} - node.range().start(); } - node.range().start();
@ -112,7 +110,7 @@ pub fn on_eq_typed(file: &SourceFile, offset: TextUnit) -> Option<LocalEdit> {
} }
if let Some(expr) = let_stmt.initializer() { if let Some(expr) = let_stmt.initializer() {
let expr_range = expr.syntax().range(); let expr_range = expr.syntax().range();
if contains_offset_nonstrict(expr_range, offset) && offset != expr_range.start() { if expr_range.contains(offset) && offset != expr_range.start() {
return None; return None;
} }
if file if file

View file

@ -11,8 +11,7 @@
use ra_analysis::{ use ra_analysis::{
FileId, FilePosition, FileRange, FoldKind, Query, RunnableKind, Severity, SourceChange, FileId, FilePosition, FileRange, FoldKind, Query, RunnableKind, Severity, SourceChange,
}; };
use ra_syntax::{text_utils::intersect, TextUnit, AstNode}; use ra_syntax::{TextUnit, AstNode};
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;
use std::io::Write; use std::io::Write;
@ -248,7 +247,7 @@ pub fn handle_runnables(
let mut res = Vec::new(); let mut res = Vec::new();
for runnable in world.analysis().runnables(file_id)? { for runnable in world.analysis().runnables(file_id)? {
if let Some(offset) = offset { if let Some(offset) = offset {
if !contains_offset_nonstrict(runnable.range, offset) { if !runnable.range.contains_inclusive(offset) {
continue; continue;
} }
} }
@ -650,7 +649,7 @@ pub fn handle_code_action(
.diagnostics(file_id)? .diagnostics(file_id)?
.into_iter() .into_iter()
.filter_map(|d| Some((d.range, d.fix?))) .filter_map(|d| Some((d.range, d.fix?)))
.filter(|(diag_range, _fix)| intersect(*diag_range, range).is_some()) .filter(|(diag_range, _fix)| diag_range.intersection(&range).is_some())
.map(|(_range, fix)| fix); .map(|(_range, fix)| fix);
let mut res = Vec::new(); let mut res = Vec::new();

View file

@ -14,7 +14,7 @@ itertools = "0.8.0"
drop_bomb = "0.1.4" drop_bomb = "0.1.4"
parking_lot = "0.7.0" parking_lot = "0.7.0"
rowan = "0.2.0" rowan = "0.2.0"
text_unit = "0.1.5" text_unit = "0.1.6"
ra_text_edit = { path = "../ra_text_edit" } ra_text_edit = { path = "../ra_text_edit" }
[dev-dependencies] [dev-dependencies]

View file

@ -1,15 +1,5 @@
use crate::TextRange; use crate::TextRange;
pub fn intersect(r1: TextRange, r2: TextRange) -> Option<TextRange> {
let start = r1.start().max(r2.start());
let end = r1.end().min(r2.end());
if start <= end {
Some(TextRange::from_to(start, end))
} else {
None
}
}
pub fn replace_range(mut text: String, range: TextRange, replace_with: &str) -> String { pub fn replace_range(mut text: String, range: TextRange, replace_with: &str) -> String {
let start = u32::from(range.start()) as usize; let start = u32::from(range.start()) as usize;
let end = u32::from(range.end()) as usize; let end = u32::from(range.end()) as usize;

View file

@ -1,10 +1,6 @@
use std::{fmt, ops}; use std::{fmt, ops};
use ra_text_edit::text_utils::contains_offset_nonstrict; use crate::{SyntaxNode, TextRange, TextUnit};
use crate::{
text_utils::intersect,
SyntaxNode, TextRange, TextUnit,
};
#[derive(Clone)] #[derive(Clone)]
pub struct SyntaxText<'a> { pub struct SyntaxText<'a> {
@ -23,7 +19,7 @@ pub fn chunks(&self) -> impl Iterator<Item = &'a str> {
let range = self.range; let range = self.range;
self.node.descendants().filter_map(move |node| { self.node.descendants().filter_map(move |node| {
let text = node.leaf_text()?; let text = node.leaf_text()?;
let range = intersect(range, node.range())?; let range = range.intersection(&node.range())?;
let range = range - node.range().start(); let range = range - node.range().start();
Some(&text[range]) Some(&text[range])
}) })
@ -92,13 +88,13 @@ pub trait SyntaxTextSlice: fmt::Debug {
impl SyntaxTextSlice for TextRange { impl SyntaxTextSlice for TextRange {
fn restrict(&self, range: TextRange) -> Option<TextRange> { fn restrict(&self, range: TextRange) -> Option<TextRange> {
intersect(*self, range) self.intersection(&range)
} }
} }
impl SyntaxTextSlice for ops::RangeTo<TextUnit> { impl SyntaxTextSlice for ops::RangeTo<TextUnit> {
fn restrict(&self, range: TextRange) -> Option<TextRange> { fn restrict(&self, range: TextRange) -> Option<TextRange> {
if !contains_offset_nonstrict(range, self.end) { if !range.contains_inclusive(self.end) {
return None; return None;
} }
Some(TextRange::from_to(range.start(), self.end)) Some(TextRange::from_to(range.start(), self.end))
@ -107,7 +103,7 @@ fn restrict(&self, range: TextRange) -> Option<TextRange> {
impl SyntaxTextSlice for ops::RangeFrom<TextUnit> { impl SyntaxTextSlice for ops::RangeFrom<TextUnit> {
fn restrict(&self, range: TextRange) -> Option<TextRange> { fn restrict(&self, range: TextRange) -> Option<TextRange> {
if !contains_offset_nonstrict(range, self.start) { if !range.contains_inclusive(self.start) {
return None; return None;
} }
Some(TextRange::from_to(self.start, range.end())) Some(TextRange::from_to(self.start, range.end()))

View file

@ -6,7 +6,7 @@ authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
publish = false publish = false
[dependencies] [dependencies]
text_unit = "0.1.5" text_unit = "0.1.6"
proptest = "0.8.7" proptest = "0.8.7"
[dev-dependencies] [dev-dependencies]

View file

@ -1,5 +1,4 @@
mod text_edit; mod text_edit;
pub mod text_utils;
pub mod test_utils; pub mod test_utils;
pub use crate::text_edit::{TextEdit, TextEditBuilder}; pub use crate::text_edit::{TextEdit, TextEditBuilder};

View file

@ -1,5 +1,4 @@
use crate::AtomTextEdit; use crate::AtomTextEdit;
use crate::text_utils::contains_offset_nonstrict;
use text_unit::{TextRange, TextUnit}; use text_unit::{TextRange, TextUnit};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -28,7 +27,7 @@ pub fn finish(self) -> TextEdit {
pub fn invalidates_offset(&self, offset: TextUnit) -> bool { pub fn invalidates_offset(&self, offset: TextUnit) -> bool {
self.atoms self.atoms
.iter() .iter()
.any(|atom| contains_offset_nonstrict(atom.delete, offset)) .any(|atom| atom.delete.contains_inclusive(offset))
} }
} }

View file

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