Author Topic: HighlightLanguage from filename  (Read 5515 times)

daniloz

  • Guest
HighlightLanguage from filename
« on: September 08, 2011, 10:23:31 am »
Hi All,

I'm writing a plugin and right now I'm struggling with the following, so maybe you can help me. :-)

Based on a file selected on the project pane, I want to discover the HighlightLanguage for it, but I can't get it to work. What I'm doing is
Code
...
EditorColourSet* theme = ed->GetColourSet();
HighlightLanguage lang = theme->GetLanguageForFilename(prjFile->file.GetFullName());
which returns "CC" for a .cpp file.

However, it should be "C/C++", as confirmed by
Code
wxArrayString langs = theme->GetAllHighlightLanguages();
In the langs array, there is no "CC", but "C/C++" indeed.

What am I missing here? What's the catch?

BTW, the first code returns an empty string for a .h file.  :o

Offline danselmi

  • Developer
  • Almost regular
  • *****
  • Posts: 206
Re: HighlightLanguage from filename
« Reply #1 on: September 08, 2011, 10:41:23 am »
This one returns the string you expected (at least on my system):
Code
Manager::Get()->GetEditorManager()->GetColourSet()->GetLanguageName(ed->GetLanguage() );

daniloz

  • Guest
Re: HighlightLanguage from filename
« Reply #2 on: September 08, 2011, 11:02:15 am »
Thanks for the answer danselmi.

I haven't tested it yet, but one thing I forgot to mention is that the file is not necessarily opened on an editor, so I cannot rely on "ed->GetLanguage()". I was just using "ed" to get to "EditorColourSet* theme". BTW, if there's a clever or better way to do it, I'd like to hear it...

Edit: top answer my question above, I just realized that I can simply do
Code
Manager::Get()->GetEditorManager()->GetColourSet()->GetLanguageForFilename(...)
without going through the "theme" variable, but I still have the problem, of course.
« Last Edit: September 08, 2011, 11:05:24 am by daniloz »

daniloz

  • Guest
Re: HighlightLanguage from filename
« Reply #3 on: September 08, 2011, 11:20:17 am »
Ok, following danselmi post, I found a solution (or should I say workaround) to my problem.

The way I'm doing it right now is
Code
HighlightLanguage langWrong = Manager::Get()->GetEditorManager()->GetColourSet()->GetLanguageForFilename( _T(".")+prjFile->file.GetExt() );
HighlightLanguage lang = Manager::Get()->GetEditorManager()->GetColourSet()->GetLanguageName(langWrong);

What I have, for example is:
langWrong = "CC" , lang = "C/C++"
langWrong = "MASM_Assembly", lang = "MASM Assembly"

Nonetheless, I'd like to understand what's the difference between both highlight language names and why do we have two. I suspect it has something to do with the lexers name and syntax highlighting, but I don't understand the C::B sources good enough to figure it out, so any explanation or clarification would be pretty much appreciated.

Thx!

Offline danselmi

  • Developer
  • Almost regular
  • *****
  • Posts: 206
Re: HighlightLanguage from filename
« Reply #4 on: September 08, 2011, 11:39:38 am »
The GetLanguage...() methods returning a HighlightLanguage which is an identifier for the language. The LanguageName is a human readable name for the Language (methods returning a wxString).