From ccabb31c6544f68b4e69383b35a6697e267788be Mon Sep 17 00:00:00 2001
From: Anton Leherbauer <anton.leherbauer@windriver.com>
Date: Fri, 25 Jan 2008 10:08:32 +0000
Subject: [PATCH] More mark occurrences tests and fixes

---
 .../core/dom/parser/cpp/CPPVisitor.java       | 15 +++--
 .../resources/ceditor/occurrences.cpp         | 17 +++--
 .../resources/ceditor/occurrences.h           |  6 ++
 .../cdt/ui/tests/text/BasicCEditorTest.java   |  9 ++-
 .../cdt/ui/tests/text/EditorTestHelper.java   | 15 +++--
 .../cdt/ui/tests/text/MarkOccurrenceTest.java | 62 ++++++++++++++++++-
 .../cdt/internal/ui/editor/CEditor.java       | 18 +++---
 7 files changed, 112 insertions(+), 30 deletions(-)
 create mode 100644 core/org.eclipse.cdt.ui.tests/resources/ceditor/occurrences.h

diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java
index 3752a9698c5..736bcde0c9d 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java
@@ -1408,15 +1408,15 @@ public class CPPVisitor {
 			        boolean found = false;
 			        if( binding instanceof ICPPUsingDeclaration ){
 	                    try {
-	                        found = ArrayUtil.contains( ((ICPPUsingDeclaration)binding).getDelegates(), candidate ); 
+	                        found = ArrayUtil.containsEqual( ((ICPPUsingDeclaration)binding).getDelegates(), candidate ); 
 	                    } catch ( DOMException e ) {
 	                    }
 				    } else if( potential instanceof ICPPUsingDeclaration ){
-				        found = ( binding == ((ICPPDelegate)candidate).getBinding() );   
+				        found = sameBinding(binding, ((ICPPDelegate)candidate).getBinding());   
 				    } else {
-				    	found = (binding == candidate );
+				    	found = sameBinding(binding, candidate);
 				    }
-				        
+			        
 				    if( found ){
 						if( refs.length == idx ){
 							IASTName [] temp = new IASTName[ refs.length * 2 ];
@@ -1433,6 +1433,13 @@ public class CPPVisitor {
 			}
 			return PROCESS_CONTINUE;
 		}
+		private boolean sameBinding(IBinding binding1, IBinding binding2) {
+			if (binding1 == binding2)
+				return true;
+			if (binding1.equals(binding2))
+				return true;
+			return false;
+		}
 		public IASTName[] getReferences(){
 			if( idx < refs.length ){
 				IASTName [] temp = new IASTName[ idx ];
diff --git a/core/org.eclipse.cdt.ui.tests/resources/ceditor/occurrences.cpp b/core/org.eclipse.cdt.ui.tests/resources/ceditor/occurrences.cpp
index f0e9e934475..f7f808153ca 100644
--- a/core/org.eclipse.cdt.ui.tests/resources/ceditor/occurrences.cpp
+++ b/core/org.eclipse.cdt.ui.tests/resources/ceditor/occurrences.cpp
@@ -1,3 +1,5 @@
+#include "occurrences.h"
+
 #define INT      int
 #define FUNCTION_MACRO(arg) globalFunc(arg)
 #define EMPTY_MACRO(arg) 
@@ -22,18 +24,19 @@ class Base1 {
 	Base1();
 	~Base1();
 };
-class Base2 {
-};
 
 Base1::~Base1() {}
 Base1::Base1() {}
 
+Base2::Base2() {}
+void Base2::foo() {}
+
 class ClassContainer : Base1, Base2 {
 public:
 	static int staticPubField;
 	const int constPubField;
 	const static int constStaticPubField;
-	int pubField;
+	size_t pubField;
 
 	static INT staticPubMethod(int arg) {
 		FUNCTION_MACRO(arg);
@@ -82,6 +85,7 @@ int namespaceFunc() {
 	case ONE: case THREE:
 		return 1;
 	}
+	size_t size;
 	return namespaceVar;
 }
 }
@@ -92,9 +96,12 @@ INT ClassContainer::pubMethod() {
 	return pubField + localVar;
 }
 
+using namespace ns;
+//using ns::namespaceVar;
+
 INT ClassContainer::staticPrivMethod() {
 	CppStruct* st= new CppStruct();
-	st->structField= 1;
+	st->structField= namespaceVar;
 	CppUnion un;
 	un.unionField= 2;
 	staticPubMethod(staticPubField);
@@ -108,7 +115,7 @@ label:
 template<int X>
 class ConstantTemplate {
 public:
-	int foo(int y) {
+	size_t foo(size_t y) {
 		return X;
 	}
 };
diff --git a/core/org.eclipse.cdt.ui.tests/resources/ceditor/occurrences.h b/core/org.eclipse.cdt.ui.tests/resources/ceditor/occurrences.h
new file mode 100644
index 00000000000..65c2173170b
--- /dev/null
+++ b/core/org.eclipse.cdt.ui.tests/resources/ceditor/occurrences.h
@@ -0,0 +1,6 @@
+class Base2 {
+	Base2();
+	void foo();
+};
+
+typedef int size_t;
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/BasicCEditorTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/BasicCEditorTest.java
index 7d497f64a48..bd70f5a05e8 100644
--- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/BasicCEditorTest.java
+++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/BasicCEditorTest.java
@@ -19,7 +19,6 @@ import junit.framework.TestSuite;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.core.runtime.Path;
 import org.eclipse.jface.text.DocumentEvent;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.IDocumentListener;
@@ -125,7 +124,7 @@ public class BasicCEditorTest extends BaseUITestCase {
 
 	public void testEditTranslationUnit() throws Exception {
 		final String file= "/ceditor/src/main.cpp";
-		fCProject= EditorTestHelper.createCProject("ceditor", "resources/ceditor", false);
+		fCProject= EditorTestHelper.createCProject("ceditor", "resources/ceditor", false, false);
 		IFile mainFile= ResourceTestHelper.findFile(file);
 		assertNotNull(mainFile);
 		setUpEditor(mainFile);
@@ -187,7 +186,7 @@ public class BasicCEditorTest extends BaseUITestCase {
 	//	}
 	//}
 	public void testEditNewTranslationUnit() throws Exception {
-		fCProject= EditorTestHelper.createCProject("ceditor", "resources/ceditor", false);
+		fCProject= EditorTestHelper.createCProject("ceditor", "resources/ceditor", false, false);
 		IFile newFile= createFile(fCProject.getProject(), "Point.cpp", "");
 		assertNotNull(newFile);
 		setUpEditor(newFile);
@@ -261,7 +260,7 @@ public class BasicCEditorTest extends BaseUITestCase {
 
 	public void testEditExternalTranslationUnit() throws Exception {
 		final String file= "/ceditor/src/main.cpp";
-		fCProject= EditorTestHelper.createCProject("ceditor", "resources/ceditor", false);
+		fCProject= EditorTestHelper.createCProject("ceditor", "resources/ceditor", false, false);
 		IFile mainFile= ResourceTestHelper.findFile(file);
 		assertNotNull(mainFile);
 		File tmpFile= File.createTempFile("tmp", ".cpp");
@@ -306,7 +305,7 @@ public class BasicCEditorTest extends BaseUITestCase {
 		colorMgr.bindColor(ICColorConstants.PP_DIRECTIVE, new RGB(7,7,7));
 		final Color ppDirectiveColor= colorMgr.getColor(ICColorConstants.PP_DIRECTIVE);
 		final String file= "/ceditor/src/main.cpp";
-		fCProject= EditorTestHelper.createCProject("ceditor", "resources/ceditor", false);
+		fCProject= EditorTestHelper.createCProject("ceditor", "resources/ceditor", false, false);
 		setUpEditor(file);
 		fSourceViewer= EditorTestHelper.getSourceViewer(fEditor);
 		assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 0, 10000, 100));
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/EditorTestHelper.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/EditorTestHelper.java
index f07ba3cc1f5..f218fe25e81 100644
--- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/EditorTestHelper.java
+++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/EditorTestHelper.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2007 IBM Corporation and others.
+ * Copyright (c) 2000, 2008 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -72,7 +72,6 @@ import org.eclipse.ui.wizards.datatransfer.IImportStructureProvider;
 import org.eclipse.ui.wizards.datatransfer.ImportOperation;
 
 import org.eclipse.cdt.core.CCorePlugin;
-import org.eclipse.cdt.core.dom.IPDOMManager;
 import org.eclipse.cdt.core.index.IIndexManager;
 import org.eclipse.cdt.core.model.ICProject;
 import org.eclipse.cdt.core.testplugin.CProjectHelper;
@@ -434,11 +433,15 @@ public class EditorTestHelper {
 	}
 	
 	public static ICProject createCProject(String project, String externalSourceFolder) throws CoreException {
-		return createCProject(project, externalSourceFolder, false);
+		return createCProject(project, externalSourceFolder, false, false);
 	}
 	
 	public static ICProject createCProject(String project, String externalSourceFolder, boolean linkSourceFolder) throws CoreException {
-		ICProject cProject= CProjectHelper.createCCProject(project, "bin", IPDOMManager.ID_NO_INDEXER);
+		return createCProject(project, externalSourceFolder, linkSourceFolder, false);
+	}
+
+	public static ICProject createCProject(String project, String externalSourceFolder, boolean linkSourceFolder, boolean useIndexer) throws CoreException {
+		ICProject cProject= CProjectHelper.createCCProject(project, "bin", useIndexer ? IIndexManager.ID_FAST_INDEXER : IIndexManager.ID_NO_INDEXER);
 		IFolder folder;
 		if (linkSourceFolder)
 			folder= ResourceHelper.createLinkedFolder((IProject) cProject.getUnderlyingResource(), new Path("src"), CTestPlugin.getDefault(), new Path(externalSourceFolder));
@@ -449,6 +452,10 @@ public class EditorTestHelper {
 		Assert.assertNotNull(folder);
 		Assert.assertTrue(folder.exists());
 		CProjectHelper.addCContainer(cProject, "src");
+		if (useIndexer) {
+			IIndexManager indexManager= CCorePlugin.getIndexManager();
+			indexManager.joinIndexer(5000, new NullProgressMonitor());
+		}
 		return cProject;
 	}
 
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/MarkOccurrenceTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/MarkOccurrenceTest.java
index 477c73bb382..61f4892fb0b 100644
--- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/MarkOccurrenceTest.java
+++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/MarkOccurrenceTest.java
@@ -16,7 +16,6 @@ import java.util.Iterator;
 
 import junit.extensions.TestSetup;
 import junit.framework.Test;
-import junit.framework.TestCase;
 import junit.framework.TestSuite;
 
 import org.eclipse.core.resources.IFile;
@@ -50,6 +49,7 @@ import org.eclipse.cdt.core.model.ICProject;
 import org.eclipse.cdt.core.testplugin.CProjectHelper;
 import org.eclipse.cdt.ui.CUIPlugin;
 import org.eclipse.cdt.ui.PreferenceConstants;
+import org.eclipse.cdt.ui.tests.BaseUITestCase;
 
 import org.eclipse.cdt.internal.ui.editor.CEditor;
 import org.eclipse.cdt.internal.ui.viewsupport.ISelectionListenerWithAST;
@@ -61,7 +61,7 @@ import org.eclipse.cdt.internal.ui.viewsupport.SelectionListenerWithASTManager;
  * 
  * @since 5.0
  */
-public class MarkOccurrenceTest extends TestCase {
+public class MarkOccurrenceTest extends BaseUITestCase {
 	
 	private static final String PROJECT = "MarkOccurrenceTest";
 
@@ -87,7 +87,7 @@ public class MarkOccurrenceTest extends TestCase {
 		}
 		protected void setUp() throws Exception {
 			super.setUp();
-			fCProject= EditorTestHelper.createCProject(PROJECT, "resources/ceditor");
+			fCProject= EditorTestHelper.createCProject(PROJECT, "resources/ceditor", false, true);
 		}
 		protected void tearDown () throws Exception {
 			if (fCProject != null)
@@ -193,6 +193,34 @@ public class MarkOccurrenceTest extends TestCase {
 		assertOccurrencesInWidget();
 	}
 
+	public void testMarkTypeOccurrences3() {
+		try {
+			fMatch= fFindReplaceDocumentAdapter.find(0, "Base2", true, true, true, false);
+		} catch (BadLocationException e) {
+			fail();
+		}
+		assertNotNull(fMatch);
+
+		fEditor.selectAndReveal(fMatch.getOffset(), fMatch.getLength());
+		
+		assertOccurrences(3);
+		assertOccurrencesInWidget();
+	}
+
+	public void testMarkTypedefOccurrences() {
+		try {
+			fMatch= fFindReplaceDocumentAdapter.find(0, "size_t", true, true, true, false);
+		} catch (BadLocationException e) {
+			fail();
+		}
+		assertNotNull(fMatch);
+
+		fEditor.selectAndReveal(fMatch.getOffset(), fMatch.getLength());
+		
+		assertOccurrences(4);
+		assertOccurrencesInWidget();
+	}
+
 	public void testMarkClassTemplateOccurrences() {
 		try {
 			fMatch= fFindReplaceDocumentAdapter.find(0, "TemplateClass", true, true, true, false);
@@ -380,6 +408,34 @@ public class MarkOccurrenceTest extends TestCase {
 		assertOccurrencesInWidget();
 	}
 
+	public void testMarkNamespaceOccurrences() {
+		try {
+			fMatch= fFindReplaceDocumentAdapter.find(0, "ns", true, true, true, false);
+		} catch (BadLocationException e) {
+			fail();
+		}
+		assertNotNull(fMatch);
+
+		fEditor.selectAndReveal(fMatch.getOffset(), fMatch.getLength());
+		
+		assertOccurrences(3);
+		assertOccurrencesInWidget();
+	}
+
+	public void testMarkNamespaceVariableOccurrences() {
+		try {
+			fMatch= fFindReplaceDocumentAdapter.find(0, "namespaceVar", true, true, true, false);
+		} catch (BadLocationException e) {
+			fail();
+		}
+		assertNotNull(fMatch);
+
+		fEditor.selectAndReveal(fMatch.getOffset(), fMatch.getLength());
+		
+		assertOccurrences(4);
+		assertOccurrencesInWidget();
+	}
+
 	public void testNoOccurrencesIfDisabled() {
 		CUIPlugin.getDefault().getPreferenceStore().setValue(PreferenceConstants.EDITOR_MARK_OCCURRENCES, false);
 		fOccurrences= Integer.MAX_VALUE;
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java
index 26e179deceb..5de7312cb03 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java
@@ -1093,8 +1093,6 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC
 	public static final String INACTIVE_CODE_ENABLE = "inactiveCodeEnable"; //$NON-NLS-1$
 	/** Preference key for inactive code painter color */
 	public static final String INACTIVE_CODE_COLOR = "inactiveCodeColor"; //$NON-NLS-1$
-	/** Preference key for code formatter tab size */
-	private final static String CODE_FORMATTER_TAB_SIZE = DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE;
 	/** Preference key for inserting spaces rather than tabs */
 	public final static String SPACES_FOR_TABS = DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR;
 	/** Preference key for automatically closing strings */
@@ -1383,11 +1381,6 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC
 					return;
 				}
 
-				if (CODE_FORMATTER_TAB_SIZE.equals(property) && isTabsToSpacesConversionEnabled()) {
-					uninstallTabsToSpacesConverter();
-					installTabsToSpacesConverter();
-				}
-
 				if (DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE.equals(property)
 						|| DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE.equals(property)
 						|| DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR.equals(property)) {
@@ -1395,6 +1388,12 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC
 					int tabWidth= getSourceViewerConfiguration().getTabWidth(asv);
 					if (textWidget.getTabs() != tabWidth)
 						textWidget.setTabs(tabWidth);
+					if (isTabsToSpacesConversionEnabled()) {
+						uninstallTabsToSpacesConverter();
+						installTabsToSpacesConverter();
+					} else {
+						updateIndentPrefixes();
+					}
 					return;
 				}
 
@@ -2596,13 +2595,14 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC
 	protected String[] collectContextMenuPreferencePages() {
 		// Add C/C++ Editor relevant pages
 		String[] parentPrefPageIds = super.collectContextMenuPreferencePages();
-		String[] prefPageIds = new String[parentPrefPageIds.length + 9];
+		String[] prefPageIds = new String[parentPrefPageIds.length + 10];
 		int nIds = 0;
 		prefPageIds[nIds++] = "org.eclipse.cdt.ui.preferences.CEditorPreferencePage"; //$NON-NLS-1$
 		prefPageIds[nIds++] = "org.eclipse.cdt.ui.preferences.CodeAssistPreferencePage"; //$NON-NLS-1$
 		prefPageIds[nIds++] = "org.eclipse.cdt.ui.preferences.CodeAssistPreferenceAdvanced"; //$NON-NLS-1$
 		prefPageIds[nIds++] = "org.eclipse.cdt.ui.preferences.HoverPreferencePage"; //$NON-NLS-1$
 		prefPageIds[nIds++] = "org.eclipse.cdt.ui.preferences.FoldingPreferencePage"; //$NON-NLS-1$
+		prefPageIds[nIds++] = "org.eclipse.cdt.ui.preferences.MarkOccurrencesPreferencePage"; //$NON-NLS-1$
 		prefPageIds[nIds++] = "org.eclipse.cdt.ui.preferences.CodeColoringPreferencePage"; //$NON-NLS-1$
 		prefPageIds[nIds++] = "org.eclipse.cdt.ui.preferences.TemplatePreferencePage"; //$NON-NLS-1$
 		prefPageIds[nIds++] = "org.eclipse.cdt.ui.preferences.SmartTypingPreferencePage"; //$NON-NLS-1$
@@ -2645,7 +2645,7 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC
 		}
 
 		// delayed installation of mark occurrences
-		if (isMarkingOccurrences() && !fMarkOccurrenceAnnotations)
+		if (!fMarkOccurrenceAnnotations && isMarkingOccurrences())
 			getSite().getShell().getDisplay().asyncExec(new Runnable() {
 				public void run() {
 					installOccurrencesFinder(true);