Skip to content

Commit

Permalink
Merge pull request #505 from simoncozens/write-others-earliest
Browse files Browse the repository at this point in the history
Find earliest opportunity to insert lookups/mark classes/anchors
  • Loading branch information
moyogo authored Jun 30, 2021
2 parents 5ae989d + 5830ca8 commit 0a4b703
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions Lib/ufo2ft/featureWriters/baseFeatureWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def _insert(
# Collect insert markers in blocks
insertComments = self.context.insertComments

wroteOthers = False
indices = []
for feature in features:
if insertComments and feature.name in insertComments:
block, comment = insertComments[feature.name]
Expand Down Expand Up @@ -209,29 +209,26 @@ def _insert(

else:
index = len(statements)

# Write classDefs, anchorsDefs, markClassDefs, lookups on the first
# iteration.
if not wroteOthers:
others = []
# Insert classDefs, anchorsDefs, markClassDefs
for defs in [classDefs, anchorDefs, markClassDefs]:
if defs:
others.extend(defs)
others.append(ast.Comment(""))
# Insert lookups
if lookups:
if index > 0 and not others:
others.append(ast.Comment(""))
others.extend(lookups)
if others:
feaFile.statements = statements = (
statements[:index] + others + statements[index:]
)
index = index + len(others)
wroteOthers = True

statements.insert(index, feature)
indices.append(index)

# Write classDefs, anchorsDefs, markClassDefs, lookups at earliest
# opportunity.
others = []
minindex = min(indices)
for defs in [classDefs, anchorDefs, markClassDefs]:
if defs:
others.extend(defs)
others.append(ast.Comment(""))
# Insert lookups
if lookups:
if minindex > 0 and not others:
others.append(ast.Comment(""))
others.extend(lookups)
if others:
feaFile.statements = statements = (
statements[:minindex] + others + statements[minindex:]
)

@staticmethod
def collectInsertMarkers(feaFile, insertFeatureMarker, featureTags):
Expand Down

0 comments on commit 0a4b703

Please sign in to comment.