diff --git a/core/org.eclipse.cdt.ui/plugin.properties b/core/org.eclipse.cdt.ui/plugin.properties index 50ed106906b..c984588b68f 100644 --- a/core/org.eclipse.cdt.ui/plugin.properties +++ b/core/org.eclipse.cdt.ui/plugin.properties @@ -330,7 +330,8 @@ cCompareFontDefiniton.description= The C/C++ compare text font is used by C/C++ ExternalSearchEditor.name=External Search Editor #--- templates -c.contextType.name = C +c.contextType.name = C/C++ +comment.contextType.name = Comment # completion diff --git a/core/org.eclipse.cdt.ui/plugin.xml b/core/org.eclipse.cdt.ui/plugin.xml index a56a1c36c63..f7f2210e1ea 100644 --- a/core/org.eclipse.cdt.ui/plugin.xml +++ b/core/org.eclipse.cdt.ui/plugin.xml @@ -1371,6 +1371,11 @@ class="org.eclipse.cdt.internal.corext.template.c.CContextType" id="org.eclipse.cdt.ui.text.templates.c"> + + @@ -1402,7 +1407,7 @@ id="DOM" priority="1"/> null). + */ + public CommentContext(TemplateContextType type, IDocument document, + int completionOffset, int completionLength, ITranslationUnit translationUnit) { + super(type, document, completionOffset, completionLength, translationUnit); + } + + /* + * @see TemplateContext#canEvaluate(Template templates) + */ + public boolean canEvaluate(Template template) { + String key= getKey(); + + if (fForceEvaluation) + return true; + + return + template.matches(key, getContextType().getId()) && + (key.length() != 0) && template.getName().toLowerCase().startsWith(key.toLowerCase()); + } + + /* + * @see DocumentTemplateContext#getStart() + */ + public int getStart() { + if (/*fIsManaged &&*/ getCompletionLength() > 0) + return super.getStart(); + + try { + IDocument document= getDocument(); + + if (getCompletionLength() == 0) { + int start= getCompletionOffset(); + + while ((start != 0) && !Character.isWhitespace(document.getChar(start - 1))) + start--; + + if ((start != 0) && !Character.isWhitespace(document.getChar(start - 1))) + start--; + + return start; + + } + + int start= getCompletionOffset(); + int end= getCompletionOffset() + getCompletionLength(); + + while (start != 0 && !Character.isWhitespace(document.getChar(start - 1))) + start--; + + while (start != end && Character.isWhitespace(document.getChar(start))) + start++; + + if (start == end) + start= getCompletionOffset(); + + return start; + + + } catch (BadLocationException e) { + return getCompletionOffset(); + } + } + + /* + * @see org.eclipse.jdt.internal.corext.template.DocumentTemplateContext#getEnd() + */ + public int getEnd() { + + if (/*fIsManaged ||*/ getCompletionLength() == 0) + return super.getEnd(); + + try { + IDocument document= getDocument(); + + int start= getCompletionOffset(); + int end= getCompletionOffset() + getCompletionLength(); + + while (start != end && Character.isWhitespace(document.getChar(end - 1))) + end--; + + return end; + + } catch (BadLocationException e) { + return super.getEnd(); + } + } + + /* + * @see org.eclipse.jdt.internal.corext.template.DocumentTemplateContext#getKey() + */ + public String getKey() { + + if (getCompletionLength() == 0) + return super.getKey(); + + try { + IDocument document= getDocument(); + + int start= getStart(); + int end= getCompletionOffset(); + return start <= end + ? document.get(start, end - start) + : ""; //$NON-NLS-1$ + + } catch (BadLocationException e) { + return super.getKey(); + } + } + + /* + * @see TemplateContext#evaluate(Template) + */ + public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException { + TemplateTranslator translator= new TemplateTranslator(); + TemplateBuffer buffer= translator.translate(template); + + getContextType().resolve(buffer, this); + + // don't use code formatter for comment templates + boolean useCodeFormatter= false; + + ICProject project= getCProject(); + CFormatter formatter= new CFormatter(TextUtilities.getDefaultLineDelimiter(getDocument()), getIndentationLevel(), useCodeFormatter, project); + formatter.format(buffer, this); + + return buffer; + } + +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CommentContextType.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CommentContextType.java new file mode 100644 index 00000000000..eba784e346e --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CommentContextType.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Anton Leherbauer (Wind River Systems) - initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.internal.corext.template.c; + +import org.eclipse.jface.text.IDocument; + +import org.eclipse.cdt.core.model.ITranslationUnit; + +/** + * A context type for comments. + * + * @since 4.0 + */ +public class CommentContextType extends TranslationUnitContextType { + + public static final String ID= "org.eclipse.cdt.ui.text.templates.comment"; //$NON-NLS-1$ + + /** + * Creates a comment context type. + */ + public CommentContextType() { + super(); + } + + /* + * @see org.eclipse.cdt.internal.corext.template.c.TranslationUnitContextType#createContext(org.eclipse.jface.text.IDocument, int, int, org.eclipse.cdt.core.model.ITranslationUnit) + */ + public TranslationUnitContext createContext(IDocument document, int offset, + int length, ITranslationUnit translationUnit) { + return new CommentContext(this, document, offset, length, translationUnit); + } + +} 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 bbe3c27e735..a996b83f8d8 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,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2005 IBM Corporation and others. + * Copyright (c) 2000, 2007 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 @@ -8,27 +8,38 @@ * Contributors: * IBM Corporation - initial API and implementation * QNX Software System + * Anton Leherbauer (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.corext.template.c; -import org.eclipse.cdt.core.model.CModelException; -import org.eclipse.cdt.core.model.ICElement; -import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.templates.DocumentTemplateContext; import org.eclipse.jface.text.templates.TemplateContextType; +import org.eclipse.cdt.core.model.CModelException; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.model.ITranslationUnit; + +import org.eclipse.cdt.internal.corext.util.CodeFormatterUtil; + +import org.eclipse.cdt.internal.ui.util.Strings; + /** - * A compilation unit context. + * A translation unit context. */ public abstract class TranslationUnitContext extends DocumentTemplateContext { - /** The compilation unit, may be null. */ + /** The translation unit, may be null. */ private final ITranslationUnit fTranslationUnit; + /** A flag to force evaluation in head-less mode. */ + protected boolean fForceEvaluation; /** - * Creates a compilation unit context. + * Creates a translation unit context. * * @param type the context type * @param document the document @@ -44,7 +55,7 @@ public abstract class TranslationUnitContext extends DocumentTemplateContext { } /** - * Returns the compilation unit if one is associated with this context, null otherwise. + * Returns the translation unit if one is associated with this context, null otherwise. */ public final ITranslationUnit getTranslationUnit() { return fTranslationUnit; @@ -70,6 +81,42 @@ public abstract class TranslationUnitContext extends DocumentTemplateContext { } } + /** + * Sets whether evaluation is forced or not. + * + * @param evaluate true in order to force evaluation, + * false otherwise + */ + public void setForceEvaluation(boolean evaluate) { + fForceEvaluation= evaluate; + } + + /** + * Get the associated ICProject. + * @return the associated ICProject or null + */ + protected ICProject getCProject() { + ITranslationUnit translationUnit= getTranslationUnit(); + ICProject project= translationUnit == null ? null : translationUnit.getCProject(); + return project; + } + + /** + * Get the indentation level at the position of code completion. + * @return the indentation level at the position of code completion + */ + protected int getIndentationLevel() { + int start= getStart(); + IDocument document= getDocument(); + try { + IRegion region= document.getLineInformationOfOffset(start); + String lineContent= document.get(region.getOffset(), region.getLength()); + ICProject project= getCProject(); + return Strings.computeIndent(lineContent, CodeFormatterUtil.getTabWidth(project), CodeFormatterUtil.getIndentWidth(project)); + } catch (BadLocationException e) { + return 0; + } + } } 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 8e3b1fec6a9..9c2e724c9d8 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,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2005 IBM Corporation and others. + * Copyright (c) 2000, 2007 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 @@ -8,6 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation * QNX Software System + * Anton Leherbauer (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.corext.template.c; @@ -16,24 +17,16 @@ 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.GlobalTemplateVariables; import org.eclipse.jface.text.templates.TemplateContext; import org.eclipse.jface.text.templates.TemplateContextType; import org.eclipse.jface.text.templates.TemplateVariableResolver; /** - * Compilation unit context type. + * A context type for translation units. */ public abstract class TranslationUnitContextType extends TemplateContextType { - - /** the document string */ - protected String fString; - /** the completion position within the document string */ - protected int fPosition; - - /** the associated compilation unit, may be null */ - protected ITranslationUnit fTranslationUnit; - protected static class ReturnType extends TemplateVariableResolver { public ReturnType() { super("return_type", TemplateMessages.getString("CContextType.variable.description.return.type")); //$NON-NLS-1$ //$NON-NLS-2$ @@ -151,19 +144,26 @@ public abstract class TranslationUnitContextType extends TemplateContextType { } /* - * @see ContextType#ContextType(String) + * @see TemplateContextType#TemplateContextType() */ public TranslationUnitContextType() { super(); - } - - /** - * Sets context parameters. Needs to be called before createContext(). - */ - public void setContextParameters(String string, int position, ITranslationUnit translationUnit) { - fString= string; - fPosition= position; - fTranslationUnit= translationUnit; + // global + addResolver(new GlobalTemplateVariables.Cursor()); + addResolver(new GlobalTemplateVariables.WordSelection()); + addResolver(new GlobalTemplateVariables.LineSelection()); + addResolver(new GlobalTemplateVariables.Dollar()); + addResolver(new GlobalTemplateVariables.Date()); + addResolver(new GlobalTemplateVariables.Year()); + addResolver(new GlobalTemplateVariables.Time()); + addResolver(new GlobalTemplateVariables.User()); + + // translation unit + addResolver(new File()); + addResolver(new ReturnType()); + addResolver(new Method()); + addResolver(new Project()); + addResolver(new Arguments()); } public abstract TranslationUnitContext createContext(IDocument document, int offset, int length, ITranslationUnit translationUnit); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CTemplatePreferencePage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CTemplatePreferencePage.java index baa5bb14fe6..28c818848a0 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CTemplatePreferencePage.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CTemplatePreferencePage.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2002, 2006 QNX Software Systems and others. + * Copyright (c) 2002, 2007 QNX Software Systems 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 @@ -16,7 +16,13 @@ import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.jface.text.Document; import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.contentassist.ContentAssistant; +import org.eclipse.jface.text.contentassist.IContentAssistant; +import org.eclipse.jface.text.source.ISourceViewer; import org.eclipse.jface.text.source.SourceViewer; +import org.eclipse.jface.text.templates.ContextTypeRegistry; +import org.eclipse.jface.text.templates.Template; +import org.eclipse.jface.window.Window; import org.eclipse.swt.SWT; import org.eclipse.swt.accessibility.AccessibleAdapter; import org.eclipse.swt.accessibility.AccessibleEvent; @@ -24,11 +30,13 @@ import org.eclipse.swt.graphics.Font; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.texteditor.templates.TemplatePreferencePage; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.PreferenceConstants; +import org.eclipse.cdt.ui.text.ICPartitions; import org.eclipse.cdt.internal.ui.ICHelpContextIds; import org.eclipse.cdt.internal.ui.editor.CSourceViewer; @@ -36,10 +44,53 @@ import org.eclipse.cdt.internal.ui.text.CSourceViewerConfiguration; import org.eclipse.cdt.internal.ui.text.CTextTools; /** - * CTemplatePreferencePage + * Template preference page for C/C++ editor templates. */ public class CTemplatePreferencePage extends TemplatePreferencePage { + /** + * A dialog to edit a template. + */ + protected class CEditTemplateDialog extends EditTemplateDialog { + + public CEditTemplateDialog(Shell shell, Template template, + boolean edit, boolean isNameModifiable, + ContextTypeRegistry contextTypeRegistry) { + super(shell, template, edit, isNameModifiable, contextTypeRegistry); + } + /* + * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage.EditTemplateDialog#createViewer(org.eclipse.swt.widgets.Composite) + */ + protected SourceViewer createViewer(Composite parent) { + IPreferenceStore store= CUIPlugin.getDefault().getCombinedPreferenceStore(); + CSourceViewer viewer= new CSourceViewer(parent, null, null, false, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL, store); + CTextTools tools= CUIPlugin.getDefault().getTextTools(); + CSourceViewerConfiguration configuration= new CSourceViewerConfiguration(tools.getColorManager(), store, null, tools.getDocumentPartitioning()) { + public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) { + ContentAssistant assistant= new ContentAssistant(); + assistant.enableAutoActivation(true); + assistant.enableAutoInsert(true); + assistant.setContentAssistProcessor(getTemplateProcessor(), IDocument.DEFAULT_CONTENT_TYPE); + assistant.setContentAssistProcessor(getTemplateProcessor(), ICPartitions.C_MULTI_LINE_COMMENT); + assistant.setContentAssistProcessor(getTemplateProcessor(), ICPartitions.C_SINGLE_LINE_COMMENT); + assistant.setContentAssistProcessor(getTemplateProcessor(), ICPartitions.C_PREPROCESSOR); + return assistant; + } + }; + IDocument document = new Document(); + tools.setupCDocument(document); + viewer.configure(configuration); + viewer.setEditable(true); + viewer.setDocument(document); + + Font font= JFaceResources.getFontRegistry().get(PreferenceConstants.EDITOR_TEXT_FONT); + viewer.getTextWidget().setFont(font); + + CSourcePreviewerUpdater.registerPreviewer(viewer, configuration, CUIPlugin.getDefault().getCombinedPreferenceStore()); + return viewer; + } + } + public CTemplatePreferencePage() { setPreferenceStore(CUIPlugin.getDefault().getPreferenceStore()); setTemplateStore(CUIPlugin.getDefault().getTemplateStore()); @@ -61,6 +112,17 @@ public class CTemplatePreferencePage extends TemplatePreferencePage { return PreferenceConstants.TEMPLATES_USE_CODEFORMATTER; } + /* + * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createTemplateEditDialog2(org.eclipse.jface.text.templates.Template, boolean, boolean) + */ + protected Template editTemplate(Template template, boolean edit, boolean isNameModifiable) { + CEditTemplateDialog dialog= new CEditTemplateDialog(getShell(), template, edit, isNameModifiable, getContextTypeRegistry()); + if (dialog.open() == Window.OK) { + return dialog.getTemplate(); + } + return null; + } + /* * @see org.eclipse.jface.preference.IPreferencePage#performOk() */ diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CSourceViewerConfiguration.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CSourceViewerConfiguration.java index a3f4fb015f6..7f57b91b7d2 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CSourceViewerConfiguration.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CSourceViewerConfiguration.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2006 IBM Corporation and others. + * Copyright (c) 2005, 2007 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 @@ -83,6 +83,7 @@ import org.eclipse.cdt.internal.ui.text.c.hover.CEditorTextHoverDescriptor; import org.eclipse.cdt.internal.ui.text.c.hover.CEditorTextHoverProxy; import org.eclipse.cdt.internal.ui.text.contentassist.CCompletionProcessor2; import org.eclipse.cdt.internal.ui.text.contentassist.ContentAssistPreference; +import org.eclipse.cdt.internal.ui.text.template.CTemplateCompletionProcessor; import org.eclipse.cdt.internal.ui.util.ExternalEditorInput; @@ -348,6 +349,7 @@ public class CSourceViewerConfiguration extends TextSourceViewerConfiguration { IContentAssistProcessor processor = new CCompletionProcessor2(getEditor()); assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE); + assistant.setContentAssistProcessor(new CTemplateCompletionProcessor(getEditor()), ICPartitions.C_MULTI_LINE_COMMENT); ContentAssistPreference.configure(assistant, fPreferenceStore); assistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/template/CTemplateCompletionProcessor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/template/CTemplateCompletionProcessor.java new file mode 100644 index 00000000000..134b0d87546 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/template/CTemplateCompletionProcessor.java @@ -0,0 +1,182 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Anton Leherbauer (Wind River Systems) - initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.internal.ui.text.template; + +import java.util.List; + +import org.eclipse.core.runtime.Assert; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.ITextViewer; +import org.eclipse.jface.text.TextUtilities; +import org.eclipse.jface.text.contentassist.ICompletionProposal; +import org.eclipse.jface.text.contentassist.IContentAssistProcessor; +import org.eclipse.jface.text.contentassist.IContextInformation; +import org.eclipse.jface.text.contentassist.IContextInformationValidator; +import org.eclipse.jface.text.templates.TemplateContextType; +import org.eclipse.ui.IEditorPart; + +import org.eclipse.cdt.core.dom.ast.ASTCompletionNode; +import org.eclipse.cdt.core.model.IWorkingCopy; +import org.eclipse.cdt.ui.CUIPlugin; +import org.eclipse.cdt.ui.text.ICPartitions; +import org.eclipse.cdt.ui.text.contentassist.ICompletionContributor; + +import org.eclipse.cdt.internal.corext.template.c.CContextType; +import org.eclipse.cdt.internal.corext.template.c.CommentContextType; + +/** + * A completion processor for templates. + * Can be used directly as implementation of {@link IContentAssistProcessor} or + * as implementation of the extension point interface {@link ICompletionContributor}. + * + * @since 4.0 + */ +public class CTemplateCompletionProcessor implements IContentAssistProcessor, ICompletionContributor { + + private static final ICompletionProposal[] NO_PROPOSALS= new ICompletionProposal[0]; + private static final IContextInformation[] NO_CONTEXTS= new IContextInformation[0]; + + private IEditorPart fEditor; + + private final TemplateEngine fCTemplateEngine; + private final TemplateEngine fCommentTemplateEngine; + + /** + * Create a new template completion processor to be used as IContentAssistProcessor. + * + * @param editor the editor, may not be null + */ + public CTemplateCompletionProcessor(IEditorPart editor) { + this(); + Assert.isNotNull(editor); + fEditor= editor; + } + + /** + * Default constructor is required (executable extension). + */ + public CTemplateCompletionProcessor() { + TemplateContextType contextType= CUIPlugin.getDefault().getTemplateContextRegistry().getContextType(CContextType.ID); + if (contextType == null) { + contextType= new CContextType(); + CUIPlugin.getDefault().getTemplateContextRegistry().addContextType(contextType); + } + if (contextType != null) + fCTemplateEngine= new TemplateEngine(contextType); + else + fCTemplateEngine= null; + contextType= CUIPlugin.getDefault().getTemplateContextRegistry().getContextType(CommentContextType.ID); + if (contextType == null) { + contextType= new CommentContextType(); + CUIPlugin.getDefault().getTemplateContextRegistry().addContextType(contextType); + } + if (contextType != null) + fCommentTemplateEngine= new TemplateEngine(contextType); + else + fCommentTemplateEngine= null; + } + + /* + * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer, int) + */ + public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) { + Assert.isNotNull(fEditor); + TemplateEngine engine; + try { + String partition= TextUtilities.getContentType(viewer.getDocument(), ICPartitions.C_PARTITIONING, offset, true); + if (partition.equals(ICPartitions.C_MULTI_LINE_COMMENT)) + engine= fCommentTemplateEngine; + else + engine= fCTemplateEngine; + } catch (BadLocationException x) { + return NO_PROPOSALS; + } + + if (engine != null) { + IWorkingCopy workingCopy = CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(fEditor.getEditorInput()); + if (workingCopy == null) + return NO_PROPOSALS; + + engine.reset(); + engine.complete(viewer, offset, workingCopy); + + List result= engine.getResults(); + + return (ICompletionProposal[]) result.toArray(new ICompletionProposal[result.size()]); + } + return NO_PROPOSALS; + } + + /* + * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(org.eclipse.jface.text.ITextViewer, int) + */ + public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) { + return NO_CONTEXTS; + } + + /* + * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getCompletionProposalAutoActivationCharacters() + */ + public char[] getCompletionProposalAutoActivationCharacters() { + return null; + } + + /* + * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationAutoActivationCharacters() + */ + public char[] getContextInformationAutoActivationCharacters() { + return null; + } + + /* + * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationValidator() + */ + public IContextInformationValidator getContextInformationValidator() { + return null; + } + + /* + * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getErrorMessage() + */ + public String getErrorMessage() { + return null; + } + + /* + * @see org.eclipse.cdt.ui.text.contentassist.ICompletionContributor#contributeCompletionProposals(org.eclipse.jface.text.ITextViewer, int, org.eclipse.cdt.core.model.IWorkingCopy, org.eclipse.cdt.core.dom.ast.ASTCompletionNode, java.lang.String, java.util.List) + */ + public void contributeCompletionProposals(ITextViewer viewer, int offset, + IWorkingCopy workingCopy, ASTCompletionNode completionNode, + String prefix, List proposals) { + // TODO We should use the completion node to determine the proper context for the templates + // For now we just keep the current behavior + TemplateEngine engine; + try { + String partition= TextUtilities.getContentType(viewer.getDocument(), ICPartitions.C_PARTITIONING, offset, true); + if (partition.equals(ICPartitions.C_MULTI_LINE_COMMENT)) + engine= fCommentTemplateEngine; + else + engine= fCTemplateEngine; + } catch (BadLocationException x) { + return; + } + + if (engine != null) { + engine.reset(); + engine.complete(viewer, offset, workingCopy); + + List result= engine.getResults(); + proposals.addAll(result); + } + } + +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/template/TemplateEngine.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/template/TemplateEngine.java index bf6c7d3a2e8..2381e0a76fd 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/template/TemplateEngine.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/template/TemplateEngine.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2006 IBM Corporation and others. + * Copyright (c) 2000, 2007 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 @@ -8,12 +8,14 @@ * Contributors: * IBM Corporation - initial API and implementation * QNX Software System + * Anton Leherbauer (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.ui.text.template; import java.util.ArrayList; import java.util.List; +import org.eclipse.core.runtime.Assert; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IInformationControl; import org.eclipse.jface.text.IInformationControlCreator; @@ -24,18 +26,14 @@ import org.eclipse.jface.text.templates.Template; import org.eclipse.jface.text.templates.TemplateContext; import org.eclipse.jface.text.templates.TemplateContextType; import org.eclipse.jface.text.templates.TemplateProposal; -import org.eclipse.core.runtime.Assert; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Shell; -import org.eclipse.cdt.core.dom.ast.ASTCompletionNode; import org.eclipse.cdt.core.model.ITranslationUnit; -import org.eclipse.cdt.core.model.IWorkingCopy; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.text.ICCompletionProposal; -import org.eclipse.cdt.ui.text.contentassist.ICompletionContributor; import org.eclipse.cdt.internal.corext.template.c.CContextType; import org.eclipse.cdt.internal.corext.template.c.TranslationUnitContext; @@ -44,7 +42,7 @@ import org.eclipse.cdt.internal.corext.template.c.TranslationUnitContextType; import org.eclipse.cdt.internal.ui.CPluginImages; import org.eclipse.cdt.internal.ui.text.c.hover.SourceViewerInformationControl; -public class TemplateEngine implements ICompletionContributor { +public class TemplateEngine { private TemplateContextType fContextType; private ArrayList fProposals= new ArrayList(); @@ -91,7 +89,7 @@ public class TemplateEngine implements ICompletionContributor { * This is the default constructor used by the new content assist extension point */ public TemplateEngine() { - fContextType = CUIPlugin.getDefault().getTemplateContextRegistry().getContextType(CContextType.CCONTEXT_TYPE); + fContextType = CUIPlugin.getDefault().getTemplateContextRegistry().getContextType(CContextType.ID); if (fContextType == null) { fContextType= new CContextType(); CUIPlugin.getDefault().getTemplateContextRegistry().addContextType(fContextType); @@ -129,7 +127,6 @@ public class TemplateEngine implements ICompletionContributor { Point selection= viewer.getSelectedRange(); - ((TranslationUnitContextType) fContextType).setContextParameters(document.get(), completionPosition, translationUnit); TranslationUnitContext context= ((TranslationUnitContextType) fContextType).createContext(document, completionPosition, selection.y, translationUnit); int start= context.getStart(); int end= context.getEnd(); @@ -141,17 +138,5 @@ public class TemplateEngine implements ICompletionContributor { fProposals.add(new CTemplateProposal(templates[i], context, region, CPluginImages.get(CPluginImages.IMG_OBJS_TEMPLATE))); } - public void contributeCompletionProposals(ITextViewer viewer, - int offset, - IWorkingCopy workingCopy, - ASTCompletionNode completionNode, - String prefix, - List proposals) - { - // TODO We should use the completion node to determine the proper context for the templates - // For now we just keep the current behavior - complete(viewer, offset, workingCopy); - proposals.addAll(fProposals); - } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CUIPlugin.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CUIPlugin.java index 2850bdf88c9..d160f2d877d 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CUIPlugin.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CUIPlugin.java @@ -63,6 +63,7 @@ import org.eclipse.cdt.core.model.IWorkingCopyProvider; import org.eclipse.cdt.internal.core.model.IBufferFactory; import org.eclipse.cdt.internal.corext.template.c.CContextType; +import org.eclipse.cdt.internal.corext.template.c.CommentContextType; import org.eclipse.cdt.internal.ui.CElementAdapterFactory; import org.eclipse.cdt.internal.ui.ICStatusConstants; @@ -728,7 +729,8 @@ public class CUIPlugin extends AbstractUIPlugin { public ContextTypeRegistry getTemplateContextRegistry() { if (fContextTypeRegistry == null) { fContextTypeRegistry= new ContributionContextTypeRegistry(); - fContextTypeRegistry.addContextType(CContextType.CCONTEXT_TYPE); + fContextTypeRegistry.addContextType(CContextType.ID); + fContextTypeRegistry.addContextType(CommentContextType.ID); } return fContextTypeRegistry; } diff --git a/core/org.eclipse.cdt.ui/templates/default-templates.xml b/core/org.eclipse.cdt.ui/templates/default-templates.xml index 43642cfbf46..1250b2a6dec 100644 --- a/core/org.eclipse.cdt.ui/templates/default-templates.xml +++ b/core/org.eclipse.cdt.ui/templates/default-templates.xml @@ -81,6 +81,6 @@ private: - +