Eclipse CDT是個很好的C/C++ IDE,但是其編輯器卻有個非常糟糕的槽點:代碼自動完成只支持由‘.’、‘:’、‘>’這三個字符觸發,這比大多數C/C++ IDE都差得多。這個問題很早就被提出來,並且解決的辦法並不複雜,很多人都給出瞭解決方案,但是不知道爲什麼CDT的作者一直沒有改進。既然原生CDT功能不足,那就自己動手,豐衣足食。
以下製作方法來源於網上素材,我在這裏不過是重新整理了一遍,沒啥創造性。寫這篇博客不過是爲了留下文字,免得以後還要改造的時候找不到相關的帖子了。
1、首先要下載eclipse插件開發環境,在eclipse官網下載Eclipse for RCP and RAP Developpers:
2、解壓縮並運行Eclipse for RCP and RAP;
3、執行Help菜單的Install New Software菜單項,在彈出對話框的“Work with”處填寫地址:http://download.eclipse.org/tools/cdt/releases/9.10,不過,隨着CDT的升級,這個地址後面的版本號可能會不同,比如可能會變爲10.0、10.1之類。
4、回車之後,對話框下方會列出可用的組件,勾選CDT Main Features和CDT Optional Features兩個組件:
5、點擊“Next”按鈕後,eclipse會加載勾選的CDT組件。
6、等組件下載完成後,選擇Window菜單中的Show View子菜單中的Plug-Ins菜單項:
7、在左側的plug-ins列表中找到org.eclipse.cdt.ui:
8、鼠標右擊org.eclipse.cdt.ui插件,在彈出菜單中選擇“import as ...”子菜單的Source Project菜單項,將CDT插件源代碼導入到當前工作臺中:
9、切換到左側的Project Explorer界面,在src文件夾下找到org.eclipse.cdt.internal.ui.text.contentassist包:
10、打開org.eclipse.cdt.internal.ui.text.contentassist包裏面的CContentAssistProcessor.java,找到protected boolean verifyAutoActivation(ITextViewer viewer, int offset)函數,做如下修改:
@Override
protected boolean verifyAutoActivation(ITextViewer viewer, int offset) {
IDocument doc = viewer.getDocument();
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;
default:
// return ((activationChar >= 97) && (activationChar <= 122)) ? true : (activationChar >= 65 && activationChar <= 90);
return Character.isAlphabetic(activationChar);
}
} catch (BadLocationException e) {
}
return false;
}
上述代碼是在原本代碼的switch塊中加了default分支,用於對“:>.”之外的字符進行處理。
11、打開org.eclipse.cdt.internal.ui.text.contentassist包裏面的ContentAssistProcessor.java,找到public void setCompletionProposalAutoActivationCharacters(char[] activationSet)函數,做如下修改:
public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
// fCompletionAutoActivationCharacters = activationSet;
String myActivationSet = ".:>ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char[] triggers = myActivationSet.toCharArray();
fCompletionAutoActivationCharacters = triggers;
}
這一處修改是重新定義了觸發自動完成功能的字符序列,新的字符序列由myActivationSet定義。
至此,代碼修改工作已經完成,接下來就是編譯和打包導出。
12、鼠標右擊org.eclipse.cdt.ui,在彈出菜單中選擇“Export ...”:
13、在彈出對話框中選擇JAR File:
14、點擊finish按鈕後,即可生成修改版的org.eclipse.cdt.ui.v.vv.xxxxxxxxxxxx.jar插件包(其中v.vv.xxxxxxxxxxxx是插件的版本號,跟當時下載的CDT插件版本有關),用這個插件包替換掉原生版的org.eclipse.cdt.ui.v.vv.xxxxxxxxxxxx.jar文件即可。需要注意的是,原生版CDT的jar文件的版本號可能跟修改所用的CDT代碼版本號不同,只需要把修改後的jar軟件版本號重命名爲原生版cdt.ui插件版本號即可。
15、展示效果: