-5

Sometimes when a question is asked/answer with a pretty massive code, it would be nice if we could highlight all the occurrence of the selected string.

For example, if the code use a variable random_variable, if I select one occurrence of it, it put all the other occurrences in yellow. (like you can have in many code editor)

I think that could increase readability.

4
  • 7
    I believe a better solution would be to avoid posting massive amounts of code in questions. Questioners should aim for the shortest code necessary to explain or reproduce the problem. Commented Apr 15, 2015 at 14:40
  • Of course, but sometimes you don't have any other way to do. I think it is far more easier to read, even if there is 10 lines of code.
    – CoMartel
    Commented Apr 15, 2015 at 14:44
  • 2
    Doesn't your browser do this? If you "find" the appropriate variable, you may find it gets highlighted (Chrome does it, for example) - I use this all the time on SO.
    – jonrsharpe
    Commented Apr 15, 2015 at 15:21
  • 1
    We should definitely reduce the max length of questions. Definitely.
    – user1228
    Commented Apr 15, 2015 at 18:01

1 Answer 1

4

You can already do it manually if you use HTML.
Changed one of my answers here to bold cout, wherever it occurs:

Just specialize that one function:

template <typename template_type> class awesome_class{
public:
    void some_function(void){
        cout << "I am not of type_a and doing my normal methods";
    }
};

template<> void awesome_class<type_a>::some_function(void){
    cout << "I am of type_a and doing type_a specific methods";
}

Of course, if things are more complicated, you might have to use inheritance and SFINAE.

Just specialize that one function:

<!-- language: lang-cpp -->

<pre><code>template &lt;typename template_type> class awesome_class{
public:
    void some_function(void){
        <b>cout</b> &lt;&lt; "I am not of type_a and doing my normal methods";
    }
};

template&lt;> void awesome_class&lt;type_a>::some_function(void){
    <b>cout</b> &lt;&lt; "I am of type_a and doing type_a specific methods";
}</code></pre>

Of course, if things are more complicated, you might have to use
inheritance and SFINAE.

Anyway, most of the time reducing the code and using the languages standard code-comments where needed is a better idea.

5
  • How would I do this for an HTML code example? Commented Apr 29, 2015 at 20:40
  • @brentonstrine: Just html-encode your code, and then substitute it between the code-tags. Naturally the highlighting-hint is only used here because this is meta, and not an appropriately tagged question on main. Commented Apr 29, 2015 at 21:11
  • So something like <!-- langauge: lang-html --> &lt;p&gt;the problem is <b>here</b> in my HTML code.&lt;p&gt;? Would the HTML still receive syntax hilighting? Commented Apr 29, 2015 at 21:24
  • Try it out. and you put it in the wrong place, I said between the <code>-tags. Commented Apr 29, 2015 at 21:33
  • Sorry, wasn't sure what 'substitute between code tags' meant. Seems to work if I wrap it in both a <code> and a <pre>. Thanks! Commented Apr 29, 2015 at 22:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .