Skip to content

Commit 21cdaa0

Browse files
authored
docs: ensure new line after colon (#1691)
* docs: ensure new line after colon * add comment * update goldens * add another test case * add another test case * remove test code
1 parent 2349465 commit 21cdaa0

6 files changed

Lines changed: 51 additions & 9 deletions

File tree

packages/gapic-generator/gapic/utils/lines.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import re
1516
import textwrap
1617
from typing import Iterable, Optional
1718

@@ -77,6 +78,12 @@ def wrap(text: str, width: int, *, offset: Optional[int] = None, indent: int = 0
7778

7879
# Break off the first line of the string to address non-zero offsets.
7980
first = text.split('\n')[0] + '\n'
81+
82+
# Ensure that there are 2 new lines after a colon, otherwise
83+
# the sphinx docs build will fail.
84+
if first.endswith(":\n"):
85+
first += "\n"
86+
8087
if len(first) > width - offset:
8188
# Ensure `break_on_hyphens` is set to `False` when using
8289
# `textwrap.wrap` to avoid breaking hyperlinks with hyphens.
@@ -95,6 +102,11 @@ def wrap(text: str, width: int, *, offset: Optional[int] = None, indent: int = 0
95102

96103
# Save the new `first` line.
97104
first = f'{initial[0]}\n'
105+
106+
# Ensure that there are 2 new lines after a colon, otherwise
107+
# the sphinx docs build will fail.
108+
text = re.sub(r':\n([^\n])', r':\n\n\1', text)
109+
98110
text = text[len(first):].strip()
99111
if not text:
100112
return first.strip()
@@ -109,7 +121,9 @@ def wrap(text: str, width: int, *, offset: Optional[int] = None, indent: int = 0
109121
tokens.append(token)
110122
token = ''
111123
token += line + '\n'
112-
if len(line) < width * 0.75:
124+
125+
# Preserve line breaks for lines that are short or end with colon.
126+
if len(line) < width * 0.75 or line.endswith(':'):
113127
tokens.append(token)
114128
token = ''
115129
if token:

packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/async_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ async def sample_create_feed():
584584
exported. The asset feed must be created
585585
within a project, organization, or
586586
folder. Supported destinations are:
587+
587588
Pub/Sub topics.
588589
589590
"""
@@ -691,6 +692,7 @@ async def sample_get_feed():
691692
exported. The asset feed must be created
692693
within a project, organization, or
693694
folder. Supported destinations are:
695+
694696
Pub/Sub topics.
695697
696698
"""
@@ -919,6 +921,7 @@ async def sample_update_feed():
919921
exported. The asset feed must be created
920922
within a project, organization, or
921923
folder. Supported destinations are:
924+
922925
Pub/Sub topics.
923926
924927
"""

packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ def sample_create_feed():
782782
exported. The asset feed must be created
783783
within a project, organization, or
784784
folder. Supported destinations are:
785+
785786
Pub/Sub topics.
786787
787788
"""
@@ -889,6 +890,7 @@ def sample_get_feed():
889890
exported. The asset feed must be created
890891
within a project, organization, or
891892
folder. Supported destinations are:
893+
892894
Pub/Sub topics.
893895
894896
"""
@@ -1103,6 +1105,7 @@ def sample_update_feed():
11031105
exported. The asset feed must be created
11041106
within a project, organization, or
11051107
folder. Supported destinations are:
1108+
11061109
Pub/Sub topics.
11071110
11081111
"""

packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/rest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,7 @@ def __call__(self,
756756
exported. The asset feed must be created
757757
within a project, organization, or
758758
folder. Supported destinations are:
759+
759760
Pub/Sub topics.
760761
761762
"""
@@ -998,6 +999,7 @@ def __call__(self,
998999
exported. The asset feed must be created
9991000
within a project, organization, or
10001001
folder. Supported destinations are:
1002+
10011003
Pub/Sub topics.
10021004
10031005
"""
@@ -1385,6 +1387,7 @@ def __call__(self,
13851387
exported. The asset feed must be created
13861388
within a project, organization, or
13871389
folder. Supported destinations are:
1390+
13881391
Pub/Sub topics.
13891392
13901393
"""

packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/types/asset_service.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,7 @@ class Feed(proto.Message):
839839
An asset feed filter controls what updates are exported. The
840840
asset feed must be created within a project, organization, or
841841
folder. Supported destinations are:
842+
842843
Pub/Sub topics.
843844
844845
Attributes:

packages/gapic-generator/tests/unit/utils/test_lines.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,16 @@ def test_wrap_with_short_lines():
102102

103103

104104
def test_list_each_item_in_list_has_new_line():
105-
s = """Type of weather:
105+
input = """Type of weather:
106106
- Hail
107107
- Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain
108108
- Snow"""
109-
assert lines.wrap(s, width=80) == s
109+
expected = """Type of weather:
110+
111+
- Hail
112+
- Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain
113+
- Snow"""
114+
assert lines.wrap(input, width=80) == expected
110115

111116

112117
def test_list_items_are_indented():
@@ -126,7 +131,7 @@ def test_list_items_are_indented():
126131
assert lines.wrap(input, width=60) == expected
127132

128133

129-
def test_list_new_line_preserved_after_colon():
134+
def test_list_items_short_text_before_list_with_new_line_preserved():
130135
input = """Today's forecast will have different types of weather:
131136
132137
- A mix of hail and snow, followed by rain clouds, then finally clear sky
@@ -142,20 +147,33 @@ def test_list_new_line_preserved_after_colon():
142147
assert lines.wrap(input, width=60, indent=16) == expected
143148

144149

145-
def test_list_items_longer_text_before_list():
150+
def test_list_items_long_text_before_list_with_new_line_preserved():
146151
input = """Weather Weather Weather Weather Weather Weather Weather
147-
Weather Weather Weather Weather Weather Weather Weather
148-
Type of weather:
152+
Weather Weather Weather Weather Weather Weather Type of weather:
149153
150154
- Hail
151155
- Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain
152156
- Snow"""
153157
expected = """Weather Weather Weather Weather Weather Weather Weather
154-
Weather Weather Weather Weather Weather Weather Weather Type
155-
of weather:
158+
Weather Weather Weather Weather Weather Weather Type of
159+
weather:
156160
157161
- Hail
158162
- Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain Rain
159163
Rain
160164
- Snow"""
161165
assert lines.wrap(input, width=60) == expected
166+
167+
168+
def test_new_line_added_short_text_before_list():
169+
input = """Today's forecast will have different weather:
170+
- A mix of hail and snow, followed by rain clouds, then finally clear sky
171+
- Rain
172+
- Snow"""
173+
expected = """Today's forecast will have different weather:
174+
175+
- A mix of hail and snow, followed by rain clouds, then
176+
finally clear sky
177+
- Rain
178+
- Snow"""
179+
assert lines.wrap(input, width=60) == expected

0 commit comments

Comments
 (0)