Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix for formatting of multiple files; small clean-ups to format strategy

  • Loading branch information...
commit 1deb48b1572220ecba4990d26c26b1f54c1ba4e6 1 parent 73cba47
@mdr mdr authored
View
21 org.scala-ide.sdt.core/src/scala/tools/eclipse/formatter/ScalaFormatterCleanUpProvider.scala
@@ -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)
}
View
3  org.scala-ide.sdt.core/src/scala/tools/eclipse/formatter/ScalaFormattingStrategy.scala
@@ -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
@@ -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 {
View
11 org.scala-ide.sdt.core/src/scala/tools/eclipse/util/EclipseUtils.scala
@@ -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 {
@@ -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)
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.