2021-01-06 19:43:43 +01:00
|
|
|
module doc
|
|
|
|
|
2021-03-23 09:07:09 +01:00
|
|
|
import v.token
|
|
|
|
|
2021-01-06 19:43:43 +01:00
|
|
|
const (
|
|
|
|
example_pattern = '\x01 Example: '
|
|
|
|
)
|
|
|
|
|
|
|
|
pub struct DocComment {
|
|
|
|
pub mut:
|
|
|
|
text string // Raw text content of the comment, excluding the comment token chars ('//, /*, */')
|
|
|
|
is_multi bool // Is a block / multi-line comment
|
2022-01-26 11:36:28 +01:00
|
|
|
pos token.Pos
|
2021-01-06 19:43:43 +01:00
|
|
|
}
|
|
|
|
|
2022-04-02 17:29:12 +02:00
|
|
|
// is_example returns true if the contents of this comment is an inline doc example.
|
2021-01-06 19:43:43 +01:00
|
|
|
// The current convention is '// Example: <content>'
|
|
|
|
pub fn (dc DocComment) is_example() bool {
|
2022-04-02 17:29:12 +02:00
|
|
|
return dc.text.trim_space().starts_with(doc.example_pattern)
|
2021-01-06 19:43:43 +01:00
|
|
|
}
|
|
|
|
|
2022-04-02 17:29:12 +02:00
|
|
|
// example returns the content of the inline example body
|
2021-01-06 19:43:43 +01:00
|
|
|
pub fn (dc DocComment) example() string {
|
2021-01-25 10:26:20 +01:00
|
|
|
return dc.text.all_after(doc.example_pattern)
|
2021-01-06 19:43:43 +01:00
|
|
|
}
|
2022-04-02 17:29:12 +02:00
|
|
|
|
|
|
|
// is_multi_line_example returns true if an example line has no inline code
|
|
|
|
pub fn (dc DocComment) is_multi_line_example() bool {
|
|
|
|
return dc.text.trim_space() == '\x01 Example:'
|
|
|
|
}
|
|
|
|
|
|
|
|
// has_triple_backtick returns true if the comment starts or ends a markdown code block
|
|
|
|
pub fn (dc DocComment) has_triple_backtick() bool {
|
|
|
|
return dc.text.starts_with('\x01 ```')
|
|
|
|
}
|