Linear Locator (#46)

This commit is contained in:
Jeong, YunWon 2023-06-01 13:53:31 +09:00 committed by GitHub
parent fdec727f80
commit 5e9e8a7589
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 1080 additions and 20 deletions

View file

@ -1125,10 +1125,13 @@ class LocatedDefVisitor(EmitVisitor):
self.emit_located_impl(variant_info)
if not info.no_cfg(self.type_info):
self.emit('#[cfg(feature = "all-nodes-with-ranges")]', 0)
cfg = '#[cfg(feature = "all-nodes-with-ranges")]'
else:
cfg = ''
self.emit(
f"""
{cfg}
impl Located for {info.full_type_name} {{
fn range(&self) -> SourceRange {{
match self {{
@ -1136,6 +1139,14 @@ class LocatedDefVisitor(EmitVisitor):
}}
}}
}}
{cfg}
impl LocatedMut for {info.full_type_name} {{
fn range_mut(&mut self) -> &mut SourceRange {{
match self {{
{sum_match_arms.replace('range()', 'range_mut()')}
}}
}}
}}
""".lstrip(),
0,
)
@ -1157,15 +1168,24 @@ class LocatedDefVisitor(EmitVisitor):
def emit_located_impl(self, info):
if not info.no_cfg(self.type_info):
self.emit('#[cfg(feature = "all-nodes-with-ranges")]', 0)
cfg = '#[cfg(feature = "all-nodes-with-ranges")]'
else:
cfg = ''
self.emit(
f"""
{cfg}
impl Located for {info.full_type_name} {{
fn range(&self) -> SourceRange {{
self.range
}}
}}
{cfg}
impl LocatedMut for {info.full_type_name} {{
fn range_mut(&mut self) -> &mut SourceRange {{
&mut self.range
}}
}}
""",
0,
)