vimcats 1.1.0

LuaCATS parser and vimdoc transformer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
## Writing LuaCATS

> [!NOTE]
>
> `vimcats` aims to be compatible with [LLS LuaCATS annotations]https://luals.github.io/wiki/annotations/
> with some small add-ons to better support the vimdoc generation.

Following are the tags that you can use to create docs

### Brief

This tag can be used to describe a module or even to add some footnote etc.

- Syntax

```lua
---@brief [[
---@comment
---@brief ]]
```

- Input

```lua
---@brief [[
---Any summary you wanted to write you can write here.
---There is no formatting here,
---the way you write in here, will be shown
---exactly in the help-doc
---
---An empty line can be used to denote a paragraph
---
---You can also write anything, like ordered list
---    1. first
---    2. second
---    3. third
---
---Some code blocks, but IDK whether it will be highlighted or not
---
--->
---    for i = 1, 10, 1 do
---        print(("%s Lua is awesome"):format(i))
---    end
---<
---
---NOTE: remember there is no formatting or text wrapping
---@brief ]]
```

- Output

```help
Any summary you wanted to write you can write here.
There is no formatting here,
the way you write in here, will be shown
exactly in the help-doc

An empty line can be used to denote a paragraph

You can also write anything, like ordered list
    1. first
    2. second
    3. third

Some code blocks, but IDK whether it will be highlighted or not

>
    for i = 1, 10, 1 do
        print(("%s Lua is awesome"):format(i))
    end
<

NOTE: remember there is no formatting or text wrapping
```

### Module

This tag can be used to add a heading for a section. This tag also has the following properties:

1. This can appear multiple times in a file but only the last `---@mod` will be used to rename prefixes.

   > Use `--prefix-{func,alias,class,type}` cli options to rename function, alias, class, and type name prefixes relatively
   > See [`tests/renaming`]./tests/renaming.rs

2. Also adds a entries in the [`Table of Contents`]#table-of-contents

- Syntax

```lua
---@mod <name> [desc]
```

- Input

```lua
---@mod mod.intro Introduction
---@brief [[
---
---We can have multiple `---@mod` tags so that we can have a block only for text.
---This is for the cases where you want bunch of block only just for text
---and does not contains any code.
---
---You can write anything in here like some usage or something:
---
--->
---require('Comment').setup({
---    ignore = '^$',
---    pre_hook = function(ctx)
---        require('Comment.jsx').calculate(ctx)
---    end
---})
---<
---
---@brief ]]

---@mod mod.Human Human module

local H = {}

---@class Human The Homosapien
---@field legs number Total number of legs
---@field hands number Total number of hands
---@field brain boolean Does humans have brain?

---Default traits of a human
---@type Human
H.DEFAULT = {
    legs = 2,
    hands = 2,
    brain = false,
}

---Creates a Human
---@return Human
---@usage `require('Human'):create()`
function H:create()
    return setmetatable(self.DEFAULT, { __index = self })
end

return H
```

- Output

```help
================================================================================
Introduction                                                         *mod.intro*


We can have multiple `---@mod` tags so that we can have a block only for text.
This is for the cases where you want bunch of block only just for text
and does not contains any code.

You can write anything in here like some usage or something:

>
require('Comment').setup({
    ignore = '^$',
    pre_hook = function(ctx)
        require('Comment.jsx').calculate(ctx)
    end
})
<


================================================================================
Human module                                                         *mod.Human*

Human                                                                    *Human*
    The Homosapien

    Fields: ~
        {legs}   (number)   Total number of legs
        {hands}  (number)   Total number of hands
        {brain}  (boolean)  Does humans have brain?


U.DEFAULT                                                            *U.DEFAULT*
    Default traits of a human

    Type: ~
        (Human)


U:create()                                                            *U:create*
    Creates a Human

    Returns: ~
        {Human}

    Usage: ~
>lua
        require('Human'):create()
<
```

### Table of Contents

This tag can be used to generate a _Table of Contents_ section. It uses [`---@mod`](#module) tags for the entries.

- Syntax

```lua
---@toc <tag>
```

- Input

```lua
---@toc my-plugin.contents

---@mod first.module First Module

---@mod second.module Second Module

---@mod third.module Third Module

local U = {}

return U
```

- Output

```help
================================================================================
Table of Contents                                           *my-plugin.contents*

First Module······················································|first.module|
Second Module····················································|second.module|
Third Module······················································|third.module|

================================================================================
First Module                                                      *first.module*

================================================================================
Second Module                                                    *second.module*

================================================================================
Third Module                                                      *third.module*
```

### Tag

This tag can used to create an alternate tag for your module, functions etc.

- Syntax

```lua
---@tag <name>
```

- Input

```lua
---@tag cool-tag
---@tag another-cool-tag
```

- Output

```
                                                                      *cool-tag*
                                                              *another-cool-tag*
```

### Divider

This tag can be used to add a divider/separator between section or anything you desire

- Syntax

```lua
---@divider <char>
```

- Input

```lua
---@divider -
---@divider =
---@divider ~
```

- Output

```help
--------------------------------------------------------------------------------

================================================================================

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

### Functions

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

- Syntax

```lua
---@comment
---@param <name[?]> <type[|type...]> [description]
---@comment
---@return <type> [<name> [comment] | [name] #<comment>]
---@comment
---@see <ref>
---@usage `<code>`
```

> NOTE: All tag can be used multiple times except `---@usage`

- Input

```lua
local U = {}

---NOTE: Local functions are not part of the documentation
---Multiply two integer and print it
---@param this number First number
---@param that number Second number
local function mul(this, that)
    print(this * that)
end

---Add two integer and print it
---@param this number First number
---@param that number Second number
---@usage `require("module.U").sum(10, 5)`
function U.sum(this, that)
    print(this + that)
end

---Subtract second from the first integer
---@param this number First number
---@param that number Second number
---@return number
---@usage [[
---local M = require("module.U")
---
---print(M.sub(10 - 5))
---@usage ]]
function U.sub(this, that)
    return this - that
end

---This is a magical function
---@param this number Non-magical number #1
---@param that number Non-magical number #2
---@return number _ The magical number #1
---@return number _ The magical number #2
---@see U.mul
---@see U.sum
---@see U.sub
U.magical = function(this, that)
    return (U.mul(this, that) / U.sum(that, this)), (U.sum(this, that) * U.sub(that, this))
end

---Trigger a rebuild of one or more projects.
---@param opts table|nil optional configuration options:
---  * {select_mode} (JdtProjectSelectMode) Show prompt
---     to select projects or select all. Defaults
---     to 'prompt'
---
---  * {full_build} (boolean) full rebuild or
---     incremental build. Defaults to true (full build)
---@param reserverd table|nil reserved for the future use
---@return boolean _ This is description of return
---statement that can be expanded to mutliple lines
function U.multi_line(opts, reserverd)
    print(vim.inspect(opts), vim.inspect(reserverd))
    return true
end

return U
```

- Output

```help
U.sum({this}, {that})                                                    *U.sum*
    Add two integer and print it

    Parameters: ~
        {this}  (number)  First number
        {that}  (number)  Second number

    Usage: ~
>lua
        require("module.U").sum(10, 5)
<

U.sub({this}, {that})                                                    *U.sub*
    Subtract second from the first integer

    Parameters: ~
        {this}  (number)  First number
        {that}  (number)  Second number

    Returns: ~
        {number}

    Usage: ~
>lua
        local M = require("module.U")

        print(M.sub(10 - 5))
<


U.magical({this}, {that})                                            *U.magical*
    This is a magical function

    Parameters: ~
        {this}  (number)  Non-magical number #1
        {that}  (number)  Non-magical number #2

    Returns: ~
        {number}  The magical number #1
        {number}  The magical number #2

    See: ~
        |U.mul|
        |U.sum|
        |U.sub|


U.multi_line({opts}, {reserverd})                                 *U.multi_line*
    Trigger a rebuild of one or more projects.

    Parameters: ~
        {opts}       (table|nil)  optional configuration options:
                                    * {select_mode} (JdtProjectSelectMode) Show prompt
                                       to select projects or select all. Defaults
                                       to 'prompt'

                                    * {full_build} (boolean) full rebuild or
                                       incremental build. Defaults to true (full build)
        {reserverd}  (table|nil)  reserved for the future use

    Returns: ~
        {boolean}  This is description of return
                   statement that can be expanded to mutliple lines
```

### Class

Classes can be used to better structure your code and can be referenced as an argument to a function or it's return value. You can define it once and use it multiple times.

- Syntax

```lua
---@comment
---@class [(exact)] <name>[: <parent>]
---@comment
---@field [public|protected|private] <name[?]> <type> [desc]
---@see <ref>
```

> NOTE: `---@field` and `---@see` can be used multiple times

- Input

```lua
local H = {}

---The Homosapien
---@class Human
---@field legs number Total number of legs
---@field hands number Total number of hands
---@field brain boolean Does humans have brain?
---Traits that one human can have
---It could be one, two or hundered
---@field trait table
---@field protected heart boolean Heart is protected
---@field private IQ number We need to hide this

---@class XMen : Human
---@field power number Power quantifier

---Creates a Human
---@return Human
---@usage `require('Human'):create()`
function H:create()
    return setmetatable({
        legs = 2,
        hands = 2,
        brain = false
    }, { __index = self })
end

return H
```

- Output

```help
Human                                                                    *Human*
    The Homosapien

    Fields: ~
        {legs}   (number)   Total number of legs
        {hands}  (number)   Total number of hands
        {brain}  (boolean)  Does humans have brain?
        {trait}  (table)    Traits that one human can have
                            It could be one, two or hundered


XMen : Homosapien                                                         *XMen*

    Fields: ~
        {power}  (number)  Power quantifier


H:create()                                                            *H:create*
    Creates a Human

    Returns: ~
        {Human}

    Usage: ~
>lua
        require('Human'):create()
<
```

### Type

You can use `---@type` to document static objects, constants etc.

- Syntax

```lua
---@comment
---@type <type> [desc]
---@see <tag>
---@usage `<code>`
```

- Input

```lua
local U = {}

---@class Chai Ingredients for making chai
---@field milk string 1.5 cup
---@field water string 0.5 cup
---@field sugar string 3 tablespoon
---@field tea_leaves string 2 tablespoon
---@field cardamom string 2 pieces

---A object containing the recipe for making chai
---@type Chai
U.chai = {
    milk = "1.5 Cup",
    water = "0.5 Cup",
    sugar = "3 table spoon",
    tea_leaves = "2 table spoon",
    cardamom = "2 pieces",
}

return U
```

- Output

```help
Chai                                                                      *Chai*
    Ingredients for making chai

    Fields: ~
        {milk}        (string)  1.5 cup
        {water}       (string)  0.5 cup
        {sugar}       (string)  3 tablespoon
        {tea_leaves}  (string)  2 tablespoon
        {cardamom}    (string)  2 pieces


U.chai                                                                  *U.chai*
    A object containing the recipe for making chai

    Type: ~
        (Chai)
```

### Usage

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`).

- Syntax

1. Single-line

```lua
---@usage [lang] `<code>`
```

2. Multi-line

```lua
---@usage [lang] [[
---<code>...
---@usage ]]
```

- Input

```lua
local U = {}

---Prints a message
---@param msg string Message
---@usage lua [[
---require("module.U").sum(10, 5)
---@usage ]]
function U.echo(msg)
    print(msg)
end

---Add two integer and print it
---@param this number First number
---@param that number Second number
---@usage `require("module.U").sum(10, 5)`
function U.sum(this, that)
    print(this + that)
end

return U
```

- Output

```
U.echo({msg})                                                           *U.echo*
    Prints a message

    Parameters: ~
        {msg}  (string)  Message

    Usage: ~
>lua
        require("module.U").sum(10, 5)
<


U.sum({this}, {that})                                                    *U.sum*
    Add two integer and print it

    Parameters: ~
        {this}  (number)  First number
        {that}  (number)  Second number

    Usage: ~
>lua
        require("module.U").sum(10, 5)
<
```

### Alias

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

- Syntax

```lua
---@comment
---@alias <name> <type>
```

- Input

```lua
local U = {}

---All the lines in the buffer
---@alias Lines string[]

---Returns all the content of the buffer
---@return Lines
function U.get_all()
    return vim.api.nvim_buf_get_lines(0, 0, -1, false)
end

return U
```

- Output

```help
Lines                                                                    *Lines*
    All the lines in the buffer

    Type: ~
        string[]


U.get_all()                                                          *U.get_all*
    Returns all the content of the buffer

    Returns: ~
        {Lines}
```

### Enum

You can define a (pseudo) enum using [`---@alias`](#alias).

- Syntax

```lua
---@alias <name> <type>
---| '<literal>' [# description]
---| `<identifier>` [# description]
```

- Input

```lua
local U = {}

---Vim operator-mode motions.
---
---Read `:h map-operator`
---@alias VMode
---| '"line"' # Vertical motion
---| '"char"' # Horizontal motion
---| 'v'
---| `some.ident` # Some identifier

---Global vim mode
---@type VMode
U.VMODE = 'line'

return U
```

- Output

```help
VMode                                                                    *VMode*
    Vim operator-mode motions.

    Read `:h map-operator`

    Variants: ~
        ("line")      Vertical motion
        ("char")      Horizontal motion
        ("v")
        (some.ident)  Some identifier


U.VMODE                                                                *U.VMODE*
    Global vim mode

    Type: ~
        (VMode)
```

### Private

One of the following tags can be used to discard any part of the code that is not considered a part of the public API. All these tags behaves exactly same when it comes to vimdoc generation but have different use cases when used together with LLS.

- Spec: [`---@private`]https://github.com/sumneko/lua-language-server/wiki/Annotations#private, [`---@protected`]https://github.com/sumneko/lua-language-server/wiki/Annotations#protected, [`---@package`]https://github.com/sumneko/lua-language-server/wiki/Annotations#package

- Syntax

```lua
---@private

---@protected

---@package
```

- Input

```lua
local U = {}

---@private
---This is a private function which is exported
---But not considered as part of the API
function U.private()
    print('I am private!')
end

---Only this will be documented
function U.ok()
    print('Ok! I am exported')
end

---@protected
function U.no_luacats()
    print('Protected func with no LuaCATS!')
end

return U
```

- Output

```help
U.ok()                                                                    *U.ok*
    Only this will be documented
```

### Export

This tag is used to manually tag the exported object. This is required for cases where `vimcats` is unable to parse the `return` statement at the end such as `return setmetatable(...)`. But keep in mind the following:

1. Anything after this tag is NA, so make sure this is the last tag
2. Tag should be followed by the exact identifier that needs to be exported
3. This has nothing to do with `---@mod`

- Syntax

```lua
---@export <name>
```

- Input

```lua
---@mod module.config Configuration

local Config = {}

---Get the config
---@return number
function Config:get()
    return 3.14
end

---@export Config
return setmetatable(Config, {
    __index = function(this, k)
        return this.state[k]
    end,
    __newindex = function(this, k, v)
        this.state[k] = v
    end,
})
```

- Output

```help
================================================================================
Configuration                                                    *module.config*

Config:get()                                                        *Config:get*
    Get the config

    Returns: ~
        {number}
```