sekogan / MarkdownLight

Markdown syntax highlighting plugin and color scheme for Sublime Text 3
MIT License
41 stars 16 forks source link

Something wrong with the code block display #2

Open Gnnng opened 9 years ago

Gnnng commented 9 years ago

At first, thanks for this bravo plugin.

When I use the syntax ``` (code block) in markdown to put my c++ code in, the rendered area exceeds the code section. It looks like this:

wrong

But the normal situation is this (which I remove the type string "c++" to compare with the previous image)

right

And the most important thing is, this bug is decided by the code I put in the block. The display in the second block of the above image is correct. I guess some code string broke the parser and cause the issue but I am not sure.

The issue doesn't bother me too much, because I can fix it by declaring nothing about the language type. But I wish it could be fixed in the plugin.

I am using Sublime Text 3 Build 3065.

sekogan commented 9 years ago

It's an interesting question, thanks for bringing it up. I noticed the issue myself and it is really annoying. Finally I've got a good reason for doing a little research :-)

First of all the issue is not something that is unique for Markdown Light: all markdown packages for Sublime have it. It doesn't prove anything but is a symptom that the reason is somewhere outside my package.

I've done some experiments with C++ syntax file. Class definitions are handled by a block at line 402. That block captures keywords "class"/"struct" followed by an identifier. There is another block at line 458 inside the previous one for capturing curly braces and everything inside them. That block includes $base at line 496. Actually I did't find any documentation regarding $base but it seems to me that the original intent was in applying all C++ grammar rules recursively to everything inside curly braces.

Let's step back and recall that the C++ code is located inside Markdown file and is enclosed within fenced code block.

The problem is that $base refers to Markdown syntax, not C++. One can easily verify it in Sublime with the following example:

```c++
class A {
***This text will be surprisingly bold in Sublime***
};


Replacing `$base` with `$self` in C++ syntax file solves the issue because `$self` refers to current grammar which is C++ in our case.

Again, I am not 100% sure because the `$base`/`$self` topic is poorly covered in documentation, but for me this is the most reasonable explanation.

So the workaround is:
1. Use PackageResourceViewer -> Open Resource -> C++ -> C++.tmLanguage
2. Save it in your Packages directory.
3. Replace all `$base` with `$self`.
Gnnng commented 9 years ago

Thanks a lot for your reply and providing the solution, it works :)

sekogan commented 8 years ago

Found a good explanation of the $base/$self issue: textmate/textmate#1276

sekogan commented 8 years ago

According to the textmate issue the workaround that I proposed earlier is not correct. It affects highlighting of C++ files.

Seems like there is currently no good solution for the issue because it is caused by limitations of the Sublime's syntax highlighting engine. All Markdown packages have the same issue. If you aware of a syntax definition for Sublime that can embed other syntaxes and doesn't have this issue please share.