Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5e33650

Browse files
committed
elaborate SyntaxEdit comments
1 parent 2311e96 commit 5e33650

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/tools/rust-analyzer/crates/syntax/src/syntax_editor.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,28 @@ pub struct SyntaxEdit {
6161
}
6262

6363
impl SyntaxEdit {
64+
/// Root of the modified syntax tree
6465
pub fn root(&self) -> &SyntaxNode {
6566
&self.root
6667
}
6768

69+
/// Which syntax elements in the modified syntax tree were modified as part
70+
/// of the edit.
71+
///
72+
/// Note that for syntax nodes, only the upper-most parent of a set of
73+
/// changes is included, not any child elements that may have been modified.
6874
pub fn changed_elements(&self) -> &[SyntaxElement] {
6975
self.changed_elements.as_slice()
7076
}
7177

72-
pub fn find_annotation(&self, annotation: SyntaxAnnotation) -> Option<&[SyntaxElement]> {
73-
self.annotations.get(&annotation).as_ref().map(|it| it.as_slice())
78+
/// Finds which syntax elements have been annotated with the given
79+
/// annotation.
80+
///
81+
/// Note that an annotation might not appear in the modified syntax tree if
82+
/// the syntax elements that were annotated did not make it into the final
83+
/// syntax tree.
84+
pub fn find_annotation(&self, annotation: SyntaxAnnotation) -> &[SyntaxElement] {
85+
self.annotations.get(&annotation).as_ref().map_or(&[], |it| it.as_slice())
7486
}
7587
}
7688

@@ -83,9 +95,8 @@ impl SyntaxAnnotation {
8395
pub fn new() -> Self {
8496
static COUNTER: AtomicU32 = AtomicU32::new(1);
8597

86-
// We want the id to be unique across threads, but we don't want to
87-
// tie it to other `SeqCst` operations.
88-
let id = COUNTER.fetch_add(1, Ordering::AcqRel);
98+
// Only consistency within a thread matters, as SyntaxElements are !Send
99+
let id = COUNTER.fetch_add(1, Ordering::Relaxed);
89100

90101
Self(NonZeroU32::new(id).expect("syntax annotation id overflow"))
91102
}
@@ -328,6 +339,6 @@ mod tests {
328339

329340
let expect = expect![];
330341
expect.assert_eq(&edit.root.to_string());
331-
assert_eq!(edit.find_annotation(placeholder_snippet).map(|it| it.len()), Some(2));
342+
assert_eq!(edit.find_annotation(placeholder_snippet).len(), 2);
332343
}
333344
}

0 commit comments

Comments
 (0)