Skip to content

Commit

Permalink
docs: syntax highlighting in documentation (#651)
Browse files Browse the repository at this point in the history
Closes #439

### Summary of Changes

Add syntax highlighting for Safe-DS code in the generated documentation.

---------

Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
  • Loading branch information
lars-reimann and megalinter-bot committed Oct 19, 2023
1 parent 4ba2c2c commit b5615c0
Show file tree
Hide file tree
Showing 22 changed files with 222 additions and 126 deletions.
2 changes: 2 additions & 0 deletions .github/linters/.ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ ignore = [
"RET507",
# superfluous-else-break (sometimes it's more readable)
"RET508",
# mutable-class-default (not helpful for the lexer)
"RUF012",
# private-member-access (we cannot always avoid it if we want a clean API)
"SLF001",
# if-else-block-instead-of-if-exp (an if-else block can be more readable)
Expand Down
2 changes: 1 addition & 1 deletion docs/development/formatting-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ formatting test.

2. Add the original unformatted Safe-DS code to the top of the file. The code must be syntactically valid.
3. Add the following separator to the file:
```txt
```sds
// -----------------------------------------------------------------------------
```
4. Add the expected formatted Safe-DS code to the file below the separator.
Expand Down
4 changes: 2 additions & 2 deletions docs/language/common/comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Safe-DS has two types of comments, namely _line comments_, which end at a linebr

Line comments stop at the end of a line:

```txt
```sds
// This is a comment.
```

Expand All @@ -18,7 +18,7 @@ As we can see here, they start with two slashes. There must be no space between

Block comments have a start and end delimiter, which allows them to cover multiple lines:

```txt
```sds
/*
This
is
Expand Down
16 changes: 8 additions & 8 deletions docs/language/common/imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Safe-DS has two kinds of imports, namely a _qualified import_, which imports a s

A _qualified import_ makes a single declaration available. Here is an example that imports the [class][classes] `DecisionTree` in the [package][packages] `safeds.model.regression`:

```txt
```sds
from safeds.model.regression import DecisionTree
```

Expand All @@ -21,21 +21,21 @@ The syntax consists of the following parts:

Once the declaration is imported, we can refer to it by its name. Here is, for example, a [call][calls] to the constructor of the `DecisionTree` class:

```txt
```sds
DecisionTree()
```

Multiple declarations can be imported from the same package in a single import statement by separating them with commas:

```txt
```sds
from safeds.model.regression import DecisionTree, RandomForest
```

### Qualified Imports with Alias

Sometimes the name of the imported declaration can conflict with other declarations that are imported or that are contained in the importing file. To counter this, declarations can be imported under an alias:

```txt
```sds
from safeds.model.regression import DecisionTree as StdlibDecisionTree
```

Expand All @@ -50,13 +50,13 @@ Let us take apart the syntax:

Afterwards, the declaration can **only** be accessed using the alias. The next example shows how to create a new instance of the class now by invoking its constructor:

```txt
```sds
StdlibDecisionTree()
```

Multiple declarations can be imported with or without an alias in a single import statement by separating them with commas:

```txt
```sds
from safeds.model.regression import DecisionTree as StdlibDecisionTree, RandomForest
```

Expand All @@ -66,7 +66,7 @@ We can also import all declarations in a [package][packages] with a single impor

Nevertheless, let us look at an example, which imports all declarations from the [package][packages] `safeds.model.regression`:

```txt
```sds
from safeds.model.regression import *
```

Expand All @@ -79,7 +79,7 @@ Here is the breakdown of the syntax:

Afterwards, we can again access declarations by their simple name, such as the [class][classes] `DecisionTree`:

```txt
```sds
DecisionTree()
```

Expand Down
12 changes: 6 additions & 6 deletions docs/language/common/packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

_Packages_ are used to prevent conflicts when multiple files have declarations with the same name. They accomplish this by grouping all declarations in a file into a namespace. Here is an example for a package declaration:

```txt
```sds
package de.unibonn.speedPrediction
```

Expand Down Expand Up @@ -35,7 +35,7 @@ class DecisionTree:

This file contains the actual implementation of the Python class `DecisionTree`. We now want to make this Python class available in Safe-DS, which requires the following Safe-DS stub file:

```txt
```sds
// Safe-DS file "safeds/model/regression/_decision_tree/DecisionTree.sdsstub"
package safeds.model.regression._decision_tree
Expand Down Expand Up @@ -85,7 +85,7 @@ from safeds.model.regression import DecisionTree

Note the omission of the suffix `._decision_tree` after `safeds.model.regression`. Likewise, we can now update the Safe-DS stub code. We again just take everything between `from` and `import` and use this as the Safe-DS package name:

```txt
```sds
// Safe-DS file "safeds/model/regression/DecisionTree.sdsstub"
package safeds.model.regression
Expand Down Expand Up @@ -115,7 +115,7 @@ class DecisionTree:

Our original solution leads to a warning because the Safe-DS package name contains the segment `_decision_tree`, which is not `lowerCamelCase` due to the underscores:

```txt
```sds
// Safe-DS file "safeds/model/regression/_decision_tree/DecisionTree.sdsstub"
package safeds.model.regression._decision_tree
Expand All @@ -125,7 +125,7 @@ class DecisionTree()

By [calling][annotation-calls] the [annotation][annotations] `@PythonModule`, we can also specify the corresponding [Python module][python-modules], however. If this [annotation][annotations] is [called][annotation-calls], it takes precedence over the Safe-DS package name. This allows us to pick an arbitrary Safe-DS package that respects the Safe-DS naming convention. We can even group multiple [Python modules][python-modules] together in one Safe-DS package without relying on Python's `__init__.py` files:

```txt
```sds
// Safe-DS file "safeds/model/regression/DecisionTree.sdsstub"
@PythonModule("safeds.model.regression._decision_tree")
Expand All @@ -140,7 +140,7 @@ Here is a breakdown of this:
- We [call][annotation-calls] the `@PythonModule` [annotation][annotations] before we declare the Safe-DS package. The [Python module][python-modules] that exports the Python declarations that correspond to the Safe-DS declarations in this stub file is passed as a [string literal][string-literals] (here `safeds.model.regression._decision_tree`). This is used only for code generation and does not affect users of Safe-DS.
- We specify the Safe-DS package as usual. This must be used when we [import][imports] the declaration in another Safe-DS file:

```txt
```sds
// Safe-DS
from safeds.model.regression import DecisionTree
Expand Down
12 changes: 6 additions & 6 deletions docs/language/common/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ _Parameters_ define the expected inputs of some declaration that can be [called]

_Required parameters_ must always be passed when the declaration is [called][calls]. Let us look at an example:

```txt
```sds
requiredParameter: Int
```

Expand All @@ -23,7 +23,7 @@ Here are the pieces of syntax:

_Optional parameters_ have a default value and, thus, need not be passed as an [argument][calls] unless the default value does not fit. Here is an example:

```txt
```sds
optionalParameter: Int = 1
```

Expand All @@ -39,7 +39,7 @@ These are the syntactic elements:

Let us now look at a full example of a [segment][segments] called `doSomething` with one [required parameter](#required-parameters) and one [optional parameter](#optional-parameters):

```txt
```sds
segment doSomething(requiredParameter: Int, optionalParameter: Boolean = false) {
// ...
}
Expand Down Expand Up @@ -81,7 +81,7 @@ def accuracy(x_pred: Dataset, x_test: Dataset) -> float:
pass
```

```txt
```sds
// Safe-DS code
fun accuracy(
Expand Down Expand Up @@ -121,7 +121,7 @@ def required(a: int):
pass
```

```txt
```sds
// Safe-DS code
fun required(a: Int)
Expand All @@ -136,7 +136,7 @@ def optional(a: int = 1):
pass
```

```txt
```sds
// Safe-DS code
fun optional(a: Int = 1)
Expand Down
14 changes: 7 additions & 7 deletions docs/language/common/results.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

_Results_ define the outputs of some declaration when it is [called][calls]. Here is an example:

```txt
```sds
result: Int
```

Expand All @@ -16,7 +16,7 @@ Here is a breakdown of the syntax:

Let us now look at a full example of a [segment][segments] called `doSomething` with two results:

```txt
```sds
segment doSomething() -> (result1: Int, result2: Boolean) {
// ...
}
Expand All @@ -33,23 +33,23 @@ The interesting part is the list of results, which uses the following syntactic

In case that the callable produces only a single result, we can omit the parentheses. The following two declarations are, hence, equivalent:

```txt
```sds
segment doSomething1() -> (result: Int) {}
```

```txt
```sds
segment doSomething2() -> result: Int {}
```

## Shorthand Version: No Results

In case that the callable produces no results, we can usually omit the entire results list. The following two declarations are, hence equivalent:

```txt
```sds
segment doSomething1() -> () {}
```

```txt
```sds
segment doSomething2() {}
```

Expand All @@ -66,6 +66,6 @@ Since Python results do not have a name, the names of Safe-DS results can be arb
[stub-language]: ../stub-language/README.md
[types]: types.md
[types-python]: types.md#corresponding-python-code
[callable-types]: types.md#callable-type
[callable-types]: types.md#callable-types
[segments]: ../pipeline-language/segments.md
[calls]: ../pipeline-language/expressions.md#calls
Loading

0 comments on commit b5615c0

Please sign in to comment.