Skip to content

Commit

Permalink
Fix for formatting of multiple files; small clean-ups to format strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
mdr committed Apr 11, 2011
1 parent 73cba47 commit 1deb48b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
Expand Up @@ -10,22 +10,27 @@ import scalariform.formatter.ScalaFormatter
import scalariform.parser.ScalaParserException
import scalariform.utils.TextEdit
import scala.tools.eclipse.contribution.weaving.jdt.ui.javaeditor.formatter.IFormatterCleanUpProvider
import scala.tools.eclipse.util.EclipseUtils._
import org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter

class ScalaFormatterCleanUpProvider extends IFormatterCleanUpProvider {

def createCleanUp(cu: ICompilationUnit): ICleanUpFix = {
val project = cu.getJavaProject.getProject
val document = cu.getBuffer.asInstanceOf[DocumentAdapter].getDocument
val document = cu.getBuffer match {
case adapter: DocumentAdapter => adapter.getDocument
case _ => new Document(cu.getBuffer.getContents)
}
val lineDelimiter = TextUtilities.getDefaultLineDelimiter(document)

val preferences = FormatterPreferences.getPreferences(cu.getJavaProject.getProject)
val edits =
try ScalaFormatter.formatAsEdits(cu.getSource, FormatterPreferences.getPreferences(project), Some(lineDelimiter))
try ScalaFormatter.formatAsEdits(cu.getSource, preferences, 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))
val change = new CompilationUnitChange("", cu)
change.setEdit(resultEdit);

val multiEdit = new MultiTextEdit
multiEdit.addChildren(edits map asEclipseTextEdit toArray)
val change = new CompilationUnitChange("Formatting", cu)
change.setEdit(multiEdit)
new CodeFormatFix(change)
}

Expand Down
Expand Up @@ -4,6 +4,7 @@ import org.eclipse.core.resources.IProject
import org.eclipse.core.runtime.IAdaptable
import org.eclipse.jdt.core.IJavaElement
import org.eclipse.jface.text._
import org.eclipse.jface.text.TextUtilities.getDefaultLineDelimiter
import org.eclipse.jface.text.formatter._
import org.eclipse.jface.text.source.ISourceViewer
import org.eclipse.jface.preference.IPreferenceStore
Expand Down Expand Up @@ -34,7 +35,7 @@ class ScalaFormattingStrategy(val editor: ITextEditor) extends IFormattingStrate
def format() {
val preferences = FormatterPreferences.getPreferences(getProject)
var edits =
try ScalaFormatter.formatAsEdits(document.get, preferences, document.defaultLineDelimiter)
try ScalaFormatter.formatAsEdits(document.get, preferences, Some(getDefaultLineDelimiter(document)))
catch { case _: ScalaParserException => return }

val (offset, length) = expandToWholeLines(regionOpt match {
Expand Down
@@ -1,8 +1,11 @@
package scala.tools.eclipse.util

import org.eclipse.text.edits.{ TextEdit => EclipseTextEdit, _ }
import scalariform.utils.TextEdit
import org.eclipse.jface.text.IDocumentExtension4
import org.eclipse.jface.text.IDocument
import org.eclipse.core.runtime.IAdaptable

import PartialFunction._

object EclipseUtils {
Expand All @@ -21,12 +24,10 @@ object EclipseUtils {

class PimpedDocument(document: IDocument) {

def defaultLineDelimiter: Option[String] = condOpt(document) {
case d4: IDocumentExtension4 => d4.getDefaultLineDelimiter
}

def apply(offset: Int): Character = document.getChar(offset)

}

implicit def asEclipseTextEdit(edit: TextEdit): EclipseTextEdit = new ReplaceEdit(edit.position, edit.length, edit.replacement)

}

0 comments on commit 1deb48b

Please sign in to comment.