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:
parent
058ee238ee
commit
6335d2b66f
3 changed files with 80 additions and 20 deletions
|
@ -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
|
* 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
|
||||||
|
@ -26,7 +26,6 @@ public class CompletionTest_SingleName_Method_NoPrefix extends CompletionPropos
|
||||||
private final String headerFileFullPath ="resources/contentassist/" + headerFileName;
|
private final String headerFileFullPath ="resources/contentassist/" + headerFileName;
|
||||||
private final String expectedPrefix = "";
|
private final String expectedPrefix = "";
|
||||||
|
|
||||||
//TODO Hoda - please update this constant with what it is supposed to be
|
|
||||||
private final String[] expectedResults = {
|
private final String[] expectedResults = {
|
||||||
"AStruct",
|
"AStruct",
|
||||||
"XStruct",
|
"XStruct",
|
||||||
|
@ -59,7 +58,8 @@ public class CompletionTest_SingleName_Method_NoPrefix extends CompletionPropos
|
||||||
public CompletionTest_SingleName_Method_NoPrefix(String name) {
|
public CompletionTest_SingleName_Method_NoPrefix(String name) {
|
||||||
super(name);
|
super(name);
|
||||||
// operators should not be proposed
|
// operators should not be proposed
|
||||||
setExpectFailure(77777);
|
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172304
|
||||||
|
setExpectFailure(172304);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Test suite() {
|
public static Test suite() {
|
||||||
|
|
|
@ -121,6 +121,7 @@ public class CompletionTests extends AbstractCompletionTest {
|
||||||
//void gfunc() {C1 v; v.m/*cursor*/
|
//void gfunc() {C1 v; v.m/*cursor*/
|
||||||
public void _testLocalVariable() throws Exception {
|
public void _testLocalVariable() throws Exception {
|
||||||
// fails because of additional m1private(void)
|
// fails because of additional m1private(void)
|
||||||
|
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172305
|
||||||
final String[] expected= {
|
final String[] expected= {
|
||||||
"m123(void)", "m12(void)", "m13(void)"
|
"m123(void)", "m12(void)", "m13(void)"
|
||||||
};
|
};
|
||||||
|
@ -130,6 +131,7 @@ public class CompletionTests extends AbstractCompletionTest {
|
||||||
//void gfunc() {C1 v; v.fMySelf.m/*cursor*/
|
//void gfunc() {C1 v; v.fMySelf.m/*cursor*/
|
||||||
public void _testLocalVariable_MemberVariable() throws Exception {
|
public void _testLocalVariable_MemberVariable() throws Exception {
|
||||||
// fails because of additional m1private(void)
|
// fails because of additional m1private(void)
|
||||||
|
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172305
|
||||||
final String[] expected= {
|
final String[] expected= {
|
||||||
"m123(void)", "m12(void)", "m13(void)"
|
"m123(void)", "m12(void)", "m13(void)"
|
||||||
};
|
};
|
||||||
|
@ -139,6 +141,7 @@ public class CompletionTests extends AbstractCompletionTest {
|
||||||
//void gfunc() {C1 v; v.m12().m/*cursor*/
|
//void gfunc() {C1 v; v.m12().m/*cursor*/
|
||||||
public void _testLocalVariable_MemberFunction() throws Exception {
|
public void _testLocalVariable_MemberFunction() throws Exception {
|
||||||
// fails because of additional m1private(void)
|
// fails because of additional m1private(void)
|
||||||
|
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172305
|
||||||
final String[] expected= {
|
final String[] expected= {
|
||||||
"m123(void)", "m12(void)", "m13(void)"
|
"m123(void)", "m12(void)", "m13(void)"
|
||||||
};
|
};
|
||||||
|
@ -148,6 +151,7 @@ public class CompletionTests extends AbstractCompletionTest {
|
||||||
//void gfunc() {gfC1().m/*cursor*/
|
//void gfunc() {gfC1().m/*cursor*/
|
||||||
public void _testGlobalFunction() throws Exception {
|
public void _testGlobalFunction() throws Exception {
|
||||||
// fails because of additional m1private(void)
|
// fails because of additional m1private(void)
|
||||||
|
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172305
|
||||||
final String[] expected= {
|
final String[] expected= {
|
||||||
"m123(void)", "m12(void)", "m13(void)"
|
"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*/
|
//void gfunc() {try{int bla;}catch(C1 v) {v.fMySelf.m/*cursor*/
|
||||||
public void _testCatchBlock1() throws Exception {
|
public void _testCatchBlock1() throws Exception {
|
||||||
// fails because of additional m1private(void)
|
// fails because of additional m1private(void)
|
||||||
|
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172305
|
||||||
final String[] expected= {
|
final String[] expected= {
|
||||||
"m123(void)", "m12(void)", "m13(void)"
|
"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*/
|
//void gfunc() {try{int bla;}catch(C2 c){} catch(C1 v) {v.fMySelf.m/*cursor*/
|
||||||
public void _testCatchBlock2() throws Exception {
|
public void _testCatchBlock2() throws Exception {
|
||||||
// fails because of additional m1private(void)
|
// fails because of additional m1private(void)
|
||||||
|
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172305
|
||||||
final String[] expected= {
|
final String[] expected= {
|
||||||
"m123(void)", "m12(void)", "m13(void)"
|
"m123(void)", "m12(void)", "m13(void)"
|
||||||
};
|
};
|
||||||
|
@ -255,6 +261,7 @@ public class CompletionTests extends AbstractCompletionTest {
|
||||||
//void f() {C1* l1; l1.m/*cursor*/
|
//void f() {C1* l1; l1.m/*cursor*/
|
||||||
public void _testMethods_GlobalScope() throws Exception {
|
public void _testMethods_GlobalScope() throws Exception {
|
||||||
// fails because of additional m1private(void)
|
// fails because of additional m1private(void)
|
||||||
|
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172305
|
||||||
final String[] expected= {
|
final String[] expected= {
|
||||||
"m123(void)", "m12(void)", "m13(void)"
|
"m123(void)", "m12(void)", "m13(void)"
|
||||||
};
|
};
|
||||||
|
@ -264,6 +271,7 @@ public class CompletionTests extends AbstractCompletionTest {
|
||||||
//void C3::f() {m/*cursor*/
|
//void C3::f() {m/*cursor*/
|
||||||
public void _testMethods_MethodScope() throws Exception {
|
public void _testMethods_MethodScope() throws Exception {
|
||||||
// fails because of additional m1private(void)
|
// fails because of additional m1private(void)
|
||||||
|
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172305
|
||||||
final String[] expected= {
|
final String[] expected= {
|
||||||
"m123(void)", "m12(void)", "m13(void)"
|
"m123(void)", "m12(void)", "m13(void)"
|
||||||
};
|
};
|
||||||
|
@ -536,4 +544,12 @@ public class CompletionTests extends AbstractCompletionTest {
|
||||||
};
|
};
|
||||||
assertCompletionResults(fCursorOffset, expected, true);
|
assertCompletionResults(fCursorOffset, expected, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//#i/*cursor*/
|
||||||
|
public void testCompletePreprocessorDirective() throws Exception {
|
||||||
|
final String[] expected= {
|
||||||
|
"#if", "#ifdef", "#ifndef", "#include"
|
||||||
|
};
|
||||||
|
assertCompletionResults(fCursorOffset, expected, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
@ -7,6 +7,7 @@
|
||||||
*
|
*
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* IBM - Initial API and implementation
|
* IBM - Initial API and implementation
|
||||||
|
* Anton Leherbauer (Wind River Systems)
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.ui.text.contentassist;
|
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.IASTFieldReference;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||||
import org.eclipse.cdt.core.model.IWorkingCopy;
|
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.core.parser.Keywords;
|
||||||
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
|
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
|
||||||
import org.eclipse.cdt.ui.CUIPlugin;
|
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.ui.text.contentassist.ICompletionContributor;
|
||||||
import org.eclipse.jface.resource.ImageDescriptor;
|
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.ITextViewer;
|
||||||
|
import org.eclipse.jface.text.TextUtilities;
|
||||||
import org.eclipse.swt.graphics.Image;
|
import org.eclipse.swt.graphics.Image;
|
||||||
|
|
||||||
public class KeywordCompletionContributor implements ICompletionContributor {
|
public class KeywordCompletionContributor implements ICompletionContributor {
|
||||||
|
@ -34,22 +40,28 @@ public class KeywordCompletionContributor implements ICompletionContributor {
|
||||||
if (prefix.length() == 0)
|
if (prefix.length() == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!validContext(completionNode))
|
String[] keywords;
|
||||||
return;
|
if(inPreprocessorDirective(viewer.getDocument(), offset)) {
|
||||||
|
keywords= preprocessorKeywords;
|
||||||
String[] keywords = cppkeywords; // default to C++
|
} else {
|
||||||
if (workingCopy != null && workingCopy.isCLanguage())
|
if (!validContext(completionNode))
|
||||||
keywords = ckeywords;
|
return;
|
||||||
|
|
||||||
if (prefix.length() > 0)
|
keywords = cppkeywords; // default to C++
|
||||||
for (int i = 0; i < keywords.length; ++i)
|
if (workingCopy != null && workingCopy.isCLanguage())
|
||||||
if (keywords[i].startsWith(prefix)) {
|
keywords = ckeywords;
|
||||||
ImageDescriptor imagedesc = CElementImageProvider.getKeywordImageDescriptor();
|
|
||||||
Image image = imagedesc != null ? CUIPlugin.getImageDescriptorRegistry().get(imagedesc) : null;
|
}
|
||||||
int repLength = prefix.length();
|
// add matching keyword proposals
|
||||||
int repOffset = offset - repLength;
|
ImageDescriptor imagedesc = CElementImageProvider.getKeywordImageDescriptor();
|
||||||
proposals.add(new CCompletionProposal(keywords[i], repOffset, repLength, image, keywords[i], 1, viewer));
|
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
|
// TODO This is copied from the search completion contributor
|
||||||
|
@ -82,6 +94,25 @@ public class KeywordCompletionContributor implements ICompletionContributor {
|
||||||
return valid;
|
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
|
// These are the keywords we complete
|
||||||
// We only do the ones that are >= 5 characters long
|
// We only do the ones that are >= 5 characters long
|
||||||
private static String [] ckeywords = {
|
private static String [] ckeywords = {
|
||||||
|
@ -160,4 +191,17 @@ public class KeywordCompletionContributor implements ICompletionContributor {
|
||||||
Keywords.WHILE
|
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,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue