Writerside Help

Tutorial: Reference code from the repository

From this tutorial, you will learn how to reference the code example from the repository without copy-pasting it into documentation and how to add a check it is valid so that it always stays in sync with the actual solution.

Reference the code example from the repository

To include the code example from another file in the repository:

  1. Specify the name of the directory where you will place the code files in the snippets attribute of the writerside.cfg file.

    <snippets src="directoryName"/>
  2. Use the src attribute to specify the filename.

    ```kotlin ``` {src="newTest.kt"}
  3. (Optionally) Use the include-lines attribute to specify a line range or a comma-separated list of lines to include.

  4. (Optionally) Use the include-symbol attribute to specify a specific code construct, like, method or class, to include.

Check the markup examples below.

```kotlin ``` {src="newTest.kt" include-lines="2-4"}
fun testSum() { val expected = 42 assertEquals(expected, testSample.sum(40, 2))
```kotlin ``` {src="newTest.kt" include-symbol="testSum"}
@Test fun testSum() { val expected = 42 assertEquals(expected, testSample.sum(40, 2)) }

Add code validation

On the help page, a user can copy the code block in a single click to try it out right away. Add validation to the code snippet to check if it is valid.

  1. Set validate attribute to true to validate the code in a snippet. It works only for the IntelliJ-supported set of languages.

  2. If the code example is invalid, Writerside will display an error in the preview area.

In this tutorial, we have included a code example from another file and added a check for its validity.

Check what other attributes you can apply to the code block in Code snippets.

Last modified: 28 July 2023