Ways to have highlighting for Mako generating C++ code in Visual Studio Code

246 views Asked by At

I frequently use Mako templates to automate writing repetitive C++ code like large switch statements and test code in my projects.

Mako is an embedded language that mixes in with the original C++ code. It works really well from a functionality point of view, it is a sort of better replacement for the C++ preprocessor - and C++ templated system as well in many cases.

However highlighting is a problem. Let's say I have a source code switch_table.cpp that I want to automate with Mako as in the picture below. the lines starting with the percentage % mark are Mako lines. In this particular case, they do a simple loop generating N switch case statements.

You can see that the squiggles are pointing to the Mako lines because they are not valid C++ code. This is a nuisance.

enter image description here

If I switch the file name to have the Mako extension as switch_table.mako, I get the Mako lines highlighted correctly but not the C++ lines, with are the majority. It is even worse.

enter image description here

My question now is - can I have a mode where I can combine both highlighting modes - the standard vscode c++ highlighting mixed with an extension? Or perhaps, is there an extension that does that? Alternatively I would be happy if it was possible to offer a regex such that vscode c++ highlighter would ignore all the lines starting with a percentage mark.

Any ideas on how to solve this nuisance?

Thank you!

This is the entire code block so you can test:

<%
def genargs(n): 
    return ",".join([ "args[%d]" % j for j in range(n) ])
%>

#include <vector>
#include <limits>

namespace trivial {

template <size_t N, typename... Args>
struct FnType {
    using Call = typename FnType<N - 1, double, Args...>::Call;
};

template <typename... Args>
struct FnType<0, Args...> {
    typedef double (*Call)(Args...);
};

double callfn(void* fn, const std::vector<double>& args) {
    switch (args.size()) {
% for row in range(MAXARGS+1):
        case ${row}: return FnType<${row}>::Call(fn)(${genargs(row)}); break;
% endfor
    }
    return std::numeric_limits<double>::quiet_NaN();
}

} // namespace trivial
0

There are 0 answers