StefanSalewski / gintro

High level GObject-Introspection based GTK3/GTK4 bindings for Nim language
MIT License
297 stars 20 forks source link

GtkSourceView syntax highlighting and color schemes #71

Open tobimensch opened 4 years ago

tobimensch commented 4 years ago

Hello Stefan,

first of all I want to thank you for developing and maintaining this nice module.

I wasn't successful with enabling syntax highlighting and choosing color schemes for GtkSourceView.

Did you ever try that and have success with it?

I'm doing all the same steps that examples for python and other languages are doing, so I'm out of my wits currently what I could be doing wrong.

StefanSalewski commented 4 years ago

GtkSourceView is special in many ways.

Some people have to install it separately as it may be not part of the main gtk package.

And we have GtkSourceView 3 and 4 now. I think gedit of gtk3 is using GtkSourceView 4. That is really confusing. My NEd editor supports syntax highlighting for Nim, but it uses GtkSourceView 3, and it uses oldgtk3, not gintro. I guess it may not compile with latest Nim.

Does for you gedit support Nim syntax highlighting? When it does, then it should work for GtkSourceView. I guess you have to install the Nim syntax file in the right directory. You may consult NEd docs for details, but note that we now have GtkSourceView 4. NEd has a syntax highlighting file, maybe you have to tweak it when it contains a version tag, and you have to copy it in the right directory.

Does syntax highlighting work in gintro GtkSourceView for other languages like C or Python?

What are you doing with GtkSourceView, writing your own editor?

Unfortunately I am busy currently with latest changes for GTK4, see

https://discourse.gnome.org/t/recent-gtk-3-98-nim-example-fails-to-compile/3262/9

It is much more work than expected.

tobimensch commented 4 years ago

And we have GtkSourceView 3 and 4 now. I think gedit of gtk3 is using GtkSourceView 4. That is really confusing. My NEd editor supports syntax highlighting for Nim, but it uses GtkSourceView 3, and it uses oldgtk3, not gintro. I guess it may not compile with latest Nim.

It works in gedit.

I'm using this Nim code to try to change the scheme and enable syntax highlighting:

  var manager = newLanguageManager()
  var styleSchemeManager = newStyleSchemeManager()
  var language = manager.getLanguage("python")
  sourceView.setBuffer(buffer)
  buffer.setHighlightSyntax(true)
  buffer.setHighlightMatchingBrackets(true)
  buffer.setStyleScheme(styleSchemeManager.getScheme("cobalt"))
  for scheme in styleSchemeManager.getSchemeIds():
    echo scheme
  #let sch = buffer.getStyleScheme()
  #echo sch.getName()
  buffer.setLanguage(language)
  sourceView.setBuffer(buffer)

Using python highlighting would work just fine for me, it's similar enough to Nim (in syntax). I verified that styleSchemeManager.getScheme("cobalt") returns a valid scheme. But the two commented out lines fail, so something prevents the scheme provided by the manager from being set on the buffer correctly.

I'm writing a debugging tool for Nim projects, that's why I need SourceView.

StefanSalewski commented 4 years ago

Thanks for reporting. I will try to investigate it on Sunday.

StefanSalewski commented 4 years ago

Are you using Linux?

And have you installed GtkSourceView 3 or 4, or both?

tobimensch commented 4 years ago

@StefanSalewski Yes, Fedora 32.

Both are installed.

Thanks for looking into it!

tobimensch commented 4 years ago

@StefanSalewski I added a test buffer in glade and wrongly assumed that it shouldn't have any effect on the result. After removing it, finally I get syntax highlighting and colors. Sorry, for bothering you with this, maybe this can be classified as a glade bug, I'm not sure.

StefanSalewski commented 4 years ago

Great that it works for you now.

Personally I am not using Glade -- I know some people like E. Bassi recomments using glade, but I see no real benefit by using glade when using a high level language like Nim.

For your bug, maybe we should ask on the GTK forum. People there generally prefer C examples, but maybe you can provide a small self contained Nim example that shows your problem, and maybe E. Bassi can guess the problem source. If not we can still translate the Nim code back to C and ask again.

tobimensch commented 4 years ago

@StefanSalewski It's the same reason why I use designer for Qt projects. WYSIWYG interfaces are really handy to try different layouts and ideas, if I had to do that in code my productivity would be lower (could be different in your case, of course), and if glade had a couple less bugs and a few more features, I'd actually love it, currently it's a hate love relationship. :-) I'll keep in mind that I should contact E. Bassi, I think I know him from IRC.

Another benefit of ui files (designer, glade) is that I think it makes it easier to maintain code, will you still know every quirk and line of your code 10 or 20 years down the line, when you haven't looked at it? But you can always open glade/designer and add another button or change its position or labeling, without even having to know anything about Gtk/Nim.