1414class ComposerIterator :
1515 """An iterator that composer uses to build the file"""
1616
17- prepend : str = ""
18- previous_prepend : str = ""
19- lines : str = ""
20- previous_addition : str = ""
17+ prepend : str = "" # To store tabs
18+ previous_prepend : str = "" # To store prev tabs
19+ lines : str = "" # To store current progress
20+ previous_addition : str = "" # To store what data was previously added (for comments)
2121
2222
2323class Composer :
@@ -65,12 +65,17 @@ def __work_out_lines_comments(self, node: Node) -> None:
6565
6666 Args:
6767 node: Node - the starting node
68+
69+ Note:
70+ The only difference between this function and the ordinary work out lines function
71+ is adding of comments to the end of lines - which requires use of previous_addition
72+ so that there is no confusing of comments for data
6873 """
6974
7075 comment_for_after = ""
7176 if node .get_comments ():
7277 for comment in node .get_comments ():
73- if comment [1 ]: # Need to insert above current line
78+ if comment [1 ]: # Need to insert above current line (the true false variable in the comment tuple)
7479 before_comment = self .__composer_iterator .lines .split ("\n " )[:- 1 ]
7580 post_comment = self .__composer_iterator .lines .split ("\n " )[- 1 ]
7681 before_comment_str = '\n ' .join (before_comment ) + "\n " + self .__composer_iterator .prepend + comment [0 ]
@@ -190,32 +195,50 @@ def __work_out_lines_no_comments(self, node: Node) -> None:
190195 Args:
191196 node: Node - the starting node
192197 """
193- if isinstance (node , ConnectorNode ):
194- if len (node .get_connected_nodes ()) > 1 :
198+
199+ if isinstance (node , ConnectorNode ): # Most will be connector nodes
200+ if len (node .get_connected_nodes ()) > 1 : # To check if we should split it with curly braecs or not
201+
202+ # Managing the placing of the start of the group
195203 if self .__composer_iterator .lines [- 1 ] != ":" :
196- if self .__composer_iterator .lines [- 1 ] == "." :
204+ if self .__composer_iterator .lines [- 1 ] == "." : # if this is already part of a path like x. already exists
197205 self .__composer_iterator .lines += node .get_name () + " = {\n "
198- elif self .__composer_iterator .lines [- 1 ] == "\n " :
206+ elif self .__composer_iterator .lines [- 1 ] == "\n " : # new line so not part of path - means that the for tabs needs to be added
199207 self .__composer_iterator .lines += self .__composer_iterator .prepend + node .get_name () + " = {\n "
200208 else :
201209 raise ErrorComposingFileFromTree (
202210 f"There was an error composing the file from the tree, here is what has been generated already { self .__composer_iterator .lines } "
203211 )
204212 else :
205- self .__composer_iterator .lines += "\n \n {\n "
206- self .__composer_iterator .previous_prepend = self .__composer_iterator .prepend
207- self .__composer_iterator .prepend += " "
213+ self .__composer_iterator .lines += "\n \n {\n " # If it is a new file! (only run on first iteration)
214+ self .__composer_iterator .previous_prepend = self .__composer_iterator .prepend # need to indent in
215+ self .__composer_iterator .prepend += " "
216+
217+ # indent managing
218+ self .__composer_iterator .previous_prepend = self .__composer_iterator .prepend # Updating the prepend (to go down indent later)
219+ self .__composer_iterator .prepend += " " # indenting a bit more - we just opened curly braces!
220+
208221 for singular_node in node .get_connected_nodes ():
222+ # Recursive calling!
209223 self .__work_out_lines_no_comments (singular_node )
224+
225+
210226 if self .__composer_iterator .previous_prepend != "" :
211- self .__composer_iterator .lines += self .__composer_iterator .previous_prepend + "};\n "
212- else : # Then it is the end of the file
227+ self .__composer_iterator .lines += self .__composer_iterator .previous_prepend + "};\n " # Need to shut the group
228+ else : # Then it is the end of the file as we have shut the final group (the large {})
213229 pass
230+
231+ # Updating prepends again to go back
214232 self .__composer_iterator .prepend = self .__composer_iterator .previous_prepend
215233 self .__composer_iterator .previous_prepend = self .__composer_iterator .previous_prepend [2 :]
234+
235+ # This is to make the base indent level more spaced out
216236 if len (self .__composer_iterator .prepend ) == 2 :
217237 self .__composer_iterator .lines += "\n "
218- elif len (node .get_connected_nodes ()) == 1 :
238+
239+ elif len (node .get_connected_nodes ()) == 1 : # If there is only one child
240+
241+ # Slightly different to above - no curly brace
219242 if self .__composer_iterator .lines [- 1 ] != ":" :
220243 if self .__composer_iterator .lines [- 1 ] == "." :
221244 self .__composer_iterator .lines += node .get_name () + "."
@@ -227,27 +250,30 @@ def __work_out_lines_no_comments(self, node: Node) -> None:
227250 )
228251 else :
229252 self .__composer_iterator .lines += "\n \n {\n "
230- self .__composer_iterator .previous_prepend = self .__composer_iterator .prepend
253+ self .__composer_iterator .previous_prepend = self .__composer_iterator .prepend # need to indent in
231254 self .__composer_iterator .prepend += " "
255+
256+ # call it for the singular node
232257 self .__work_out_lines_no_comments (node .get_connected_nodes ()[0 ])
233258 else :
234259 pass
235260 elif isinstance (node , VariableNode ):
261+ # getting "x =" from "y.z.x = gosh" node
236262 data = node .get_name ().split ("." )[- 1 ] + " = "
237263
238264 if node .get_type () == Types .LIST :
239- if "'" not in node .get_data ():
265+ if "'" not in node .get_data (): # If it isnt a list of strings
240266 data_as_list = node .get_data ().split (" " )
241267 data_as_list = data_as_list [1 :- 1 ]
242- if len (data_as_list ) >= 3 :
268+ if len (data_as_list ) >= 3 : # Splitting the list to multiple lines if longer than 2 elements
243269 data += "[\n "
244270 for list_item in data_as_list :
245271 data += self .__composer_iterator .prepend + " " + list_item + "\n "
246272 data += self .__composer_iterator .prepend + "]"
247273 else :
248274 data += node .get_data ()
249275 else :
250- data_as_list = node .get_data ().split ("' '" )
276+ data_as_list = node .get_data ().split ("' '" ) # note the different splitting required
251277 data_as_list = data_as_list [1 :- 1 ]
252278 if len (data_as_list ) >= 3 :
253279 data += "[\n "
@@ -268,10 +294,11 @@ def __work_out_lines_no_comments(self, node: Node) -> None:
268294 data = re .sub (rf"\({ with_clause } \)\." , "" , data )
269295 data = data .split ("=" )[0 ] + "= with " + with_clause + ";" + data .split ("=" )[1 ]
270296
271- if self .__composer_iterator .lines [- 1 ] == "." :
297+ # handling adding it in
298+ if self .__composer_iterator .lines [- 1 ] == "." : # end of a connector like x.y now adding z = enable
272299 self .__composer_iterator .lines += data + ";\n "
273300 elif self .__composer_iterator .lines [- 1 ] == "\n " :
274- self .__composer_iterator .lines += self .__composer_iterator .prepend + data + ";\n "
301+ self .__composer_iterator .lines += self .__composer_iterator .prepend + data + ";\n " # indenting!!!!
275302 else :
276303 raise ErrorComposingFileFromTree (
277304 f"There was an error composing the file from the tree, the previous character was unexpected, here is what there is currently: { self .__composer_iterator .lines } "
0 commit comments