在获得一点理解之后,我不得不编辑整个问题。
在我的eclipse插件中,我使用CDT的CEditor
和CContentOutlinePage
用于语言接近C的文件。大纲有很多我不想错过的功能。但它也伴随着不良行为。当我打开两个具有相同名称的文件时,Outline视图始终显示首先打开的源文件的功能。
说,我有一个文件/subdir1/file.extension
和/subdir2/file.extension
具有不同的内容,我可以在它们之间切换而不更改大纲视图。
在我的编辑器中,我从超类getOutlinePage()
复制了方法CEditor
:
public class MyEditor extends CEditor {
private final String id = "de.blub.ide.myeditor";
private MyOutlinePage outlinePage;
/**
* Default constructor.
*/
public MyEditor() {
super();
outlinePage = new MyOutlinePage(this);
System.out.println("Working example");
}
/**
* Returns the outline page of the C/C++ editor.
* @return Outline page.
*/
public CContentOutlinePage getOutlinePage() {
if (outlinePage == null) {
outlinePage = new MyOutlinePage(this);
outlinePage.addSelectionChangedListener(this);
}
IEditorInput input = getEditorInput();
IWorkingCopyManager manager = CUIPlugin.getDefault().getWorkingCopyManager();
IWorkingCopy workingCopy = manager.getWorkingCopy(input);
if (workingCopy != outlinePage.getRoot()) {
outlinePage.setInput(workingCopy);
}
return outlinePage;
}
@SuppressWarnings("unchecked")
@Override
public <T> T getAdapter(Class<T> adapterClass) {
if (adapterClass.isAssignableFrom(IContentOutlinePage.class)) {
return (T) getOutlinePage();
}
return super.getAdapter(adapterClass);
}
}
这个类与supeclass具有完全相同的行为,但它让我有机会修改行为。我可以验证workingCopy
有错误的值,即使input
是正确的。虽然input
的路径是/project/subdir1/file.extension
,但workingCopy
有一个指向`/project/subdir2/file.extension的资源。
这是由于CDT核心的一个错误。 CElement中的equals方法检查类型,名称和父级,但不检查资源:
public static boolean equals(ICElement lhs, ICElement rhs) {
if (lhs == rhs) {
return true;
}
if (lhs.getElementType() != rhs.getElementType()) {
return false;
}
String lhsName = lhs.getElementName();
String rhsName = rhs.getElementName();
if (lhsName == null || rhsName == null || lhsName.length() != rhsName.length() || !lhsName.equals(rhsName)) {
return false;
}
if (lhs instanceof ISourceReference && rhs instanceof ISourceReference) {
if (((ISourceReference) lhs).getIndex() != ((ISourceReference)rhs).getIndex()) {
return false;
}
}
ICElement lhsParent = lhs.getParent();
ICElement rhsParent = rhs.getParent();
if (lhsParent == rhsParent) {
return true;
}
return lhsParent != null && lhsParent.equals(rhsParent);
}
由于getParent()返回项目,如果两个文件位于同一项目内,则该方法返回true。要关注此错误的更新,请参阅此处:https://bugs.eclipse.org/bugs/show_bug.cgi?id=546295