Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] Code Template Fix for PR 31679

Folks,

This is a patch to address PR 31679 where we don't have a C++ context but only a C context for the Code Templates. This means that you get C++ templates like try/catch available even when
you are doing work on C code.  This patch works to address that by:
- Adding a C++ context and moving those templates which are C++ specific to that context - Allowing you to use the C++ and C contexts when you are working in C++ code,
  but only giving you the C contexts when you are working in C code.

In order to determine what the context should be (this is a file specific thing) I look at the extension and if I can't discern from the extension (ie a .h file perhaps) I go to the project and look for a C++ or C nature. I would like to have a more generic way of collecting C vs C++ file extensions (so the user could configure them) and/or differentiating on a file by file basis but that is another issue for discussion.

Please use this as the change log if the patch is accepted.

--

Index: src/org/eclipse/cdt/internal/corext/template/ContextTypeRegistry.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/ContextTypeRegistry.java,v
retrieving revision 1.1
diff -u -r1.1 ContextTypeRegistry.java
--- src/org/eclipse/cdt/internal/corext/template/ContextTypeRegistry.java 26 Jun 2002 20:55:44 -0000 1.1 +++ src/org/eclipse/cdt/internal/corext/template/ContextTypeRegistry.java 12 Feb 2003 17:47:14 -0000
@@ -6,6 +6,8 @@
 */

import org.eclipse.cdt.internal.corext.template.c.CContextType;
+import org.eclipse.cdt.internal.corext.template.c.CppContextType;
+
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@@ -63,7 +65,7 @@
    // XXX bootstrap with C and C++ types
    private ContextTypeRegistry() {
        add(new CContextType());
-        //add(new CppContextType());
+        add(new CppContextType());
    }

}
Index: src/org/eclipse/cdt/internal/corext/template/default-templates.xml
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/default-templates.xml,v
retrieving revision 1.1
diff -u -r1.1 default-templates.xml
--- src/org/eclipse/cdt/internal/corext/template/default-templates.xml 26 Jun 2002 20:55:44 -0000 1.1 +++ src/org/eclipse/cdt/internal/corext/template/default-templates.xml 12 Feb 2003 17:47:14 -0000
@@ -56,13 +56,13 @@
    ${cursor}
}</template>

-  <template description="try catch block" name="try" context="C"
+  <template description="try catch block" name="try" context="C++"
>try {
    ${cursor}
} catch (${Exception} e) {
}</template>

-  <template description="catch block" name="catch" context="C"
+  <template description="catch block" name="catch" context="C++"
>catch (${Exception} e) {
    ${cursor}
}</template>
@@ -74,22 +74,22 @@
    ${cursor}
}</template>

-  <template description="class declaration" name="class" context="C"
+  <template description="class declaration" name="class" context="C++"
>class ${name} {
    ${cursor}
private:
};</template>

-  <template description="using a namespace" name="using" context="C"
+  <template description="using a namespace" name="using" context="C++"
 >using namespace ${namespace};
</template>

- <template description="namespace declaration" name="namespace" context="C" + <template description="namespace declaration" name="namespace" context="C++"
 >namespace ${namespace} {
    ${cursor}
}</template>

-  <template description="create new object" name="new" context="C"
+  <template description="create new object" name="new" context="C++"
>${type} ${name} = new ${type}(${arguments});
</template>

Index: src/org/eclipse/cdt/internal/ui/preferences/TemplatePreferencePage.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/TemplatePreferencePage.java,v
retrieving revision 1.5
diff -u -r1.5 TemplatePreferencePage.java
--- src/org/eclipse/cdt/internal/ui/preferences/TemplatePreferencePage.java 4 Feb 2003 20:00:46 -0000 1.5 +++ src/org/eclipse/cdt/internal/ui/preferences/TemplatePreferencePage.java 12 Feb 2003 17:47:12 -0000
@@ -356,8 +356,8 @@
Template template= new Template();

-        ContextTypeRegistry registry=ContextTypeRegistry.getInstance();
-        ContextType type= registry.getContextType("java");
+        ContextTypeRegistry registry= ContextTypeRegistry.getInstance();
+        ContextType type = registry.getContextType("C");
String contextTypeName;
        if (type != null)
Index: src/org/eclipse/cdt/internal/ui/text/CCompletionProcessor.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CCompletionProcessor.java,v
retrieving revision 1.6
diff -u -r1.6 CCompletionProcessor.java
--- src/org/eclipse/cdt/internal/ui/text/CCompletionProcessor.java 6 Feb 2003 20:48:31 -0000 1.6 +++ src/org/eclipse/cdt/internal/ui/text/CCompletionProcessor.java 12 Feb 2003 17:47:12 -0000
@@ -8,6 +8,7 @@
import org.eclipse.cdt.core.index.ITagEntry;
import org.eclipse.cdt.core.index.IndexModel;
import org.eclipse.cdt.core.index.TagFlags;
+import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.internal.corext.template.ContextType;
import org.eclipse.cdt.internal.corext.template.ContextTypeRegistry;
import org.eclipse.cdt.internal.ui.CCompletionContributorManager;
@@ -23,6 +24,7 @@
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;

+import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.BadLocationException;
@@ -46,7 +48,8 @@
    private CEditor fEditor;
    private char[] fProposalAutoActivationSet;
    private CCompletionProposalComparator fComparator;
-    private TemplateEngine fTemplateEngine;
+
+    private TemplateEngine[] fTemplateEngine;
private boolean fRestrictToMatchingCase;
    private boolean fAllowAddIncludes;
@@ -57,9 +60,44 @@
public CCompletionProcessor(IEditorPart editor) {
        fEditor= (CEditor)editor;
- ContextType contextType= ContextTypeRegistry.getInstance().getContextType("C"); //$NON-NLS-1$
-        if (contextType != null)
-            fTemplateEngine= new TemplateEngine(contextType);
+
+ //Determine if this is a C or a C++ file for the context completion + //This is _totally_ ugly and likely belongs in the main editor class.
+        String       contextNames [] = new String[2];
+        ArrayList templateList = new ArrayList(2);
+
+        String filename = null;
+        if(fEditor != null && fEditor.getEditorInput() != null) {
+            filename = fEditor.getEditorInput().getName();
+        }
+ + if(filename == null) {
+            contextNames[0] = "C";            //$NON-NLS-1$
+            contextNames[1] = "C++";        //$NON-NLS-1$
+ } else if(filename.endsWith(".c")) { //Straight C files are always C
+            contextNames[0] = "C";            //$NON-NLS-1$
+ } else if (filename.endsWith(".cpp") || filename.endsWith(".cc") || + filename.endsWith(".cxx") || filename.endsWith(".C") || filename.endsWith(".hxx")) {
+            contextNames[0] = "C++";        //$NON-NLS-1$
+            contextNames[1] = "C";            //$NON-NLS-1$
+ } else { //Defer to the nature of the project
+            IFile file = fEditor.getInputFile();
+ if (file != null && CoreModel.getDefault().hasCCNature(file.getProject())) {
+                contextNames[0] = "C++";    //$NON-NLS-1$
+                contextNames[1] = "C";        //$NON-NLS-1$
+            } else {
+                contextNames[0] = "C";         //$NON-NLS-1$
+            }
+        }
+ + ContextType contextType;
+        for(int i = 0; i < contextNames.length; i++) {
+ contextType = ContextTypeRegistry.getInstance().getContextType(contextNames[i]);
+            if (contextType != null) {
+                templateList.add(new TemplateEngine(contextType));
+            }
+        }
+ fTemplateEngine = (TemplateEngine [])templateList.toArray(new TemplateEngine[templateList.size()]);
        fRestrictToMatchingCase= false;
        fAllowAddIncludes= true;
@@ -175,16 +213,20 @@
        if(results == null)
            results = new ICCompletionProposal[0];

-        if (fTemplateEngine != null) {
+        for(int i = 0; i < fTemplateEngine.length; i++) {
+            if (fTemplateEngine[i] == null) {
+                continue;
+            }
+ try {
-                fTemplateEngine.reset();
-                fTemplateEngine.complete(viewer, documentOffset, null);
+                fTemplateEngine[i].reset();
+                fTemplateEngine[i].complete(viewer, documentOffset, null);
            } catch (Exception x) {
                System.out.println("Template Exception");
                CUIPlugin.getDefault().log(x);
} - ICCompletionProposal[] templateResults= fTemplateEngine.getResults(); + ICCompletionProposal[] templateResults= fTemplateEngine[i].getResults();
            if (results.length == 0) {
                results= templateResults;
            } else {




Back to the top