aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-13 12:24:22 +0100
committerAleksey Kladov <[email protected]>2018-08-13 12:24:22 +0100
commited2ac1713326df6b926062efcc6109a20cdf7c37 (patch)
treec1c75d09e2adbbd54190c5b6ce6efdb7ea251da3
parent18486a02fae5966e61f16ea7bc5c33c6c7c69487 (diff)
smol_str to a crate
-rw-r--r--crates/libeditor/Cargo.toml1
-rw-r--r--crates/libeditor/src/lib.rs1
-rw-r--r--crates/libeditor/src/symbols.rs3
-rw-r--r--crates/libsyntax2/Cargo.toml1
-rw-r--r--crates/libsyntax2/src/ast/mod.rs9
-rw-r--r--crates/libsyntax2/src/lib.rs4
-rw-r--r--crates/libsyntax2/src/yellow/green.rs17
-rw-r--r--crates/libsyntax2/src/yellow/syntax.rs6
-rw-r--r--crates/server/src/main_loop/handlers.rs5
-rw-r--r--crates/smol_str/Cargo.toml6
-rw-r--r--crates/smol_str/src/lib.rs (renamed from crates/libsyntax2/src/smol_str.rs)71
11 files changed, 101 insertions, 23 deletions
diff --git a/crates/libeditor/Cargo.toml b/crates/libeditor/Cargo.toml
index fe688bc20..1d210f3c1 100644
--- a/crates/libeditor/Cargo.toml
+++ b/crates/libeditor/Cargo.toml
@@ -9,4 +9,5 @@ itertools = "0.7.8"
9superslice = "0.1.0" 9superslice = "0.1.0"
10 10
11libsyntax2 = { path = "../libsyntax2" } 11libsyntax2 = { path = "../libsyntax2" }
12smol_str = { path = "../smol_str" }
12assert_eq_text = { path = "../assert_eq_text" } 13assert_eq_text = { path = "../assert_eq_text" }
diff --git a/crates/libeditor/src/lib.rs b/crates/libeditor/src/lib.rs
index 1e88f1471..e5933cbd6 100644
--- a/crates/libeditor/src/lib.rs
+++ b/crates/libeditor/src/lib.rs
@@ -1,6 +1,7 @@
1extern crate libsyntax2; 1extern crate libsyntax2;
2extern crate superslice; 2extern crate superslice;
3extern crate itertools; 3extern crate itertools;
4extern crate smol_str;
4 5
5mod extend_selection; 6mod extend_selection;
6mod symbols; 7mod symbols;
diff --git a/crates/libeditor/src/symbols.rs b/crates/libeditor/src/symbols.rs
index 5cd781c80..03f2313f7 100644
--- a/crates/libeditor/src/symbols.rs
+++ b/crates/libeditor/src/symbols.rs
@@ -1,3 +1,4 @@
1use smol_str::SmolStr;
1use libsyntax2::{ 2use libsyntax2::{
2 SyntaxKind, SyntaxNodeRef, SyntaxRoot, AstNode, 3 SyntaxKind, SyntaxNodeRef, SyntaxRoot, AstNode,
3 ast::{self, NameOwner}, 4 ast::{self, NameOwner},
@@ -11,7 +12,7 @@ use TextRange;
11#[derive(Debug)] 12#[derive(Debug)]
12pub struct FileSymbol { 13pub struct FileSymbol {
13 pub parent: Option<usize>, 14 pub parent: Option<usize>,
14 pub name: String, 15 pub name: SmolStr,
15 pub name_range: TextRange, 16 pub name_range: TextRange,
16 pub node_range: TextRange, 17 pub node_range: TextRange,
17 pub kind: SyntaxKind, 18 pub kind: SyntaxKind,
diff --git a/crates/libsyntax2/Cargo.toml b/crates/libsyntax2/Cargo.toml
index 4c4040fe5..810952a0f 100644
--- a/crates/libsyntax2/Cargo.toml
+++ b/crates/libsyntax2/Cargo.toml
@@ -10,6 +10,7 @@ text_unit = "0.1.2"
10itertools = "0.7.5" 10itertools = "0.7.5"
11drop_bomb = "0.1.4" 11drop_bomb = "0.1.4"
12parking_lot = "0.6.0" 12parking_lot = "0.6.0"
13smol_str = { path = "../smol_str" }
13 14
14[dev-dependencies] 15[dev-dependencies]
15assert_eq_text = { path = "../assert_eq_text" } 16assert_eq_text = { path = "../assert_eq_text" }
diff --git a/crates/libsyntax2/src/ast/mod.rs b/crates/libsyntax2/src/ast/mod.rs
index 56bc099fe..e9362d048 100644
--- a/crates/libsyntax2/src/ast/mod.rs
+++ b/crates/libsyntax2/src/ast/mod.rs
@@ -1,6 +1,9 @@
1mod generated; 1mod generated;
2 2
3use std::sync::Arc; 3use std::sync::Arc;
4
5use smol_str::SmolStr;
6
4use { 7use {
5 SyntaxNode, SyntaxRoot, TreeRoot, SyntaxError, 8 SyntaxNode, SyntaxRoot, TreeRoot, SyntaxError,
6 SyntaxKind::*, 9 SyntaxKind::*,
@@ -64,7 +67,9 @@ impl<R: TreeRoot> Function<R> {
64} 67}
65 68
66impl<R: TreeRoot> Name<R> { 69impl<R: TreeRoot> Name<R> {
67 pub fn text(&self) -> String { 70 pub fn text(&self) -> SmolStr {
68 self.syntax().text() 71 let ident = self.syntax().first_child()
72 .unwrap();
73 ident.leaf_text().unwrap()
69 } 74 }
70} 75}
diff --git a/crates/libsyntax2/src/lib.rs b/crates/libsyntax2/src/lib.rs
index ca33618a0..feef542c4 100644
--- a/crates/libsyntax2/src/lib.rs
+++ b/crates/libsyntax2/src/lib.rs
@@ -21,10 +21,11 @@
21//#![warn(unreachable_pub)] // rust-lang/rust#47816 21//#![warn(unreachable_pub)] // rust-lang/rust#47816
22 22
23extern crate itertools; 23extern crate itertools;
24extern crate text_unit;
25extern crate unicode_xid; 24extern crate unicode_xid;
26extern crate drop_bomb; 25extern crate drop_bomb;
27extern crate parking_lot; 26extern crate parking_lot;
27extern crate smol_str;
28extern crate text_unit;
28 29
29pub mod algo; 30pub mod algo;
30pub mod ast; 31pub mod ast;
@@ -35,7 +36,6 @@ mod grammar;
35mod parser_impl; 36mod parser_impl;
36 37
37mod syntax_kinds; 38mod syntax_kinds;
38mod smol_str;
39mod yellow; 39mod yellow;
40/// Utilities for simple uses of the parser. 40/// Utilities for simple uses of the parser.
41pub mod utils; 41pub mod utils;
diff --git a/crates/libsyntax2/src/yellow/green.rs b/crates/libsyntax2/src/yellow/green.rs
index f505b26d7..700f2704f 100644
--- a/crates/libsyntax2/src/yellow/green.rs
+++ b/crates/libsyntax2/src/yellow/green.rs
@@ -1,8 +1,8 @@
1use std::sync::Arc; 1use std::sync::Arc;
2use { 2
3 SyntaxKind, TextUnit, 3use smol_str::SmolStr;
4 smol_str::SmolStr, 4
5}; 5use {SyntaxKind, TextUnit};
6 6
7#[derive(Clone, Debug)] 7#[derive(Clone, Debug)]
8pub(crate) enum GreenNode { 8pub(crate) enum GreenNode {
@@ -31,7 +31,7 @@ impl GreenNode {
31 31
32 pub fn text_len(&self) -> TextUnit { 32 pub fn text_len(&self) -> TextUnit {
33 match self { 33 match self {
34 GreenNode::Leaf { text, ..} => TextUnit::of_str(text.as_str()), 34 GreenNode::Leaf { text, .. } => TextUnit::of_str(text.as_str()),
35 GreenNode::Branch(b) => b.text_len(), 35 GreenNode::Branch(b) => b.text_len(),
36 } 36 }
37 } 37 }
@@ -54,6 +54,13 @@ impl GreenNode {
54 } 54 }
55 } 55 }
56 } 56 }
57
58 pub fn leaf_text(&self) -> Option<SmolStr> {
59 match self {
60 GreenNode::Leaf { text, .. } => Some(text.clone()),
61 GreenNode::Branch(_) => None,
62 }
63 }
57} 64}
58 65
59#[derive(Clone, Debug)] 66#[derive(Clone, Debug)]
diff --git a/crates/libsyntax2/src/yellow/syntax.rs b/crates/libsyntax2/src/yellow/syntax.rs
index 00f76e51c..b264e008a 100644
--- a/crates/libsyntax2/src/yellow/syntax.rs
+++ b/crates/libsyntax2/src/yellow/syntax.rs
@@ -1,5 +1,7 @@
1use std::{fmt, sync::Arc}; 1use std::{fmt, sync::Arc};
2 2
3use smol_str::SmolStr;
4
3use { 5use {
4 yellow::{RedNode, TreeRoot, SyntaxRoot, RedPtr}, 6 yellow::{RedNode, TreeRoot, SyntaxRoot, RedPtr},
5 SyntaxKind::{self, *}, 7 SyntaxKind::{self, *},
@@ -116,6 +118,10 @@ impl<R: TreeRoot> SyntaxNode<R> {
116 self.first_child().is_none() 118 self.first_child().is_none()
117 } 119 }
118 120
121 pub fn leaf_text(&self) -> Option<SmolStr> {
122 self.red().green().leaf_text()
123 }
124
119 fn red(&self) -> &RedNode { 125 fn red(&self) -> &RedNode {
120 unsafe { self.red.get(&self.root) } 126 unsafe { self.red.get(&self.root) }
121 } 127 }
diff --git a/crates/server/src/main_loop/handlers.rs b/crates/server/src/main_loop/handlers.rs
index d4ae2a368..14dcafc38 100644
--- a/crates/server/src/main_loop/handlers.rs
+++ b/crates/server/src/main_loop/handlers.rs
@@ -50,9 +50,10 @@ pub fn handle_document_symbol(
50 let mut res: Vec<DocumentSymbol> = Vec::new(); 50 let mut res: Vec<DocumentSymbol> = Vec::new();
51 51
52 for symbol in libeditor::file_symbols(&file) { 52 for symbol in libeditor::file_symbols(&file) {
53 let name = symbol.name.to_string();
53 let doc_symbol = DocumentSymbol { 54 let doc_symbol = DocumentSymbol {
54 name: symbol.name.clone(), 55 name: name.clone(),
55 detail: Some(symbol.name), 56 detail: Some(name),
56 kind: symbol.kind.conv(), 57 kind: symbol.kind.conv(),
57 deprecated: None, 58 deprecated: None,
58 range: symbol.node_range.conv_with(&line_index), 59 range: symbol.node_range.conv_with(&line_index),
diff --git a/crates/smol_str/Cargo.toml b/crates/smol_str/Cargo.toml
new file mode 100644
index 000000000..83ca12f62
--- /dev/null
+++ b/crates/smol_str/Cargo.toml
@@ -0,0 +1,6 @@
1[package]
2name = "smol_str"
3version = "0.1.0"
4authors = ["Aleksey Kladov <[email protected]>"]
5
6[dependencies]
diff --git a/crates/libsyntax2/src/smol_str.rs b/crates/smol_str/src/lib.rs
index abf69dce7..4d5fef593 100644
--- a/crates/libsyntax2/src/smol_str.rs
+++ b/crates/smol_str/src/lib.rs
@@ -1,10 +1,59 @@
1use std::{sync::Arc}; 1use std::{sync::Arc, ops::Deref};
2
3#[derive(Clone, Debug)]
4pub struct SmolStr(Repr);
5
6impl SmolStr {
7 pub fn new(text: &str) -> SmolStr {
8 SmolStr(Repr::new(text))
9 }
10
11 pub fn as_str(&self) -> &str {
12 self.0.as_str()
13 }
14
15 pub fn to_string(&self) -> String {
16 self.as_str().to_string()
17 }
18}
19
20impl Deref for SmolStr {
21 type Target = str;
22
23 fn deref(&self) -> &str {
24 self.as_str()
25 }
26}
27
28impl PartialEq<str> for SmolStr {
29 fn eq(&self, other: &str) -> bool {
30 self.as_str() == other
31 }
32}
33
34impl PartialEq<SmolStr> for str {
35 fn eq(&self, other: &SmolStr) -> bool {
36 other == self
37 }
38}
39
40impl<'a> PartialEq<&'a str> for SmolStr {
41 fn eq(&self, other: &&'a str) -> bool {
42 self == *other
43 }
44}
45
46impl<'a> PartialEq<SmolStr> for &'a str {
47 fn eq(&self, other: &SmolStr) -> bool {
48 *self == other
49 }
50}
2 51
3const INLINE_CAP: usize = 22; 52const INLINE_CAP: usize = 22;
4const WS_TAG: u8 = (INLINE_CAP + 1) as u8; 53const WS_TAG: u8 = (INLINE_CAP + 1) as u8;
5 54
6#[derive(Clone, Debug)] 55#[derive(Clone, Debug)]
7pub(crate) enum SmolStr { 56enum Repr {
8 Heap(Arc<str>), 57 Heap(Arc<str>),
9 Inline { 58 Inline {
10 len: u8, 59 len: u8,
@@ -12,13 +61,13 @@ pub(crate) enum SmolStr {
12 }, 61 },
13} 62}
14 63
15impl SmolStr { 64impl Repr {
16 pub fn new(text: &str) -> SmolStr { 65 fn new(text: &str) -> Repr {
17 let len = text.len(); 66 let len = text.len();
18 if len <= INLINE_CAP { 67 if len <= INLINE_CAP {
19 let mut buf = [0; INLINE_CAP]; 68 let mut buf = [0; INLINE_CAP];
20 buf[..len].copy_from_slice(text.as_bytes()); 69 buf[..len].copy_from_slice(text.as_bytes());
21 return SmolStr::Inline { len: len as u8, buf }; 70 return Repr::Inline { len: len as u8, buf };
22 } 71 }
23 72
24 let newlines = text.bytes().take_while(|&b| b == b'\n').count(); 73 let newlines = text.bytes().take_while(|&b| b == b'\n').count();
@@ -27,23 +76,23 @@ impl SmolStr {
27 let mut buf = [0; INLINE_CAP]; 76 let mut buf = [0; INLINE_CAP];
28 buf[0] = newlines as u8; 77 buf[0] = newlines as u8;
29 buf[1] = spaces as u8; 78 buf[1] = spaces as u8;
30 return SmolStr::Inline { len: WS_TAG, buf }; 79 return Repr::Inline { len: WS_TAG, buf };
31 } 80 }
32 81
33 SmolStr::Heap( 82 Repr::Heap(
34 text.to_string().into_boxed_str().into() 83 text.to_string().into_boxed_str().into()
35 ) 84 )
36 } 85 }
37 86
38 pub fn as_str(&self) -> &str { 87 fn as_str(&self) -> &str {
39 match self { 88 match self {
40 SmolStr::Heap(data) => &*data, 89 Repr::Heap(data) => &*data,
41 SmolStr::Inline { len, buf } => { 90 Repr::Inline { len, buf } => {
42 if *len == WS_TAG { 91 if *len == WS_TAG {
43 let newlines = buf[0] as usize; 92 let newlines = buf[0] as usize;
44 let spaces = buf[1] as usize; 93 let spaces = buf[1] as usize;
45 assert!(newlines <= N_NEWLINES && spaces <= N_SPACES); 94 assert!(newlines <= N_NEWLINES && spaces <= N_SPACES);
46 return &WS[N_NEWLINES - newlines..N_NEWLINES + spaces] 95 return &WS[N_NEWLINES - newlines..N_NEWLINES + spaces];
47 } 96 }
48 97
49 let len = *len as usize; 98 let len = *len as usize;