1.前言
在使用Eclipse CDT的過程中,如果要使用代碼提示功能,有2種方法:
- 設置觸發字符,輸入這些字符后會自動觸發代碼提示
- 使用快捷鍵 ALT+/,手動觸發該功能
方法1的設置方法如下圖,可以設置 . -> :: 三種觸發類型。
但是對於其他C/C++好用的IDE,這種方式顯然有點弱,那么如何將它改造成全自動提示呢?
方法是修改CDT。。。
2.CDT開發環境搭建
本文的開發環境是Manjaro,需要安裝git,maven
yay -S git yay -S maven
git clone https://github.com/eclipse-cdt/cdt.git
git checkout CDT_9_7_0
cd cdt
mvn package
然后經過幾十分鍾的構建,可以編譯成功。
下載Eclipse Committers:https://www.eclipse.org/downloads/packages/release/2019-03/r/eclipse-ide-eclipse-committers
下載完成,打開菜單 Help -> Install New Software,然后如圖安裝新組件:
將CDT目錄導入到Eclipse中,將所有包全部導入
找到org.eclipse.cdt.target這個包,打開其中的cdt.target,右上角點擊Set As Active Target Platform
同樣打開cdt-baseline.target並點擊
然后等待好久,等聯網下載完成
點擊菜單Project -> Build All
右鍵選擇org.eclipse.cdt.ui這個包,右鍵菜單Run As Eclipse Application
不出意外,新的Eclipse就會打開,原Eclipse的下方Console窗口打印新Eclipse的log輸出
至此,CDT開發環境搭建完成
3.代碼修改
接下來到了激動人心的時刻,修改代碼使其達到我們想要的功能dsa
首先打開 cdt/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CContentAssistProcessor.java ,找到verifyAutoActivation方法,按照下面修改,添加2行:
這個方法的功能是判斷當前位置的字符是否可以觸發自動提示,default分之的加入是為了處理我們的場景,Character.isAlphabetic過濾到非a-z A-Z的字符
@Override protected boolean verifyAutoActivation(ITextViewer viewer, int offset) { IDocument doc = viewer.getDocument(); System.out.println("verifyAutoActivation"); if (doc == null) { return false; } if (offset <= 0) { return false; } try { char activationChar = doc.getChar(--offset); switch (activationChar) { case ':': return offset > 0 && doc.getChar(--offset) == ':'; case '>': return offset > 0 && doc.getChar(--offset) == '-'; case '.': // Avoid completion of float literals CHeuristicScanner scanner = new CHeuristicScanner(doc); int token = scanner.previousToken(--offset, Math.max(0, offset - 200)); // The scanner reports numbers as identifiers if (token == Symbols.TokenIDENT && !Character.isJavaIdentifierStart(doc.getChar(scanner.getPosition() + 1))) { // Not a valid identifier return false; } return true; //add start default: return Character.isAlphabetic(activationChar); //add end } } catch (org.eclipse.jface.text.BadLocationException e) { } return false; } }
找到setCompletionProposalAutoActivationCharacters方法,按照下面修改:
這里的修改是為了加上字母觸發代碼提示
public void setCompletionProposalAutoActivationCharacters(char[] activationSet) { String myAddCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; //$NON-NLS-1$ myAddCharacters += new String(activationSet); fCompletionAutoActivationCharacters = myAddCharacters.toCharArray(); //fCompletionAutoActivationCharacters = activationSet; }
至此修改完畢,右鍵選擇org.eclipse.cdt.ui這個包,右鍵菜單Run As Eclipse Application,試驗一下,發現已經可以了
4.效果展示
5.插件打包
右鍵選擇org.eclipse.cdt.ui這個包,右鍵菜單Deployable Plug-ins and fragments
選擇導出目錄,點擊Finish
在導出目錄的plugins子目錄可以找到jar,
在eclipse/plugins中有一個org.eclipse.cdt.ui_6.4.100.*****.jar,記錄該文件名並備份,然后把新的jar拷貝過來並改名,大功告成!
如果到了目標eclipse環境還是不能使用的情況,推測原因可能是版本不配套,此時可能需要導出org.eclipse.jface.text
首先是導入這個包 Window -> Show View -> Other ->Plug-ins,
在Plug-ins窗口選中org.eclipse.jface.text,右鍵Import As -> Source Project
然后和上面那個一樣的方法,導出jar,拷貝到目標eclipse環境中