GtkSource.View add / remove comment

I want to add or remove a comment at the beginning of the line
I got it that way.
Could there be an easier way?

def on_toggle_comment(self, *args):
    mark = self.buffer.get_insert()
    iter = self.buffer.get_iter_at_mark(mark)
    line_number = iter.get_line()
    cursor = self.buffer.get_iter_at_line(line_number)
    cursor.backward_sentence_starts(0)
    self.buffer.place_cursor(cursor)
    mark = self.buffer.get_insert()
    iter = self.buffer.get_iter_at_mark(mark)
    if not iter.get_char() == "#":
        self.buffer.insert(iter, "#")
    else:
        mark = self.buffer.get_insert()
        iter = self.buffer.get_iter_at_mark(mark)
        line_number = iter.get_line()
        cursor = self.buffer.get_iter_at_line(line_number)
        cursor.forward_cursor_positions(1)
        self.buffer.place_cursor(cursor)
        next_mark = self.buffer.get_insert()
        next_iter = self.buffer.get_iter_at_mark(next_mark)
        self.buffer.delete(iter,next_iter)

Unfortunately I can not really help you.

But may I ask what you are doing? Writing a new editor?

I used gtksourceview four years ago for my initial Nim editor with the oldgtk3 bindings. As that editor does not work really well with latest GTK3 I do intent a rewrite with GTK4 and gintro. I assume you are using GTK4 now also? Maybe Mr. Toshio Sekiya can help you a bit, he used gtksourceview for parts of his tutorial. I have still to learn what this tepl on top of gtksourceview is, but when I visited it some months ago it was available for GTK3 only. Four years ago gtksourceview was unfortunately very restricted and not well suited for languages with whitespace aligned blocks like Python, Haskell or Nim.

Do you know if there is support for these Language-Server-Protokol available for latest GtkSourceView? When I asked Google for it maybe one year ago I found nothing, but of course even Google can not know all. I think we do not really need Language-Server-Protokol, it may even have restrictions compared to communicate directly over UDP with language servers like nimsuggest, but modern plugins for VS-Code or neovim seems to use it now.

I rediscovered Gtk, so to speak, after using PyQt5 for many years.

Also so as not to forget everything and learn new things.

I’m currently working on this editor. github site

I do not use Gtk4 because it’s not available for Mint 20.1

As I said, commenting / uncommenting works with my method, but I think there might be an easier way.

That is interesting, so we have at least one active gtksourceview user. GtkSourceView was hard for me four years ago when I wrote the Ned editor. Understanding the API was really not easy, and unfortunately I forget all of it. Looking at the gedit sources may help, but I don’t know how outdated that sources are.

For GTK version, I would really suggest using GTK4 now, as it is official released already. For my Gentoo it is not available as a package, but there are other ways to install it, I did it from gitlab source.

You may have your reasons why you use Python – for an editor where performance is not that important it may be OK. I had used Ruby for some years. Now I think there exists many much more interesting new languages, and most have GTK support.

One question: Have you already find out how we can set the font for a sourceview widget in a non deprecated way? I regard the CSS way as described in the Salewski book as very laborious: http://ssalewski.de/gtkprogramming.html#_fontchooserdialog. And storing the selected font with GSettings seems to be not possible in one single line also :frowning:

[EDIT]

Well there is an example for setting the font in the GtkSourceView API nearly to the top:

https://developer.gnome.org/gtksourceview/stable/GtkSourceView.html

So I think that CSS is indeed the way – we have to construct the CSS string from the data available from the FontChooserDialog.

And maybe another question as your main target is Python: Have you already an idea how we can nicely display indented blocks, so that indent levels are nicely viewable? Four years ago there was no way for gtksourceview, while VS-Code did it very nice.

Like https://ihatetomatoes.net/useful-vscode-extensions-visual-enhancements/

The only way I found to change the font family and font size was css.

I named the widget myeditor, then in css

#myeditor {
    font-size: 10pt;
    font-family: Noto Sans;
}

With a special name you can specify that only this widget appearance is changed via css.

This is particularly important with buttons, as otherwise the buttons in File Dialogs are also changed.

1 Like

Yes, seems that CSS is the only way in GTK4. A GTK provided function which extracts all the info from a PangoFontDescriptor and collect it into a CSS string would be nice.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.