Skip to content

Commit

Permalink
Consider project specific formatting settings for auto format
Browse files Browse the repository at this point in the history
A fall back to the workspace wide formatting settings happens if the
project settings couldn't be accessed.

Fixes #1002331
  • Loading branch information
kiritsuku committed Nov 3, 2014
1 parent a7c06b2 commit d0f6a61
Showing 1 changed file with 11 additions and 4 deletions.
Expand Up @@ -4,14 +4,19 @@ package saveactions
import org.scalaide.core.IScalaPlugin
import org.scalaide.core.internal.formatter.FormatterPreferences
import org.scalaide.core.text.Replace
import org.scalaide.util.internal.eclipse.ProjectUtils

import scalariform.formatter.ScalaFormatter
import scalariform.utils.TextEdit

object AutoFormattingSetting extends SaveActionSetting(
id = ExtensionSetting.fullyQualifiedName[AutoFormatting],
name = "Auto formatting",
description = "Formats the entire document based on the configuration options in \"Scala > Editor > Formatter\".",
description =
"""|Formats the entire document based on the configuration options in \
|"Scala > Editor > Formatter". If project specific formatting is enabled, \
|these formatting options are considered instead.
|""".stripMargin.replaceAll("\\\\\n", ""),
codeExample = """|class C {
| def f = {
| val a=0
Expand All @@ -27,9 +32,11 @@ trait AutoFormatting extends SaveAction with DocumentSupport {
override def setting = AutoFormattingSetting

override def perform() = {
val formatted = ScalaFormatter.formatAsEdits(
document.text,
FormatterPreferences.getPreferences(IScalaPlugin().getPreferenceStore()))
val prefs = ProjectUtils.resourceOfSelection().map {
r => FormatterPreferences.getPreferences(r.getProject)
}.getOrElse(FormatterPreferences.getPreferences(IScalaPlugin().getPreferenceStore()))

val formatted = ScalaFormatter.formatAsEdits(document.text, prefs)
formatted map { case TextEdit(pos, len, text) => Replace(pos, pos+len, text) }
}
}

0 comments on commit d0f6a61

Please sign in to comment.