Skip to content

Commit 3fb4b20

Browse files
committed
Filter out sampleStart/End from Knit-generated source files
1 parent 1f4d71d commit 3fb4b20

Some content is hidden

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

51 files changed

+8
-103
lines changed

knit/src/Knit.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ const val INDEX_DIRECTIVE = "INDEX"
3636
const val CODE_START = "```kotlin"
3737
const val CODE_END = "```"
3838

39+
const val SAMPLE_START = "//sampleStart"
40+
const val SAMPLE_END = "//sampleEnd"
41+
3942
const val TEST_START = "```text"
4043
const val TEST_END = "```"
4144

@@ -183,7 +186,9 @@ fun knit(markdownFile: File): Boolean {
183186
if (inLine.startsWith(CODE_START)) {
184187
require(testOut == null || testLines.isEmpty()) { "Previous test was not emitted with $TEST_DIRECTIVE" }
185188
codeLines += ""
186-
readUntilTo(CODE_END, codeLines)
189+
readUntilTo(CODE_END, codeLines) { line ->
190+
!line.startsWith(SAMPLE_START) && !line.startsWith(SAMPLE_END)
191+
}
187192
continue@mainLoop
188193
}
189194
if (inLine.startsWith(TEST_START)) {
@@ -328,11 +333,11 @@ private fun flushTestOut(parentDir: File?, testOut: String?, testOutLines: Mutab
328333
private fun MarkdownTextReader.readUntil(marker: String): List<String> =
329334
arrayListOf<String>().also { readUntilTo(marker, it) }
330335

331-
private fun MarkdownTextReader.readUntilTo(marker: String, list: MutableList<String>) {
336+
private fun MarkdownTextReader.readUntilTo(marker: String, list: MutableList<String>, linePredicate: (String) -> Boolean = { true }) {
332337
while (true) {
333338
val line = readLine() ?: break
334339
if (line.startsWith(marker)) break
335-
list += line
340+
if (linePredicate(line)) list += line
336341
}
337342
}
338343

kotlinx-coroutines-core/jvm/test/guide/example-basic-03.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ package kotlinx.coroutines.guide.basic03
88
import kotlinx.coroutines.*
99

1010
fun main() = runBlocking {
11-
//sampleStart
1211
val job = GlobalScope.launch { // launch a new coroutine and keep a reference to its Job
1312
delay(1000L)
1413
println("World!")
1514
}
1615
println("Hello,")
1716
job.join() // wait until child coroutine completes
18-
//sampleEnd
1917
}

kotlinx-coroutines-core/jvm/test/guide/example-basic-07.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ package kotlinx.coroutines.guide.basic07
88
import kotlinx.coroutines.*
99

1010
fun main() = runBlocking {
11-
//sampleStart
1211
GlobalScope.launch {
1312
repeat(1000) { i ->
1413
println("I'm sleeping $i ...")
1514
delay(500L)
1615
}
1716
}
1817
delay(1300L) // just quit after delay
19-
//sampleEnd
2018
}

kotlinx-coroutines-core/jvm/test/guide/example-cancel-01.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package kotlinx.coroutines.guide.cancel01
88
import kotlinx.coroutines.*
99

1010
fun main() = runBlocking {
11-
//sampleStart
1211
val job = launch {
1312
repeat(1000) { i ->
1413
println("job: I'm sleeping $i ...")
@@ -20,5 +19,4 @@ fun main() = runBlocking {
2019
job.cancel() // cancels the job
2120
job.join() // waits for job's completion
2221
println("main: Now I can quit.")
23-
//sampleEnd
2422
}

kotlinx-coroutines-core/jvm/test/guide/example-cancel-02.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package kotlinx.coroutines.guide.cancel02
88
import kotlinx.coroutines.*
99

1010
fun main() = runBlocking {
11-
//sampleStart
1211
val startTime = currentTimeMillis()
1312
val job = launch(Dispatchers.Default) {
1413
var nextPrintTime = startTime
@@ -25,5 +24,4 @@ fun main() = runBlocking {
2524
println("main: I'm tired of waiting!")
2625
job.cancelAndJoin() // cancels the job and waits for its completion
2726
println("main: Now I can quit.")
28-
//sampleEnd
2927
}

kotlinx-coroutines-core/jvm/test/guide/example-cancel-03.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package kotlinx.coroutines.guide.cancel03
88
import kotlinx.coroutines.*
99

1010
fun main() = runBlocking {
11-
//sampleStart
1211
val startTime = currentTimeMillis()
1312
val job = launch(Dispatchers.Default) {
1413
var nextPrintTime = startTime
@@ -25,5 +24,4 @@ fun main() = runBlocking {
2524
println("main: I'm tired of waiting!")
2625
job.cancelAndJoin() // cancels the job and waits for its completion
2726
println("main: Now I can quit.")
28-
//sampleEnd
2927
}

kotlinx-coroutines-core/jvm/test/guide/example-cancel-04.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package kotlinx.coroutines.guide.cancel04
88
import kotlinx.coroutines.*
99

1010
fun main() = runBlocking {
11-
//sampleStart
1211
val job = launch {
1312
try {
1413
repeat(1000) { i ->
@@ -23,5 +22,4 @@ fun main() = runBlocking {
2322
println("main: I'm tired of waiting!")
2423
job.cancelAndJoin() // cancels the job and waits for its completion
2524
println("main: Now I can quit.")
26-
//sampleEnd
2725
}

kotlinx-coroutines-core/jvm/test/guide/example-cancel-05.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package kotlinx.coroutines.guide.cancel05
88
import kotlinx.coroutines.*
99

1010
fun main() = runBlocking {
11-
//sampleStart
1211
val job = launch {
1312
try {
1413
repeat(1000) { i ->
@@ -27,5 +26,4 @@ fun main() = runBlocking {
2726
println("main: I'm tired of waiting!")
2827
job.cancelAndJoin() // cancels the job and waits for its completion
2928
println("main: Now I can quit.")
30-
//sampleEnd
3129
}

kotlinx-coroutines-core/jvm/test/guide/example-cancel-06.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ package kotlinx.coroutines.guide.cancel06
88
import kotlinx.coroutines.*
99

1010
fun main() = runBlocking {
11-
//sampleStart
1211
withTimeout(1300L) {
1312
repeat(1000) { i ->
1413
println("I'm sleeping $i ...")
1514
delay(500L)
1615
}
1716
}
18-
//sampleEnd
1917
}

kotlinx-coroutines-core/jvm/test/guide/example-cancel-07.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package kotlinx.coroutines.guide.cancel07
88
import kotlinx.coroutines.*
99

1010
fun main() = runBlocking {
11-
//sampleStart
1211
val result = withTimeoutOrNull(1300L) {
1312
repeat(1000) { i ->
1413
println("I'm sleeping $i ...")
@@ -17,5 +16,4 @@ fun main() = runBlocking {
1716
"Done" // will get cancelled before it produces this result
1817
}
1918
println("Result is $result")
20-
//sampleEnd
2119
}

kotlinx-coroutines-core/jvm/test/guide/example-channel-01.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import kotlinx.coroutines.*
99
import kotlinx.coroutines.channels.*
1010

1111
fun main() = runBlocking {
12-
//sampleStart
1312
val channel = Channel<Int>()
1413
launch {
1514
// this might be heavy CPU-consuming computation or async logic, we'll just send five squares
@@ -18,5 +17,4 @@ fun main() = runBlocking {
1817
// here we print five received integers:
1918
repeat(5) { println(channel.receive()) }
2019
println("Done!")
21-
//sampleEnd
2220
}

kotlinx-coroutines-core/jvm/test/guide/example-channel-02.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import kotlinx.coroutines.*
99
import kotlinx.coroutines.channels.*
1010

1111
fun main() = runBlocking {
12-
//sampleStart
1312
val channel = Channel<Int>()
1413
launch {
1514
for (x in 1..5) channel.send(x * x)
@@ -18,5 +17,4 @@ fun main() = runBlocking {
1817
// here we print received values using `for` loop (until the channel is closed)
1918
for (y in channel) println(y)
2019
println("Done!")
21-
//sampleEnd
2220
}

kotlinx-coroutines-core/jvm/test/guide/example-channel-03.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ fun CoroutineScope.produceSquares(): ReceiveChannel<Int> = produce {
1313
}
1414

1515
fun main() = runBlocking {
16-
//sampleStart
1716
val squares = produceSquares()
1817
squares.consumeEach { println(it) }
1918
println("Done!")
20-
//sampleEnd
2119
}

kotlinx-coroutines-core/jvm/test/guide/example-channel-04.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ import kotlinx.coroutines.*
99
import kotlinx.coroutines.channels.*
1010

1111
fun main() = runBlocking {
12-
//sampleStart
1312
val numbers = produceNumbers() // produces integers from 1 and on
1413
val squares = square(numbers) // squares integers
1514
for (i in 1..5) println(squares.receive()) // print first five
1615
println("Done!") // we are done
1716
coroutineContext.cancelChildren() // cancel children coroutines
18-
//sampleEnd
1917
}
2018

2119
fun CoroutineScope.produceNumbers() = produce<Int> {

kotlinx-coroutines-core/jvm/test/guide/example-channel-05.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ import kotlinx.coroutines.*
99
import kotlinx.coroutines.channels.*
1010

1111
fun main() = runBlocking {
12-
//sampleStart
1312
var cur = numbersFrom(2)
1413
for (i in 1..10) {
1514
val prime = cur.receive()
1615
println(prime)
1716
cur = filter(cur, prime)
1817
}
1918
coroutineContext.cancelChildren() // cancel all children to let main finish
20-
//sampleEnd
2119
}
2220

2321
fun CoroutineScope.numbersFrom(start: Int) = produce<Int> {

kotlinx-coroutines-core/jvm/test/guide/example-channel-06.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ import kotlinx.coroutines.*
99
import kotlinx.coroutines.channels.*
1010

1111
fun main() = runBlocking<Unit> {
12-
//sampleStart
1312
val producer = produceNumbers()
1413
repeat(5) { launchProcessor(it, producer) }
1514
delay(950)
1615
producer.cancel() // cancel producer coroutine and thus kill them all
17-
//sampleEnd
1816
}
1917

2018
fun CoroutineScope.produceNumbers() = produce<Int> {

kotlinx-coroutines-core/jvm/test/guide/example-channel-07.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ import kotlinx.coroutines.*
99
import kotlinx.coroutines.channels.*
1010

1111
fun main() = runBlocking {
12-
//sampleStart
1312
val channel = Channel<String>()
1413
launch { sendString(channel, "foo", 200L) }
1514
launch { sendString(channel, "BAR!", 500L) }
1615
repeat(6) { // receive first six
1716
println(channel.receive())
1817
}
1918
coroutineContext.cancelChildren() // cancel all children to let main finish
20-
//sampleEnd
2119
}
2220

2321
suspend fun sendString(channel: SendChannel<String>, s: String, time: Long) {

kotlinx-coroutines-core/jvm/test/guide/example-channel-08.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import kotlinx.coroutines.*
99
import kotlinx.coroutines.channels.*
1010

1111
fun main() = runBlocking<Unit> {
12-
//sampleStart
1312
val channel = Channel<Int>(4) // create buffered channel
1413
val sender = launch { // launch sender coroutine
1514
repeat(10) {
@@ -20,5 +19,4 @@ fun main() = runBlocking<Unit> {
2019
// don't receive anything... just wait....
2120
delay(1000)
2221
sender.cancel() // cancel sender coroutine
23-
//sampleEnd
2422
}

kotlinx-coroutines-core/jvm/test/guide/example-channel-09.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package kotlinx.coroutines.guide.channel09
88
import kotlinx.coroutines.*
99
import kotlinx.coroutines.channels.*
1010

11-
//sampleStart
1211
data class Ball(var hits: Int)
1312

1413
fun main() = runBlocking {
@@ -28,4 +27,3 @@ suspend fun player(name: String, table: Channel<Ball>) {
2827
table.send(ball) // send the ball back
2928
}
3029
}
31-
//sampleEnd

kotlinx-coroutines-core/jvm/test/guide/example-compose-01.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ import kotlinx.coroutines.*
99
import kotlin.system.*
1010

1111
fun main() = runBlocking<Unit> {
12-
//sampleStart
1312
val time = measureTimeMillis {
1413
val one = doSomethingUsefulOne()
1514
val two = doSomethingUsefulTwo()
1615
println("The answer is ${one + two}")
1716
}
1817
println("Completed in $time ms")
19-
//sampleEnd
2018
}
2119

2220
suspend fun doSomethingUsefulOne(): Int {

kotlinx-coroutines-core/jvm/test/guide/example-compose-02.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ import kotlinx.coroutines.*
99
import kotlin.system.*
1010

1111
fun main() = runBlocking<Unit> {
12-
//sampleStart
1312
val time = measureTimeMillis {
1413
val one = async { doSomethingUsefulOne() }
1514
val two = async { doSomethingUsefulTwo() }
1615
println("The answer is ${one.await() + two.await()}")
1716
}
1817
println("Completed in $time ms")
19-
//sampleEnd
2018
}
2119

2220
suspend fun doSomethingUsefulOne(): Int {

kotlinx-coroutines-core/jvm/test/guide/example-compose-03.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import kotlinx.coroutines.*
99
import kotlin.system.*
1010

1111
fun main() = runBlocking<Unit> {
12-
//sampleStart
1312
val time = measureTimeMillis {
1413
val one = async(start = CoroutineStart.LAZY) { doSomethingUsefulOne() }
1514
val two = async(start = CoroutineStart.LAZY) { doSomethingUsefulTwo() }
@@ -19,7 +18,6 @@ fun main() = runBlocking<Unit> {
1918
println("The answer is ${one.await() + two.await()}")
2019
}
2120
println("Completed in $time ms")
22-
//sampleEnd
2321
}
2422

2523
suspend fun doSomethingUsefulOne(): Int {

kotlinx-coroutines-core/jvm/test/guide/example-compose-04.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package kotlinx.coroutines.guide.compose04
88
import kotlinx.coroutines.*
99
import kotlin.system.*
1010

11-
//sampleStart
1211
// note that we don't have `runBlocking` to the right of `main` in this example
1312
fun main() {
1413
val time = measureTimeMillis {
@@ -23,7 +22,6 @@ fun main() {
2322
}
2423
println("Completed in $time ms")
2524
}
26-
//sampleEnd
2725

2826
fun somethingUsefulOneAsync() = GlobalScope.async {
2927
doSomethingUsefulOne()

kotlinx-coroutines-core/jvm/test/guide/example-compose-05.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ import kotlinx.coroutines.*
99
import kotlin.system.*
1010

1111
fun main() = runBlocking<Unit> {
12-
//sampleStart
1312
val time = measureTimeMillis {
1413
println("The answer is ${concurrentSum()}")
1514
}
1615
println("Completed in $time ms")
17-
//sampleEnd
1816
}
1917

2018
suspend fun concurrentSum(): Int = coroutineScope {

kotlinx-coroutines-core/jvm/test/guide/example-context-01.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package kotlinx.coroutines.guide.context01
88
import kotlinx.coroutines.*
99

1010
fun main() = runBlocking<Unit> {
11-
//sampleStart
1211
launch { // context of the parent, main runBlocking coroutine
1312
println("main runBlocking : I'm working in thread ${Thread.currentThread().name}")
1413
}
@@ -21,5 +20,4 @@ fun main() = runBlocking<Unit> {
2120
launch(newSingleThreadContext("MyOwnThread")) { // will get its own new thread
2221
println("newSingleThreadContext: I'm working in thread ${Thread.currentThread().name}")
2322
}
24-
//sampleEnd
2523
}

kotlinx-coroutines-core/jvm/test/guide/example-context-02.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package kotlinx.coroutines.guide.context02
88
import kotlinx.coroutines.*
99

1010
fun main() = runBlocking<Unit> {
11-
//sampleStart
1211
launch(Dispatchers.Unconfined) { // not confined -- will work with main thread
1312
println("Unconfined : I'm working in thread ${Thread.currentThread().name}")
1413
delay(500)
@@ -19,5 +18,4 @@ fun main() = runBlocking<Unit> {
1918
delay(1000)
2019
println("main runBlocking: After delay in thread ${Thread.currentThread().name}")
2120
}
22-
//sampleEnd
2321
}

kotlinx-coroutines-core/jvm/test/guide/example-context-03.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import kotlinx.coroutines.*
1010
fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
1111

1212
fun main() = runBlocking<Unit> {
13-
//sampleStart
1413
val a = async {
1514
log("I'm computing a piece of the answer")
1615
6
@@ -20,5 +19,4 @@ fun main() = runBlocking<Unit> {
2019
7
2120
}
2221
log("The answer is ${a.await() * b.await()}")
23-
//sampleEnd
2422
}

0 commit comments

Comments
 (0)