Let's just continue to use code comments to indicate the line with the error or other area which is desired to be highlighted
The ability to put comments in code is nearly ubiquitous across (almost) all programming languages. Indicating things like where the error, why something is done a certain way, or what's been changed (in an example in an SO answer, but usually not in production code) are some of the things such comments are for. If we can't get users to use an already existing, default feature of the language they are using, I'm skeptical that we'd get them to use other additional formatting that's unique to Stack Overflow, unless there's a GUI which basically makes it required.
While formatting of code within the code format block might be helpful in very limited circumstances, I doubt that it would be used all that much. We already struggle to have users post questions with properly formatted basic code blocks. In addition, such additional formatting within code blocks could be substantially abused. Code is much more readable as just straight text.
To have any significant amount of reasonable use, there would need to be a GUI built to have people enter their code along with the errors and to indicate what line the error occurs on. While such a GUI would probably be beneficial, it's not a small amount of work to have it applicable over a large spectrum of questions/languages.
Another part of this is that Stack Exchange/Stack Overflow uses CommonMark. Other than using HTML for the code blocks (see below) there isn't anything in CommonMark which would allow this. The method chosen would need to be compatible for editing both with the above mentioned GUI and just with straight text. It would need to be something which isn't easy to mess up with simple text editing (e.g. it couldn't just be a specified line number within the code block, as those might change with editing by the OP or others).
Overall, this is something which is much easier to include in the code block as just a code comment and already has near-universal support.
Formatting within code blocks is already available, but is almost never used (which is a good thing, IMO)
You can already apply different formatting within code blocks, including having a different color, by using HTML to generate the code block and then adding additional formatting. You can use <a>
elements to get a different color text (link color), along with a tooltip, using the title
attribute. For details about what HTML can be used on Stack Overflow, see: What HTML tags are allowed on Stack Exchange sites?
Note: The highlight.js syntax highlighter will remove any formatting in a <code>
block and apply its own formatting. If you're going to use an HTML <pre><code>foobar</code></pre>
block to add your own formatting, as shown in the example below, then you need to be sure to turn off syntax highlighting for that code block or for the entire post.
Example
Formatted <pre><code>
block
function hello() {
const foo = "bold, italics, superscript, subscript, keyboard, strike-through";
console.log('hello')); // This line has a tooltip
}
The Markdown/HTML which produced the above formatted <pre><code>
block
<!-- language: none -->
<pre><code>function hello() {
const foo = "<strong>bold</strong>, <i>italics</i>, <sup>superscript</sup>, <sub>subscript</sub>, <kbd>keyboard</kbd>, <s>strike-through</s>";
<a href="https://meta.stackoverflow.com/q/412823" title="This is the line with an error. It has an intentional syntax error.">console.log('hello')); // This line has a tooltip</a>
<img src="/oisajfosidjfoi" alt="// Another line with a tooltip" title="hello there. This uses an <img>.">
}</code></pre>
Personally, I would strongly discourage any significant amount of such formatting, but it might be useful in some very limited circumstances. However, it's not something which I'd expect a new user to do, and certainly not do reasonably.