Vim script to switch between header and implementation file using cscope
Asked Answered
C

1

2

I currently use the A.vim plugin to swap between header and implementation file. The limitation of this script is that it only works if both are in the same folder.

If you have a cscope database for your code you can easily find the header for a particular implementation file by doing :cscope find f ImplementationFileName.h.

How would you script this to take the current file name without the extension and search for that name with the added .h suffix?

Callosity answered 24/2, 2011 at 17:49 Comment(1)
no cscope requirement: #17171402Compensatory
G
5

This should be possible out of the box using A.vim. Check out :help alternate-config. Specifically the section on search paths:

b) Search Paths: In many projects the location of the source files and the corresponding header files is not always the same directory. This plugin allows the search path it uses to locate source and header files to be configured. The search path is specified by setting the g:alternateSearchPath variable. The default setting is as follows,

g:alternateSearchPath = 'sfr:../source,sfr:../src,sfr:../include,sfr:../inc'

This indicates that the corresponding file will be searched for in ../source, ../src. ../include and ../inc all relative to the current file being switched from. The value of the g:alternateSearchPath variable is simply a comma separated list of prefixes and directories. The "sfr:" prefix indicates that the path is relative to the file. Other prefixes are "wdr:" which indicates that the directory is relative to the current working directory and "abs:" which indicates the path is absolute. If no prefix is specified "sfr:" is assumed.

Gainer answered 24/2, 2011 at 19:48 Comment(1)
Great tip. The advantage of the cscope approach is that it doesn't matter where the include files are. The reason it doesn't work sometimes is that this particular project has include files in a non-standard location. Rather than add-in project specific paths to g:alternateSearchPath, it would be nice to fall back to my cscope index.Callosity

© 2022 - 2024 — McMap. All rights reserved.