Skip to content

Commit 1a23b6a

Browse files
feat: add source
1 parent 6ea01ae commit 1a23b6a

File tree

6 files changed

+62
-19
lines changed

6 files changed

+62
-19
lines changed

crates/core/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod node;
66
pub mod ops;
77
mod pattern;
88
mod replacer;
9+
mod source;
910
pub mod traversal;
1011
mod ts_parser;
1112

@@ -21,7 +22,6 @@ use language::Language;
2122
use node::Root;
2223
use ts_parser::{Edit, TSParseError};
2324

24-
#[derive(Clone)]
2525
pub struct AstGrep<L: Language> {
2626
inner: Root<L>,
2727
}
@@ -64,7 +64,7 @@ impl<L: Language> AstGrep<L> {
6464
}
6565

6666
pub fn generate(self) -> String {
67-
self.inner.source
67+
self.inner.source.to_string()
6868
}
6969
}
7070

crates/core/src/match_tree.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ mod test {
268268
inner: goal.root_node().child(0).unwrap(),
269269
root: &Root {
270270
inner: goal.clone(),
271-
source: s1.to_string(),
271+
source: s1.into(),
272272
lang: Tsx,
273273
},
274274
};
@@ -277,7 +277,7 @@ mod test {
277277
inner: cand.root_node(),
278278
root: &Root {
279279
inner: cand.clone(),
280-
source: s2.to_string(),
280+
source: s2.into(),
281281
lang: Tsx,
282282
},
283283
};
@@ -298,7 +298,7 @@ mod test {
298298
inner: goal.root_node().child(0).unwrap(),
299299
root: &Root {
300300
inner: goal.clone(),
301-
source: s1.to_string(),
301+
source: s1.into(),
302302
lang: Tsx,
303303
},
304304
};
@@ -307,7 +307,7 @@ mod test {
307307
inner: cand.root_node(),
308308
root: &Root {
309309
inner: cand.clone(),
310-
source: s2.to_string(),
310+
source: s2.into(),
311311
lang: Tsx,
312312
},
313313
};

crates/core/src/node.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use crate::language::Language;
22
use crate::matcher::{FindAllNodes, Matcher, NodeMatch};
33
use crate::replacer::Replacer;
4+
use crate::source::{Content, Source};
45
use crate::traversal::{Pre, Visitor};
56
use crate::ts_parser::{parse, perform_edit, Edit, TSParseError};
67

78
use std::borrow::Cow;
89

910
/// Represents [`tree_sitter::Tree`] and owns source string
1011
/// Note: Root is generic against [`Language`](crate::language::Language)
11-
#[derive(Clone)]
1212
pub struct Root<L: Language> {
1313
pub(crate) inner: tree_sitter::Tree,
14-
pub(crate) source: String,
14+
pub(crate) source: Source,
1515
pub(crate) lang: L,
1616
}
1717

@@ -30,7 +30,7 @@ impl<L: Language> Root<L> {
3030
}
3131
// extract non generic implementation to reduce code size
3232
pub fn do_edit(&mut self, edit: Edit) -> Result<(), TSParseError> {
33-
let input = unsafe { self.source.as_mut_vec() };
33+
let input = self.source.as_mut_vec();
3434
let input_edit = perform_edit(&mut self.inner, input, &edit);
3535
self.inner.edit(&input_edit);
3636
self.inner = parse(&self.source, Some(&self.inner), self.lang.get_ts_language())?;

crates/core/src/pattern.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use crate::language::Language;
22
use crate::match_tree::{extract_var_from_node, match_end_non_recursive, match_node_non_recursive};
33
use crate::matcher::{KindMatcher, Matcher};
44
use crate::{meta_var::MetaVarEnv, Node, Root};
5+
use std::sync::Arc;
56

67
use bit_set::BitSet;
78

89
#[derive(Clone)]
910
pub struct Pattern<L: Language> {
10-
pub root: Root<L>,
11+
pub root: Arc<Root<L>>,
1112
/// used in contextual pattern, specify which AST subpart is considered as pattern
1213
/// e.g. in js`class { $F }` we set selector to public_field_definition
1314
selector: Option<KindMatcher<L>>,
@@ -22,7 +23,7 @@ impl<L: Language> Pattern<L> {
2223
todo!("multi-children pattern is not supported yet.")
2324
}
2425
Self {
25-
root,
26+
root: Arc::new(root),
2627
selector: None,
2728
}
2829
}
@@ -39,7 +40,7 @@ impl<L: Language> Pattern<L> {
3940
todo!("use result to indicate failure");
4041
}
4142
Self {
42-
root,
43+
root: Arc::new(root),
4344
selector: Some(kind_matcher),
4445
}
4546
}

crates/core/src/source.rs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use std::ops::Deref;
2+
3+
pub trait Content: ToString + Deref<Target = str> {
4+
fn as_mut_vec(&mut self) -> &mut Vec<u8>;
5+
}
6+
7+
pub enum Source {
8+
Plain(String),
9+
Customized(Box<dyn Content + Sync + Send>),
10+
}
11+
12+
use Source::*;
13+
14+
impl From<&str> for Source {
15+
fn from(s: &str) -> Self {
16+
Plain(s.into())
17+
}
18+
}
19+
20+
impl Deref for Source {
21+
type Target = str;
22+
fn deref(&self) -> &Self::Target {
23+
match self {
24+
Plain(s) => s.deref(),
25+
Customized(c) => c.deref(),
26+
}
27+
}
28+
}
29+
30+
impl ToString for Source {
31+
fn to_string(&self) -> String {
32+
match self {
33+
Self::Plain(s) => s.to_owned(),
34+
Self::Customized(c) => c.to_string(),
35+
}
36+
}
37+
}
38+
39+
impl Content for Source {
40+
fn as_mut_vec(&mut self) -> &mut Vec<u8> {
41+
match self {
42+
Plain(s) => unsafe { s.as_mut_vec() },
43+
Customized(c) => c.as_mut_vec(),
44+
}
45+
}
46+
}

crates/lsp/src/lib.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub use tower_lsp::{LspService, Server};
1414
pub trait LSPLang: Language + Eq + Send + Sync + 'static {}
1515
impl<T> LSPLang for T where T: Language + Eq + Send + Sync + 'static {}
1616

17-
#[derive(Clone)]
1817
struct VersionedAst<L: Language> {
1918
version: i32,
2019
root: AstGrep<L>,
@@ -223,17 +222,16 @@ impl<L: LSPLang> Backend<L> {
223222
}
224223
async fn on_open(&self, params: DidOpenTextDocumentParams) -> Option<()> {
225224
let text_doc = params.text_document;
226-
let uri = text_doc.uri.as_str();
225+
let uri = text_doc.uri.as_str().to_owned();
227226
let text = text_doc.text;
228227
let lang = Self::infer_lang_from_uri(&text_doc.uri)?;
229228
let root = AstGrep::new(text, lang);
230229
let versioned = VersionedAst {
231230
version: text_doc.version,
232231
root,
233232
};
234-
let copied = versioned.clone();
233+
self.publish_diagnostics(text_doc.uri, &versioned).await;
235234
self.map.insert(uri.to_owned(), versioned); // don't lock dashmap
236-
self.publish_diagnostics(text_doc.uri, &copied).await;
237235
Some(())
238236
}
239237
async fn on_change(&self, params: DidChangeTextDocumentParams) -> Option<()> {
@@ -251,9 +249,7 @@ impl<L: LSPLang> Backend<L> {
251249
version: text_doc.version,
252250
root,
253251
};
254-
let copied = versioned.clone();
255-
drop(versioned); // don't lock dashmap
256-
self.publish_diagnostics(text_doc.uri, &copied).await;
252+
self.publish_diagnostics(text_doc.uri, &versioned).await;
257253
Some(())
258254
}
259255
async fn on_close(&self, params: DidCloseTextDocumentParams) {

0 commit comments

Comments
 (0)