Skip to content

Commit

Permalink
Hide irrelevant code snippet fragments in scaladoc
Browse files Browse the repository at this point in the history
  • Loading branch information
KacperFKorban authored and pikinier20 committed Apr 28, 2021
1 parent 9b4ac4b commit 57722c8
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 32 deletions.
7 changes: 6 additions & 1 deletion scaladoc-js/resources/scaladoc-searchbar.css
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,10 @@

.pull-right {
float: right;
margin-left: auto
margin-left: auto;
}

.hide-snippet-comments-button {
position: absolute;
left: 90%;
}
30 changes: 25 additions & 5 deletions scaladoc-js/src/searchbar/code-snippets/CodeSnippets.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,34 @@ import org.scalajs.dom._
import org.scalajs.dom.ext._

class CodeSnippets:
val replacePatternsAndResults = Seq(
// ("", ""), // hide block directives
("""(\/\/.*)(\n|\$)""", ""), // single line comment
// ("", ""), // multi line comment
val replacePatternsAndResults: Seq[(String, String)] = Seq(
"""(\/\/{{\n)((.|\n)*?)(\/\/}}\n)""" -> """<span class="hideable">$2</span>""", // wrap content of block directives
"""(\/\/.*?)(\n|\$)""" -> """<span class="hideable">$1</span>$2""", // wrap single line comment
"""(\/\*)((.|\n)*?)(\*\/)""" -> """<span class="hideable">$0</span>""", // wrap multi line comment
)

document.querySelectorAll("code").foreach {
case e: html.Element => e.innerHTML = replacePatternsAndResults.foldLeft(e.innerHTML) {
case (acc, (pattern, result)) => acc.replaceAll(pattern, """<span class="hideable;">$1</span>""")
case (acc, (pattern, result)) =>
acc.replaceAll(pattern, result)
}
}

def toggleHide(e: html.Element | html.Document) = e.querySelectorAll("code span.hideable").foreach {
case e: html.Element if e.style.getPropertyValue("display").isEmpty => e.style.setProperty("display", "none")
case e: html.Element => e.style.removeProperty("display")
}

toggleHide(document)

document.querySelectorAll("pre").foreach {
case e: html.Element =>
val a = document.createElement("a")
a.textContent = "Show"
a.addEventListener("click", { (_: MouseEvent) =>
a.textContent = if a.textContent == "Show" then "Hide" else "Show"
toggleHide(e)
})
a.classList.add("hide-snippet-comments-button")
e.insertBefore(a, e.firstChild)
}
22 changes: 0 additions & 22 deletions scaladoc-testcases/src/tests/methodsAndConstructors.scala
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
package tests.methodsAndConstructors


/**
* This is my codeblock
*
* ```
* //{{
* import xd
* import xd2
* //}}
*
*
* val x = 1 // This is my valid comment
*
* /*
* Is this my comment?
* */
*
* val y = 2
* ```
*
* The end of my codeblock
*/
class A
class B extends A
class C
Expand Down
31 changes: 31 additions & 0 deletions scaladoc-testcases/src/tests/snippetComments.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tests.snippetComments


/**
* This is my codeblock
*
* ```
* //{{
* import xd
* import xd2
* //}}
*
*
* val x = 1 // This is my valid comment
*
* /*
* multi line comment
* */
*
* val y = 2 // comment in the same line
* // comment in new line
* val z = 3
*
* //{{
* val hideMe = 7
* //}}
* ```
*
* The end of my codeblock
*/
class A
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,7 @@ trait MemberLookup {
}

if owner.isPackageDef then
findMatch(hackMembersOf(owner).flatMap {
s =>
(if s.name.endsWith("package$") then hackMembersOf(s) else Iterator.empty) ++ Iterator(s)
})
findMatch(hackMembersOf(owner))
else
owner.tree match {
case tree: TypeDef =>
Expand Down

0 comments on commit 57722c8

Please sign in to comment.