Skip to content

Commit cdcd301

Browse files
committed
fix(docs): include object map suggestions
1 parent bf87d4b commit cdcd301

2 files changed

Lines changed: 158 additions & 7 deletions

File tree

internal/cmd/docs_suggestions.go

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,18 @@ func enumerateDocsSuggestions(doc *docs.Document) []docsSuggestionListItem {
107107
}
108108

109109
items := make([]docsSuggestionListItem, 0)
110-
collectDocsSuggestionSegment(&items, "body", bodyContent(doc.Body))
110+
collectDocsSuggestionSegment(&items, doc, "body", bodyContent(doc.Body))
111111
for _, id := range sortedDocsMapKeys(doc.Headers) {
112112
header := doc.Headers[id]
113-
collectDocsSuggestionSegment(&items, "header:"+id, header.Content)
113+
collectDocsSuggestionSegment(&items, doc, "header:"+id, header.Content)
114114
}
115115
for _, id := range sortedDocsMapKeys(doc.Footers) {
116116
footer := doc.Footers[id]
117-
collectDocsSuggestionSegment(&items, "footer:"+id, footer.Content)
117+
collectDocsSuggestionSegment(&items, doc, "footer:"+id, footer.Content)
118118
}
119119
for _, id := range sortedDocsMapKeys(doc.Footnotes) {
120120
footnote := doc.Footnotes[id]
121-
collectDocsSuggestionSegment(&items, "footnote:"+id, footnote.Content)
121+
collectDocsSuggestionSegment(&items, doc, "footnote:"+id, footnote.Content)
122122
}
123123
return items
124124
}
@@ -141,6 +141,7 @@ func sortedDocsMapKeys[T any](values map[string]T) []string {
141141

142142
func collectDocsSuggestionSegment(
143143
items *[]docsSuggestionListItem,
144+
doc *docs.Document,
144145
segment string,
145146
content []*docs.StructuralElement,
146147
) {
@@ -164,6 +165,14 @@ func collectDocsSuggestionSegment(
164165
)
165166
}
166167
if element.Paragraph != nil {
168+
appendDocsParagraphMapSuggestions(
169+
items,
170+
lastByKey,
171+
doc,
172+
segment,
173+
element.StartIndex,
174+
element.Paragraph,
175+
)
167176
for _, paragraphElement := range element.Paragraph.Elements {
168177
if paragraphElement == nil {
169178
continue
@@ -173,6 +182,7 @@ func collectDocsSuggestionSegment(
173182
lastByKey,
174183
segment,
175184
paragraphElement,
185+
doc.InlineObjects,
176186
insertionIDs,
177187
deletionIDs,
178188
)
@@ -211,15 +221,61 @@ func collectDocsSuggestionSegment(
211221
walk(content, nil, nil)
212222
}
213223

224+
func appendDocsParagraphMapSuggestions(
225+
items *[]docsSuggestionListItem,
226+
lastByKey map[string]int,
227+
doc *docs.Document,
228+
segment string,
229+
anchorIndex int64,
230+
paragraph *docs.Paragraph,
231+
) {
232+
if paragraph.Bullet != nil {
233+
if list, ok := doc.Lists[paragraph.Bullet.ListId]; ok {
234+
appendDocsSuggestionRange(
235+
items,
236+
lastByKey,
237+
segment,
238+
anchorIndex,
239+
anchorIndex,
240+
"",
241+
[]string{list.SuggestedInsertionId},
242+
list.SuggestedDeletionIds,
243+
)
244+
}
245+
}
246+
247+
objectIDs := append([]string(nil), paragraph.PositionedObjectIds...)
248+
for _, suggestionID := range sortedDocsMapKeys(paragraph.SuggestedPositionedObjectIds) {
249+
objectIDs = append(objectIDs, paragraph.SuggestedPositionedObjectIds[suggestionID].ObjectIds...)
250+
}
251+
for _, objectID := range sortedUniqueStrings(objectIDs) {
252+
object, ok := doc.PositionedObjects[objectID]
253+
if !ok {
254+
continue
255+
}
256+
appendDocsSuggestionRange(
257+
items,
258+
lastByKey,
259+
segment,
260+
anchorIndex,
261+
anchorIndex,
262+
"",
263+
[]string{object.SuggestedInsertionId},
264+
object.SuggestedDeletionIds,
265+
)
266+
}
267+
}
268+
214269
func appendDocsSuggestionElement(
215270
items *[]docsSuggestionListItem,
216271
lastByKey map[string]int,
217272
segment string,
218273
element *docs.ParagraphElement,
274+
inlineObjects map[string]docs.InlineObject,
219275
inheritedInsertionIDs []string,
220276
inheritedDeletionIDs []string,
221277
) {
222-
text, insertionIDs, deletionIDs, ok := docsParagraphElementSuggestions(element)
278+
text, insertionIDs, deletionIDs, ok := docsParagraphElementSuggestions(element, inlineObjects)
223279
if !ok {
224280
return
225281
}
@@ -269,7 +325,10 @@ func appendDocsSuggestionRange(
269325
appendIDs("deletion", deletionIDs)
270326
}
271327

272-
func docsParagraphElementSuggestions(element *docs.ParagraphElement) (
328+
func docsParagraphElementSuggestions(
329+
element *docs.ParagraphElement,
330+
inlineObjects map[string]docs.InlineObject,
331+
) (
273332
text string,
274333
insertionIDs []string,
275334
deletionIDs []string,
@@ -294,7 +353,13 @@ func docsParagraphElementSuggestions(element *docs.ParagraphElement) (
294353
case element.HorizontalRule != nil:
295354
return "", element.HorizontalRule.SuggestedInsertionIds, element.HorizontalRule.SuggestedDeletionIds, true
296355
case element.InlineObjectElement != nil:
297-
return "", element.InlineObjectElement.SuggestedInsertionIds, element.InlineObjectElement.SuggestedDeletionIds, true
356+
insertionIDs := element.InlineObjectElement.SuggestedInsertionIds
357+
deletionIDs := element.InlineObjectElement.SuggestedDeletionIds
358+
if object, ok := inlineObjects[element.InlineObjectElement.InlineObjectId]; ok {
359+
insertionIDs = mergeDocsSuggestionIDs(insertionIDs, []string{object.SuggestedInsertionId})
360+
deletionIDs = mergeDocsSuggestionIDs(deletionIDs, object.SuggestedDeletionIds)
361+
}
362+
return "", insertionIDs, deletionIDs, true
298363
case element.PageBreak != nil:
299364
return "", element.PageBreak.SuggestedInsertionIds, element.PageBreak.SuggestedDeletionIds, true
300365
case element.Person != nil:

internal/cmd/docs_suggestions_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,92 @@ func TestEnumerateDocsSuggestions_SectionBreak(t *testing.T) {
189189
}
190190
}
191191

192+
func TestEnumerateDocsSuggestions_PositionedObjects(t *testing.T) {
193+
t.Parallel()
194+
195+
doc := &docs.Document{
196+
Body: &docs.Body{Content: []*docs.StructuralElement{{
197+
StartIndex: 10,
198+
EndIndex: 20,
199+
Paragraph: &docs.Paragraph{
200+
PositionedObjectIds: []string{"deleted"},
201+
SuggestedPositionedObjectIds: map[string]docs.ObjectReferences{
202+
"insert-suggestion": {ObjectIds: []string{"inserted"}},
203+
},
204+
},
205+
}}},
206+
PositionedObjects: map[string]docs.PositionedObject{
207+
"inserted": {SuggestedInsertionId: "insert-suggestion"},
208+
"deleted": {SuggestedDeletionIds: []string{"delete-suggestion"}},
209+
},
210+
}
211+
212+
got := enumerateDocsSuggestions(doc)
213+
want := []docsSuggestionListItem{
214+
{SuggestionID: "delete-suggestion", Kind: "deletion", Segment: "body", StartIndex: 10, EndIndex: 10},
215+
{SuggestionID: "insert-suggestion", Kind: "insertion", Segment: "body", StartIndex: 10, EndIndex: 10},
216+
}
217+
if !reflect.DeepEqual(got, want) {
218+
t.Fatalf("suggestions mismatch\n got: %#v\nwant: %#v", got, want)
219+
}
220+
}
221+
222+
func TestEnumerateDocsSuggestions_InlineObjectMap(t *testing.T) {
223+
t.Parallel()
224+
225+
doc := &docs.Document{
226+
Body: &docs.Body{Content: []*docs.StructuralElement{{
227+
Paragraph: &docs.Paragraph{Elements: []*docs.ParagraphElement{{
228+
StartIndex: 4,
229+
EndIndex: 5,
230+
InlineObjectElement: &docs.InlineObjectElement{InlineObjectId: "image"},
231+
}}},
232+
}}},
233+
InlineObjects: map[string]docs.InlineObject{
234+
"image": {
235+
SuggestedInsertionId: "insert",
236+
SuggestedDeletionIds: []string{"delete"},
237+
},
238+
},
239+
}
240+
241+
got := enumerateDocsSuggestions(doc)
242+
want := []docsSuggestionListItem{
243+
{SuggestionID: "insert", Kind: "insertion", Segment: "body", StartIndex: 4, EndIndex: 5},
244+
{SuggestionID: "delete", Kind: "deletion", Segment: "body", StartIndex: 4, EndIndex: 5},
245+
}
246+
if !reflect.DeepEqual(got, want) {
247+
t.Fatalf("suggestions mismatch\n got: %#v\nwant: %#v", got, want)
248+
}
249+
}
250+
251+
func TestEnumerateDocsSuggestions_ListMap(t *testing.T) {
252+
t.Parallel()
253+
254+
doc := &docs.Document{
255+
Body: &docs.Body{Content: []*docs.StructuralElement{{
256+
StartIndex: 3,
257+
EndIndex: 7,
258+
Paragraph: &docs.Paragraph{Bullet: &docs.Bullet{ListId: "list"}},
259+
}}},
260+
Lists: map[string]docs.List{
261+
"list": {
262+
SuggestedInsertionId: "insert",
263+
SuggestedDeletionIds: []string{"delete"},
264+
},
265+
},
266+
}
267+
268+
got := enumerateDocsSuggestions(doc)
269+
want := []docsSuggestionListItem{
270+
{SuggestionID: "insert", Kind: "insertion", Segment: "body", StartIndex: 3, EndIndex: 3},
271+
{SuggestionID: "delete", Kind: "deletion", Segment: "body", StartIndex: 3, EndIndex: 3},
272+
}
273+
if !reflect.DeepEqual(got, want) {
274+
t.Fatalf("suggestions mismatch\n got: %#v\nwant: %#v", got, want)
275+
}
276+
}
277+
192278
func TestDocsSuggestionsList_JSON(t *testing.T) {
193279
t.Parallel()
194280

0 commit comments

Comments
 (0)