mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 14:42:11 +02:00
Fix for Bug 170461 - [Content Assist] Templates need a context type "Comment"
This commit is contained in:
parent
273a31b365
commit
bdfd719f05
15 changed files with 579 additions and 156 deletions
|
@ -330,7 +330,8 @@ cCompareFontDefiniton.description= The C/C++ compare text font is used by C/C++
|
||||||
ExternalSearchEditor.name=External Search Editor
|
ExternalSearchEditor.name=External Search Editor
|
||||||
|
|
||||||
#--- templates
|
#--- templates
|
||||||
c.contextType.name = C
|
c.contextType.name = C/C++
|
||||||
|
comment.contextType.name = Comment
|
||||||
|
|
||||||
# completion
|
# completion
|
||||||
|
|
||||||
|
|
|
@ -1371,6 +1371,11 @@
|
||||||
class="org.eclipse.cdt.internal.corext.template.c.CContextType"
|
class="org.eclipse.cdt.internal.corext.template.c.CContextType"
|
||||||
id="org.eclipse.cdt.ui.text.templates.c">
|
id="org.eclipse.cdt.ui.text.templates.c">
|
||||||
</contextType>
|
</contextType>
|
||||||
|
<contextType
|
||||||
|
class="org.eclipse.cdt.internal.corext.template.c.CommentContextType"
|
||||||
|
id="org.eclipse.cdt.ui.text.templates.comment"
|
||||||
|
name="%comment.contextType.name">
|
||||||
|
</contextType>
|
||||||
<include
|
<include
|
||||||
file="templates/default-templates.xml"
|
file="templates/default-templates.xml"
|
||||||
translations="$nl$/templates/default-templates.properties">
|
translations="$nl$/templates/default-templates.properties">
|
||||||
|
@ -1402,7 +1407,7 @@
|
||||||
id="DOM"
|
id="DOM"
|
||||||
priority="1"/>
|
priority="1"/>
|
||||||
<contributor
|
<contributor
|
||||||
class="org.eclipse.cdt.internal.ui.text.template.TemplateEngine"
|
class="org.eclipse.cdt.internal.ui.text.template.CTemplateCompletionProcessor"
|
||||||
id="CodeTemplates"
|
id="CodeTemplates"
|
||||||
priority="2"/>
|
priority="2"/>
|
||||||
<contributor
|
<contributor
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2005 IBM Corporation and others.
|
* Copyright (c) 2005, 2007 IBM Corporation and others.
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
|
@ -8,20 +8,13 @@
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* IBM Corporation - initial API and implementation
|
* IBM Corporation - initial API and implementation
|
||||||
* QnX Software System
|
* QnX Software System
|
||||||
|
* Anton Leherbauer (Wind River Systems)
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.corext.template.c;
|
package org.eclipse.cdt.internal.corext.template.c;
|
||||||
|
|
||||||
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;
|
|
||||||
import org.eclipse.cdt.ui.CUIPlugin;
|
|
||||||
import org.eclipse.cdt.ui.PreferenceConstants;
|
|
||||||
|
|
||||||
import org.eclipse.jface.preference.IPreferenceStore;
|
import org.eclipse.jface.preference.IPreferenceStore;
|
||||||
import org.eclipse.jface.text.BadLocationException;
|
import org.eclipse.jface.text.BadLocationException;
|
||||||
import org.eclipse.jface.text.IDocument;
|
import org.eclipse.jface.text.IDocument;
|
||||||
import org.eclipse.jface.text.IRegion;
|
|
||||||
import org.eclipse.jface.text.TextUtilities;
|
import org.eclipse.jface.text.TextUtilities;
|
||||||
import org.eclipse.jface.text.templates.Template;
|
import org.eclipse.jface.text.templates.Template;
|
||||||
import org.eclipse.jface.text.templates.TemplateBuffer;
|
import org.eclipse.jface.text.templates.TemplateBuffer;
|
||||||
|
@ -29,17 +22,19 @@ import org.eclipse.jface.text.templates.TemplateContextType;
|
||||||
import org.eclipse.jface.text.templates.TemplateException;
|
import org.eclipse.jface.text.templates.TemplateException;
|
||||||
import org.eclipse.jface.text.templates.TemplateTranslator;
|
import org.eclipse.jface.text.templates.TemplateTranslator;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.core.model.ICProject;
|
||||||
|
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||||
|
import org.eclipse.cdt.ui.CUIPlugin;
|
||||||
|
import org.eclipse.cdt.ui.PreferenceConstants;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A context for c/c++
|
* A context for C/C++
|
||||||
*/
|
*/
|
||||||
public class CContext extends TranslationUnitContext {
|
public class CContext extends TranslationUnitContext {
|
||||||
|
|
||||||
/** The platform default line delimiter. */
|
|
||||||
private static final String PLATFORM_LINE_DELIMITER= System.getProperty("line.separator"); //$NON-NLS-1$
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a javadoc template context.
|
* Creates a C/C++ code template context.
|
||||||
*
|
*
|
||||||
* @param type the context type
|
* @param type the context type
|
||||||
* @param document the document
|
* @param document the document
|
||||||
|
@ -119,10 +114,12 @@ public class CContext extends TranslationUnitContext {
|
||||||
* @see TemplateContext#canEvaluate(Template templates)
|
* @see TemplateContext#canEvaluate(Template templates)
|
||||||
*/
|
*/
|
||||||
public boolean canEvaluate(Template template) {
|
public boolean canEvaluate(Template template) {
|
||||||
|
if (fForceEvaluation)
|
||||||
|
return true;
|
||||||
|
|
||||||
String key= getKey();
|
String key= getKey();
|
||||||
return template.matches(key, getContextType().getId())
|
return template.matches(key, getContextType().getId())
|
||||||
&& key.length() != 0 && template.getName().toLowerCase().startsWith(key.toLowerCase());
|
&& key.length() != 0 && template.getName().toLowerCase().startsWith(key.toLowerCase());
|
||||||
//return template.matches(getKey(), getContextType().getName());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -137,38 +134,14 @@ public class CContext extends TranslationUnitContext {
|
||||||
|
|
||||||
getContextType().resolve(buffer, this);
|
getContextType().resolve(buffer, this);
|
||||||
|
|
||||||
String lineDelimiter= null;
|
|
||||||
try {
|
|
||||||
lineDelimiter= getDocument().getLineDelimiter(0);
|
|
||||||
} catch (BadLocationException e) {
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lineDelimiter == null) {
|
|
||||||
lineDelimiter= PLATFORM_LINE_DELIMITER;
|
|
||||||
}
|
|
||||||
|
|
||||||
IPreferenceStore prefs= CUIPlugin.getDefault().getPreferenceStore();
|
IPreferenceStore prefs= CUIPlugin.getDefault().getPreferenceStore();
|
||||||
boolean useCodeFormatter= prefs.getBoolean(PreferenceConstants.TEMPLATES_USE_CODEFORMATTER);
|
boolean useCodeFormatter= prefs.getBoolean(PreferenceConstants.TEMPLATES_USE_CODEFORMATTER);
|
||||||
|
|
||||||
ICProject project= getTranslationUnit() != null ? getTranslationUnit().getCProject() : null;
|
ICProject project= getCProject();
|
||||||
CFormatter formatter= new CFormatter(TextUtilities.getDefaultLineDelimiter(getDocument()), getIndentationLevel(project), useCodeFormatter, project);
|
CFormatter formatter= new CFormatter(TextUtilities.getDefaultLineDelimiter(getDocument()), getIndentationLevel(), useCodeFormatter, project);
|
||||||
formatter.format(buffer, this);
|
formatter.format(buffer, this);
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the indentation level at the position of code completion.
|
|
||||||
*/
|
|
||||||
private int getIndentationLevel(ICProject project) {
|
|
||||||
int start= getStart();
|
|
||||||
IDocument document= getDocument();
|
|
||||||
try {
|
|
||||||
IRegion region= document.getLineInformationOfOffset(start);
|
|
||||||
String lineContent= document.get(region.getOffset(), region.getLength());
|
|
||||||
return Strings.computeIndent(lineContent, CodeFormatterUtil.getTabWidth(project), CodeFormatterUtil.getIndentWidth(project));
|
|
||||||
} catch (BadLocationException e) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,46 +1,30 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2002, 2005 QNX Software Systems and others.
|
* Copyright (c) 2002, 2007 QNX Software Systems and others.
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
* http://www.eclipse.org/legal/epl-v10.html
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
*
|
*
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* QNX Software Systems - Initial API and implementation
|
* QNX Software Systems - Initial API and implementation
|
||||||
|
* Anton Leherbauer (Wind River Systems)
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
package org.eclipse.cdt.internal.corext.template.c;
|
package org.eclipse.cdt.internal.corext.template.c;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.model.ITranslationUnit;
|
|
||||||
import org.eclipse.jface.text.IDocument;
|
import org.eclipse.jface.text.IDocument;
|
||||||
import org.eclipse.jface.text.templates.GlobalTemplateVariables;
|
|
||||||
|
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CContextType
|
* A context type for C/C++ code.
|
||||||
*/
|
*/
|
||||||
public class CContextType extends TranslationUnitContextType {
|
public class CContextType extends TranslationUnitContextType {
|
||||||
|
|
||||||
public final static String CCONTEXT_TYPE = "org.eclipse.cdt.ui.text.templates.c"; //$NON-NLS-1$
|
public final static String ID = "org.eclipse.cdt.ui.text.templates.c"; //$NON-NLS-1$
|
||||||
|
|
||||||
public CContextType() {
|
public CContextType() {
|
||||||
super();
|
super();
|
||||||
// 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());
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
|
|
|
@ -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
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
|
@ -8,7 +8,7 @@
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* IBM Corporation - initial API and implementation
|
* IBM Corporation - initial API and implementation
|
||||||
* Qnx Software System
|
* Qnx Software System
|
||||||
* Anton Leherbauer (Wind River Systems) - Fixed bug 126617
|
* Anton Leherbauer (Wind River Systems)
|
||||||
* Sergey Prigogin, Google
|
* Sergey Prigogin, Google
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.corext.template.c;
|
package org.eclipse.cdt.internal.corext.template.c;
|
||||||
|
@ -56,8 +56,6 @@ public class CFormatter {
|
||||||
|
|
||||||
private static final String COMMENT_START= "/*-"; //$NON-NLS-1$
|
private static final String COMMENT_START= "/*-"; //$NON-NLS-1$
|
||||||
private static final String COMMENT_END= "*/"; //$NON-NLS-1$
|
private static final String COMMENT_END= "*/"; //$NON-NLS-1$
|
||||||
// private static final String CURSOR= "cursor"; //$NON-NLS-1$
|
|
||||||
// private static final String MARKER= "/*${cursor}*/"; //$NON-NLS-1$
|
|
||||||
|
|
||||||
/** The line delimiter to use if code formatter is not used. */
|
/** The line delimiter to use if code formatter is not used. */
|
||||||
private final String fLineDelimiter;
|
private final String fLineDelimiter;
|
||||||
|
@ -161,40 +159,10 @@ public class CFormatter {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// private static int getCaretOffset(TemplateVariable[] variables) {
|
|
||||||
// for (int i= 0; i != variables.length; i++) {
|
|
||||||
// TemplateVariable variable= variables[i];
|
|
||||||
//
|
|
||||||
// if (variable.getName().equals(CURSOR)) {
|
|
||||||
// return variable.getOffsets()[0];
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// return -1;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// private boolean isInsideCommentOrString(String string, int offset) {
|
|
||||||
//
|
|
||||||
// IDocument document= new Document(string);
|
|
||||||
// CUIPlugin.getDefault().getTextTools().setupCDocument(document);
|
|
||||||
//
|
|
||||||
// try {
|
|
||||||
// ITypedRegion partition= document.getPartition(offset);
|
|
||||||
// String partitionType= partition.getType();
|
|
||||||
//
|
|
||||||
// return partitionType != null && (
|
|
||||||
// partitionType.equals(ICPartitions.C_MULTILINE_COMMENT) ||
|
|
||||||
// partitionType.equals(ICPartitions.C_SINGLE_LINE_COMMENT) ||
|
|
||||||
// partitionType.equals(ICPartitions.C_STRING));
|
|
||||||
// } catch (BadLocationException e) {
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
private void format(IDocument doc, TranslationUnitContext context) throws BadLocationException {
|
private void format(IDocument doc, TranslationUnitContext context) throws BadLocationException {
|
||||||
Map options;
|
Map options;
|
||||||
if (context.getTranslationUnit() != null)
|
if (fProject != null)
|
||||||
options= context.getTranslationUnit().getCProject().getOptions(true);
|
options= fProject.getOptions(true);
|
||||||
else
|
else
|
||||||
options= CCorePlugin.getOptions();
|
options= CCorePlugin.getOptions();
|
||||||
|
|
||||||
|
@ -253,6 +221,7 @@ public class CFormatter {
|
||||||
ICPartitions.C_SINGLE_LINE_COMMENT,
|
ICPartitions.C_SINGLE_LINE_COMMENT,
|
||||||
ICPartitions.C_STRING,
|
ICPartitions.C_STRING,
|
||||||
ICPartitions.C_CHARACTER,
|
ICPartitions.C_CHARACTER,
|
||||||
|
ICPartitions.C_PREPROCESSOR,
|
||||||
IDocument.DEFAULT_CONTENT_TYPE
|
IDocument.DEFAULT_CONTENT_TYPE
|
||||||
};
|
};
|
||||||
FastPartitioner partitioner= new FastPartitioner(new FastCPartitionScanner(), types);
|
FastPartitioner partitioner= new FastPartitioner(new FastCPartitionScanner(), types);
|
||||||
|
|
|
@ -0,0 +1,169 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* 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.BadLocationException;
|
||||||
|
import org.eclipse.jface.text.IDocument;
|
||||||
|
import org.eclipse.jface.text.TextUtilities;
|
||||||
|
import org.eclipse.jface.text.templates.Template;
|
||||||
|
import org.eclipse.jface.text.templates.TemplateBuffer;
|
||||||
|
import org.eclipse.jface.text.templates.TemplateContextType;
|
||||||
|
import org.eclipse.jface.text.templates.TemplateException;
|
||||||
|
import org.eclipse.jface.text.templates.TemplateTranslator;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.core.model.ICProject;
|
||||||
|
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A context for (block) comments.
|
||||||
|
*
|
||||||
|
* @since 4.0
|
||||||
|
*/
|
||||||
|
public class CommentContext extends TranslationUnitContext {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a comment template context.
|
||||||
|
*
|
||||||
|
* @param type the context type.
|
||||||
|
* @param document the document.
|
||||||
|
* @param completionOffset the completion offset within the document.
|
||||||
|
* @param completionLength the completion length within the document.
|
||||||
|
* @param translationUnit the translation unit (may be <code>null</code>).
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
|
@ -8,27 +8,38 @@
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* IBM Corporation - initial API and implementation
|
* IBM Corporation - initial API and implementation
|
||||||
* QNX Software System
|
* QNX Software System
|
||||||
|
* Anton Leherbauer (Wind River Systems)
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
package org.eclipse.cdt.internal.corext.template.c;
|
package org.eclipse.cdt.internal.corext.template.c;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.model.CModelException;
|
import org.eclipse.jface.text.BadLocationException;
|
||||||
import org.eclipse.cdt.core.model.ICElement;
|
|
||||||
import org.eclipse.cdt.core.model.ITranslationUnit;
|
|
||||||
import org.eclipse.jface.text.IDocument;
|
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.DocumentTemplateContext;
|
||||||
import org.eclipse.jface.text.templates.TemplateContextType;
|
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 {
|
public abstract class TranslationUnitContext extends DocumentTemplateContext {
|
||||||
|
|
||||||
/** The compilation unit, may be <code>null</code>. */
|
/** The translation unit, may be <code>null</code>. */
|
||||||
private final ITranslationUnit fTranslationUnit;
|
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 type the context type
|
||||||
* @param document the document
|
* @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, <code>null</code> otherwise.
|
* Returns the translation unit if one is associated with this context, <code>null</code> otherwise.
|
||||||
*/
|
*/
|
||||||
public final ITranslationUnit getTranslationUnit() {
|
public final ITranslationUnit getTranslationUnit() {
|
||||||
return fTranslationUnit;
|
return fTranslationUnit;
|
||||||
|
@ -70,6 +81,42 @@ public abstract class TranslationUnitContext extends DocumentTemplateContext {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets whether evaluation is forced or not.
|
||||||
|
*
|
||||||
|
* @param evaluate <code>true</code> in order to force evaluation,
|
||||||
|
* <code>false</code> otherwise
|
||||||
|
*/
|
||||||
|
public void setForceEvaluation(boolean evaluate) {
|
||||||
|
fForceEvaluation= evaluate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the associated <code>ICProject</code>.
|
||||||
|
* @return the associated <code>ICProject</code> or <code>null</code>
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
|
@ -8,6 +8,7 @@
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* IBM Corporation - initial API and implementation
|
* IBM Corporation - initial API and implementation
|
||||||
* QNX Software System
|
* QNX Software System
|
||||||
|
* Anton Leherbauer (Wind River Systems)
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
package org.eclipse.cdt.internal.corext.template.c;
|
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.IFunctionDeclaration;
|
||||||
import org.eclipse.cdt.core.model.ITranslationUnit;
|
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||||
import org.eclipse.jface.text.IDocument;
|
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.TemplateContext;
|
||||||
import org.eclipse.jface.text.templates.TemplateContextType;
|
import org.eclipse.jface.text.templates.TemplateContextType;
|
||||||
import org.eclipse.jface.text.templates.TemplateVariableResolver;
|
import org.eclipse.jface.text.templates.TemplateVariableResolver;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compilation unit context type.
|
* A context type for translation units.
|
||||||
*/
|
*/
|
||||||
public abstract class TranslationUnitContextType extends TemplateContextType {
|
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 <code>null</code> */
|
|
||||||
protected ITranslationUnit fTranslationUnit;
|
|
||||||
|
|
||||||
protected static class ReturnType extends TemplateVariableResolver {
|
protected static class ReturnType extends TemplateVariableResolver {
|
||||||
public ReturnType() {
|
public ReturnType() {
|
||||||
super("return_type", TemplateMessages.getString("CContextType.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$
|
||||||
|
@ -151,19 +144,26 @@ public abstract class TranslationUnitContextType extends TemplateContextType {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @see ContextType#ContextType(String)
|
* @see TemplateContextType#TemplateContextType()
|
||||||
*/
|
*/
|
||||||
public TranslationUnitContextType() {
|
public TranslationUnitContextType() {
|
||||||
super();
|
super();
|
||||||
}
|
// 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
|
||||||
* Sets context parameters. Needs to be called before createContext().
|
addResolver(new File());
|
||||||
*/
|
addResolver(new ReturnType());
|
||||||
public void setContextParameters(String string, int position, ITranslationUnit translationUnit) {
|
addResolver(new Method());
|
||||||
fString= string;
|
addResolver(new Project());
|
||||||
fPosition= position;
|
addResolver(new Arguments());
|
||||||
fTranslationUnit= translationUnit;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract TranslationUnitContext createContext(IDocument document, int offset, int length, ITranslationUnit translationUnit);
|
public abstract TranslationUnitContext createContext(IDocument document, int offset, int length, ITranslationUnit translationUnit);
|
||||||
|
|
|
@ -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
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* 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.resource.JFaceResources;
|
||||||
import org.eclipse.jface.text.Document;
|
import org.eclipse.jface.text.Document;
|
||||||
import org.eclipse.jface.text.IDocument;
|
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.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.SWT;
|
||||||
import org.eclipse.swt.accessibility.AccessibleAdapter;
|
import org.eclipse.swt.accessibility.AccessibleAdapter;
|
||||||
import org.eclipse.swt.accessibility.AccessibleEvent;
|
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.layout.GridData;
|
||||||
import org.eclipse.swt.widgets.Composite;
|
import org.eclipse.swt.widgets.Composite;
|
||||||
import org.eclipse.swt.widgets.Control;
|
import org.eclipse.swt.widgets.Control;
|
||||||
|
import org.eclipse.swt.widgets.Shell;
|
||||||
import org.eclipse.ui.PlatformUI;
|
import org.eclipse.ui.PlatformUI;
|
||||||
import org.eclipse.ui.texteditor.templates.TemplatePreferencePage;
|
import org.eclipse.ui.texteditor.templates.TemplatePreferencePage;
|
||||||
|
|
||||||
import org.eclipse.cdt.ui.CUIPlugin;
|
import org.eclipse.cdt.ui.CUIPlugin;
|
||||||
import org.eclipse.cdt.ui.PreferenceConstants;
|
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.ICHelpContextIds;
|
||||||
import org.eclipse.cdt.internal.ui.editor.CSourceViewer;
|
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;
|
import org.eclipse.cdt.internal.ui.text.CTextTools;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CTemplatePreferencePage
|
* Template preference page for C/C++ editor templates.
|
||||||
*/
|
*/
|
||||||
public class CTemplatePreferencePage extends TemplatePreferencePage {
|
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() {
|
public CTemplatePreferencePage() {
|
||||||
setPreferenceStore(CUIPlugin.getDefault().getPreferenceStore());
|
setPreferenceStore(CUIPlugin.getDefault().getPreferenceStore());
|
||||||
setTemplateStore(CUIPlugin.getDefault().getTemplateStore());
|
setTemplateStore(CUIPlugin.getDefault().getTemplateStore());
|
||||||
|
@ -61,6 +112,17 @@ public class CTemplatePreferencePage extends TemplatePreferencePage {
|
||||||
return PreferenceConstants.TEMPLATES_USE_CODEFORMATTER;
|
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()
|
* @see org.eclipse.jface.preference.IPreferencePage#performOk()
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -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
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* 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.c.hover.CEditorTextHoverProxy;
|
||||||
import org.eclipse.cdt.internal.ui.text.contentassist.CCompletionProcessor2;
|
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.contentassist.ContentAssistPreference;
|
||||||
|
import org.eclipse.cdt.internal.ui.text.template.CTemplateCompletionProcessor;
|
||||||
import org.eclipse.cdt.internal.ui.util.ExternalEditorInput;
|
import org.eclipse.cdt.internal.ui.util.ExternalEditorInput;
|
||||||
|
|
||||||
|
|
||||||
|
@ -348,6 +349,7 @@ public class CSourceViewerConfiguration extends TextSourceViewerConfiguration {
|
||||||
IContentAssistProcessor processor = new CCompletionProcessor2(getEditor());
|
IContentAssistProcessor processor = new CCompletionProcessor2(getEditor());
|
||||||
assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
|
assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
|
||||||
|
|
||||||
|
assistant.setContentAssistProcessor(new CTemplateCompletionProcessor(getEditor()), ICPartitions.C_MULTI_LINE_COMMENT);
|
||||||
ContentAssistPreference.configure(assistant, fPreferenceStore);
|
ContentAssistPreference.configure(assistant, fPreferenceStore);
|
||||||
|
|
||||||
assistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);
|
assistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);
|
||||||
|
|
|
@ -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 <code>IContentAssistProcessor</code>.
|
||||||
|
*
|
||||||
|
* @param editor the editor, may not be <code>null</code>
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
|
@ -8,12 +8,14 @@
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* IBM Corporation - initial API and implementation
|
* IBM Corporation - initial API and implementation
|
||||||
* QNX Software System
|
* QNX Software System
|
||||||
|
* Anton Leherbauer (Wind River Systems)
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.ui.text.template;
|
package org.eclipse.cdt.internal.ui.text.template;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.eclipse.core.runtime.Assert;
|
||||||
import org.eclipse.jface.text.IDocument;
|
import org.eclipse.jface.text.IDocument;
|
||||||
import org.eclipse.jface.text.IInformationControl;
|
import org.eclipse.jface.text.IInformationControl;
|
||||||
import org.eclipse.jface.text.IInformationControlCreator;
|
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.TemplateContext;
|
||||||
import org.eclipse.jface.text.templates.TemplateContextType;
|
import org.eclipse.jface.text.templates.TemplateContextType;
|
||||||
import org.eclipse.jface.text.templates.TemplateProposal;
|
import org.eclipse.jface.text.templates.TemplateProposal;
|
||||||
import org.eclipse.core.runtime.Assert;
|
|
||||||
import org.eclipse.swt.SWT;
|
import org.eclipse.swt.SWT;
|
||||||
import org.eclipse.swt.graphics.Image;
|
import org.eclipse.swt.graphics.Image;
|
||||||
import org.eclipse.swt.graphics.Point;
|
import org.eclipse.swt.graphics.Point;
|
||||||
import org.eclipse.swt.widgets.Shell;
|
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.ITranslationUnit;
|
||||||
import org.eclipse.cdt.core.model.IWorkingCopy;
|
|
||||||
import org.eclipse.cdt.ui.CUIPlugin;
|
import org.eclipse.cdt.ui.CUIPlugin;
|
||||||
import org.eclipse.cdt.ui.text.ICCompletionProposal;
|
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.CContextType;
|
||||||
import org.eclipse.cdt.internal.corext.template.c.TranslationUnitContext;
|
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.CPluginImages;
|
||||||
import org.eclipse.cdt.internal.ui.text.c.hover.SourceViewerInformationControl;
|
import org.eclipse.cdt.internal.ui.text.c.hover.SourceViewerInformationControl;
|
||||||
|
|
||||||
public class TemplateEngine implements ICompletionContributor {
|
public class TemplateEngine {
|
||||||
|
|
||||||
private TemplateContextType fContextType;
|
private TemplateContextType fContextType;
|
||||||
private ArrayList fProposals= new ArrayList();
|
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
|
* This is the default constructor used by the new content assist extension point
|
||||||
*/
|
*/
|
||||||
public TemplateEngine() {
|
public TemplateEngine() {
|
||||||
fContextType = CUIPlugin.getDefault().getTemplateContextRegistry().getContextType(CContextType.CCONTEXT_TYPE);
|
fContextType = CUIPlugin.getDefault().getTemplateContextRegistry().getContextType(CContextType.ID);
|
||||||
if (fContextType == null) {
|
if (fContextType == null) {
|
||||||
fContextType= new CContextType();
|
fContextType= new CContextType();
|
||||||
CUIPlugin.getDefault().getTemplateContextRegistry().addContextType(fContextType);
|
CUIPlugin.getDefault().getTemplateContextRegistry().addContextType(fContextType);
|
||||||
|
@ -129,7 +127,6 @@ public class TemplateEngine implements ICompletionContributor {
|
||||||
|
|
||||||
Point selection= viewer.getSelectedRange();
|
Point selection= viewer.getSelectedRange();
|
||||||
|
|
||||||
((TranslationUnitContextType) fContextType).setContextParameters(document.get(), completionPosition, translationUnit);
|
|
||||||
TranslationUnitContext context= ((TranslationUnitContextType) fContextType).createContext(document, completionPosition, selection.y, translationUnit);
|
TranslationUnitContext context= ((TranslationUnitContextType) fContextType).createContext(document, completionPosition, selection.y, translationUnit);
|
||||||
int start= context.getStart();
|
int start= context.getStart();
|
||||||
int end= context.getEnd();
|
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)));
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,6 +63,7 @@ import org.eclipse.cdt.core.model.IWorkingCopyProvider;
|
||||||
|
|
||||||
import org.eclipse.cdt.internal.core.model.IBufferFactory;
|
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.CContextType;
|
||||||
|
import org.eclipse.cdt.internal.corext.template.c.CommentContextType;
|
||||||
|
|
||||||
import org.eclipse.cdt.internal.ui.CElementAdapterFactory;
|
import org.eclipse.cdt.internal.ui.CElementAdapterFactory;
|
||||||
import org.eclipse.cdt.internal.ui.ICStatusConstants;
|
import org.eclipse.cdt.internal.ui.ICStatusConstants;
|
||||||
|
@ -728,7 +729,8 @@ public class CUIPlugin extends AbstractUIPlugin {
|
||||||
public ContextTypeRegistry getTemplateContextRegistry() {
|
public ContextTypeRegistry getTemplateContextRegistry() {
|
||||||
if (fContextTypeRegistry == null) {
|
if (fContextTypeRegistry == null) {
|
||||||
fContextTypeRegistry= new ContributionContextTypeRegistry();
|
fContextTypeRegistry= new ContributionContextTypeRegistry();
|
||||||
fContextTypeRegistry.addContextType(CContextType.CCONTEXT_TYPE);
|
fContextTypeRegistry.addContextType(CContextType.ID);
|
||||||
|
fContextTypeRegistry.addContextType(CommentContextType.ID);
|
||||||
}
|
}
|
||||||
return fContextTypeRegistry;
|
return fContextTypeRegistry;
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,6 +81,6 @@ private:
|
||||||
</template>
|
</template>
|
||||||
<template name="stdout" description="%printtostdout" context="org.eclipse.cdt.ui.text.templates.c" id="org.eclipse.cdt.ui.text.templates.c.printf" enabled="true">printf(${cursor});</template>
|
<template name="stdout" description="%printtostdout" context="org.eclipse.cdt.ui.text.templates.c" id="org.eclipse.cdt.ui.text.templates.c.printf" enabled="true">printf(${cursor});</template>
|
||||||
<template name="stderr" description="%printtostderr" context="org.eclipse.cdt.ui.text.templates.c" id="org.eclipse.cdt.ui.text.templates.c.fprintf" enabled="true">fprintf(stderr, ${cursor});</template>
|
<template name="stderr" description="%printtostderr" context="org.eclipse.cdt.ui.text.templates.c" id="org.eclipse.cdt.ui.text.templates.c.fprintf" enabled="true">fprintf(stderr, ${cursor});</template>
|
||||||
<template name="author" description="%authorname" context="org.eclipse.cdt.ui.text.templates.c" id="org.eclipse.cdt.ui.text.templates.c.author" enabled="true">author ${user}</template>
|
<template name="author" description="%authorname" context="org.eclipse.cdt.ui.text.templates.comment" id="org.eclipse.cdt.ui.text.templates.comment.author" enabled="true">author ${user}</template>
|
||||||
|
|
||||||
</templates>
|
</templates>
|
||||||
|
|
Loading…
Add table
Reference in a new issue