1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

Fix for Bug 175283 - [Content Assist] Invalid keyword completion (Patch by Bryan Wilkinson)

This commit is contained in:
Anton Leherbauer 2007-02-26 09:34:22 +00:00
parent b2b1287072
commit 0a05580aef
5 changed files with 220 additions and 176 deletions

View file

@ -1582,21 +1582,6 @@
name="%CDTIndexer.domsourceindexer"/>
</extension>
<extension
point="org.eclipse.cdt.ui.completionContributors">
<contributor
class="org.eclipse.cdt.internal.ui.text.contentassist.KeywordCompletionContributor"
id="Keywords"
priority="10"/>
<contributor
class="org.eclipse.cdt.internal.ui.text.contentassist.HelpCompletionContributor"
id="Help"
priority="99"/>
<!--contributor
class="org.eclipse.cdt.internal.ui.text.contentassist.PDOMCompletionContributor"
id="PDOM"
priority="3"/-->
</extension>
<extension
point="org.eclipse.cdt.ui.completionProposalComputer"
@ -1651,6 +1636,31 @@
<partition type="__c_preprocessor"/>
</completionProposalComputer>
</extension>
<extension
id="KeywordCompletionProposalComputer"
point="org.eclipse.cdt.ui.completionProposalComputer">
<completionProposalComputer
categoryId="org.eclipse.cdt.ui.parserProposalCategory"
class="org.eclipse.cdt.internal.ui.text.contentassist.KeywordCompletionProposalComputer">
<partition
type="__dftl_partition_content_type">
</partition>
<partition
type="__c_preprocessor">
</partition>
</completionProposalComputer>
</extension>
<extension
id="HelpCompletionProposalComputer"
point="org.eclipse.cdt.ui.completionProposalComputer">
<completionProposalComputer
categoryId="org.eclipse.cdt.ui.parserProposalCategory"
class="org.eclipse.cdt.internal.ui.text.contentassist.HelpCompletionProposalComputer">
<partition
type="__dftl_partition_content_type">
</partition>
</completionProposalComputer>
</extension>
<!-- template proposals -->
<extension
point="org.eclipse.cdt.ui.completionProposalComputer"

View file

@ -1,104 +0,0 @@
/**********************************************************************
* Copyright (c) 2004, 2005 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.ui.text.contentassist;
import java.util.List;
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.internal.ui.CHelpProviderManager;
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.IFunctionSummary;
import org.eclipse.cdt.ui.text.ICHelpInvocationContext;
import org.eclipse.cdt.ui.text.contentassist.ICompletionContributor;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.ContextInformation;
import org.eclipse.swt.graphics.Image;
public class HelpCompletionContributor implements ICompletionContributor {
public void contributeCompletionProposals(ITextViewer viewer, int offset,
IWorkingCopy workingCopy, ASTCompletionNode completionNode, String prefix,
List proposals)
{
final IWorkingCopy fWorkingCopy = workingCopy;
if (completionNode != null) {
// Find matching functions
ICHelpInvocationContext context = new ICHelpInvocationContext() {
public IProject getProject() {
return fWorkingCopy.getCProject().getProject();
}
public ITranslationUnit getTranslationUnit() {
return fWorkingCopy.getTranslationUnit();
}
};
IASTName[] names = completionNode.getNames();
for (int i = 0; i < names.length; ++i) {
IASTName name = names[i];
if (name.getTranslationUnit() == null)
// Not connected
continue;
// ignore if this is a member access
if (name.getParent() instanceof IASTFieldReference)
continue;
IFunctionSummary[] summaries = CHelpProviderManager.getDefault().getMatchingFunctions(context, prefix);
if (summaries == null )
continue;
int repOffset = offset - prefix.length();
int repLength = prefix.length();
Image image = CUIPlugin.getImageDescriptorRegistry().get(CElementImageProvider.getFunctionImageDescriptor());
for (int j = 0; j < summaries.length; j++) {
IFunctionSummary summary = summaries[j];
String fname = summary.getName() + "()"; //$NON-NLS-1$
String fdesc = summary.getDescription();
IFunctionSummary.IFunctionPrototypeSummary fproto = summary.getPrototype();
String fargs = fproto.getArguments();
CCompletionProposal proposal;
proposal = new CCompletionProposal(fname,
repOffset,
repLength,
image,
fproto.getPrototypeString(true),
2,
viewer);
if (fdesc != null) {
proposal.setAdditionalProposalInfo(fdesc);
}
if (fargs != null && fargs.length() > 0) {
proposal.setContextInformation(new ContextInformation(fname, fargs));
// set the cursor before the closing bracket
proposal.setCursorPosition(fname.length() - 1);
}
proposals.add(proposal);
}
}
}
}
}

View file

@ -0,0 +1,126 @@
/*******************************************************************************
* Copyright (c) 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Bryan Wilkinson (QNX) - Initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.ui.text.contentassist;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.swt.graphics.Image;
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.IFunctionSummary;
import org.eclipse.cdt.ui.text.ICHelpInvocationContext;
import org.eclipse.cdt.internal.ui.CHelpProviderManager;
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
public class HelpCompletionProposalComputer extends ParsingBasedProposalComputer {
protected List computeCompletionProposals(
CContentAssistInvocationContext cContext,
ASTCompletionNode completionNode, String prefix)
throws CoreException {
boolean handleHelp = false;
if (completionNode != null) {
IASTName[] names = completionNode.getNames();
for (int i = 0; i < names.length; ++i) {
IASTName name = names[i];
// ignore if not connected
if (name.getTranslationUnit() == null)
continue;
// ignore if this is a member access
if (name.getParent() instanceof IASTFieldReference)
continue;
handleHelp = true;
break;
}
}
if (!handleHelp) {
return Collections.EMPTY_LIST;
}
final ITranslationUnit tu = cContext.getTranslationUnit();
// Find matching functions
ICHelpInvocationContext helpContext = new ICHelpInvocationContext() {
public IProject getProject() {
return tu.getCProject().getProject();
}
public ITranslationUnit getTranslationUnit() {
return tu;
}
};
IFunctionSummary[] summaries = CHelpProviderManager.getDefault()
.getMatchingFunctions(helpContext, prefix);
if (summaries == null)
return Collections.EMPTY_LIST;
int repOffset = cContext.getInvocationOffset() - prefix.length();
int repLength = prefix.length();
Image image = CUIPlugin.getImageDescriptorRegistry().get(
CElementImageProvider.getFunctionImageDescriptor());
List proposals = new ArrayList();
for (int j = 0; j < summaries.length; j++) {
IFunctionSummary summary = summaries[j];
String fname = summary.getName() + "()"; //$NON-NLS-1$
String fdesc = summary.getDescription();
IFunctionSummary.IFunctionPrototypeSummary fproto = summary
.getPrototype();
String fargs = fproto.getArguments();
CCompletionProposal proposal;
proposal = new CCompletionProposal(
fname,
repOffset,
repLength,
image,
fproto.getPrototypeString(true),
2,
cContext.getViewer());
if (fdesc != null) {
proposal.setAdditionalProposalInfo(fdesc);
}
if (!cContext.isContextInformationStyle()) {
// set the cursor before the closing bracket
proposal.setCursorPosition(fname.length() - 1);
}
if (fargs != null && fargs.length() > 0) {
CProposalContextInformation info = new CProposalContextInformation(image, fname, fargs);
info.setContextInformationPosition(cContext.getContextInformationOffset());
proposal.setContextInformation(info);
}
proposals.add(proposal);
}
return proposals;
}
}

View file

@ -1,99 +1,106 @@
/*******************************************************************************
* Copyright (c) 2005, 2007 IBM Corporation and others.
* Copyright (c) 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM - Initial API and implementation
* Anton Leherbauer (Wind River Systems)
* Bryan Wilkinson (QNX) - Initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.ui.text.contentassist;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextUtilities;
import org.eclipse.swt.graphics.Image;
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.parser.Directives;
import org.eclipse.cdt.core.parser.Keywords;
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.text.ICPartitions;
import org.eclipse.cdt.ui.text.contentassist.ICompletionContributor;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.TextUtilities;
import org.eclipse.swt.graphics.Image;
import org.eclipse.cdt.ui.text.contentassist.ICompletionProposalComputer;
public class KeywordCompletionContributor implements ICompletionContributor {
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
public void contributeCompletionProposals(ITextViewer viewer, int offset,
IWorkingCopy workingCopy, ASTCompletionNode completionNode,
String prefix, List proposals) {
public class KeywordCompletionProposalComputer extends ParsingBasedProposalComputer implements ICompletionProposalComputer {
// No prefix, no completions
if (prefix.length() == 0)
return;
protected List computeCompletionProposals(
CContentAssistInvocationContext context,
ASTCompletionNode completionNode, String prefix)
throws CoreException {
// No prefix, no completions
if (prefix.length() == 0 || context.isContextInformationStyle())
return Collections.EMPTY_LIST;
String[] keywords;
if(inPreprocessorDirective(viewer.getDocument(), offset)) {
if(inPreprocessorDirective(context.getDocument(), context.getInvocationOffset())) {
keywords= preprocessorKeywords;
} else {
if (!validContext(completionNode))
return;
if (!isValidContext(completionNode))
return Collections.EMPTY_LIST;
ITranslationUnit tu = context.getTranslationUnit();
keywords = cppkeywords; // default to C++
if (workingCopy != null && workingCopy.isCLanguage())
if (tu != null && tu.isCLanguage())
keywords = ckeywords;
}
List proposals = new ArrayList();
// add matching keyword proposals
ImageDescriptor imagedesc = CElementImageProvider.getKeywordImageDescriptor();
Image image = imagedesc != null ? CUIPlugin.getImageDescriptorRegistry().get(imagedesc) : null;
for (int i = 0; i < keywords.length; ++i) {
if (keywords[i].startsWith(prefix)) {
int repLength = prefix.length();
int repOffset = offset - repLength;
proposals.add(new CCompletionProposal(keywords[i], repOffset, repLength, image, keywords[i], 1, viewer));
int repOffset = context.getInvocationOffset() - repLength;
proposals.add(new CCompletionProposal(keywords[i], repOffset,
repLength, image, keywords[i], 1, context.getViewer()));
}
}
}
// TODO This is copied from the search completion contributor
// We should make this common
private boolean validContext(ASTCompletionNode completionNode) {
if (completionNode == null)
// No completion node, assume true
return true;
boolean valid = true;
IASTName[] names = completionNode.getNames();
for (int i = 0; i < names.length; i++) {
IASTName name = names[i];
// not hooked up, not a valid name, ignore
if (name.getTranslationUnit() == null)
continue;
// member access currently isn't valid
if (name.getParent() instanceof IASTFieldReference) {
valid = false;
continue;
}
// found one that was valid
return true;
}
// Couldn't find a valid context
return valid;
}
return proposals;
}
/**
* Checks whether the given invocation context looks valid for template completion.
*
* @param context the content assist invocation context
* @return <code>false</code> if the given invocation context looks like a field reference
*/
private boolean isValidContext(ASTCompletionNode completionNode) {
IASTName[] names = completionNode.getNames();
for (int i = 0; i < names.length; ++i) {
IASTName name = names[i];
// ignore if not connected
if (name.getTranslationUnit() == null)
continue;
// ignore if this is a member access
if (name.getParent() instanceof IASTFieldReference)
continue;
return true;
}
return false;
}
/**
* Check if given offset is inside a preprocessor directive.
*
@ -112,7 +119,7 @@ public class KeywordCompletionContributor implements ICompletionContributor {
}
return false;
}
// These are the keywords we complete
// We only do the ones that are >= 5 characters long
private static String [] ckeywords = {

View file

@ -13,6 +13,7 @@
package org.eclipse.cdt.internal.ui.text.contentassist;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.core.runtime.CoreException;
@ -45,6 +46,10 @@ public class LegacyCompletionProposalComputer extends ParsingBasedProposalComput
CContentAssistInvocationContext context,
ASTCompletionNode completionNode, String prefix) throws CoreException {
if (context.isContextInformationStyle()) {
// context information cannot be supported by completionContributors
return Collections.EMPTY_LIST;
}
ITextViewer viewer = context.getViewer();
int offset = context.getInvocationOffset();
IWorkingCopy workingCopy = context.getTranslationUnit().getWorkingCopy();