Skip to content

Commit

Permalink
Set source range for program docblocks
Browse files Browse the repository at this point in the history
Summary: This was an oversight

Reviewed By: vitaut

Differential Revision: D50804792

fbshipit-source-id: 5bb18fd1fce5d4246078832469696734efd647db
  • Loading branch information
iahs authored and facebook-github-bot committed Oct 30, 2023
1 parent ccdeeed commit 9e88edb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
4 changes: 4 additions & 0 deletions thrift/compiler/generate/t_ast_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ void t_ast_generator::generate_program() {
auto program_id = positionToId<apache::thrift::type::ProgramId>(pos);
program_index[&program] = program_id;
hydrate_const(programs.emplace_back(), *schema_source.gen_schema(program));
if (program.has_doc()) {
programs.back().attrs()->docs()->sourceRange() =
src_range(program.doc_range(), &program);
}

auto is_root_program = std::exchange(is_root_program_, false);
for (auto* include : program.get_included_programs()) {
Expand Down
20 changes: 13 additions & 7 deletions thrift/compiler/test/ast_generator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ def test_service(self):
self.assertEqual(func.returnType.name.type, TypeName.Type.i32Type)

func = serviceDef.functions[2]
print(func)
self.assertEqual(func.attrs.name, "baz")
self.assertEqual(len(func.returnTypes), 0) # streams excluded
self.assertEqual(func.returnType.name.type, TypeName.Type.EMPTY)
Expand Down Expand Up @@ -202,6 +201,8 @@ def test_docs(self):
"foo.thrift",
textwrap.dedent(
"""
/** File docblock */
namespace cpp2 foo
/** This is a struct and its name is Foo. */
struct Foo {
1: i64 Bar; ///< This is a field and its name is Bar.
Expand All @@ -211,29 +212,34 @@ def test_docs(self):
)

ast = self.run_thrift("foo.thrift")
print(ast.definitions)

docs = ast.programs[0].attrs.docs
self.assertEqual(docs.contents.rstrip(), "File docblock")
self.assertEqual(docs.sourceRange.programId, 1)
self.assertEqual(docs.sourceRange.beginLine, 2)

self.assertEqual(ast.definitions[0].structDef.attrs.name, "Foo")
srcRange = ast.definitions[0].structDef.attrs.sourceRange
self.assertEqual(srcRange.programId, 1)
self.assertEqual(srcRange.beginLine, 3)
self.assertEqual(srcRange.beginLine, 5)
docs = ast.definitions[0].structDef.attrs.docs
self.assertEqual(
docs.contents.rstrip(), "This is a struct and its name is Foo."
)
self.assertEqual(docs.sourceRange.programId, 1)
self.assertEqual(docs.sourceRange.beginLine, 2)
self.assertEqual(docs.sourceRange.beginLine, 4)
self.assertEqual(docs.sourceRange.beginColumn, 1)
self.assertEqual(docs.sourceRange.endLine, 2)
self.assertEqual(docs.sourceRange.endLine, 4)
self.assertEqual(docs.sourceRange.endColumn, 45)

docs = ast.definitions[0].structDef.fields[0].attrs.docs
self.assertEqual(docs.contents.rstrip(), "This is a field and its name is Bar.")
if os.name == "nt":
return # line separators differ on windows and mess up source ranges
self.assertEqual(docs.sourceRange.programId, 1)
self.assertEqual(docs.sourceRange.beginLine, 4)
self.assertEqual(docs.sourceRange.beginLine, 6)
self.assertEqual(docs.sourceRange.beginColumn, 18)
self.assertEqual(docs.sourceRange.endLine, 5)
self.assertEqual(docs.sourceRange.endLine, 7)
self.assertEqual(docs.sourceRange.endColumn, 1)

def test_program(self):
Expand Down

0 comments on commit 9e88edb

Please sign in to comment.