Skip to content

Commit

Permalink
Tried to fix complex indent conditions when apply diff on formatted t…
Browse files Browse the repository at this point in the history
…ext.
  • Loading branch information
dcaoyuan committed Nov 19, 2015
1 parent e8bd381 commit ab08c69
Showing 1 changed file with 39 additions and 11 deletions.
Expand Up @@ -25,19 +25,15 @@ class ScalaReformatter(source: Source, context: Context) extends ReformatTask {

private val doc = context.document.asInstanceOf[BaseDocument]

val diffOptions = HuntDiff.Options(
ignoreCase = false,
ignoreInnerWhitespace = false,
ignoreLeadingAndtrailingWhitespace = false)

@throws(classOf[BadLocationException])
def reformat() {
val cs = CodeStyle.get(doc)
val fo = source.getFileObject
if (fo == null) {
return
}

val cs = CodeStyle.get(doc)

val project = FileOwnerQuery.getOwner(fo)
val prefs = if (project != null) {
val formPrefsProvider = project.getLookup.lookup(classOf[ScalariformPrefsProvider])
Expand All @@ -53,7 +49,6 @@ class ScalaReformatter(source: Source, context: Context) extends ReformatTask {
val indentRegions = context.indentRegions
java.util.Collections.reverse(indentRegions)
val regions = indentRegions.iterator

while (regions.hasNext) {
val region = regions.next
val start = region.getStartOffset
Expand All @@ -72,11 +67,9 @@ class ScalaReformatter(source: Source, context: Context) extends ReformatTask {
if (formattedText != null && formattedText.length > 0) {
val root = doc.getDefaultRootElement

val diffs = HuntDiff.diff(new StringReader(text), new StringReader(formattedText), diffOptions)
val diffs = HuntDiff.diff(new StringReader(text), new StringReader(formattedText), ScalaReformatter.diffOptions)
// reverse the order so we can modify text forward from the end
java.util.Arrays.sort(diffs, new java.util.Comparator[Diff]() {
def compare(o1: Diff, o2: Diff) = -o1.firstStart.compareTo(o2.firstStart)
})
java.util.Arrays.sort(diffs, DiffComparator)

for (diff <- diffs) {
diff.tpe match {
Expand Down Expand Up @@ -143,6 +136,11 @@ class ScalaReformatter(source: Source, context: Context) extends ReformatTask {
}

object ScalaReformatter {
val diffOptions = HuntDiff.Options(
ignoreCase = false,
ignoreInnerWhitespace = false,
ignoreLeadingAndtrailingWhitespace = false)

/**
* Reformat task factory produces reformat tasks for the given context.
* <br/>
Expand Down Expand Up @@ -170,3 +168,33 @@ object ScalaReformatter {
.setPreference(AlignParameters, true)
.setPreference(AlignSingleLineCaseStatements, true)
}

object DiffComparator extends java.util.Comparator[Diff] {
def compare(o1: Diff, o2: Diff) = {
if (o1.firstStart < o2.firstStart) {
1
} else if (o1.firstStart == o2.firstStart) {
if (o1.firstEnd < o2.firstEnd) {
1
} else if (o1.firstEnd == o2.firstEnd) {
if (o1.secondStart < o2.secondStart) {
1
} else if (o1.secondStart == o2.secondStart) {
if (o1.secondEnd < o2.secondEnd) {
1
} else if (o1.secondEnd == o2.secondEnd) {
0
} else {
-1
}
} else {
-1
}
} else {
-1
}
} else {
-1
}
}
}

0 comments on commit ab08c69

Please sign in to comment.