diff --git a/org.scala-ide.sdt.core/src/scala/tools/eclipse/formatter/ScalaFormatterCleanUpProvider.scala b/org.scala-ide.sdt.core/src/scala/tools/eclipse/formatter/ScalaFormatterCleanUpProvider.scala index 646a94f047..88ea33ce4a 100644 --- a/org.scala-ide.sdt.core/src/scala/tools/eclipse/formatter/ScalaFormatterCleanUpProvider.scala +++ b/org.scala-ide.sdt.core/src/scala/tools/eclipse/formatter/ScalaFormatterCleanUpProvider.scala @@ -1,22 +1,25 @@ package scala.tools.eclipse.formatter import org.eclipse.jdt.internal.corext.fix.CodeFormatFix -import scala.tools.eclipse.contribution.weaving.jdt.ui.javaeditor.formatter.IFormatterCleanUpProvider import org.eclipse.jdt.core.ICompilationUnit -import org.eclipse.jface.text.{ Document, TextUtilities }; +import org.eclipse.jdt.core.refactoring.CompilationUnitChange import org.eclipse.jdt.ui.cleanup.ICleanUpFix -import scalariform.formatter.ScalaFormatter +import org.eclipse.jface.text.{ Document, TextUtilities } import org.eclipse.text.edits.{ TextEdit => JFaceTextEdit, _ } +import scalariform.formatter.ScalaFormatter +import scalariform.parser.ScalaParserException import scalariform.utils.TextEdit -import org.eclipse.jdt.core.refactoring.CompilationUnitChange +import scala.tools.eclipse.contribution.weaving.jdt.ui.javaeditor.formatter.IFormatterCleanUpProvider +import org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter class ScalaFormatterCleanUpProvider extends IFormatterCleanUpProvider { def createCleanUp(cu: ICompilationUnit): ICleanUpFix = { - val contents = cu.getBuffer.getContents - val document = new Document(contents) + val document = cu.getBuffer.asInstanceOf[DocumentAdapter].getDocument val lineDelimiter = TextUtilities.getDefaultLineDelimiter(document) - val edits = ScalaFormatter.formatAsEdits(cu.getSource, FormatterPreferencePage.getPreferences, Some(lineDelimiter)) + val edits = + try ScalaFormatter.formatAsEdits(cu.getSource, FormatterPreferencePage.getPreferences, Some(lineDelimiter)) + catch { case e: ScalaParserException => return null } val resultEdit = new MultiTextEdit for (TextEdit(start, length, replacement) <- edits) resultEdit.addChild(new ReplaceEdit(start, length, replacement))