1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-29 11:55:40 +02:00

Fix for Bug 141970 - Completion of preprocessor directives

This commit is contained in:
Anton Leherbauer 2007-01-31 13:09:21 +00:00
parent 058ee238ee
commit 6335d2b66f
3 changed files with 80 additions and 20 deletions

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2004 IBM Corporation and others.
* Copyright (c) 2004, 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
@ -26,7 +26,6 @@ public class CompletionTest_SingleName_Method_NoPrefix extends CompletionPropos
private final String headerFileFullPath ="resources/contentassist/" + headerFileName;
private final String expectedPrefix = "";
//TODO Hoda - please update this constant with what it is supposed to be
private final String[] expectedResults = {
"AStruct",
"XStruct",
@ -59,7 +58,8 @@ public class CompletionTest_SingleName_Method_NoPrefix extends CompletionPropos
public CompletionTest_SingleName_Method_NoPrefix(String name) {
super(name);
// operators should not be proposed
setExpectFailure(77777);
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172304
setExpectFailure(172304);
}
public static Test suite() {

View file

@ -121,6 +121,7 @@ public class CompletionTests extends AbstractCompletionTest {
//void gfunc() {C1 v; v.m/*cursor*/
public void _testLocalVariable() throws Exception {
// fails because of additional m1private(void)
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172305
final String[] expected= {
"m123(void)", "m12(void)", "m13(void)"
};
@ -130,6 +131,7 @@ public class CompletionTests extends AbstractCompletionTest {
//void gfunc() {C1 v; v.fMySelf.m/*cursor*/
public void _testLocalVariable_MemberVariable() throws Exception {
// fails because of additional m1private(void)
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172305
final String[] expected= {
"m123(void)", "m12(void)", "m13(void)"
};
@ -139,6 +141,7 @@ public class CompletionTests extends AbstractCompletionTest {
//void gfunc() {C1 v; v.m12().m/*cursor*/
public void _testLocalVariable_MemberFunction() throws Exception {
// fails because of additional m1private(void)
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172305
final String[] expected= {
"m123(void)", "m12(void)", "m13(void)"
};
@ -148,6 +151,7 @@ public class CompletionTests extends AbstractCompletionTest {
//void gfunc() {gfC1().m/*cursor*/
public void _testGlobalFunction() throws Exception {
// fails because of additional m1private(void)
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172305
final String[] expected= {
"m123(void)", "m12(void)", "m13(void)"
};
@ -173,6 +177,7 @@ public class CompletionTests extends AbstractCompletionTest {
//void gfunc() {try{int bla;}catch(C1 v) {v.fMySelf.m/*cursor*/
public void _testCatchBlock1() throws Exception {
// fails because of additional m1private(void)
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172305
final String[] expected= {
"m123(void)", "m12(void)", "m13(void)"
};
@ -182,6 +187,7 @@ public class CompletionTests extends AbstractCompletionTest {
//void gfunc() {try{int bla;}catch(C2 c){} catch(C1 v) {v.fMySelf.m/*cursor*/
public void _testCatchBlock2() throws Exception {
// fails because of additional m1private(void)
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172305
final String[] expected= {
"m123(void)", "m12(void)", "m13(void)"
};
@ -255,6 +261,7 @@ public class CompletionTests extends AbstractCompletionTest {
//void f() {C1* l1; l1.m/*cursor*/
public void _testMethods_GlobalScope() throws Exception {
// fails because of additional m1private(void)
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172305
final String[] expected= {
"m123(void)", "m12(void)", "m13(void)"
};
@ -264,6 +271,7 @@ public class CompletionTests extends AbstractCompletionTest {
//void C3::f() {m/*cursor*/
public void _testMethods_MethodScope() throws Exception {
// fails because of additional m1private(void)
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172305
final String[] expected= {
"m123(void)", "m12(void)", "m13(void)"
};
@ -536,4 +544,12 @@ public class CompletionTests extends AbstractCompletionTest {
};
assertCompletionResults(fCursorOffset, expected, true);
}
//#i/*cursor*/
public void testCompletePreprocessorDirective() throws Exception {
final String[] expected= {
"#if", "#ifdef", "#ifndef", "#include"
};
assertCompletionResults(fCursorOffset, expected, true);
}
}

View file

@ -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
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@ -7,6 +7,7 @@
*
* Contributors:
* IBM - Initial API and implementation
* Anton Leherbauer (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.internal.ui.text.contentassist;
@ -16,12 +17,17 @@ 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.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;
public class KeywordCompletionContributor implements ICompletionContributor {
@ -34,22 +40,28 @@ public class KeywordCompletionContributor implements ICompletionContributor {
if (prefix.length() == 0)
return;
if (!validContext(completionNode))
return;
String[] keywords = cppkeywords; // default to C++
if (workingCopy != null && workingCopy.isCLanguage())
keywords = ckeywords;
if (prefix.length() > 0)
for (int i = 0; i < keywords.length; ++i)
if (keywords[i].startsWith(prefix)) {
ImageDescriptor imagedesc = CElementImageProvider.getKeywordImageDescriptor();
Image image = imagedesc != null ? CUIPlugin.getImageDescriptorRegistry().get(imagedesc) : null;
int repLength = prefix.length();
int repOffset = offset - repLength;
proposals.add(new CCompletionProposal(keywords[i], repOffset, repLength, image, keywords[i], 1, viewer));
}
String[] keywords;
if(inPreprocessorDirective(viewer.getDocument(), offset)) {
keywords= preprocessorKeywords;
} else {
if (!validContext(completionNode))
return;
keywords = cppkeywords; // default to C++
if (workingCopy != null && workingCopy.isCLanguage())
keywords = ckeywords;
}
// 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));
}
}
}
// TODO This is copied from the search completion contributor
@ -82,6 +94,25 @@ public class KeywordCompletionContributor implements ICompletionContributor {
return valid;
}
/**
* Check if given offset is inside a preprocessor directive.
*
* @param doc the document
* @param offset the offset to check
* @return <code>true</code> if offset is inside a preprocessor directive
*/
private boolean inPreprocessorDirective(IDocument doc, int offset) {
if (offset > 0 && offset == doc.getLength()) {
--offset;
}
try {
return ICPartitions.C_PREPROCESSOR
.equals(TextUtilities.getContentType(doc, ICPartitions.C_PARTITIONING, offset, false));
} catch (BadLocationException exc) {
}
return false;
}
// These are the keywords we complete
// We only do the ones that are >= 5 characters long
private static String [] ckeywords = {
@ -160,4 +191,17 @@ public class KeywordCompletionContributor implements ICompletionContributor {
Keywords.WHILE
};
private static String [] preprocessorKeywords = {
Directives.POUND_DEFINE,
Directives.POUND_ELIF,
Directives.POUND_ELSE,
Directives.POUND_ENDIF,
Directives.POUND_ERROR,
Directives.POUND_IF,
Directives.POUND_IFDEF,
Directives.POUND_IFNDEF,
Directives.POUND_INCLUDE,
Directives.POUND_PRAGMA,
Directives.POUND_UNDEF,
};
}