Skip to content

Scaladoc/hide snippets comments #11822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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%;
}
1 change: 1 addition & 0 deletions scaladoc-js/src/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package dotty.tools.scaladoc
object Main extends App {
Searchbar()
SocialLinks()
CodeSnippets()
}
37 changes: 37 additions & 0 deletions scaladoc-js/src/searchbar/code-snippets/CodeSnippets.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dotty.tools.scaladoc

import org.scalajs.dom._
import org.scalajs.dom.ext._

class CodeSnippets:
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, 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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be misleading: just "Show" and just "Hide" may indicate that it applies to the whole snippet. I like the way it was done in Dokka - there was "+" and "-" which enlarged and reduced snippet. Maybe you will invent sth better

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right about misleading texts. If we want to stick to the word names rather than some symbols, maybe Expand and Contract. Anyway we could think of some more modern design.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, here is how dokka handles it
obraz

obraz

toggleHide(e)
})
a.classList.add("hide-snippet-comments-button")
e.insertBefore(a, e.firstChild)
}
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