normen / vim-pio

Vim Plugin for PlatformIO
52 stars 4 forks source link

New feature suggestion: PIOCheck #9

Open ubaldot opened 10 months ago

ubaldot commented 10 months ago

I want to share a custom command so that I have all the issues found from pio check in the quickfix list. In this way the user can jump directly to the issue.

command! PIOCheck {
    var current_makeprg = &l:makeprg
    setlocal makeprg=pio
    silent make check
    execute "setlocal makeprg=" .. current_makeprg
    copen
}

It is written in Vim9, but perhaps it can be integrated in the current vim-pio?

normen commented 10 months ago

Nice, thanks. I wanted to look into the whole makeprg stuffs for vim-pio anyway. The main issue is that currently vim-pio is using a makefile as well so we'll have to check if we can transfer all the functionality over.

ubaldot commented 10 months ago

I recently looked at that for another plugin that I wrote (unfortunately only in Vim9 which is the only I can use).

In a nutshell: with makeprg you assign any program to:make. For example, if you :set makeprg=ls then when you use :make in reality it will call :!ls. The good thing of this approach is that the output will be automatically placed in the quickfix list (but you can also place it in the location list).

Regarding the Makefile issue it is not really a problem because in reality you should call :!make which call the make you have installed in your system, instead of :makewhich is a sort of alias (it calls whenever you specify in the makeprg program). In the current case of vim-pio everything works because makeprg=make and therefore :make upload calls make. :) So, the fact that :make upload works is only because makeprg=make, but in reality it would be more appropriate to use :!make upload .

Finally, to don't pollute the quickfix list with all the rubbish coming from the console, you can filter the output from the standard output with the errorformat option. However, this is a nightmare to setup (at least for me), the best is to copy an existing. However, given that both clang-tidy and cppcheck produce a nice formatted output, it should be possible to re-use an existing errorformat.

The easiest to write a compiler plugin (i.e. a plugin that uses such a makeprg thing) is to look at what has been done, with

next $VIMRUNTIME/compiler/*.vim

Type :next to go to the next plugin file, as explained in:h write-compiler-plugin. As you will see, all the files are pretty much identical. All one has to do is a mere copy-paste and tweak very few things.

I know, vim is not the smoothest tool, but I hope that my sharing was a bit helpful. :)

EDIT: I like the current setup of using a Makefilein combination with :!make for all the things directly connected to the board and :PIO for all the project-related stuff.

ubaldot commented 10 months ago

Yet another example where I use the more appropriate :compile command.

Say that you have defined your vim-pio/compiler/pio.vim where you set makeprg=pio -f -c vim etc. Then, you could define a command like this:

def PioCheck()
    # Save previous compiler
    var previous_compiler = ""
    if exists("b:current_compiler")
        previous_compiler = b:current_compiler # b:current_compler is a special internal Vim variable
    endif

    # Use compiler/pio.vim
    compiler pio 
    silent execute "make check"

    # restore previous compiler 
    if !empty(previous_compiler)
        execute "compiler " .. previous_compiler
    endif

    # Open quickfix list
    copen
enddef

command! PIOCheck vim9cmd PioCheck()
normen commented 10 months ago

Thanks, I have used this for homebridge development (javascript plugins running on a server) where the error format is a bit different than standard javascript and thus the default tools wouldn't quite work.

I don't quite agree though that calling :!make is "more appropriate" - Calling make is exactly what the :make command is for, the option to actually change and adapt the makeprg in vim came later. Its not a coincidence that I create a Makefile, managing the project build cycle with make :) Thats what I usually do for coding projects of any kind (even golang etc.) as it would work outside of vim as well. So thats also why its like that in this plugin.

But especially for windows users where make isn't that common (though vim isn't either really) it would probably be a good idea to go for using pio directly via the makeprg option.

ubaldot commented 10 months ago

Calling make is exactly what the :make command is for, the option to actually change and adapt the makeprg in vim came later.

Oh ok! I wasn't aware of it, that's interesting! :)