From c9e12353d63395d7e064afbcd6a0b51e8511c593 Mon Sep 17 00:00:00 2001 From: Alain Magloire Date: Sat, 26 Feb 2005 03:12:35 +0000 Subject: [PATCH] 2005-02-25 Alain Magloire Fix PR 82292: Code template variable not working. * src/org/eclipse/cdt/internal/corext/template/c/CContext.java * src/org/eclipse/cdt/internal/corext/template/c/CContextType.java * src/org/eclipse/cdt/internal/corext/template/c/TemplateMessage.properties * src/org/eclipse/cdt/internal/corext/template/c/TranslationUnitContext.java * src/org/eclipse/cdt/internal/corext/template/c/TranslationUnitContextType.java * src/org/eclipse/cdt/internal/ui/text/contentassit/CCompletionProcessor.java * templates/default-templates.xml --- core/org.eclipse.cdt.ui/ChangeLog | 10 ++ .../internal/corext/template/c/CContext.java | 3 +- .../corext/template/c/CContextType.java | 2 - .../template/c/TemplateMessages.properties | 28 +-- .../template/c/TranslationUnitContext.java | 12 ++ .../c/TranslationUnitContextType.java | 163 ++++++++---------- .../contentassist/CCompletionProcessor.java | 7 +- .../templates/default-templates.xml | 2 +- 8 files changed, 117 insertions(+), 110 deletions(-) diff --git a/core/org.eclipse.cdt.ui/ChangeLog b/core/org.eclipse.cdt.ui/ChangeLog index 6a3fb009132..ed2f60108b6 100644 --- a/core/org.eclipse.cdt.ui/ChangeLog +++ b/core/org.eclipse.cdt.ui/ChangeLog @@ -1,3 +1,13 @@ +2005-02-25 Alain Magloire + Fix PR 82292: Code template variable not working. + * src/org/eclipse/cdt/internal/corext/template/c/CContext.java + * src/org/eclipse/cdt/internal/corext/template/c/CContextType.java + * src/org/eclipse/cdt/internal/corext/template/c/TemplateMessage.properties + * src/org/eclipse/cdt/internal/corext/template/c/TranslationUnitContext.java + * src/org/eclipse/cdt/internal/corext/template/c/TranslationUnitContextType.java + * src/org/eclipse/cdt/internal/ui/text/contentassit/CCompletionProcessor.java + * templates/default-templates.xml + 2005-02-25 Alain Magloire Fix PR 69572: Key binding action for Matching Bracket ... Ctrl+Shift+P * src/org/eclipse/cdt/internal/ui/ICHelpContextIds.java diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CContext.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CContext.java index dcdb8261db9..1b90ee988e4 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CContext.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CContext.java @@ -39,8 +39,7 @@ public class CContext extends TranslationUnitContext { * @param unit the compilation unit (may be null). */ public CContext(TemplateContextType type, IDocument document, int completionOffset, int completionLength, - ITranslationUnit translationUnit) - { + ITranslationUnit translationUnit) { super(type, document, completionOffset, completionLength, translationUnit); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CContextType.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CContextType.java index e820c6cdd50..2ad797865f7 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CContextType.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CContextType.java @@ -41,8 +41,6 @@ public class CContextType extends TranslationUnitContextType { addResolver(new File()); addResolver(new ReturnType()); addResolver(new Method()); - addResolver(new Type()); - addResolver(new Package()); addResolver(new Project()); addResolver(new Arguments()); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TemplateMessages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TemplateMessages.properties index 6a95267b954..e5faa746e0f 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TemplateMessages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TemplateMessages.properties @@ -13,17 +13,17 @@ GlobalVariables.variable.description.user=User name JavaDocContextType.variable.description.file=Filename of compilation unit -JavaContextType.variable.description.file=Filename of compilation unit -JavaContextType.variable.description.array=A proposal for an array -JavaContextType.variable.description.array.type=A proposal for the element type of an array -JavaContextType.variable.description.array.element=A proposal for the element name of an array -JavaContextType.variable.description.index=A proposal for an index (int) -JavaContextType.variable.description.collector=A proposal for a collector (java.util.Collector) -JavaContextType.variable.description.iterator=A proposal for an iterator (java.util.Iterator) -JavaContextType.variable.description.arguments=Method arguments (evaluates to empty string) -JavaContextType.variable.description.enclosing.method=Enclosing method name -JavaContextType.variable.description.enclosing.type=Enclosing type name -JavaContextType.variable.description.enclosing.package=Enclosing package name -JavaContextType.variable.description.enclosing.project=Enclosing project name -JavaContextType.variable.description.enclosing.method.arguments=Argument names of enclosing method -JavaContextType.variable.description.return.type=Enclosing method return type +CContextType.variable.description.file=Filename of compilation unit +CContextType.variable.description.array=A proposal for an array +CContextType.variable.description.array.type=A proposal for the element type of an array +CContextType.variable.description.array.element=A proposal for the element name of an array +CContextType.variable.description.index=A proposal for an index (int) +CContextType.variable.description.collector=A proposal for a collector (java.util.Collector) +CContextType.variable.description.iterator=A proposal for an iterator (java.util.Iterator) +CContextType.variable.description.arguments=Method arguments (evaluates to empty string) +CContextType.variable.description.enclosing.method=Enclosing method name +CContextType.variable.description.enclosing.type=Enclosing type name +CContextType.variable.description.enclosing.package=Enclosing package name +CContextType.variable.description.enclosing.project=Enclosing project name +CContextType.variable.description.enclosing.method.arguments=Argument names of enclosing method +CContextType.variable.description.return.type=Enclosing method return type diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TranslationUnitContext.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TranslationUnitContext.java index 9e6f0945a50..898d24ee9a2 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TranslationUnitContext.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TranslationUnitContext.java @@ -1,3 +1,15 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004, 2005 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + * QNX Software System + *******************************************************************************/ + package org.eclipse.cdt.internal.corext.template.c; /* diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TranslationUnitContextType.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TranslationUnitContextType.java index 5dd67da69e6..9796b982b08 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TranslationUnitContextType.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/TranslationUnitContextType.java @@ -1,17 +1,25 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004, 2005 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + * QNX Software System + *******************************************************************************/ + package org.eclipse.cdt.internal.corext.template.c; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.IFunctionDeclaration; import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.templates.TemplateContext; import org.eclipse.jface.text.templates.TemplateContextType; import org.eclipse.jface.text.templates.TemplateVariableResolver; -/* - * (c) Copyright IBM Corp. 2000, 2001. - * All Rights Reserved. - */ - - /** * Compilation unit context type. */ @@ -28,111 +36,106 @@ public abstract class TranslationUnitContextType extends TemplateContextType { protected static class ReturnType extends TemplateVariableResolver { public ReturnType() { - super("return_type", TemplateMessages.getString("JavaContextType.variable.description.return.type")); //$NON-NLS-1$ //$NON-NLS-2$ + super("return_type", TemplateMessages.getString("CContextType.variable.description.return.type")); //$NON-NLS-1$ //$NON-NLS-2$ } - public String evaluate(TemplateContext context) { - /* IJavaElement element= ((TranslationUnitContext) context).findEnclosingElement(IJavaElement.METHOD); - if (element == null) + public String resolve(TemplateContext context) { + ICElement element= ((TranslationUnitContext) context).findEnclosingElement(ICElement.C_METHOD); + if (element == null) { + element= ((TranslationUnitContext) context).findEnclosingElement(ICElement.C_FUNCTION); + } + if (element == null) { return null; + } - try { - return Signature.toString(((IMethod) element).getReturnType()); - } catch (JavaModelException e) { - return null; - } */ + if (element instanceof IFunctionDeclaration) { + return ((IFunctionDeclaration) element).getReturnType(); + } return null; } - public boolean isResolved(TemplateContext context) { - return evaluate(context) != null; - } } protected static class File extends TemplateVariableResolver { public File() { - super("file", TemplateMessages.getString("JavaContextType.variable.description.file")); //$NON-NLS-1$ //$NON-NLS-2$ + super("file", TemplateMessages.getString("CContextType.variable.description.file")); //$NON-NLS-1$ //$NON-NLS-2$ } - public String evaluate(TemplateContext context) { - //ICompilationUnit unit= ((TranslationUnitContext) context).getCompilationUnit(); + public String resolve(TemplateContext context) { + ITranslationUnit unit= ((TranslationUnitContext) context).getTranslationUnit(); - //return (unit == null) ? null : unit.getElementName(); - return null; + return (unit == null) ? null : unit.getElementName(); } - public boolean isResolved(TemplateContext context) { - return evaluate(context) != null; - } } - protected static class EnclosingJavaElement extends TemplateVariableResolver { + protected static class EnclosingCElement extends TemplateVariableResolver { protected final int fElementType; - public EnclosingJavaElement(String name, String description, int elementType) { + public EnclosingCElement(String name, String description, int elementType) { super(name, description); fElementType= elementType; } - public String evaluate(TemplateContext context) { - /*IJavaElement element= ((TranslationUnitContext) context).findEnclosingElement(fElementType); - return (element == null) ? null : element.getElementName(); */ - return null; - } - public boolean isResolved(TemplateContext context) { - return evaluate(context) != null; + public String resolve(TemplateContext context) { + ICElement element= ((TranslationUnitContext) context).findEnclosingElement(fElementType); + return (element == null) ? null : element.getElementName(); } } - protected static class Method extends EnclosingJavaElement { + protected static class Method extends EnclosingCElement { public Method() { - //super("enclosing_method", TemplateMessages.getString("JavaContextType.variable.description.enclosing.method"), IJavaElement.METHOD); - super("enclosing_method", TemplateMessages.getString("JavaContextType.variable.description.enclosing.method"), 0); //$NON-NLS-1$ //$NON-NLS-2$ + super("enclosing_method", TemplateMessages.getString("CContextType.variable.description.enclosing.method"), ICElement.C_METHOD); //$NON-NLS-1$ //$NON-NLS-2$ } } - protected static class Type extends EnclosingJavaElement { + /* + protected static class Type extends EnclosingCElement { public Type() { - super("enclosing_type", TemplateMessages.getString("JavaContextType.variable.description.enclosing.type"), 0); //$NON-NLS-1$ //$NON-NLS-2$ + super("enclosing_type", TemplateMessages.getString("CContextType.variable.description.enclosing.type"), 0); //$NON-NLS-1$ //$NON-NLS-2$ } } -/* - protected static class SuperClass extends EnclosingJavaElement { + protected static class SuperClass extends EnclosingCElement { public Type() { - super("super_class", TemplateMessages.getString("JavaContextType.variable.description.type"), IJavaElement.TYPE); + super("super_class", TemplateMessages.getString("CContextType.variable.description.type"), IJavaElement.TYPE); } } -*/ - protected static class Package extends EnclosingJavaElement { + + protected static class Package extends EnclosingCElement { public Package() { - super("enclosing_package", TemplateMessages.getString("JavaContextType.variable.description.enclosing.package"), 0); //$NON-NLS-1$ //$NON-NLS-2$ - } - } - - protected static class Project extends EnclosingJavaElement { - public Project() { - super("enclosing_project", TemplateMessages.getString("JavaContextType.variable.description.enclosing.project"), 0); //$NON-NLS-1$ //$NON-NLS-2$ - } - } -/* - protected static class Project2 extends TemplateVariable { - public Project2() { - super("project", TemplateMessages.getString("JavaContextType.variable.description.project")); - } - public String evaluate(TemplateContext context) { - ICompilationUnit unit= ((JavaContext) context).getUnit(); - return (unit == null) ? null : unit.getJavaProject().getElementName(); + super("enclosing_package", TemplateMessages.getString("CContextType.variable.description.enclosing.package"), 0); //$NON-NLS-1$ //$NON-NLS-2$ } } */ +// protected static class Project extends EnclosingCElement { +// public Project() { +// super("enclosing_project", TemplateMessages.getString("CContextType.variable.description.enclosing.project"), ICElement.C_PROJECT); //$NON-NLS-1$ //$NON-NLS-2$ +// } +// } + + protected static class Project extends TemplateVariableResolver { + public Project() { + super("enclosing_project", TemplateMessages.getString("CContextType.variable.description.project")); + } + public String resolve(TemplateContext context) { + ITranslationUnit unit= ((CContext) context).getTranslationUnit(); + return (unit == null) ? null : unit.getCProject().getElementName(); + } + } + protected static class Arguments extends TemplateVariableResolver { public Arguments() { - super("enclosing_method_arguments", TemplateMessages.getString("JavaContextType.variable.description.enclosing.method.arguments")); //$NON-NLS-1$ //$NON-NLS-2$ + super("enclosing_method_arguments", TemplateMessages.getString("CContextType.variable.description.enclosing.method.arguments")); //$NON-NLS-1$ //$NON-NLS-2$ } - public String evaluate(TemplateContext context) { - /*IJavaElement element= ((TranslationUnitContext) context).findEnclosingElement(IJavaElement.METHOD); - if (element == null) - return null; - - IMethod method= (IMethod) element; - - try { - String[] arguments= method.getParameterNames(); + public String resolve(TemplateContext context) { + ICElement element= ((TranslationUnitContext) context).findEnclosingElement(ICElement.C_FUNCTION); + if (element == null) { + element= ((TranslationUnitContext) context).findEnclosingElement(ICElement.C_FUNCTION_DECLARATION); + if (element == null) { + element= ((TranslationUnitContext) context).findEnclosingElement(ICElement.C_METHOD); + if (element == null) { + element= ((TranslationUnitContext) context).findEnclosingElement(ICElement.C_METHOD_DECLARATION); + } + } + } + + if (element instanceof IFunctionDeclaration) { + String[] arguments= ((IFunctionDeclaration)element).getParameterTypes(); StringBuffer buffer= new StringBuffer(); for (int i= 0; i < arguments.length; i++) { @@ -142,25 +145,11 @@ public abstract class TranslationUnitContextType extends TemplateContextType { } return buffer.toString(); - - } catch (JavaModelException e) { - return null; - } */ + } return null; } } -/* - protected static class Line extends TemplateVariable { - public Line() { - super("line", TemplateMessages.getString("TranslationUnitContextType.variable.description.line")); - } - public String evaluate(TemplateContext context) { - return ((JavaTemplateContext) context).guessLineNumber(); - } - } -*/ - /* * @see ContextType#ContextType(String) */ diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProcessor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProcessor.java index f2e6acc9238..d0c7a67ff0f 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProcessor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProcessor.java @@ -97,7 +97,7 @@ public class CCompletionProcessor implements IContentAssistProcessor { } } - + protected IWorkingCopyManager fManager; private CEditor fEditor; private char[] fProposalAutoActivationSet; private CCompletionProposalComparator fComparator; @@ -123,7 +123,7 @@ public class CCompletionProcessor implements IContentAssistProcessor { public CCompletionProcessor(IEditorPart editor) { fEditor = (CEditor) editor; - + fManager= CUIPlugin.getDefault().getWorkingCopyManager(); // Needed for search searchResultCollector = new BasicSearchResultCollector (); resultCollector = new ResultCollector(); @@ -257,7 +257,6 @@ public class CCompletionProcessor implements IContentAssistProcessor { } private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset) { - IWorkingCopyManager fManager = CUIPlugin.getDefault().getWorkingCopyManager(); IWorkingCopy unit = fManager.getWorkingCopy(fEditor.getEditorInput()); IDocument document = viewer.getDocument(); @@ -373,7 +372,7 @@ public class CCompletionProcessor implements IContentAssistProcessor { if (fTemplateEngine != null) { try { fTemplateEngine.reset(); - fTemplateEngine.complete(viewer, fCurrentOffset, null); + fTemplateEngine.complete(viewer, fCurrentOffset, fCurrentSourceUnit); } catch (Exception x) { CUIPlugin.getDefault().log(x); } diff --git a/core/org.eclipse.cdt.ui/templates/default-templates.xml b/core/org.eclipse.cdt.ui/templates/default-templates.xml index 85848ad59d5..7ef7863862d 100644 --- a/core/org.eclipse.cdt.ui/templates/default-templates.xml +++ b/core/org.eclipse.cdt.ui/templates/default-templates.xml @@ -26,7 +26,7 @@ All Rights Reserved. + * Window>Preferences>C>Editor>Templates."> ]>