jeff-hykin / better-cpp-syntax

💾 The source of VS Code's C++ syntax highlighting
GNU General Public License v3.0
156 stars 29 forks source link

Add trailing return type #405

Open rianquinn opened 5 years ago

rianquinn commented 5 years ago

Checklist

The code with a problem is:

Deleter &
get_deleter() noexcept
{
    return m_deleter;
}

const Deleter &
get_deleter() const noexcept
{
    return m_deleter;
}

auto
get_deleter() noexcept -> Deleter &
{
    return m_deleter;
}

auto
get_deleter() const noexcept -> const Deleter &
{
    return m_deleter;
}

It looks like:

screenshot + theme name preferable image

It should look like:

"const" and "noexcept" show different colors based on return type. It appears that trailing returns are not highlighting properly

rianquinn commented 5 years ago

As shown here, "const" and "noexcept" are not the same color, and "Deleter" changes color as well

matter123 commented 5 years ago

This is due to a lack of support for trailing return types.

@jeff-hykin Additionally, keywords should be avoided inside the member access.

jeff-hykin commented 5 years ago

Hmmm I thought we had a previous issue about trailing return types but I can't find it.

Sadly @rianquinn , as Matter123 said, we haven't worked on trailing return types yet so all highlighting for them (if any) is accidental. Current highlighting is probably from pointer access e.g.this->member_var

jeff-hykin commented 5 years ago

additionally (just fyi) having auto on the line-before and the { on the line after would make it nearly impossible for the parser to know get_deleter() was a function-definition and not a function call, which would change how the trailing return type was parsed even once we do have support.

matter123 commented 5 years ago

A trailing return type is the only valid syntax that looks like that in the function head.

More precisely a line that starts with foo() can only be a function call when inside of a method context. Therefore outside of a method context any line that has the form foo() -> bar must contain a trailing return type.

@jeff-hykin I think the grammar can be more aggressive in asserting that its a function definition rather than a variable declaration when a trailing return type is present (https://github.com/jeff-hykin/cpp-textmate-grammar/issues/198)

std::string s("comment") -> bar should never be valid.

matter123 commented 5 years ago

Previous discussion https://github.com/jeff-hykin/cpp-textmate-grammar/issues/371

matter123 commented 5 years ago

Care needs to be taken about user-defined template deduction guides.

template<class C>
A(C b) -> typename C::type

Though I am not sure that there actually is a difference un how syntax highlighting would be performed.

starball5 commented 1 year ago

Related on VS Code: VS Code syntax highlighting issue when explicitly specifying return type for C++ lambda function