Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: multiline ---@return description #33

Merged
merged 1 commit into from
Aug 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions emmylua.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ A function contains multiple tags which form its structure. Like `---@param` for
---@param <name> <type> [desc]
---@comment
---@return <type> [name] [desc]
---@comment
---@see <ref>
---@usage `<code>`
```
Expand Down Expand Up @@ -359,7 +360,8 @@ end
--- * {full_build} (boolean) full rebuild or
--- incremental build. Defaults to true (full build)
---@param reserverd table|nil reserved for the future use
---@return boolean
---@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
Expand Down Expand Up @@ -426,7 +428,8 @@ U.multi_line({opts}, {reserverd}) *U.multi_line*
{reserverd} (table|nil) reserved for the future use

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

### Class
Expand Down
39 changes: 29 additions & 10 deletions src/parser/tags/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,27 @@ parser!(Param, {
pub struct Return {
pub ty: String,
pub name: Option<String>,
pub desc: Option<String>,
pub desc: Vec<String>,
}

parser!(Return, {
select! {
TagType::Return { ty, name, desc } => (ty, name, desc)
}
.then(select! { TagType::Comment(x) => x }.repeated())
.map(|((ty, name, desc), extra)| {
let desc = match desc {
Some(d) => Vec::from([d])
.into_iter()
.chain(extra.into_iter())
.collect(),
None => extra,
};

Self { name, ty, desc }
})
});

#[derive(Debug, Clone)]
pub struct Func {
pub name: String,
Expand All @@ -52,7 +70,7 @@ parser!(Func, {
}
.repeated()
.then(Param::parse().repeated())
.then(select! { TagType::Return { ty, name, desc } => Return { ty, name, desc } }.repeated())
.then(Return::parse().repeated())
.then(See::parse())
.then(Usage::parse().or_not())
.then(select! { TagType::Func { prefix, name, kind } => (prefix, name, kind) })
Expand Down Expand Up @@ -125,9 +143,9 @@ impl Display for Func {

for param in &self.params {
table.add_row([
&format!("{{{}}}", param.name),
&format!("({})", param.ty),
&param.desc.join("\n"),
format!("{{{}}}", param.name),
format!("({})", param.ty),
param.desc.join("\n"),
]);
}

Expand All @@ -141,11 +159,12 @@ impl Display for Func {

for entry in &self.returns {
table.add_row([
&format!("{{{}}}", entry.ty),
entry
.desc
.as_deref()
.unwrap_or_else(|| entry.name.as_deref().unwrap_or_default()),
format!("{{{}}}", entry.ty),
if entry.desc.is_empty() {
entry.name.clone().unwrap_or_default()
} else {
entry.desc.join("\n")
},
]);
}

Expand Down
11 changes: 10 additions & 1 deletion tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ fn functions() {
---@param this number
---@param that number Second number
---@return number
---Some secret number that
---we don't know about
---@usage `require('module.U').sub(10, 5)`
function U.sub(this, that)
return this - that
Expand All @@ -212,6 +214,9 @@ fn functions() {
---@param that number Non-magical number #2
---@return number _ The magical number #1
---@return number _ The magical number #2
---and the fun part is the description can span
---
---over mulitple lines and preserves empty lines
---@see U.mul
---@see U.sum
---@see U.sub
Expand Down Expand Up @@ -247,7 +252,8 @@ U.sub({this}, {that}) *U.sub*
{that} (number) Second number

Returns: ~
{number}
{number} Some secret number that
we don't know about

Usage: ~
>
Expand All @@ -265,6 +271,9 @@ U.magical({this}, {that}) *U.magical*
Returns: ~
{number} The magical number #1
{number} The magical number #2
and the fun part is the description can span

over mulitple lines and preserves empty lines

See: ~
|U.mul|
Expand Down