forked from scalameta/scalafmt
/
Readme.scala
224 lines (189 loc) · 6.59 KB
/
Readme.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package org.scalafmt.readme
import scalatags.Text.TypedTag
import scalatags.Text.all._
import java.text.SimpleDateFormat
import java.util.Date
import com.twitter.util.Eval
import org.scalafmt.Scalafmt
import org.scalafmt.cli.Cli
import org.scalafmt.cli.CliArgParser
import org.scalafmt.config._
import org.scalafmt.rewrite._
object hl extends scalatex.site.Highlighter
object Readme {
def gitter = a(
href := "https://gitter.im/scalameta/scalafmt",
img(
src := "https://camo.githubusercontent.com/da2edb525cde1455a622c58c0effc3a90b9a181c/68747470733a2f2f6261646765732e6769747465722e696d2f4a6f696e253230436861742e737667",
alt := "Join the chat at https://gitter.im/scalameta/scalameta",
maxWidth := "100%;"
)
)
val eval = new Eval()
implicit def bool2frag(boolean: Boolean): StringFrag =
stringFrag(boolean.toString)
def flag(str: String) = {
println(str)
Config.fromHoconString(str).get
code(str)
}
/**
* repl session, inspired by tut.
*
* Example: code="1 + 1" returns
* "scala> 1 + 1
* res0: Int = 2"
*/
def repl(code: String) = {
import scala.meta._
val expressions = s"{$code}".parse[Stat].get.asInstanceOf[Term.Block].stats
val evaluated = eval[Any](code)
val output = evaluated match {
case s: String =>
s"""
|"$s"""".stripMargin
case x => x.toString
}
val result = s"""${expressions
.map(x => s"scala> ${x.toString().trim}")
.mkString("\n")}
|res0: ${evaluated.getClass.getName} = $output
|""".stripMargin
hl.scala(result)
}
def config(frags: Frag*) = {
cliFlags(frags.render)
}
def cliFlags(flags: String) = {
Config.fromHoconString(flags).get
hl.scala(flags)
}
def note = b("NOTE")
def github: String = "https://github.com"
def repo: String = "https://github.com/scalameta/scalafmt"
def gitRepo: String = repo + ".git"
def user(name: String) = a(href := s"$github/$name", s"@$name")
def users(names: String*) =
span(
names.dropRight(1).map(x => span(user(x), ", ")) :+ user(names.last): _*
)
def pr(id: Int) = a(href := repo + s"/pull/$id", s"#$id")
def issue(id: Int) = a(href := repo + s"/issues/$id", s"#$id")
def issues(ids: Int*) = span(ids.map(issue): _*)
def half(frags: Frag*) = div(frags, width := "50%", float.left)
def ignore(frags: Frag*) = span("")
def pairs(frags: Frag*) = div(frags, div(clear := "both"))
def sideBySide(left: String, right: String) =
pairs(List(left, right).map(x => half(hl.scala(x))): _*)
def demo(code: String) = {
val formatted = Scalafmt.format(code, ScalafmtConfig.default40).get
sideBySide(code, formatted)
}
def demoStyle(style: ScalafmtConfig)(code: String) = {
val formatted =
Scalafmt.format(code, style.copy(runner = ScalafmtRunner.sbt)).get
sideBySide(code, formatted)
}
def example(code: String): TypedTag[String] = {
example(code, ScalafmtConfig.default40)
}
def exampleAlign(code: String): TypedTag[String] = {
val formatted = Scalafmt
.format(
code,
ScalafmtConfig.default40.copy(
align =
ScalafmtConfig.default40.align.copy(tokens = AlignToken.default)))
.get
hl.scala(formatted)
}
val stripMarginStyle =
ScalafmtConfig.default.copy(assumeStandardLibraryStripMargin = true)
val rewriteInfix =
ScalafmtConfig.default.copy(
rewrite = ScalafmtConfig.default.rewrite.copy(
rules = Seq(AvoidInfix)
))
val rewriteImportSelectors =
ScalafmtConfig.default.copy(
rewrite = ScalafmtConfig.default.rewrite.copy(
rules = Seq(ExpandImportSelectors)
))
val rewriteBraces =
ScalafmtConfig.default.copy(
rewrite = ScalafmtConfig.default.rewrite.copy(
redundantBraces = ScalafmtConfig.default.rewrite.redundantBraces.copy(
stringInterpolation = true
),
rules = Seq(RedundantBraces)
))
val rewriteParens =
ScalafmtConfig.default.copy(
rewrite = ScalafmtConfig.default.rewrite.copy(
rules = Seq(RedundantParens)
))
val rewriteImports =
ScalafmtConfig.default.copy(
rewrite = ScalafmtConfig.default.rewrite.copy(
rules = Seq(SortImports)
))
val rewriteAsciiImports =
ScalafmtConfig.default.copy(
rewrite = ScalafmtConfig.default.rewrite.copy(
rules = Seq(AsciiSortImports)
))
val rewriteSortModifiers =
ScalafmtConfig.default120.copy(
rewrite = ScalafmtConfig.default.rewrite.copy(
rules = Seq(SortModifiers)
))
/**
* This looks way too hacky. But can't seem to find a typeclass
* that ought to "encode" the ``ModKey`` enum.
*
* Additionally, a Vector of Strings is simply concatenated, hence
* the extra .mkString.
* {{{
* [error] found : Vector[org.scalafmt.config.SortSettings.ModKey]
* [error] required: scalatags.Text.Modifier
* [error] (which expands to) scalatags.generic.Modifier[scalatags.text.Builder]
* [error] @code{rewrite.sortModifiers.order} = @rewriteSortModifiers.rewrite.sortModifiers.order
* }}}
*/
val rewriteSortModifiersDefaultString =
SortSettings.defaultOrder
.map(_.productPrefix)
.mkString("[\"", "\", \"", "\"]")
val rewritePreferCurlyFors =
ScalafmtConfig.default.copy(
rewrite = ScalafmtConfig.default.rewrite.copy(
rules = Seq(PreferCurlyFors)
))
def fmt(style: ScalafmtConfig)(code: String): TypedTag[String] =
example(code, style)
def lastUpdated =
new SimpleDateFormat("MMM d, y").format(new Date(CliArgParser.buildTimeMs))
def format(code: String): TypedTag[String] = {
format(ScalafmtConfig.default)(code)
}
val alignNone = ScalafmtConfig.default.copy(align = Align.none)
val alignSome = ScalafmtConfig.default.copy(align = Align.some)
val alignMore = ScalafmtConfig.default.copy(align = Align.more)
val alignMost = ScalafmtConfig.default.copy(align = Align.most)
val alignCaseArrow = ScalafmtConfig.default
val alignArrowEnum = ScalafmtConfig.defaultWithAlign.copy(
align = Align.default.copy(arrowEnumeratorGenerator = true))
val alignModuleId = ScalafmtConfig.defaultWithAlign
def format(style: ScalafmtConfig)(code: String): TypedTag[String] = {
example(code, style.copy(runner = ScalafmtRunner.sbt))
}
def example(code: String, style: ScalafmtConfig): TypedTag[String] = {
val formatted = Scalafmt.format(code, style).get
hl.scala(formatted)
}
def image(url: String) = img(
src := url,
width := "100%",
textAlign := "center"
)
}