dmitry7 dmitry7 -4 years ago 147
HTML Question

Eclipse - how to extend HTML editor to add custom tags?

I write an application and inside of HTML code I have custom tags (of course these tags are parsed on server side and end user gets them as valid HTML code). Example of custom tag usage:

<html>
<body>
...
<Gallery type="grid" title="My Gallery" />
...
</body>
</html>


1.) How can I have eclipse recognize my custom tags inside of HTML code and add syntax highlighting to them?

2.) How can I add auto-suggestions to my custom tags? For example if I type "<Gallery " press "Ctrl+Space" - in the list of available attributes it shows me "type" and "title" and if I type "<Gallery type=" press "Ctrl+Space" I would see list of available values only for tag "Gallery" and its attribute "type".

Thanks in advance!

Answer Source

Sadly, there is no easy way: you have to roll your own parser for this, and then add both your extra elements and the base grammar (HTML) to it.

If you have your parser, you could use it to do syntax highlighting (strictly speaking, for that simple lexing is enough); and a good parser can support content assist (auto-suggestions in your terminology).

Caveats:

  • Creating a parser for HTML is not an easy task. Maybe by aiming at a more often used subset is feasible.
  • If a parser exists, the editor parts are still hard to get well.

Some help on the other hand: you could use some text editor generators to ease your work:

  • Eclipse IMP http://www.eclipse.org/imp/ can in theory handle any type of parser, but currently it is most optimized for LPG. The documentation is scarce, but the developers are helpful in the forums.
  • Xtext http://www.eclipse.org/Xtext/ got quite a hype for creating text editors for DSLs. The generated editors are quite nice out of the box, but is not the best solution for large files. Has a really helpful developer community.
  • EMFText http://www.emftext.org/index.php/EMFText is a lesser known entity - I don't know it in details, but I guess, it is similar to Xtext.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download