def apply_to_rewrite_cursor(self, rewrite_cursor): import_clause = ScalaImportParser.search(rewrite_cursor) while import_clause is not None: rewritten_clauses = self.apply_rewrite(import_clause) if rewritten_clauses is None: # Spit out the original text, with any wrapping, whitespace or other idiosyncracies it may have. This makes # sure that We only modify files where needed, and not just because of such non-germane differences. rewrite_cursor.emit(import_clause.src_text) else: new_text = '\n'.join([repr(x) for x in rewritten_clauses]) + '\n' rewrite_cursor.emit(new_text) import_clause = ScalaImportParser.search(rewrite_cursor)
def apply_to_rewrite_cursor(self, rewrite_cursor): # Search for the first import in the first import block. import_clause = ScalaImportParser.search(rewrite_cursor) while import_clause is not None: import_block = [] num_blank_lines = 0 while import_clause is not None: # Note that we don't care about blank lines in the middle of an import block, we only emit ones after the # end of a block. num_blank_lines = self.skip_blank_lines(rewrite_cursor) import_block.append(import_clause) import_clause = ScalaImportParser.match(rewrite_cursor) # Note use of match, not search. # We're on the first non-import, non-blank line after an import block, or at the end of the text. processed_imports = self._process_import_block(import_block) rewrite_cursor.emit(processed_imports) rewrite_cursor.emit('\n' * num_blank_lines) # Search for the first import in the next block. import_clause = ScalaImportParser.search(rewrite_cursor)
def _do_test_matcher(self, line, expected_path, expected_selectors): line += '\n' rewrite_cursor = RewriteCursor('filename', line) import_clause = ScalaImportParser.match(rewrite_cursor) if expected_path is None: self.assertIsNone(import_clause) else: self.assertIsNotNone(import_clause) expected_import_clause = ScalaImportClause('', expected_path) for (name, as_name) in expected_selectors: expected_import_clause.add_import(name, as_name) self.assertEqual(expected_import_clause, import_clause)