diff options
-rw-r--r-- | crates/libeditor/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/libeditor/src/lib.rs | 1 | ||||
-rw-r--r-- | crates/libeditor/src/symbols.rs | 3 | ||||
-rw-r--r-- | crates/libsyntax2/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/libsyntax2/src/ast/mod.rs | 9 | ||||
-rw-r--r-- | crates/libsyntax2/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/libsyntax2/src/yellow/green.rs | 17 | ||||
-rw-r--r-- | crates/libsyntax2/src/yellow/syntax.rs | 6 | ||||
-rw-r--r-- | crates/server/src/main_loop/handlers.rs | 5 | ||||
-rw-r--r-- | crates/smol_str/Cargo.toml | 6 | ||||
-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" | |||
9 | superslice = "0.1.0" | 9 | superslice = "0.1.0" |
10 | 10 | ||
11 | libsyntax2 = { path = "../libsyntax2" } | 11 | libsyntax2 = { path = "../libsyntax2" } |
12 | smol_str = { path = "../smol_str" } | ||
12 | assert_eq_text = { path = "../assert_eq_text" } | 13 | assert_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 @@ | |||
1 | extern crate libsyntax2; | 1 | extern crate libsyntax2; |
2 | extern crate superslice; | 2 | extern crate superslice; |
3 | extern crate itertools; | 3 | extern crate itertools; |
4 | extern crate smol_str; | ||
4 | 5 | ||
5 | mod extend_selection; | 6 | mod extend_selection; |
6 | mod symbols; | 7 | mod 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 @@ | |||
1 | use smol_str::SmolStr; | ||
1 | use libsyntax2::{ | 2 | use 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)] |
12 | pub struct FileSymbol { | 13 | pub 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" | |||
10 | itertools = "0.7.5" | 10 | itertools = "0.7.5" |
11 | drop_bomb = "0.1.4" | 11 | drop_bomb = "0.1.4" |
12 | parking_lot = "0.6.0" | 12 | parking_lot = "0.6.0" |
13 | smol_str = { path = "../smol_str" } | ||
13 | 14 | ||
14 | [dev-dependencies] | 15 | [dev-dependencies] |
15 | assert_eq_text = { path = "../assert_eq_text" } | 16 | assert_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 @@ | |||
1 | mod generated; | 1 | mod generated; |
2 | 2 | ||
3 | use std::sync::Arc; | 3 | use std::sync::Arc; |
4 | |||
5 | use smol_str::SmolStr; | ||
6 | |||
4 | use { | 7 | use { |
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 | ||
66 | impl<R: TreeRoot> Name<R> { | 69 | impl<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 | ||
23 | extern crate itertools; | 23 | extern crate itertools; |
24 | extern crate text_unit; | ||
25 | extern crate unicode_xid; | 24 | extern crate unicode_xid; |
26 | extern crate drop_bomb; | 25 | extern crate drop_bomb; |
27 | extern crate parking_lot; | 26 | extern crate parking_lot; |
27 | extern crate smol_str; | ||
28 | extern crate text_unit; | ||
28 | 29 | ||
29 | pub mod algo; | 30 | pub mod algo; |
30 | pub mod ast; | 31 | pub mod ast; |
@@ -35,7 +36,6 @@ mod grammar; | |||
35 | mod parser_impl; | 36 | mod parser_impl; |
36 | 37 | ||
37 | mod syntax_kinds; | 38 | mod syntax_kinds; |
38 | mod smol_str; | ||
39 | mod yellow; | 39 | mod yellow; |
40 | /// Utilities for simple uses of the parser. | 40 | /// Utilities for simple uses of the parser. |
41 | pub mod utils; | 41 | pub 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 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | use { | 2 | |
3 | SyntaxKind, TextUnit, | 3 | use smol_str::SmolStr; |
4 | smol_str::SmolStr, | 4 | |
5 | }; | 5 | use {SyntaxKind, TextUnit}; |
6 | 6 | ||
7 | #[derive(Clone, Debug)] | 7 | #[derive(Clone, Debug)] |
8 | pub(crate) enum GreenNode { | 8 | pub(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 @@ | |||
1 | use std::{fmt, sync::Arc}; | 1 | use std::{fmt, sync::Arc}; |
2 | 2 | ||
3 | use smol_str::SmolStr; | ||
4 | |||
3 | use { | 5 | use { |
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] | ||
2 | name = "smol_str" | ||
3 | version = "0.1.0" | ||
4 | authors = ["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 @@ | |||
1 | use std::{sync::Arc}; | 1 | use std::{sync::Arc, ops::Deref}; |
2 | |||
3 | #[derive(Clone, Debug)] | ||
4 | pub struct SmolStr(Repr); | ||
5 | |||
6 | impl 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 | |||
20 | impl Deref for SmolStr { | ||
21 | type Target = str; | ||
22 | |||
23 | fn deref(&self) -> &str { | ||
24 | self.as_str() | ||
25 | } | ||
26 | } | ||
27 | |||
28 | impl PartialEq<str> for SmolStr { | ||
29 | fn eq(&self, other: &str) -> bool { | ||
30 | self.as_str() == other | ||
31 | } | ||
32 | } | ||
33 | |||
34 | impl PartialEq<SmolStr> for str { | ||
35 | fn eq(&self, other: &SmolStr) -> bool { | ||
36 | other == self | ||
37 | } | ||
38 | } | ||
39 | |||
40 | impl<'a> PartialEq<&'a str> for SmolStr { | ||
41 | fn eq(&self, other: &&'a str) -> bool { | ||
42 | self == *other | ||
43 | } | ||
44 | } | ||
45 | |||
46 | impl<'a> PartialEq<SmolStr> for &'a str { | ||
47 | fn eq(&self, other: &SmolStr) -> bool { | ||
48 | *self == other | ||
49 | } | ||
50 | } | ||
2 | 51 | ||
3 | const INLINE_CAP: usize = 22; | 52 | const INLINE_CAP: usize = 22; |
4 | const WS_TAG: u8 = (INLINE_CAP + 1) as u8; | 53 | const WS_TAG: u8 = (INLINE_CAP + 1) as u8; |
5 | 54 | ||
6 | #[derive(Clone, Debug)] | 55 | #[derive(Clone, Debug)] |
7 | pub(crate) enum SmolStr { | 56 | enum 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 | ||
15 | impl SmolStr { | 64 | impl 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; |