findfile() and &path and gf and includeexpr

126 visningar
Hoppa till det första olästa meddelandet

David Fishburn

oläst,
7 juni 2010 14:33:352010-06-07
till vim_use, David Fishburn
Looking for the best approach here.

Some background at the top, and at the bottom is a 6 step process which I believe Vim does 1-4 already.  Just seems to be missing the 5 and 6.



In my Flex MXML ftplugin, I set the following:

setlocal includeexpr=substitute(v:fname,'\\.','/','g')
setlocal suffixesadd+=.mxml
setlocal suffixesadd+=.as

I also added a public function so that you can do something like this:

    autocmd bufread */MyDirectory/*
                \ let buildFile = substitute(fnamemodify(expand("<afile>"), ':p:h'), 'MyDirectory\zs.*', '', '')|
                \ if exists("*AddSearchPath_".&ft) | call AddSearchPath_{&ft}(buildFile.'/src') | endif

Net result, once I edit a *.as or *.mxml file, and I run:
setloca
l path
path=C:\MyDirectory\src\plus\some\more


This means, when I am on an import statement, I can simply hit gf (get file):
import plus.some.more.MyEvent;

This will first search for MyEvent.(mxml|as), if it can't find it, it uses the includeexpr above and then tries again searching the path looking for:
plus\some\more\MyEvent.as

Which it finds and opens:
C:\MyDirectory\plus\some\more\MyEvent.as


This is all terrific.

But the actual code that uses MyEvent looks like this:
                    var d.MyEvent = new MyEvent();

What I really want to do is use gf while my cursor is on the word "MyEvent".

Right now, this leads to a not found.

If I change the setlocal path to this:
path=C:\MyDirectory\src\plus\some\more\**

Then it finds it, but since it is called "path", you don't really want to put /** on to the end of it, since it really isn't a path anymore.

I thought about using the findfile() function:
:echo findfile('MyEvent', &path)

This _only_ works when /** is on the path.


Also, I might also want to extend includeexpr, to call a function and use findfile() myself by modifying the &path and placing the /** on the end of each item in the comma separated list.  There are times in this case, where I might want to check my tags file if findfile() returned nothing.  Just ideas, but looking for flexibility.

So in the end, I am just looking for the _right way_ to do this in Vim that would not potentially mess up some of the other Vim bits and pieces that might rely on &path being in an _expected_ format.


So, what I really want I guess is the following:
1.  gf - look for the file in current directory (already does this)
2.  gf - look for the file in current path (already does this)
3.  gf - look for the file in current path after manipulating the name (already does this, suffixexadd)
4.  gf - look for the file in current path after manipulating the filename path (already does this, includeexpr)
5.  findfile() - pass a flag to recursively search the path (new)
6.  gf - if includeexpr is not found, call a function and perform more enhanced searching (new, let me use findfile() or taglist() and return the file I want opened).


Any ideas?

Thanks,
Dave

David Fishburn

oläst,
7 juni 2010 15:59:522010-06-07
till vim_use

So, what I really want I guess is the following:
1.  gf - look for the file in current directory (already does this)
2.  gf - look for the file in current path (already does this)
3.  gf - look for the file in current path after manipulating the name (already does this, suffixexadd)
4.  gf - look for the file in current path after manipulating the filename path (already does this, includeexpr)
5.  findfile() - pass a flag to recursively search the path (new)
6.  gf - if includeexpr is not found, call a function and perform more enhanced searching (new, let me use findfile() or taglist() and return the file I want opened).

So, includeexpr() can take a Vim function which allowed me to do this:

function FlexIncludeExpr(fname)
    let fname = a:fname
    let fname = substitute(fname,'\\.','/','g')
                                                                              
    for path in split(&path, ',')
        let fullpath = findfile(fname, substitute(path, '\w\zs$', '/**', '') )
                                                                              
        if fullpath != ''
            return fullpath
        endif
    endfor
                                                                              
    let file_tags = taglist(fname)
                                                                              
    for file_match in file_tags
        return file_match.filename
    endfor
                                                                              
    return fname
endfunction

So, that gives me #5.

What I am left with is #6.

I am not sure if I can combine these 2 together (especially if they are in the same file).
What I have above did work, but it simply left me on the same line which makes sense. 
I was hoping it would also take me to the match but it doesn't look like that is possible.

I tried changing this line to:
    for file_match in file_tags
        tag fname
        " return file_match.filename
    endfor

But it says you cannot use "tag" there.
The Dictionary returned from the taglist() call does not contain line numbers, so I can't navigate there myself either.

Dave

Svara alla
Svara författaren
Vidarebefordra
0 nya meddelanden