Skip to content

Commit 5ac62a1

Browse files
authored
Merge pull request #11582 from pikinier20/snippet-compiler
Snippet compiler
2 parents 833f5b9 + 946752f commit 5ac62a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1402
-63
lines changed

compiler/src/dotty/tools/dotc/reporting/StoreReporter.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class StoreReporter(outer: Reporter = Reporter.NoReporter) extends Reporter {
3737
if (infos != null) try infos.toList finally infos = null
3838
else Nil
3939

40-
override def pendingMessages(using Context): List[Diagnostic] = infos.toList
40+
override def pendingMessages(using Context): List[Diagnostic] = if (infos != null) infos.toList else Nil
4141

4242
override def errorsReported: Boolean = hasErrors || (outer != null && outer.errorsReported)
4343
}

dist/bin/scaladoc

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ eval "\"$JAVACMD\"" \
127127
${JAVA_OPTS:-$default_java_opts} \
128128
"${java_args[@]}" \
129129
"${jvm_cp_args-}" \
130+
-Dscala.usejavacp=true \
130131
"dotty.tools.scaladoc.Main" \
131132
"${scala_args[@]}" \
132133
"${residual_args[@]}" \

docs/css/dottydoc.css

-12
Original file line numberDiff line numberDiff line change
@@ -180,18 +180,6 @@ h5:hover a.anchor:hover {
180180
font-family: var(--font-family-monospace);
181181
}
182182

183-
/* code */
184-
pre, code {
185-
font-variant-ligatures: none;
186-
}
187-
pre {
188-
padding: 0;
189-
font-size: 13px;
190-
background: var(--pre-bg);
191-
border-radius: 2px;
192-
border: 1px solid rgba(0, 0, 0, 0.1);
193-
}
194-
195183
/* admonitions */
196184
blockquote {
197185
padding: 0 1em;

project/Build.scala

+22-5
Original file line numberDiff line numberDiff line change
@@ -1243,16 +1243,19 @@ object Build {
12431243
libraryDependencies += ("org.scala-js" %%% "scalajs-dom" % "1.1.0").cross(CrossVersion.for3Use2_13)
12441244
)
12451245

1246-
def generateDocumentation(targets: Seq[String], name: String, outDir: String, ref: String, params: Seq[String] = Nil) =
1246+
def generateDocumentation(targets: Seq[String], name: String, outDir: String, ref: String, params: Seq[String] = Nil, usingScript: Boolean = true) =
12471247
Def.taskDyn {
12481248
val distLocation = (dist / pack).value
12491249
val projectVersion = version.value
12501250
IO.createDirectory(file(outDir))
12511251
val stdLibVersion = stdlibVersion(Bootstrapped)
1252+
val scalaLib = findArtifactPath(externalCompilerClasspathTask.value, "scala-library")
1253+
val dottyLib = (`scala3-library` / Compile / classDirectory).value
12521254
// TODO add versions etc.
12531255
def srcManaged(v: String, s: String) = s"out/bootstrap/stdlib-bootstrapped/scala-$v/src_managed/main/$s-library-src"
12541256
def scalaSrcLink(v: String, s: String) = s"-source-links:$s=github://scala/scala/v$v#src/library"
12551257
def dottySrcLink(v: String, s: String) = s"-source-links:$s=github://lampepfl/dotty/$v#library/src"
1258+
12561259
val revision = Seq("-revision", ref, "-project-version", projectVersion)
12571260
val cmd = Seq(
12581261
"-d",
@@ -1264,7 +1267,20 @@ object Build {
12641267
s"-source-links:github://lampepfl/dotty/$referenceVersion",
12651268
) ++ scalacOptionsDocSettings ++ revision ++ params ++ targets
12661269
import _root_.scala.sys.process._
1267-
Def.task((s"$distLocation/bin/scaladoc" +: cmd).!)
1270+
if (usingScript)
1271+
Def.task((s"$distLocation/bin/scaladoc" +: cmd).!)
1272+
else {
1273+
val escapedCmd = cmd.map(arg => if(arg.contains(" ")) s""""$arg"""" else arg)
1274+
Def.task {
1275+
try {
1276+
(Compile / run).toTask(escapedCmd.mkString(" ", " ", "")).value
1277+
0
1278+
} catch {
1279+
case _ : Throwable => 1
1280+
}
1281+
}
1282+
}
1283+
12681284
}
12691285

12701286
val SourceLinksIntegrationTest = config("sourceLinksIntegrationTest") extend Test
@@ -1311,7 +1327,7 @@ object Build {
13111327
generateSelfDocumentation := Def.taskDyn {
13121328
generateDocumentation(
13131329
(Compile / classDirectory).value.getAbsolutePath :: Nil,
1314-
"scaladoc", "scaladoc/output/self", VersionUtil.gitHash,
1330+
"scaladoc", "scaladoc/output/self", VersionUtil.gitHash
13151331
)
13161332
}.value,
13171333
generateScalaDocumentation := Def.inputTaskDyn {
@@ -1352,7 +1368,7 @@ object Build {
13521368
s"-source-links:docs=github://lampepfl/dotty/master#docs",
13531369
"-doc-root-content", docRootFile.toString,
13541370
"-Ydocument-synthetic-types"
1355-
)
1371+
), usingScript = false
13561372
))
13571373
}.evaluated,
13581374

@@ -1361,7 +1377,8 @@ object Build {
13611377
(Test / Build.testcasesOutputDir).value,
13621378
"scaladoc testcases",
13631379
"scaladoc/output/testcases",
1364-
"master")
1380+
"master"
1381+
)
13651382
}.value,
13661383

13671384
Test / buildInfoKeys := Seq[BuildInfoKey](

scaladoc-js/resources/scaladoc-searchbar.css

+34-1
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,38 @@
100100

101101
.pull-right {
102102
float: right;
103-
margin-left: auto
103+
margin-left: auto;
104104
}
105+
106+
.snippet-comment-button {
107+
position: absolute;
108+
display: inline-block;
109+
left: 50%;
110+
width: 24px;
111+
height: 24px;
112+
background:
113+
linear-gradient(#fff, #fff),
114+
linear-gradient(#fff, #fff),
115+
#aaa;
116+
background-position: center;
117+
background-size: 50% 2px, 2px 50%;
118+
background-repeat: no-repeat;
119+
border-radius: 12px;
120+
box-shadow: 0 0 2px black;
121+
}
122+
123+
.snippet-comment-button:hover {
124+
background:
125+
linear-gradient(#444, #444),
126+
linear-gradient(#444, #444),
127+
#ddd;
128+
background-position: center;
129+
background-size: 50% 2px, 2px 50%;
130+
background-repeat: no-repeat;
131+
}
132+
133+
.hide-snippet-comments-button {
134+
-ms-transform: rotate(45deg);
135+
transform: rotate(45deg);
136+
}
137+

scaladoc-js/src/Main.scala

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ package dotty.tools.scaladoc
33
object Main extends App {
44
Searchbar()
55
SocialLinks()
6+
CodeSnippets()
67
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package dotty.tools.scaladoc
2+
3+
import org.scalajs.dom._
4+
import org.scalajs.dom.ext._
5+
6+
class CodeSnippets:
7+
def toggleHide(e: html.Element | html.Document) = e.querySelectorAll("code span.hideable").foreach {
8+
case e: html.Element if e.style.getPropertyValue("display").isEmpty => e.style.setProperty("display", "none")
9+
case e: html.Element => e.style.removeProperty("display")
10+
}
11+
12+
toggleHide(document)
13+
14+
document.querySelectorAll("pre").foreach {
15+
case e: html.Element if e.querySelectorAll("code span.hideable").nonEmpty =>
16+
val a = document.createElement("a")
17+
a.addEventListener("click", { (_: MouseEvent) =>
18+
if(a.classList.contains("hide-snippet-comments-button")) {
19+
a.classList.remove("hide-snippet-comments-button")
20+
} else {
21+
a.classList.add("hide-snippet-comments-button")
22+
}
23+
toggleHide(e)
24+
})
25+
a.classList.add("snippet-comment-button")
26+
e.insertBefore(a, e.firstChild)
27+
}

scaladoc-testcases/docs/docs/index.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
3+
4+
---
5+
6+
```scala sc:compile
7+
2 + List(0)
8+
```
9+
10+
```scala sc:compile
11+
new snippetCompiler.Snippet0 { }
12+
```
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package tests.snippetComments
2+
3+
4+
/**
5+
* This is my codeblock
6+
*
7+
* ```
8+
* //{{
9+
* import xd
10+
* import xd2
11+
* //}}
12+
*
13+
*
14+
* val x = 1 // This is my valid comment
15+
*
16+
* /*
17+
* multi line comment
18+
* */
19+
* val reallyFLongDeclaration = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
20+
* val y = 2 // comment in the same line
21+
* // comment in new line
22+
* val z = 3
23+
*
24+
* //{{
25+
* val hideMe = 7
26+
* //}}
27+
* ```
28+
*
29+
* The end of my codeblock
30+
*/
31+
class A
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package tests
2+
package snippetCompilerTests
3+
4+
/**
5+
* ```scala sc:compile
6+
* //{
7+
* import scala.collection.Seq
8+
* //}
9+
*
10+
* def a = 2
11+
* val x = 1 + List()
12+
* a
13+
*
14+
* try {
15+
* 2+3
16+
* }
17+
*
18+
* /*
19+
* Huge comment
20+
* */
21+
* val xd: String = 42
22+
*
23+
* def a(i: Int): Boolean = i match // This is a function
24+
* case 1 => true
25+
*
26+
* val b: Int = 2.3 /* Also quite a big comment */
27+
*
28+
* val d: Long = "asd"
29+
* ```
30+
*
31+
* ```scala sc:fail
32+
* def a = 2
33+
* val x = 1 + List()
34+
* a
35+
* ```
36+
*
37+
* ```scala sc:fail
38+
* def a = 2
39+
* ```
40+
*
41+
* ```scala sc:nocompile
42+
* def a = 3
43+
* a()
44+
* ```
45+
*/
46+
class A {
47+
trait B
48+
}
49+
50+
/**
51+
* ```scala sc:compile
52+
* val c: Int = 4.5
53+
* ```
54+
*/
55+
class B { }
56+
57+
trait Quotes {
58+
val reflect: reflectModule = ???
59+
trait reflectModule { self: reflect.type =>
60+
/**
61+
* ```scala sc:compile
62+
* 2 + List()
63+
* ```
64+
*
65+
*/
66+
def a = 3
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package tests.snippetTestcase1
2+
3+
class SnippetTestcase1:
4+
/**
5+
* SNIPPET(OUTERLINEOFFSET:8,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:3,INNERCOLUMNOFFSET:2)
6+
* ERROR(LINE:8,COLUMN:8)
7+
* ```scala sc:compile
8+
* 2 + List()
9+
* ```
10+
*
11+
*/
12+
def a = 3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package tests
2+
package snippetTestcase2
3+
4+
trait Quotes2[A] {
5+
val r1: r1Module[_] = ???
6+
trait r1Module[A] {
7+
type X
8+
object Y {
9+
/**
10+
* SNIPPET(OUTERLINEOFFSET:13,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:6,INNERCOLUMNOFFSET:6)
11+
* ERROR(LINE:13,COLUMN:12)
12+
* ```scala sc:compile
13+
* 2 + List()
14+
* ```
15+
*
16+
*/
17+
type YY
18+
}
19+
val z: zModule = ???
20+
trait zModule {
21+
/**
22+
* SNIPPET(OUTERLINEOFFSET:25,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:7,INNERCOLUMNOFFSET:6)
23+
* ERROR(LINE:25,COLUMN:12)
24+
* ```scala sc:compile
25+
* 2 + List()
26+
* ```
27+
*
28+
*/
29+
type ZZ
30+
}
31+
}
32+
object r2 {
33+
type X
34+
object Y {
35+
/**
36+
* SNIPPET(OUTERLINEOFFSET:39,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:5,INNERCOLUMNOFFSET:6)
37+
* ERROR(LINE:39,COLUMN:12)
38+
* ```scala sc:compile
39+
* 2 + List()
40+
* ```
41+
*
42+
*/
43+
type YY
44+
}
45+
val z: zModule = ???
46+
trait zModule {
47+
/**
48+
* SNIPPET(OUTERLINEOFFSET:51,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:6,INNERCOLUMNOFFSET:6)
49+
* ERROR(LINE:51,COLUMN:12)
50+
* ```scala sc:compile
51+
* 2 + List()
52+
* ```
53+
*
54+
*/
55+
type ZZ
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)