@@ -162,6 +162,53 @@ def parse_code(cls, index, lines, name):
162162 index += 1
163163 raise ValueError ('Snippet {} is missing an END tag' .format (name ))
164164
165+ @classmethod
166+ def parse_snippet_javadoc (csl , index , lines ):
167+ javadoc = []
168+ while index >= 0 :
169+ line = lines [index ]
170+ index -= 1
171+ javadoc .append (line )
172+ stripped_line = line .lstrip ()
173+ if not stripped_line .startswith ('*' ):
174+ raise ValueError ('Could not parse javadoc snippet:\n {}' .format (javadoc ))
175+ if stripped_line .startswith ('* <p>' ):
176+ if index >= 0 :
177+ next_line = lines [index ].strip ()
178+ if next_line == '*' :
179+ javadoc .append (next_line )
180+ return javadoc
181+ raise ValueError ('Could not parse javadoc snippet:\n {}' .format (javadoc ))
182+
183+ @classmethod
184+ def remove_snippets (csl , signature , lines , line_numbers ):
185+ """Removes javadoc snippets for a method with the provided signature.
186+
187+ This method removes the lines that correspond to javadoc snippets, returns the updated
188+ lines and updates the line_numbers data structure in the process.
189+
190+ A snippet's javadoc should have the following format (as generated by to_javadoc()):
191+ * <p>...
192+ * ...
193+ * ...
194+ * ...}</pre>
195+ """
196+ index = line_numbers .get (signature )
197+ LOGGER .info ('Removing snippets for method %s' , signature )
198+ while index >= 0 :
199+ line = lines [index ].lstrip ()
200+ if line .startswith ('/**' ):
201+ return lines
202+ if line .rstrip ().endswith ('}</pre>' ):
203+ javadoc = Snippet .parse_snippet_javadoc (index , lines );
204+ new_index = index - len (javadoc )
205+ line_numbers .update (index , - len (javadoc ))
206+ lines = lines [:new_index + 1 ] + lines [index + 1 :]
207+ index = new_index
208+ else :
209+ index -= 1
210+ raise ValueError ('Could not parse javadoc snippets for method {}' .format (signautre ))
211+
165212 @classmethod
166213 def parse_snippets (cls , snippets_filename ):
167214 """Parses a file looking for code snippets. Returns a list of Snippet objects.
@@ -240,21 +287,36 @@ def write_snippets(cls, snippets, filename):
240287 class_name = os .path .splitext (os .path .basename (filename ))[0 ]
241288 methods = Method .parse_methods (string , class_name )
242289 line_numbers = LineNumbers (string , methods )
290+ for method in methods :
291+ lines = Snippet .remove_snippets (method .signature , lines , line_numbers )
243292 for snippet in snippets :
244293 target = snippet .target
245294 LOGGER .info ('Building snippet for method %s#%s.' , class_name , target )
246295 target_line = line_numbers .get (target )
247296 index = target_line - 2
297+ javadoc = snippet .to_javadoc ()
248298 while index >= 0 :
249299 stripped_line = lines [index ].strip ()
250- if (stripped_line == '*' ):
300+ if (stripped_line . startswith ( '/**' ) ):
251301 break
252302 index -= 1
253- javadoc = snippet .to_javadoc ()
303+ previous_line = ''
304+ while index <= len (lines ):
305+ stripped_line = lines [index ].strip ()
306+ if stripped_line == '*/' or stripped_line .startswith ('* @' ):
307+ if previous_line .strip () != '*' :
308+ indent = len (previous_line ) - len (previous_line .lstrip ())
309+ javadoc = javadoc + [previous_line [:indent ] + '*\n ' ]
310+ else :
311+ index -= 1
312+ break
313+ previous_line = lines [index ]
314+ index += 1
254315 lines [index :index ] = javadoc
255- line_numbers .insert (target_line , len (javadoc ))
316+ line_numbers .update (target_line , len (javadoc ))
256317 java_file .seek (0 )
257318 java_file .writelines (lines )
319+ java_file .truncate ()
258320
259321class LineNumbers (object ):
260322
@@ -274,9 +336,10 @@ def get(self, signature):
274336 """
275337 return self .line_numbers [signature ]
276338
277- def insert (self , at , count ):
339+ def update (self , at , count ):
278340 """
279- Inserts count lines at the provided position. Line numbers are updated accordingly.
341+ Inserts (if count is positive) or removes (if count is negative) count lines at the
342+ provided position. Line numbers are updated accordingly.
280343 """
281344 updated_line_numbers = {}
282345 for target in self .line_numbers :
0 commit comments