vala-lang / vala-language-server

Code Intelligence for Vala & Genie
GNU Lesser General Public License v2.1
290 stars 43 forks source link

The name Gtk doesn't exist in the context of main #287

Closed MachineTrader closed 1 year ago

MachineTrader commented 1 year ago

Hi, I was trying out Genie and I really love the language.

So I decided to install the language server.

Everything compiles like it should with valac.

The problem is that Vala language server doesn't seem to find the namespace of Gtk, so I get a lot of red markings on my code. Pkg-config finds it, the command: pkg-config --cflags gtk+-3.0

gives this response: -I/usr/include/gtk-3.0

OS: Endeavour OS KDE. IDE: Kate and VSCodium/VSCode

I have the same problem with both editors.

Vala version 0.56.5

Source code repo: yay -S vala-lanaguage-server-git

Steps to reproduce the behavior: sudo pacman -S vala gtk3 kate yay -S vala-language-server-git

Here's the code of main.gs:

[indent=0]
uses Gtk

init
    Gtk.init(ref args)
    var header = new Gtk.HeaderBar()         
    header.set_title("Genie Bot")
    header.set_subtitle("An app for ElementaryOS")
    header.set_show_close_button(true)

    var window = new Gtk.Window()
    window.window_position = Gtk.WindowPosition.CENTER
    window.destroy.connect (Gtk.main_quit)
    window.set_default_size(350, 70)
    window.set_titlebar(header)
    window.border_width = 10

    window.add(new Gtk.Button.with_label("Hello Genie"))

    window.show_all()
    Gtk.main()
Prince781 commented 1 year ago

Because Vala is a compiled language, compiler options cannot be inferred (Gtk is a namespace belonging to gtk2, gtk3, and gtk4 packages). Therefore you need a meson.build, compile_commands.json, or shebang at the top of the file. (ex: #!/bin/env valac -S --pkg gtk+-3.0 #!/bin/env -S vala --pkg gtk+-3.0)

MachineTrader commented 1 year ago

Thanks a lot for your answer! I have this Build.sh file with the shebang at the top. But even if I move it to the same folder it doesn't seem to fix anything. I haven't heard of compile_commands.json I'll google it.

#!/bin/sh
valac \
    src/main.gs \
    --pkg gee-0.8 \
    --pkg gio-2.0 \
    --pkg gtk+-3.0
benwaffle commented 1 year ago

If the shebang doesn't work, try setting up a meson build

MachineTrader commented 1 year ago

Thank you, I've been learning about Meson all day.

I have a feeling that these steps can be a big hurdle for people just to get started. I've had many days this week to get started, most people don't have that time. And will just give up and move to another language.

And also if you just have a slight OCD like me you want the langauge-server to be up and running before starting with anything beyond Hello World. Especially with an object based gui library like GTK.

If the shebang could be made to work somehow, and also be "Clearly" explained in the Read.me of this repo, that would be a big win for people who want to get started. I feel like learning the build system is something you want to do do after a few test projects, not the first thing you do.

I did try place: #!/bin/env valac -S --pkg gtk+-3.0

Before: [indent=0] in the file, with negative result.

I really appreciate your help so far!

Prince781 commented 1 year ago

@MachineTrader it's actually supposed to be #!/bin/env -S vala --pkg gtk+-3.0. Oops.

Upsidedly commented 9 months ago

How do I fix this error in a vala file?