Geany的"跳轉到標記定義“功能如何使用


geany logo

Geany是個比較輕量級的代碼編輯器,在一些不怎么需要編輯的代碼上,我比較常用它來瀏覽代碼。不過它的 跳轉到標記定義(Go to tag definition) 功能有點奇怪,一開始死活不知道怎么用,所以值得拿出來說一下。

問題

其實Geany內置了分析代碼文件的能力(記得是移植了ctags的代碼進來),當打開代碼文件的時候,能夠分析文件中的tags(包括類、函數和變量)並在 標記(Symbols) 側邊欄列出來, 也能夠在文件內或者已經打開的文件間進行跳轉(go to tag definition),但不能跳轉到未打開的文件 —— Geany稱這類tags為workspace tags

P.S. Geany還有一類Global tags,按不同編程語言提供(比如 /usr/share/geany/php.tags/usr/share/geany/python.tags,另外網站上還提供了一些額外的tags包),文件內容里不包含符號來源信息,所以只能用於自動補全和顯示calltip,跟"跳轉到標記定義"功能無關)。

解決方法

如何能夠實現跳轉到未打開的文件呢?在網上搜到如下信息:

This feature (navigating through the source code) has limitations when used alone in Geany. It can only look for definitions in the files being opened. So, to enable it to look for definitions in other closed files in the same folder, you must use this feature inside a certain project created by Geany's built-in Project feature, with the File patterns defined, and combine it with other project plugins that support Generate file list on load.
-- Top Geany features you need to know about

簡單地說,方法是: 得定義工程,並啟用GProject插件,在工程配置里配置好 source files 文件名匹配模式(比如*.cpp *.c *.h)並勾選上GProject頁面的Generate tags for all project files。然后就可以使用了。

解決方法2: 新插件GeanyCtags

雖然用上面的方法已經可以跳轉了,但有幾個缺點:

  • tags是每次打開工程時動態分析得到的,如果工程比較小(比如只有100個左右的文件)的話,速度還行,但如果工程比較大就悲劇了
  • 由於tags的分析完全是geany自己在玩,用戶就沒法用ctags的一些配置參數選項來調整或者改進了

於是,在今年年初,Geany開發組就單獨做了一個GeanyCtags插件。不過沒想到快半年過去了,Geany還是沒發布一個新版本,所以要用的朋友就只能自己編譯。

安裝方法

推薦用Geany自帶的 waf 來編譯(至少在Windows下只能用這個。雖然 http://www.geany.org/Support/BuildingOnWin32 這里說的是用 mingw32-make -f makefile.win32, 但 geany-plugins 的daily snapshot 包並沒有提供 makefile.win32):

    wget -c http://download.geany.org/geany_plugins_git.tar.gz
    tar zxf geany_plugins_git.tar.gz
    cd geany_plugins_git
    ./waf configure --enable-plugins=geanyctags,automark
    ./waf build
    sudo make install

然后在Geany的插件管理里面啟用 GeanyCtags 插件就可以了。

P.S. 一個好消息是, 這個插件可以在Geany 1.24下編譯成功,這樣你可以在Geany 1.24里面使用 。但其它一些新插件不一定能行,因為可能用到了Geany主程序的新變動,比如 ProjectOrganizer 插件。

使用方法

0.. 當然,首先得安裝Exuberant Ctags

1.. 在項目屬性中確保 File patterns 已經設置(比如設為 *.rb *.rake)

2.. 點擊菜單 Project -> Generate tags 生成tags文件,Geany會在底部的信息欄顯示生成tags文件的命令和結果

GeanyCtags generate tags

3.. 在符號(函數名、變量名)上點擊右鍵,可以看到頂部新增了兩個菜單項目: Find Tag Definition (GeanyCtags)Find Tag Declartion (GeanyCtags)

GeanyCtags context menu items

4.. 主菜單里面的 Project -> Find tag 可以用來跳轉到任意tag,自己輸入就是了

補充說明

  • GeanyCtags生成Tags文件時會在信息欄顯示生成tags文件的命令和結果:

find . -not -path '/.' ( -name ".c" -o -name ".h" ) | ctags --totals --fields=fKsSt --extra=-fq --c-kinds=+p --sort=foldcase --excmd=number -L - -f /users/bamanzi/farm/geany_plugins_git/geany_plugins.tags (in directory: /users/bamanzi/farm/geany_plugins_git)
409 files, 117412 lines (3410 kB) scanned in 0.2 seconds (17947 kB/s)
10250 tags added to tag file
10250 tags sorted in 0.01 seconds

  • 從上面的信息可以看出,生成的tags文件跟工程文件在同一個目錄,不過文件名並不是 tags ,而是 prjname.tags
  • 從上面的信息可以看出,tags文件是調用ctags命令生成的,所以格式上是標准的ctags輸出。如果你有特殊的配置選項(比如需要增加對vbs的支持),你知道怎么下手了吧

參考:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM