Upgrade to Pro — share decks privately, control downloads, hide ads and more …

scalafmtについて調べてみた

norinori3
October 24, 2018

 scalafmtについて調べてみた

Scala関西勉強会 - Summit直前スペシャルでの発表資料。

https://connpass.com/event/103702/

norinori3

October 24, 2018
Tweet

Other Decks in Research

Transcript

  1. Scalafmt ઃఆ ͳΜͱͳ͘ Θ͔Δͷ΋ ͋Δ͚Ͳɺɺ ͳΜͧɾɾ?? ↓↓↓ ͜ΕΒʹ͍ͭͯ ௐ΂ͨ͜ͱ" Λઆ໌͍͖ͯ͠·͢ɻ

    # See http://scalameta.org/scalafmt/#Configuration style = defaultWithAlign maxColumn = 130 includeCurlyBraceInSelectChains = false newlines.penalizeSingleSelectMultiArgList = false align = most align.tokens = [ {code = "<-"}, {code = "="}, {code = ":"}, {code = "->"}, {code = "=>"} ] align.openParenCallSite = false rewrite.rules = [ SortImports ] project.git = true project.excludeFilters = ["target/"] 1 2 3 4 5 6 7
  2. ͪͳΈʹɺɺɺ • addSbtPlugin("com.geirsson" % "sbt-scalafmt" % “1.5.1") ࢖ͬͯ· ͢ɻ •

    ެࣜΛݟΔͱɺʮ͍͔ͭ͘ʯͷઃఆํ๏ͱޮՌ͸ॻ͍ͯ·͢ɻ • http://scalameta.org/scalafmt/#Configuration • ͨͩɺͦͷઆ໌ͷதʹ͸ɺҎԼͷΑ͏ʹ͋Δɻ • To find all available configuration options, it's best to browse the source code of Scalafmt. A good place to start is ScalafmtConfig. Observe that this listing below is the top- level, there are more configuration options if you visited nested fields like spaces and newlines. • ιʔείʔυݟΖɻ • IntelliJͷPluginΛೖΕΔ͜ͱͰɺCtrl+Shift+l ͰscalafmtͰίʔυϑΥʔ Ϛοτ͔͚Ε·͢ɻ • build.sbtʹ scalafmtOnCompile := true Λॻ͘͜ͱͰɺίϯύΠϧ࣌ʹࣗ ಈతʹίʔυϑΥʔϚοτ͔͚Ε·͢ɻ
  3. “True”ͷ৔߹: List(1) .map { x => x + 2 }

    .filter(_ > 2) includeCurlyBraceInSelectChains “False”ͷ৔߹: List(1).map { x => x + 2 }.filter(_ > 2) style mapͳͲʹ࢖͏தׅހϒϩοΫͷ ѻ͍ํΛܾΊΔɻ 1 2 ઃఆ஋: default defaultWithAlign Scala.js IntelliJ ͍͍ײ͡ͷઃఆΛͯ͘͠ΕΔ΋ͷ? ެࣜαΠτʹ͕ͬͭΓͨ͠આ໌ͳ͠ɻίʔυΛݟΔ͔͠ͳ͍ɾɾɻ
  4. org.scalafmt.config.Newlines.scalaʹ͋Δઆ໌ * @param penalizeSingleSelectMultiArgList * If true, adds a penalty

    to newlines before a dot starting a select * chain of length one and argument list. The penalty matches the number * of arguments to the select chain application. * {{{ * // If true, favor * logger.elem(a, * b, * c) * // instead of * logger * .elem(a, b, c) * * // penalty is proportional to argument count, example: * logger.elem(a, b, c) // penalty 2 * logger.elem(a, b, c, d) // penalty 3, etc. * }}} * * If false, matches pre-v0.5 behavior. Note. this option may be * removed in a future release. 3 ެࣜαΠτʹ͕ͬͭΓͨ͠આ໌ͳ͠ɻ trueͷ৔߹͸ɺҾ਺͕ෳ਺͋Δͱ͖ʹվߦͯ͠੔ྻͤ͞Δ penalizeSingleSelectMultiArgList
  5. align “none”ͷ৔߹: x match { case 2 => 22 case

    22 => 222 } “some”ͷ৔߹(default): x match { case 2 => 22 case 22 => 222 } val x = 2 val xx = 22 case object B extends A case object BB extends A “more”ͷ৔߹: x match { case 1 => 1 -> 2 case 11 => 11 -> 22 } val x = 2 val xx = 22 case object B extends A case object BB extends A for { x <- List(1) yyy <- List(2) zzz <- new Integer { def value = 3 } } yield x * yyy “most”ͷ৔߹: x match { case 1 => 1 -> 2 case 11 => 11 -> 22 } val x = 2 val xx = 22 case object B extends A case object BB extends A for { x <- List(1) yyy <- List(2) zzz <- new Integer { def value = 3 } } yield x * yyy ΞϥΠϝϯτ(੔ྻ)ܥʹ͍ܾͭͯΊΔɻ 4
  6. x match { case 1 => 1 -> 2 case

    11 => 11 -> 22 } align.tokens = [ {code = "=>", owner = “Case"} ] ͱࢦఆͨ͠৔߹… align.openParenCallSite trueͷ৔߹: function(arg1, arg2, arg3) falseͷ৔߹: function( arg1, arg2, arg3 ) align.tokens ੔ྻͤ͞Δه߸ΛܾΊΔɻ ownerΛࢦఆ͠ͳ͚Ε͹શͯͷOwnerʹద༻͞ΕΔɻ খׅހ಺ͷཁૉͷ ੔ྻΛܾΊΔɻ 5 6 7 rewrite.rules ࢦఆͨ͠ϧʔϧͰஔ͖׵͑ͯ͘͠ΕΔ SortImports ࢦఆͨ͠৔߹: import foo.{bar, sand, Random, Zilch} ࢦఆ͠ͳ͍৔߹: import foo.{Zilch, bar, Random, sand}
  7. # See http://scalameta.org/scalafmt/#Configuration style = defaultWithAlign maxColumn = 130 includeCurlyBraceInSelectChains

    = false newlines.penalizeSingleSelectMultiArgList = false align = most align.tokens = [ {code = "<-"}, {code = "="}, {code = ":"}, {code = "->"}, {code = "=>"} ] align.openParenCallSite = false rewrite.rules = [ SortImports ] project.git = true project.excludeFilters = ["target/"] 4 5 →githubͷissueʹͷͬͯΔ͔΋ɻɻ align͸Ͳ͏ઃఆ͢Ε͹͍͍ͷ͔..·ͩௐ΂ͯ·ͤΜɻɻ