Skip to content

Commit

Permalink
Handle parse exceptions on format save action
Browse files Browse the repository at this point in the history
  • Loading branch information
mdr committed Mar 10, 2011
1 parent 05a67df commit 5553430
Showing 1 changed file with 10 additions and 7 deletions.
@@ -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))
Expand Down

0 comments on commit 5553430

Please sign in to comment.