Skip to content

Commit 5dba98a

Browse files
authored
feat: language support in ---@usage (#65)
This adds support for associating a language for `---@usage` tag which defaults to `lua` for every code block unless explicitly defined using the syntax below. #### Emmylua - Single line ```lua ---@Usage [lang] `<code>` ``` - Multiline ```lua ---@Usage [lang] [[ --- ---@Usage ]] ``` #### Help (vimdoc) ```help >{lang} code... < ```
1 parent bcb0a38 commit 5dba98a

File tree

7 files changed

+192
-62
lines changed

7 files changed

+192
-62
lines changed

emmylua.md

Lines changed: 88 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ U:create() *U:create*
186186
{Human}
187187
188188
Usage: ~
189-
>
190-
require('Human'):create()
191-
<
189+
>lua
190+
require('Human'):create()
191+
<
192192
```
193193

194194
### Table of Contents
@@ -291,7 +291,7 @@ This tag can be used to add a divider/separator between section or anything you
291291

292292
### Functions
293293

294-
A function contains multiple tags which form its structure. Like `---@param` for parameter, `---@return` for the return value, `---@see` for other related things and `---@usage` for example
294+
A function contains multiple tags which form its structure. Like `---@param` for parameter, `---@return` for the return value, `---@see` for other related things and [`---@usage`](#usage) for example
295295

296296
- Syntax
297297

@@ -305,19 +305,7 @@ A function contains multiple tags which form its structure. Like `---@param` for
305305
---@usage `<code>`
306306
```
307307

308-
> NOTE:
309-
>
310-
> 1. All tag can be used multiple times except `---@usage`
311-
>
312-
> 2. `---@usage` tag also has a multiline syntax
313-
>
314-
> ```lua
315-
> ---@usage [[
316-
> ---TEXT
317-
> ---TEXT
318-
> ---TEXT
319-
> ---@usage ]]
320-
> ```
308+
> NOTE: All tag can be used multiple times except `---@usage`
321309
322310
- Input
323311

@@ -395,9 +383,9 @@ U.sum({this}, {that}) *U.sum*
395383
{that} (number) Second number
396384
397385
Usage: ~
398-
>
399-
require("module.U").sum(10, 5)
400-
<
386+
>lua
387+
require("module.U").sum(10, 5)
388+
<
401389
402390
U.sub({this}, {that}) *U.sub*
403391
Subtract second from the first integer
@@ -410,11 +398,11 @@ U.sub({this}, {that}) *U.sub*
410398
{number}
411399
412400
Usage: ~
413-
>
414-
local M = require("module.U")
401+
>lua
402+
local M = require("module.U")
415403
416-
print(M.sub(10 - 5))
417-
<
404+
print(M.sub(10 - 5))
405+
<
418406
419407
420408
U.magical({this}, {that}) *U.magical*
@@ -528,9 +516,9 @@ H:create() *H:create*
528516
{Human}
529517
530518
Usage: ~
531-
>
532-
require('Human'):create()
533-
<
519+
>lua
520+
require('Human'):create()
521+
<
534522
```
535523

536524
### Type
@@ -592,6 +580,79 @@ U.chai *U.chai*
592580
(Chai)
593581
```
594582

583+
### Usage
584+
585+
This tag is used to show code usage of functions and [`---@type`](#type). Code inside `---@usage` will be rendered as codeblock. Optionally, a `lang` can be provided to get syntax highlighting (defaults to `lua`).
586+
587+
- Syntax
588+
589+
1. Single-line
590+
591+
```lua
592+
---@usage [lang] `<code>`
593+
```
594+
595+
2. Multi-line
596+
597+
```lua
598+
---@usage [lang] [[
599+
---<code>...
600+
---@usage ]]
601+
```
602+
603+
- Input
604+
605+
```lua
606+
local U = {}
607+
608+
---Prints a message
609+
---@param msg string Message
610+
---@usage lua [[
611+
---require("module.U").sum(10, 5)
612+
---@usage ]]
613+
function U.echo(msg)
614+
print(msg)
615+
end
616+
617+
---Add two integer and print it
618+
---@param this number First number
619+
---@param that number Second number
620+
---@usage `require("module.U").sum(10, 5)`
621+
function U.sum(this, that)
622+
print(this + that)
623+
end
624+
625+
return U
626+
```
627+
628+
- Output
629+
630+
```
631+
U.echo({msg}) *U.echo*
632+
Prints a message
633+
634+
Parameters: ~
635+
{msg} (string) Message
636+
637+
Usage: ~
638+
>lua
639+
require("module.U").sum(10, 5)
640+
<
641+
642+
643+
U.sum({this}, {that}) *U.sum*
644+
Add two integer and print it
645+
646+
Parameters: ~
647+
{this} (number) First number
648+
{that} (number) Second number
649+
650+
Usage: ~
651+
>lua
652+
require("module.U").sum(10, 5)
653+
<
654+
```
655+
595656
### Alias
596657

597658
This tag can be used to make a type alias. It is helpful if you are using the same the type multiple times.

src/lexer.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ impl Lexer {
181181
))
182182
});
183183

184+
let code_lang = ident().then_ignore(space).or_not();
185+
184186
let tag = just('@').ignore_then(choice((
185187
hidden.or(public.clone().ignored()).to(TagType::Skip),
186188
just("toc")
@@ -256,13 +258,16 @@ impl Lexer {
256258
.ignore_then(comment)
257259
.map(TagType::See),
258260
just("usage").ignore_then(space).ignore_then(choice((
259-
just("[[").to(TagType::UsageStart),
261+
code_lang
262+
.then(
263+
just('`')
264+
.ignore_then(filter(|c| *c != '`').repeated())
265+
.then_ignore(just('`'))
266+
.collect(),
267+
)
268+
.map(|(lang, code)| TagType::Usage(lang, code)),
269+
code_lang.then_ignore(just("[[")).map(TagType::UsageStart),
260270
just("]]").to(TagType::UsageEnd),
261-
just('`')
262-
.ignore_then(filter(|c| *c != '`').repeated())
263-
.then_ignore(just('`'))
264-
.collect()
265-
.map(TagType::Usage),
266271
))),
267272
just("export")
268273
.ignore_then(space)

src/lexer/token.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ pub enum TagType {
101101
/// ```
102102
See(String),
103103
/// ```lua
104-
/// ---@usage `<code>`
104+
/// ---@usage [lang] `<code>`
105105
/// ```
106-
Usage(String),
106+
Usage(Option<String>, String),
107107
/// ```lua
108-
/// ---@usage [[
108+
/// ---@usage [lang] [[
109109
/// ```
110-
UsageStart,
110+
UsageStart(Option<String>),
111111
/// ```lua
112112
/// ---@usage ]]
113113
/// ```

src/parser/tags/usage.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,23 @@ use crate::{lexer::TagType, parser::impl_parse};
77

88
#[derive(Debug, Clone)]
99
pub struct Usage {
10+
pub lang: Option<String>,
1011
pub code: String,
1112
}
1213

1314
impl_parse!(Usage, {
1415
choice((
15-
select! { TagType::Comment(x) => x }
16-
.repeated()
17-
.delimited_by(just(TagType::UsageStart), just(TagType::UsageEnd))
18-
.map(|x| Self { code: x.join("\n") }),
19-
select! { TagType::Usage(code) => Self { code } },
16+
select! {
17+
TagType::UsageStart(lang) => lang
18+
}
19+
.then(select! { TagType::Comment(x) => x }.repeated())
20+
.then_ignore(just(TagType::UsageEnd))
21+
.map(|(lang, code)| Self {
22+
lang,
23+
code: code.join("\n"),
24+
}),
25+
select! {
26+
TagType::Usage(lang, code) => Self { lang, code }
27+
},
2028
))
2129
});

src/vimdoc/usage.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ impl ToDoc for UsageDoc {
1010
fn to_doc(n: &Self::N, _: &super::Settings) -> String {
1111
let mut doc = String::new();
1212
doc.push_str(&description("Usage: ~"));
13-
doc.push_str(" >\n");
14-
doc.push_str(&textwrap::indent(&n.code, " "));
13+
doc.push('>');
14+
doc.push_str(n.lang.as_deref().unwrap_or("lua"));
1515
doc.push('\n');
16-
doc.push_str(" <\n\n");
16+
doc.push_str(&textwrap::indent(&n.code, " "));
17+
doc.push_str("\n<\n\n");
1718
doc
1819
}
1920
}

tests/basic.rs

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ U.sub({this}, {that}) *U.sub*
282282
we don't know about
283283
284284
Usage: ~
285-
>
286-
require('module.U').sub(10, 5)
287-
<
285+
>lua
286+
require('module.U').sub(10, 5)
287+
<
288288
289289
290290
U.magical({this}, {that}) *U.magical*
@@ -505,12 +505,12 @@ U:create() *U:create*
505505
(Human)
506506
507507
Usage: ~
508-
>
509-
local H = require('Human')
510-
local human = H:create()
508+
>lua
509+
local H = require('Human')
510+
local human = H:create()
511511
512-
print(getmetatable(human))
513-
<
512+
print(getmetatable(human))
513+
<
514514
515515
516516
================================================================================
@@ -650,15 +650,70 @@ U.VMODE *U.VMODE*
650650
(VMode)
651651
652652
Usage: ~
653-
>
654-
print(require('plugin').VMODE)
655-
<
653+
>lua
654+
print(require('plugin').VMODE)
655+
<
656656
657657
658658
"#
659659
)
660660
}
661661

662+
#[test]
663+
fn usage() {
664+
let src = "
665+
local U = {}
666+
667+
---Prints a message
668+
---@param msg string Message
669+
---@usage lua [[
670+
---require('module.U').sum(10, 5)
671+
---@usage ]]
672+
function U.echo(msg)
673+
print(msg)
674+
end
675+
676+
---Add a number to 10
677+
---@param this number A number
678+
---@usage `require('module.U').sum(5)`
679+
function U.sum(this)
680+
print(10 + this)
681+
end
682+
683+
return U
684+
";
685+
686+
assert_eq!(
687+
lemmy!(src),
688+
"\
689+
U.echo({msg}) *U.echo*
690+
Prints a message
691+
692+
Parameters: ~
693+
{msg} (string) Message
694+
695+
Usage: ~
696+
>lua
697+
require('module.U').sum(10, 5)
698+
<
699+
700+
701+
U.sum({this}) *U.sum*
702+
Add a number to 10
703+
704+
Parameters: ~
705+
{this} (number) A number
706+
707+
Usage: ~
708+
>lua
709+
require('module.U').sum(5)
710+
<
711+
712+
713+
"
714+
)
715+
}
716+
662717
#[test]
663718
fn private() {
664719
let src = "

tests/with_settings.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ U:create() *U:create*
6868
(number)
6969
7070
Usage: ~
71-
>
72-
require('Pi'):create()
73-
<
71+
>lua
72+
require('Pi'):create()
73+
<
7474
7575
7676
"
@@ -126,9 +126,9 @@ U:create() *awesome:create*
126126
(number)
127127
128128
Usage: ~
129-
>
130-
require('Pi'):create()
131-
<
129+
>lua
130+
require('Pi'):create()
131+
<
132132
133133
134134
"

0 commit comments

Comments
 (0)