zzzyxwvut / java-vim

Java filetype and syntax files for Vim
Vim License
1 stars 0 forks source link

add $classpath to &path #1

Open Konfekt opened 7 months ago

Konfekt commented 7 months ago

Just wondering whether at

https://github.com/zzzyxwvut/java-vim/blob/f43d1750a38edd85db60b08237bf2fdc94cede37/runtime/ftplugin/java.vim#L26

something similar to https://github.com/tpope/vim-classpath would make sense as well

zzzyxwvut commented 7 months ago

Let me get myself acquainted with the API for the linked
plugin before we proceed any further.

zzzyxwvut commented 7 months ago

If you want interoperability with the vim-classpath plugin,
let it go about its business with &path first, and later
arrange for some additional paths, e.g. JDK's source root
directory. Anything done to &path before vim-classpath
has run its course would be clobbered by it during FileType
events, so ftplugin/java.vim cannot do much to help.

On the other hand, with an additional plugin written as
~/.vim/pack/extra/opt/vim-classpath-after/plugin/classpath-after.vim:

augroup classpath-after
    autocmd!
    autocmd FileType java :let &l:path = g:ftplugin_java_source_path . ',' . &l:path
augroup END

you could execute :packadd vim-classpath, later to be
followed by :packadd vim-classpath-after.

And with the right FileType order, now &path should
contain all paths set by either plugins:

autocmd FileType java
echo &l:path

There is also a good old :help tags-file-format.

Konfekt commented 7 months ago

Thank you!

If you want interoperability with the vim-classpath plugin, let it go about its business with &path first, and later arrange for some additional paths, e.g. JDK's source root directory.

Putting

let &l:path = g:ftplugin_java_source_path . ',' . &l:path

into after/ftplugin/java.vim should work, otherwise something like

autocmd BufWinEnter <buffer>

in ftplugin/java.vim.

The question was also about whether such an automatic inclusion of $classpath also has its place in the java ftplugin itself.

zzzyxwvut commented 7 months ago

The environment variable $CLASSPATH shouldn't be queried
and copied by fplugin/java.vim when it is defined since
this script is none the wiser whether that value is meant to
be used by a particular project. The setting of $CLASSPATH
is not advised.

After all, there can be a module declaration file for which
that variable is irrelevant, or the project can use an
explicit class path with -cp or -classpath.

Konfekt commented 7 months ago

Okay, understood. Here $classpath was meant as a place holder for the classpath intended in a java project, say by the build files or even $CLASSPATH. It is recommended that it is not to be set, but if the user sets it, damage done and maybe for good reasons, why not respect her choice also in Vim? I suppose that you are fine with this adjustment of $classpath be wholly done by the referred plug-in, which is perfectly fine, I just wanted to make aware of it and ask how much of it could be useful for the general Vim user.

zzzyxwvut commented 7 months ago

In order to have $classpath included in &path, consider
assigning its value to g:ftplugin_java_source_path. That is
what this global variable for. Querying for $classpath in
ftplugin/java.vim and copying its value would duplicate the
functionality of g:ftplugin_java_source_path. You can still
automate the processing of $classpath without any further
assistance from ftplugin/java.vim.

For example, create a ~/.vim/after/ftplugin/java.vim file:

if exists("b:did_ftplugin_after")
    finish
endif

let b:did_ftplugin_after = 1

if exists("$classpath_vars")
    let paths = ''

    for var in split($classpath_vars, ',')
    let candidate = eval('$' . var)

    if !empty(candidate)
        let paths .= candidate . ','
    endif
    endfor

    if !empty(paths)
    if exists("g:ftplugin_java_source_path")
        let g:ftplugin_java_source_path = paths . g:ftplugin_java_source_path
    else
        " Strip the trailing comma.
        let g:ftplugin_java_source_path = paths[: -2]
    endif
    endif
endif

And voilà:

echo 'class Test {}' > /tmp/Test.java
classpath_vars='JSRC_UTILS_PATH,JSRC_DB_PATH,JSRC_WEB_PATH,JSRC_SMTP_PATH' \
    JSRC_UTILS_PATH=/var/tmp JSRC_WEB_PATH=/srv/tmp \
        vim +redraw +echo\ g:ftplugin_java_source_path Test.java