1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Patch for Hoda Amer.

Hi, 
This patch updates code completion to use search. It also includes my previous patch which enabled the class wizard to use search as well. 

Current Code Completion has the following restrictions: 
- It will only work within the scope of a function or a method. 
-It will look for globals (variables, functions, classes, strucs, unions, enumerations, and macros). 
-In the scope of a method, it will also look for the methods and fields that belong to the owner class of this method. 
- It will NOT search the parent classes of the method in the method scope case. 
- It will NOT de-reference after a "." or an "->"
This commit is contained in:
John Camelon 2003-08-12 20:20:13 +00:00
parent a94839573b
commit 8dba12bff3
53 changed files with 817 additions and 128 deletions

View file

@ -1,3 +1,7 @@
2003-08-12
Added CompletionProposalsTest to the suit to test the generation
of completion proposals.
2003-08-12 Bogdan Gheorghe
- Changed testVariableIndexPrefix, testVariableDeclaration to
reflect changes to the var search pattern

View file

@ -97,7 +97,7 @@ public class CModelElementsFailedTests extends TestCase {
public void testBug36379() {
TranslationUnit tu = new TranslationUnit(fCProject, headerFile);
// parse the translation unit to get the elements tree
Map newElement = tu.parse(true); // require line numbers
Map newElement = tu.parse(); // require line numbers
// tu ---> namespace: MyPackage
ArrayList tuPackages = tu.getChildrenOfType(ICElement.C_NAMESPACE);

View file

@ -0,0 +1,149 @@
/*******************************************************************************
* Copyright (c) 2001 Rational Software Corp. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* Rational Software - initial implementation
******************************************************************************/
package org.eclipse.cdt.core.codeassist.tests;
import java.io.FileInputStream;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.CCProjectNature;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.core.model.TranslationUnit;
import org.eclipse.cdt.internal.core.search.indexing.IndexManager;
import org.eclipse.cdt.internal.ui.text.CCompletionProcessor;
import org.eclipse.cdt.testplugin.CProjectHelper;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
/**
* @author hamer
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class CompletionProposalsTest extends TestCase{
private final static long MAGIC_NUMBER = 1000;
private ICProject fCProject;
private IFile headerFile;
private NullProgressMonitor monitor;
public static Test suite() {
TestSuite suite= new TestSuite(CompletionProposalsTest.class.getName());
suite.addTest(new CompletionProposalsTest("testCompletionProposals"));
return suite;
}
public CompletionProposalsTest(String name) {
super(name);
}
protected void setUp() throws Exception {
monitor = new NullProgressMonitor();
String pluginRoot=org.eclipse.core.runtime.Platform.getPlugin("org.eclipse.cdt.core.tests").find(new Path("/")).getFile();
fCProject= CProjectHelper.createCProject("TestProject1", "bin");
headerFile = fCProject.getProject().getFile("CompletionProposalsTest.h");
if (!headerFile.exists()) {
try{
FileInputStream fileIn = new FileInputStream(pluginRoot+ "resources/cfiles/CompletionProposalsTestStart.h");
headerFile.create(fileIn,false, monitor);
} catch (CoreException e) {
e.printStackTrace();
}
}
if (!fCProject.getProject().hasNature(CCProjectNature.CC_NATURE_ID)) {
addNatureToProject(fCProject.getProject(), CCProjectNature.CC_NATURE_ID, null);
}
CCorePlugin.getDefault().setUseNewParser(true);
// use the new indexer
IndexManager indexManager = CCorePlugin.getDefault().getCoreModel().getIndexManager();
indexManager.setEnabled(fCProject.getProject(),true);
}
private static void addNatureToProject(IProject proj, String natureId, IProgressMonitor monitor) throws CoreException {
IProjectDescription description = proj.getDescription();
String[] prevNatures= description.getNatureIds();
String[] newNatures= new String[prevNatures.length + 1];
System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
newNatures[prevNatures.length]= natureId;
description.setNatureIds(newNatures);
proj.setDescription(description, monitor);
}
protected void tearDown() throws Exception {
CProjectHelper.delete(fCProject);
}
public void testCompletionProposals(){
try{
TranslationUnit tu = new TranslationUnit(fCProject, headerFile);
Document document = new Document(tu.getBuffer().getContents());
int pos = 399;
int length = 0;
CCompletionProcessor completionProcessor = new CCompletionProcessor(null);
ICompletionProposal[] results = completionProcessor.evalProposals(document, pos, length, tu);
try {
Thread.sleep(MAGIC_NUMBER);
} catch (InterruptedException e1) {
fail( "Bogdan's hack did not suffice");
}
assertEquals(results.length, 9);
for (int i = 0; i<results.length; i++){
ICompletionProposal proposal = results[i];
String displayString = proposal.getDisplayString();
switch(i){
case 0:
assertEquals(displayString, "anotherField");
break;
case 1:
assertEquals(displayString, "aVariable");
break;
case 2:
assertEquals(displayString, "anotherMethod(int, char) void");
break;
case 3:
assertEquals(displayString, "aFunction(char, int) void");
break;
case 4:
assertEquals(displayString, "aClass");
break;
case 5:
assertEquals(displayString, "anotherClass");
break;
case 6:
assertEquals(displayString, "aStruct");
break;
case 7:
assertEquals(displayString, "aMacro");
break;
case 8:
assertEquals(displayString, "anEnumeration");
break;
}
}
} catch(CModelException e){
}
}
}

View file

@ -105,7 +105,7 @@ public class CModelElementsTests extends TestCase {
public void testCModelElements(){
TranslationUnit tu = new TranslationUnit(fCProject, headerFile);
// parse the translation unit to get the elements tree
Map newElement = tu.parse(true); // require line numbers
Map newElement = tu.parse();
// tu ---> include
checkInclude(tu);

View file

@ -98,7 +98,7 @@ public abstract class IntegratedCModelTest extends TestCase {
protected ITranslationUnit getTU() {
TranslationUnit tu = new TranslationUnit(fCProject, sourceFile);
// parse the translation unit to get the elements tree
Map newElement = tu.parse(false); // FALSE=require line numbers
Map newElement = tu.parse();
return tu;
}
}

View file

@ -0,0 +1,32 @@
#define aMacro(x) x+1
int aVariable = 0;
void aFunction(char P1, int P2){
int f1 = 0;
}
enum anEnumeration {
first,
second,
third
};
struct aStruct{
int aStructField = 0;
};
class aClass {
public:
int aField;
int aMethod(int P1, char P2){
return P1 + P2;
}
};
class anotherClass {
public:
int anotherField = 0;
void anotherMethod(int P1, char P2){
a
}
};

View file

@ -20,7 +20,7 @@ import org.eclipse.cdt.core.search.ICSearchConstants;
import org.eclipse.cdt.core.search.ICSearchPattern;
import org.eclipse.cdt.core.search.IMatch;
import org.eclipse.cdt.core.search.SearchEngine;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.CharOperation;
import org.eclipse.cdt.internal.core.search.matching.ClassDeclarationPattern;
import org.eclipse.cdt.internal.core.search.matching.MatchLocator;
import org.eclipse.cdt.internal.core.search.matching.OrPattern;

View file

@ -17,7 +17,7 @@ import java.util.Set;
import org.eclipse.cdt.core.search.ICSearchPattern;
import org.eclipse.cdt.core.search.SearchEngine;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.CharOperation;
import org.eclipse.cdt.internal.core.search.matching.MethodDeclarationPattern;
/**

View file

@ -18,7 +18,7 @@ import java.util.Set;
import org.eclipse.cdt.core.search.ICSearchPattern;
import org.eclipse.cdt.core.search.IMatch;
import org.eclipse.cdt.core.search.SearchEngine;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.CharOperation;
import org.eclipse.cdt.internal.core.search.matching.FieldDeclarationPattern;
import org.eclipse.cdt.internal.core.search.matching.NamespaceDeclarationPattern;
import org.eclipse.cdt.internal.core.search.matching.OrPattern;

View file

@ -19,6 +19,7 @@ import junit.textui.TestRunner;
import org.eclipse.cdt.core.build.managed.tests.AllBuildTests;
import org.eclipse.cdt.core.build.managed.tests.StandardBuildTests;
import org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest;
import org.eclipse.cdt.core.indexer.tests.IndexManagerTests;
import org.eclipse.cdt.core.model.failedTests.CModelElementsFailedTests;
import org.eclipse.cdt.core.model.tests.AllCoreTests;
@ -91,6 +92,7 @@ public class AutomatedIntegrationSuite extends TestSuite
suite.addTestSuite(OtherPatternTests.class );
suite.addTest(IndexManagerTests.suite());
suite.addTestSuite( ParseTestOnSearchFiles.class);
suite.addTestSuite( CompletionProposalsTest.class);
// Last test to trigger report generation
suite.addTest(suite.new GenerateReport("startFailedTests"));

View file

@ -1,3 +1,8 @@
2003-08-12 Hoda Amer
Moved CharOperations and Utils from internal.core.search to internal.core
Added CConventions class to validate class names
Used the new search (indexer) for Code completion in CCompletionProcessor
2003-08-11 Andrew Niefer
Added getSharedWorkingCopies to CCorePlugin.

View file

@ -84,7 +84,7 @@ public class AddFileToDependencyTree extends DependencyRequest {
try {
IPath location = resource.getLocation();
if (location != null)
this.contents = org.eclipse.cdt.internal.core.search.Util.getFileCharContent(location.toFile(), null);
this.contents = org.eclipse.cdt.internal.core.Util.getFileCharContent(location.toFile(), null);
} catch (IOException e) {
}
}

View file

@ -21,7 +21,7 @@ import java.util.zip.CRC32;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.CharOperation;
import org.eclipse.cdt.internal.core.search.SimpleLookupTable;
import org.eclipse.cdt.internal.core.search.indexing.ReadWriteMonitor;
import org.eclipse.cdt.internal.core.search.processing.JobManager;
@ -207,7 +207,7 @@ public class DependencyManager extends JobManager implements ISourceDependency {
private char[] readDTreeState() {
try {
return org.eclipse.cdt.internal.core.search.Util.getFileCharContent(savedDTreesFile, null);
return org.eclipse.cdt.internal.core.Util.getFileCharContent(savedDTreesFile, null);
} catch (IOException ignored) {
if (VERBOSE)
JobManager.verbose("Failed to read saved dTree file names"); //$NON-NLS-1$
@ -216,7 +216,7 @@ public class DependencyManager extends JobManager implements ISourceDependency {
}
private void rebuildDTree(String treeName, IPath path) {
Object target = org.eclipse.cdt.internal.core.search.Util.getTarget(ResourcesPlugin.getWorkspace().getRoot(), path, true);
Object target = org.eclipse.cdt.internal.core.Util.getTarget(ResourcesPlugin.getWorkspace().getRoot(), path, true);
if (target == null) return;
if (VERBOSE)

View file

@ -14,10 +14,10 @@ package org.eclipse.cdt.internal.core.sourcedependency;
import java.util.HashSet;
import org.eclipse.cdt.core.build.managed.ManagedBuildManager;
import org.eclipse.cdt.internal.core.Util;
import org.eclipse.cdt.internal.core.index.IQueryResult;
import org.eclipse.cdt.internal.core.index.impl.IFileDocument;
import org.eclipse.cdt.internal.core.search.SimpleLookupTable;
import org.eclipse.cdt.internal.core.search.Util;
import org.eclipse.cdt.internal.core.search.indexing.ReadWriteMonitor;
import org.eclipse.cdt.internal.core.search.processing.JobManager;
import org.eclipse.core.resources.IFile;

View file

@ -14,7 +14,7 @@ package org.eclipse.cdt.internal.core.sourcedependency.impl;
import java.util.ArrayList;
import java.util.Iterator;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.CharOperation;
/**
* @author bgheorgh

View file

@ -11,7 +11,7 @@
package org.eclipse.cdt.internal.core.sourcedependency.impl;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.CharOperation;
public final class IncludeEntryHashedArray {

View file

@ -8,7 +8,7 @@
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.search;
package org.eclipse.cdt.internal.core;
/**
* This class is a collection of helper methods to manipulate char arrays.

View file

@ -8,7 +8,7 @@
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.search;
package org.eclipse.cdt.internal.core;
import java.io.BufferedInputStream;
import java.io.File;
@ -21,6 +21,7 @@ import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import org.eclipse.cdt.internal.core.*;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
@ -33,7 +34,7 @@ public class Util {
/* Bundle containing messages */
protected static ResourceBundle bundle;
private final static String bundleName = "org.eclipse.cdt.internal.core.search.messages"; //$NON-NLS-1$
private final static String bundleName = "org.eclipse.cdt.internal.core.messages"; //$NON-NLS-1$
static {
relocalize();
}

View file

@ -15,11 +15,11 @@ import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.CharOperation;
import org.eclipse.cdt.internal.core.Util;
import org.eclipse.cdt.internal.core.index.IDocument;
import org.eclipse.cdt.internal.core.index.IEntryResult;
import org.eclipse.cdt.internal.core.index.IQueryResult;
import org.eclipse.cdt.internal.core.search.Util;
import org.eclipse.cdt.internal.core.util.LRUCache;
/**

View file

@ -10,8 +10,8 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.index.impl;
import org.eclipse.cdt.internal.core.CharOperation;
import org.eclipse.cdt.internal.core.index.IEntryResult;
import org.eclipse.cdt.internal.core.search.CharOperation;
public class EntryResult implements IEntryResult {

View file

@ -14,7 +14,7 @@ import java.io.IOException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.CharOperation;
/**
@ -54,7 +54,7 @@ public class IFileDocument extends PropertyDocument {
if (byteContents != null) return byteContents;
IPath location = file.getLocation();
if (location == null) return new byte[0];
return byteContents = org.eclipse.cdt.internal.core.search.Util.getFileByteContent(location.toFile());
return byteContents = org.eclipse.cdt.internal.core.Util.getFileByteContent(location.toFile());
}
/**
* @see org.eclipse.jdt.internal.core.index.IDocument#getCharContent()
@ -63,7 +63,7 @@ public class IFileDocument extends PropertyDocument {
if (charContents != null) return charContents;
IPath location = file.getLocation();
if (location == null) return CharOperation.NO_CHAR;
return charContents = org.eclipse.cdt.internal.core.search.Util.getFileCharContent(
return charContents = org.eclipse.cdt.internal.core.Util.getFileCharContent(
location.toFile(),
getEncoding());
}

View file

@ -10,7 +10,7 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.index.impl;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.CharOperation;
/**
* An indexBlock stores wordEntries.

View file

@ -14,7 +14,7 @@ import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.CharOperation;
/**
* An indexSummary is used when saving an index into a BlocksIndexOuput or

View file

@ -10,7 +10,7 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.index.impl;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.CharOperation;
public class WordEntry {
protected char[] fWord;

View file

@ -10,7 +10,7 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.index.impl;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.CharOperation;
public final class WordEntryHashedArray {

View file

@ -13,4 +13,17 @@
engine.searching = Searching...
exception.wrongFormat = Wrong format
process.name = CDT Indexer
manager.filesToIndex = {0} files to index
manager.filesToIndex = {0} files to index
convention.illegalIdentifier= Illegal Identifier
convention.scope.lowercaseName= Scope starts with lower case
convention.scope.nullName= Scope name is null
convention.scope.emptyName= Scope name is empty
convention.scope.dotName= Scope name starts or ends with a .
convention.scope.nameWithBlanks= Scop name has blanks
convention.class.nullName= Class name is null
convention.class.nameWithBlanks= Class name has blanks
convention.class.dollarName= Class name has $
convention.class.lowercaseName= Class name starts with lower case
convention.class.invalidName= Class name is invalid

View file

@ -26,10 +26,10 @@ import org.eclipse.cdt.core.parser.ast.IASTNamespaceDefinition;
import org.eclipse.cdt.core.parser.ast.IASTTypedefDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTVariable;
import org.eclipse.cdt.core.search.ICSearchConstants;
import org.eclipse.cdt.internal.core.CharOperation;
import org.eclipse.cdt.internal.core.index.IDocument;
import org.eclipse.cdt.internal.core.index.IIndexer;
import org.eclipse.cdt.internal.core.index.IIndexerOutput;
import org.eclipse.cdt.internal.core.search.CharOperation;
public abstract class AbstractIndexer implements IIndexer, IIndexConstants, ICSearchConstants {

View file

@ -34,7 +34,7 @@ public class AddCompilationUnitToIndex extends AddFileToIndex {
try {
IPath location = resource.getLocation();
if (location != null)
this.contents = org.eclipse.cdt.internal.core.search.Util.getFileCharContent(location.toFile(), null);
this.contents = org.eclipse.cdt.internal.core.Util.getFileCharContent(location.toFile(), null);
} catch (IOException e) {
}
}

View file

@ -11,8 +11,8 @@
package org.eclipse.cdt.internal.core.search.indexing;
import org.eclipse.cdt.internal.core.Util;
import org.eclipse.cdt.internal.core.index.IIndex;
import org.eclipse.cdt.internal.core.search.Util;
import org.eclipse.cdt.internal.core.search.processing.JobManager;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;

View file

@ -24,7 +24,7 @@ import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.core.search.Util;
import org.eclipse.cdt.internal.core.Util;
import org.eclipse.cdt.internal.core.index.IIndex;
import org.eclipse.cdt.internal.core.index.IQueryResult;
import org.eclipse.cdt.internal.core.index.impl.IFileDocument;

View file

@ -21,6 +21,16 @@ import java.util.Iterator;
import java.util.Map;
import java.util.zip.CRC32;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.internal.core.CharOperation;
import org.eclipse.cdt.internal.core.index.IIndex;
import org.eclipse.cdt.internal.core.index.impl.Index;
import org.eclipse.cdt.internal.core.model.CProject;
import org.eclipse.cdt.internal.core.search.CWorkspaceScope;
import org.eclipse.cdt.internal.core.search.IndexSelector;
import org.eclipse.cdt.internal.core.search.SimpleLookupTable;
import org.eclipse.cdt.internal.core.search.processing.IJob;
import org.eclipse.cdt.internal.core.search.processing.JobManager;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
@ -28,16 +38,7 @@ import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.cdt.internal.core.search.processing.JobManager;
import org.eclipse.cdt.internal.core.search.processing.IJob;
import org.eclipse.cdt.internal.core.search.CWorkspaceScope;
import org.eclipse.cdt.internal.core.search.IndexSelector;
import org.eclipse.cdt.internal.core.search.SimpleLookupTable;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.index.IIndex;
import org.eclipse.cdt.internal.core.index.impl.Index;
import org.eclipse.cdt.internal.core.model.CProject;
import org.eclipse.cdt.core.CCorePlugin;
public class IndexManager extends JobManager implements IIndexConstants {
/* number of file contents in memory */
@ -303,11 +304,11 @@ public class IndexManager extends JobManager implements IIndexConstants {
* Name of the background process
*/
public String processName(){
return org.eclipse.cdt.internal.core.search.Util.bind("process.name"); //$NON-NLS-1$
return org.eclipse.cdt.internal.core.Util.bind("process.name"); //$NON-NLS-1$
}
private void rebuildIndex(String indexName, IPath path) {
Object target = org.eclipse.cdt.internal.core.search.Util.getTarget(ResourcesPlugin.getWorkspace().getRoot(), path, true);
Object target = org.eclipse.cdt.internal.core.Util.getTarget(ResourcesPlugin.getWorkspace().getRoot(), path, true);
if (target == null) return;
if (VERBOSE)
@ -546,7 +547,7 @@ public class IndexManager extends JobManager implements IIndexConstants {
private char[] readIndexState() {
try {
return org.eclipse.cdt.internal.core.search.Util.getFileCharContent(savedIndexNamesFile, null);
return org.eclipse.cdt.internal.core.Util.getFileCharContent(savedIndexNamesFile, null);
} catch (IOException ignored) {
if (VERBOSE)
JobManager.verbose("Failed to read saved index file names"); //$NON-NLS-1$

View file

@ -13,9 +13,9 @@ package org.eclipse.cdt.internal.core.search.indexing;
import java.io.IOException;
import org.eclipse.cdt.internal.core.Util;
import org.eclipse.cdt.internal.core.index.IIndex;
import org.eclipse.cdt.internal.core.index.IQueryResult;
import org.eclipse.cdt.internal.core.search.Util;
import org.eclipse.cdt.internal.core.search.processing.JobManager;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;

View file

@ -200,10 +200,14 @@ public interface ICElement extends IAdaptable {
static final int CPP_PUBLIC = 0x2000;
/**
* Modifier indicating a protected class
*/
static final int CPP_PROTECTED = 0x4000;
/**
* Modifier indicating a friend class
*/
static final int CPP_FRIEND = 0x4000;
static final int CPP_FRIEND = 0x8000;
/**
* Returns whether this C element exists in the model.

View file

@ -4,6 +4,8 @@ package org.eclipse.cdt.core.model;
* (c) Copyright IBM Corp. 2000, 2001.
* All Rights Reserved.
*/
import java.util.Map;
import org.eclipse.cdt.internal.core.model.IBufferFactory;
import org.eclipse.cdt.internal.core.model.IWorkingCopy;
import org.eclipse.core.runtime.IProgressMonitor;
@ -204,4 +206,9 @@ public interface ITranslationUnit extends ICElement, IParent, IOpenable, ISource
* @return boolean
*/
boolean isWorkingCopy();
/**
* parse()
* returns a map of all new elements and their element info
*/
Map parse();
}

View file

@ -73,7 +73,9 @@ public class CModelBuilder {
{
ParserMode mode = quick ? ParserMode.QUICK_PARSE : ParserMode.COMPLETE_PARSE;
quickParseCallback = ParserFactory.createQuickParseCallback();
IParser parser = ParserFactory.createParser( ParserFactory.createScanner( new StringReader( code ), "code", new ScannerInfo(), mode, quickParseCallback), quickParseCallback, mode );
IParser parser = ParserFactory.createParser(
ParserFactory.createScanner( new StringReader( code ), "code",
new ScannerInfo(), mode, quickParseCallback), quickParseCallback, mode );
parser.setCppNature(hasCppNature);
if( ! parser.parse() && throwExceptionOnError )
throw new ParserException("Parse failure");
@ -459,8 +461,7 @@ public class CModelBuilder {
}
element.setTypeName ( getType(varDeclaration.getAbstractDeclaration()) );
element.setConst(varDeclaration.getAbstractDeclaration().isConst());
// TODO : fix volatile for variables
// element.setVolatile(varDeclaration.isVolatile());
element.setVolatile(varDeclaration.getAbstractDeclaration().isVolatile());
element.setStatic(varDeclaration.isStatic());
// add to parent
parent.addChild( element );
@ -519,6 +520,8 @@ public class CModelBuilder {
}
}
element.setVolatile(methodDeclaration.isVolatile());
element.setConst(methodDeclaration.isConst());
}
else // instance of IASTFunction
{
@ -547,10 +550,7 @@ public class CModelBuilder {
}
element.setParameterTypes(parameterTypes);
element.setReturnType( getType(functionDeclaration.getReturnType()) );
// TODO: Fix volatile and const
//element.setVolatile(functionDeclaration.isVolatile());
element.setStatic(functionDeclaration.isStatic());
//element.setConst(functionDeclaration.isConst());
// add to parent
parent.addChild( element );

View file

@ -44,7 +44,7 @@ public class CModelStatus extends Status implements ICModelStatus, ICModelStatus
/**
* Singleton OK object
*/
public static final ICModelStatus VERIFIED_OK = new CModelStatus(OK);
public static final ICModelStatus VERIFIED_OK = new CModelStatus(OK, OK, org.eclipse.cdt.internal.core.Util.bind("status.OK"));;
/**
* Constructs an C model status with no corresponding elements.
@ -59,7 +59,7 @@ public class CModelStatus extends Status implements ICModelStatus, ICModelStatus
*/
public CModelStatus(int code) {
super(ERROR, CCorePlugin.PLUGIN_ID, code, "CModelStatus", null); //$NON-NLS-1$
//fElements= CElementInfo.fgEmptyChildren;
fElements= CElementInfo.fgEmptyChildren;
}
/**
@ -76,18 +76,22 @@ public class CModelStatus extends Status implements ICModelStatus, ICModelStatus
* Constructs an C model status with no corresponding elements.
*/
public CModelStatus(int code, String string) {
super(ERROR, CCorePlugin.PLUGIN_ID, code, "CModelStatus", null); //$NON-NLS-1$
//fElements= CElementInfo.fgEmptyChildren;
this(ERROR, code, string);
}
public CModelStatus(int severity, int code, String string) {
super(severity, CCorePlugin.PLUGIN_ID, code, "CModelStatus", null); //$NON-NLS-1$
fElements= CElementInfo.fgEmptyChildren;
fPath= null;
fString = string;
}
}
/**
* Constructs an C model status with no corresponding elements.
*/
public CModelStatus(int code, Throwable throwable) {
super(ERROR, CCorePlugin.PLUGIN_ID, code, "CModelStatus", throwable); //$NON-NLS-1$
//fElements= CElementInfo.fgEmptyChildren;
fElements= CElementInfo.fgEmptyChildren;
}
/**
@ -95,7 +99,7 @@ public class CModelStatus extends Status implements ICModelStatus, ICModelStatus
*/
public CModelStatus(int code, IPath path) {
super(ERROR, CCorePlugin.PLUGIN_ID, code, "CModelStatus", null); //$NON-NLS-1$
//fElements= CElementInfo.fgEmptyChildren;
fElements= CElementInfo.fgEmptyChildren;
fPath= path;
}
@ -116,6 +120,11 @@ public class CModelStatus extends Status implements ICModelStatus, ICModelStatus
fString= string;
}
public CModelStatus(int code, ICElement element, IPath path) {
this(code, new ICElement[]{element});
fPath = path;
}
/**
* Constructs an C model status with no corresponding elements.
*/

View file

@ -321,7 +321,7 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
TranslationUnitInfo unitInfo = (TranslationUnitInfo) info;
// generate structure
Map mapping = this.parse(false); // false since this is for working copies
Map mapping = this.parse();
// this is temporary until the New Model Builder is implemented
if(mapping == null) {
@ -481,7 +481,7 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
/**
* Parse the buffer contents of this element.
*/
public Map parse(boolean requireLineNumbers){
public Map parse(){
try{
String buf =this.getBuffer().getContents();
if (buf != null) {

View file

@ -139,4 +139,74 @@ public class BasicSearchMatch implements IMatch {
public boolean isVolatile() {
return isVolatile;
}
/**
* @return
*/
public int getType() {
return type;
}
/**
* @param i
*/
public void setEndOffset(int i) {
endOffset = i;
}
/**
* @param b
*/
public void setConst(boolean b) {
isConst = b;
}
/**
* @param b
*/
public void setStatic(boolean b) {
isStatic = b;
}
/**
* @param b
*/
public void setVolatile(boolean b) {
isVolatile = b;
}
/**
* @param string
*/
public void setName(String string) {
name = string;
}
/**
* @param string
*/
public void setParentName(String string) {
parentName = string;
}
/**
* @param i
*/
public void setStartOffset(int i) {
startOffset = i;
}
/**
* @param i
*/
public void setType(int i) {
type = i;
}
/**
* @param i
*/
public void setVisibility(int i) {
visibility = i;
}
}

View file

@ -28,6 +28,7 @@ import org.eclipse.cdt.core.parser.ast.IASTEnumerationSpecifier;
import org.eclipse.cdt.core.parser.ast.IASTEnumerator;
import org.eclipse.cdt.core.parser.ast.IASTField;
import org.eclipse.cdt.core.parser.ast.IASTFunction;
import org.eclipse.cdt.core.parser.ast.IASTMacro;
import org.eclipse.cdt.core.parser.ast.IASTMethod;
import org.eclipse.cdt.core.parser.ast.IASTNamespaceDefinition;
import org.eclipse.cdt.core.parser.ast.IASTOffsetableElement;
@ -200,6 +201,8 @@ public class BasicSearchResultCollector implements ICSearchResultCollector {
match.type = ICElement.C_NAMESPACE;
} else if ( node instanceof IASTEnumerationSpecifier ){
match.type = ICElement.C_ENUMERATION;
} else if ( node instanceof IASTMacro ){
match.type = ICElement.C_MACRO;
} else if ( node instanceof IASTField ){
match.type = ICElement.C_FIELD;
IASTField field = (IASTField)node;

View file

@ -17,13 +17,13 @@ import java.util.HashSet;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.core.Util;
import org.eclipse.cdt.internal.core.model.CModelManager;
import org.eclipse.cdt.internal.core.model.IWorkingCopy;
import org.eclipse.cdt.internal.core.search.CSearchScope;
import org.eclipse.cdt.internal.core.search.CWorkspaceScope;
import org.eclipse.cdt.internal.core.search.PathCollector;
import org.eclipse.cdt.internal.core.search.PatternSearchJob;
import org.eclipse.cdt.internal.core.search.Util;
import org.eclipse.cdt.internal.core.search.indexing.IndexManager;
import org.eclipse.cdt.internal.core.search.matching.CSearchPattern;
import org.eclipse.cdt.internal.core.search.matching.MatchLocator;

View file

@ -27,12 +27,12 @@ import org.eclipse.cdt.core.parser.ast.ASTClassKind;
import org.eclipse.cdt.core.search.ICSearchConstants;
import org.eclipse.cdt.core.search.ICSearchPattern;
import org.eclipse.cdt.core.search.ICSearchScope;
import org.eclipse.cdt.internal.core.CharOperation;
import org.eclipse.cdt.internal.core.index.IEntryResult;
import org.eclipse.cdt.internal.core.index.IIndex;
import org.eclipse.cdt.internal.core.index.impl.BlocksIndexInput;
import org.eclipse.cdt.internal.core.index.impl.IndexInput;
import org.eclipse.cdt.internal.core.parser.ScannerInfo;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.search.IIndexSearchRequestor;
import org.eclipse.cdt.internal.core.search.indexing.IIndexConstants;
import org.eclipse.core.runtime.IProgressMonitor;

View file

@ -22,10 +22,10 @@ import org.eclipse.cdt.core.parser.ast.IASTEnumerationSpecifier;
import org.eclipse.cdt.core.parser.ast.IASTOffsetableNamedElement;
import org.eclipse.cdt.core.parser.ast.IASTQualifiedNameElement;
import org.eclipse.cdt.core.search.ICSearchScope;
import org.eclipse.cdt.internal.core.CharOperation;
import org.eclipse.cdt.internal.core.index.IEntryResult;
import org.eclipse.cdt.internal.core.index.impl.IndexInput;
import org.eclipse.cdt.internal.core.index.impl.IndexedFile;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.search.IIndexSearchRequestor;
import org.eclipse.cdt.internal.core.search.indexing.AbstractIndexer;

View file

@ -21,10 +21,10 @@ import org.eclipse.cdt.core.parser.ast.IASTOffsetableNamedElement;
import org.eclipse.cdt.core.parser.ast.IASTQualifiedNameElement;
import org.eclipse.cdt.core.parser.ast.IASTVariable;
import org.eclipse.cdt.core.search.ICSearchScope;
import org.eclipse.cdt.internal.core.CharOperation;
import org.eclipse.cdt.internal.core.index.IEntryResult;
import org.eclipse.cdt.internal.core.index.impl.IndexInput;
import org.eclipse.cdt.internal.core.index.impl.IndexedFile;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.search.IIndexSearchRequestor;
import org.eclipse.cdt.internal.core.search.indexing.AbstractIndexer;

View file

@ -19,10 +19,10 @@ import org.eclipse.cdt.core.parser.ISourceElementCallbackDelegate;
import org.eclipse.cdt.core.parser.ast.IASTMacro;
import org.eclipse.cdt.core.parser.ast.IASTOffsetableNamedElement;
import org.eclipse.cdt.core.search.ICSearchScope;
import org.eclipse.cdt.internal.core.CharOperation;
import org.eclipse.cdt.internal.core.index.IEntryResult;
import org.eclipse.cdt.internal.core.index.impl.IndexInput;
import org.eclipse.cdt.internal.core.index.impl.IndexedFile;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.search.IIndexSearchRequestor;
import org.eclipse.cdt.internal.core.search.indexing.AbstractIndexer;

View file

@ -25,10 +25,10 @@ import org.eclipse.cdt.core.parser.ast.IASTQualifiedNameElement;
import org.eclipse.cdt.core.parser.ast.IASTSimpleTypeSpecifier;
import org.eclipse.cdt.core.parser.ast.IASTTypeSpecifier;
import org.eclipse.cdt.core.search.ICSearchScope;
import org.eclipse.cdt.internal.core.CharOperation;
import org.eclipse.cdt.internal.core.index.IEntryResult;
import org.eclipse.cdt.internal.core.index.impl.IndexInput;
import org.eclipse.cdt.internal.core.index.impl.IndexedFile;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.search.IIndexSearchRequestor;
import org.eclipse.cdt.internal.core.search.indexing.AbstractIndexer;

View file

@ -18,10 +18,10 @@ import java.io.IOException;
import org.eclipse.cdt.core.parser.ISourceElementCallbackDelegate;
import org.eclipse.cdt.core.parser.ast.IASTNamespaceDefinition;
import org.eclipse.cdt.core.search.ICSearchScope;
import org.eclipse.cdt.internal.core.CharOperation;
import org.eclipse.cdt.internal.core.index.IEntryResult;
import org.eclipse.cdt.internal.core.index.impl.IndexInput;
import org.eclipse.cdt.internal.core.index.impl.IndexedFile;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.search.IIndexSearchRequestor;
import org.eclipse.cdt.internal.core.search.indexing.AbstractIndexer;

View file

@ -13,7 +13,7 @@
*/
package org.eclipse.cdt.internal.core.search.processing;
import org.eclipse.cdt.internal.core.search.Util;
import org.eclipse.cdt.internal.core.Util;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.SubProgressMonitor;

View file

@ -0,0 +1,137 @@
/*******************************************************************************
* Copyright (c) 2001 Rational Software Corp. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* Rational Software - initial implementation
******************************************************************************/
package org.eclipse.cdt.core;
import java.util.StringTokenizer;
import org.eclipse.cdt.internal.core.CharOperation;
import org.eclipse.cdt.internal.core.Util;
import org.eclipse.cdt.internal.core.model.CModelStatus;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
/**
* @author hamer
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class CConventions {
private final static String scopeResolutionOperator= "::";
private final static char fgDot= '.';
private final static char fgColon= ':';
/**
* Validate the given CPP class name, either simple or qualified.
* For example, <code>"A::B::C"</code>, or <code>"C"</code>.
* <p>
*
* @param name the name of a class
* @return a status object with code <code>IStatus.OK</code> if
* the given name is valid as a CPP class name,
* a status with code <code>IStatus.WARNING</code>
* indicating why the given name is discouraged,
* otherwise a status object indicating what is wrong with
* the name
*/
public static IStatus validateClassName(String name) {
if (name == null) {
return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.class.nullName"), null); //$NON-NLS-1$
}
String trimmed = name.trim();
if ((!name.equals(trimmed)) || (name.indexOf(" ") != -1) ){
return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.class.nameWithBlanks"), null); //$NON-NLS-1$
}
int index = name.lastIndexOf(scopeResolutionOperator);
char[] scannedID;
if (index == -1) {
// simple name
scannedID = name.toCharArray();
} else {
// qualified name
String pkg = name.substring(0, index).trim();
IStatus status = validateScopeName(pkg);
if (!status.isOK()) {
return status;
}
String type = name.substring(index + 1).trim();
scannedID = type.toCharArray();
}
if (scannedID != null) {
IStatus status = ResourcesPlugin.getWorkspace().validateName(new String(scannedID), IResource.FILE);
if (!status.isOK()) {
return status;
}
if (CharOperation.contains('$', scannedID)) {
return new Status(IStatus.WARNING, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.class.dollarName"), null); //$NON-NLS-1$
}
if ((scannedID.length > 0 && Character.isLowerCase(scannedID[0]))) {
return new Status(IStatus.WARNING, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.class.lowercaseName"), null); //$NON-NLS-1$
}
return CModelStatus.VERIFIED_OK;
} else {
return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.class.invalidName", name), null); //$NON-NLS-1$
}
}
/**
* Validate the given scope name.
* <p>
* @return a status object with code <code>IStatus.OK</code> if
* the given name is valid as a class name, otherwise a status
* object indicating what is wrong with the name
*/
public static IStatus validateScopeName(String name) {
if (name == null) {
return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.scope.nullName"), null); //$NON-NLS-1$
}
int length;
if ((length = name.length()) == 0) {
return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.scope.emptyName"), null); //$NON-NLS-1$
}
if (name.charAt(0) == fgDot || name.charAt(length-1) == fgDot) {
return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.scope.dotName"), null); //$NON-NLS-1$
}
if (CharOperation.isWhitespace(name.charAt(0)) || CharOperation.isWhitespace(name.charAt(name.length() - 1))) {
return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.scope.nameWithBlanks"), null); //$NON-NLS-1$
}
// int dot = 0;
// while (dot != -1 && dot < length-1) {
// if ((dot = name.indexOf(fgDot, dot+1)) != -1 && dot < length-1 && name.charAt(dot+1) == fgDot) {
// return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.package.consecutiveDotsName"), null); //$NON-NLS-1$
// }
// }
IWorkspace workspace = ResourcesPlugin.getWorkspace();
StringTokenizer st = new StringTokenizer(name, scopeResolutionOperator);
boolean firstToken = true;
while (st.hasMoreTokens()) {
String typeName = st.nextToken();
typeName = typeName.trim(); // grammar allows spaces
char[] scannedID = typeName.toCharArray();
if (scannedID == null) {
return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.illegalIdentifier", typeName), null); //$NON-NLS-1$
}
IStatus status = workspace.validateName(new String(scannedID), IResource.FOLDER);
if (!status.isOK()) {
return status;
}
if (firstToken && scannedID.length > 0 && Character.isLowerCase(scannedID[0])) {
return new Status(IStatus.WARNING, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.scope.lowercaseName"), null); //$NON-NLS-1$
}
firstToken = false;
}
return CModelStatus.VERIFIED_OK;
}
}

View file

@ -1,3 +1,7 @@
2003-08-12
Added class name validation to NewClassWizardPage
Used the new search (indexer) for Code completion in CCompletionProcessor
2003-08-11 Andrew Niefer
- Added some code to CUIPlugin to access working copies

View file

@ -7,24 +7,42 @@ package org.eclipse.cdt.internal.ui.text;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.eclipse.cdt.core.index.ITagEntry;
import org.eclipse.cdt.core.index.IndexModel;
import org.eclipse.cdt.core.index.TagFlags;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.IField;
import org.eclipse.cdt.core.model.IFunction;
import org.eclipse.cdt.core.model.IFunctionDeclaration;
import org.eclipse.cdt.core.model.IMember;
import org.eclipse.cdt.core.model.IMethod;
import org.eclipse.cdt.core.model.IMethodDeclaration;
import org.eclipse.cdt.core.model.IStructure;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.parser.ast.ASTAccessVisibility;
import org.eclipse.cdt.core.search.BasicSearchMatch;
import org.eclipse.cdt.core.search.BasicSearchResultCollector;
import org.eclipse.cdt.core.search.ICSearchConstants;
import org.eclipse.cdt.core.search.ICSearchScope;
import org.eclipse.cdt.core.search.SearchEngine;
import org.eclipse.cdt.internal.core.model.CElement;
import org.eclipse.cdt.internal.core.search.matching.OrPattern;
import org.eclipse.cdt.internal.corext.template.ContextType;
import org.eclipse.cdt.internal.corext.template.ContextTypeRegistry;
import org.eclipse.cdt.internal.ui.CCompletionContributorManager;
import org.eclipse.cdt.internal.ui.CPluginImages;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.text.template.TemplateEngine;
import org.eclipse.cdt.ui.CElementLabelProvider;
import org.eclipse.cdt.ui.CSearchResultLabelProvider;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.FunctionPrototypeSummary;
import org.eclipse.cdt.ui.IFunctionSummary;
import org.eclipse.cdt.ui.IWorkingCopyManager;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
@ -36,9 +54,7 @@ import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
/**
* C completion processor.
@ -55,11 +71,17 @@ public class CCompletionProcessor implements IContentAssistProcessor {
private boolean fRestrictToMatchingCase;
private boolean fAllowAddIncludes;
private CElementLabelProvider fElementLabelProvider;
//private ImageRegistry fImageRegistry;
BasicSearchResultCollector resultCollector = null;
SearchEngine searchEngine = null;
CSearchResultLabelProvider labelProvider = null;
public CCompletionProcessor(IEditorPart editor) {
fEditor = (CEditor) editor;
// Needed for search
labelProvider = new CSearchResultLabelProvider();
resultCollector = new BasicSearchResultCollector ();
searchEngine = new SearchEngine();
//Determine if this is a C or a C++ file for the context completion + //This is _totally_ ugly and likely belongs in the main editor class.
String contextNames[] = new String[2];
@ -102,9 +124,6 @@ public class CCompletionProcessor implements IContentAssistProcessor {
fAllowAddIncludes = true;
fComparator = new CCompletionProposalComparator();
fElementLabelProvider = new CElementLabelProvider();
//fImageRegistry= CUIPlugin.getDefault().getImageRegistry();
}
/**
@ -188,7 +207,9 @@ public class CCompletionProcessor implements IContentAssistProcessor {
* @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
*/
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
//IDocument unit= fManager.getWorkingCopy(fEditor.getEditorInput());
IWorkingCopyManager fManager = CUIPlugin.getDefault().getWorkingCopyManager();
ITranslationUnit unit = fManager.getWorkingCopy(fEditor.getEditorInput());
IDocument document = viewer.getDocument();
ICCompletionProposal[] results = null;
@ -205,9 +226,7 @@ public class CCompletionProcessor implements IContentAssistProcessor {
length = selection.y;
}
//CCompletionEvaluator evaluator= new CCompletionEvaluator(document, offset, length);
//evaluator.restrictProposalsToMatchingCases(fRestrictToMatchingCase);
results = evalProposals(document, offset, length);
results = evalProposals(document, offset, length, unit);
}
} catch (Exception e) {
CUIPlugin.getDefault().log(e);
@ -248,22 +267,67 @@ public class CCompletionProcessor implements IContentAssistProcessor {
return results;
}
private ICElement getCurrentScope(ITranslationUnit unit, int documentOffset){
// quick parse the unit
Map elements = unit.parse();
// figure out what element is the enclosing the current offset
ICElement currentScope = unit;
Iterator i = elements.keySet().iterator();
while (i.hasNext()){
CElement element = (CElement) i.next();
if ((element.getStartPos() < documentOffset )
&& ( element.getStartPos() + element.getLength() > documentOffset)
)
{
if(currentScope instanceof ITranslationUnit){
currentScope = element;
}else
if (currentScope instanceof CElement){
CElement currentScopeElement = (CElement) currentScope;
if(
(currentScopeElement.getStartPos() < element.getStartPos())
&& (
(currentScopeElement.getStartPos() + currentScopeElement.getLength() )
> (element.getStartPos() + element.getLength()) )
)
currentScope = element;
}
}
}
return currentScope;
}
/**
* Order the given proposals.
*/
private ICCompletionProposal[] order(ICCompletionProposal[] proposals) {
Arrays.sort(proposals, fComparator);
if(proposals != null)
Arrays.sort(proposals, fComparator);
return proposals;
}
/**
* Evaluate the actual proposals for C
*/
private ICCompletionProposal[] evalProposals(IDocument document, int pos, int length) {
public ICCompletionProposal[] evalProposals(IDocument document, int pos, int length, ITranslationUnit unit) {
return order (evalProposals(document, pos, length, getCurrentScope (unit, pos)));
}
private ICCompletionProposal[] evalProposals(IDocument document, int pos, int length, ICElement currentScope) {
boolean isDereference = false;
IRegion region;
String frag = "";
// TODO: Do all possible scopes
// possible scopes include IStructure, INamespace, and ITranslationUnit
if( ( !(currentScope instanceof IMethod))
&& ( !(currentScope instanceof IMethodDeclaration))
&& ( !(currentScope instanceof IFunction))
&& ( !(currentScope instanceof IFunctionDeclaration))
){
return null;
}
// Move back the pos by one the position is 0-based
if (pos > 0) {
pos--;
@ -342,7 +406,7 @@ public class CCompletionProcessor implements IContentAssistProcessor {
ArrayList completions = new ArrayList();
// Look in index manager
addProposalsFromModel(region, frag, completions);
addProposalsFromModel(region, frag,currentScope, completions);
// Loot in the contributed completions
addProposalsFromCompletionContributors(region, frag, completions);
@ -384,7 +448,8 @@ public class CCompletionProcessor implements IContentAssistProcessor {
}
}
private void addProposalsFromModel(IRegion region, String frag, ArrayList completions) {
// It is not needed to follow referenced projects since search does this for us now
/* private void addProposalsFromModel(IRegion region, String frag, ICElement currentScope, ArrayList completions) {
IProject project = null;
IEditorInput input = fEditor.getEditorInput();
if (input instanceof IFileEditorInput) {
@ -396,22 +461,182 @@ public class CCompletionProcessor implements IContentAssistProcessor {
}
}
if (project != null) {
addProjectCompletions(project, region, frag, completions);
addProjectCompletions(project, region, frag, currentScope, completions);
// Now query referenced projects
IProject referenced[];
try {
referenced = project.getReferencedProjects();
if (referenced.length > 0) {
for (int i = 0; i < referenced.length; i++) {
addProjectCompletions(referenced[i], region, frag, completions);
addProjectCompletions(referenced[i], region, frag, currentScope, completions);
}
}
} catch (CoreException e) {
}
}
}
*/
private FunctionPrototypeSummary getPrototype (BasicSearchMatch match) {
switch(match.getElementType()){
case ICElement.C_FUNCTION:
case ICElement.C_FUNCTION_DECLARATION:
case ICElement.C_METHOD:
case ICElement.C_METHOD_DECLARATION:
{
return (new FunctionPrototypeSummary ( match.getName() ));
}
default:
return null;
}
}
private int calculateRelevance (BasicSearchMatch element){
int type = element.getElementType();
switch (type){
case ICElement.C_FIELD:
return 9;
case ICElement.C_VARIABLE:
case ICElement.C_VARIABLE_DECLARATION:
return 8;
case ICElement.C_METHOD:
case ICElement.C_METHOD_DECLARATION:
return 7;
case ICElement.C_FUNCTION:
case ICElement.C_FUNCTION_DECLARATION:
return 6;
case ICElement.C_CLASS:
return 5;
case ICElement.C_STRUCT:
return 4;
case ICElement.C_UNION:
return 3;
case ICElement.C_MACRO:
return 2;
case ICElement.C_ENUMERATION:
return 1;
default :
return 0;
}
}
private void addProposalsFromModel (IRegion region, String frag, ICElement currentScope, ArrayList completions) {
List elementsFound = new LinkedList();
String prefix = frag + "*";
// TODO: change that to resource scope later
ICElement[] projectScopeElement = new ICElement[1];
projectScopeElement[0] = (ICElement)currentScope.getCProject();
ICSearchScope scope = SearchEngine.createCSearchScope(projectScopeElement, true);
OrPattern orPattern = new OrPattern();
// search for global variables, functions, classes, structs, unions, enums and macros
orPattern.addPattern(SearchEngine.createSearchPattern( prefix, ICSearchConstants.VAR, ICSearchConstants.DECLARATIONS, true ));
orPattern.addPattern(SearchEngine.createSearchPattern( prefix, ICSearchConstants.FUNCTION, ICSearchConstants.DECLARATIONS, true ));
orPattern.addPattern(SearchEngine.createSearchPattern( prefix, ICSearchConstants.FUNCTION, ICSearchConstants.DEFINITIONS, true ));
orPattern.addPattern(SearchEngine.createSearchPattern( prefix, ICSearchConstants.TYPE, ICSearchConstants.DECLARATIONS, true ));
orPattern.addPattern(SearchEngine.createSearchPattern( prefix, ICSearchConstants.ENUM, ICSearchConstants.DECLARATIONS, true ));
orPattern.addPattern(SearchEngine.createSearchPattern( prefix, ICSearchConstants.MACRO, ICSearchConstants.DECLARATIONS, true ));
searchEngine.search(CUIPlugin.getWorkspace(), orPattern, scope, resultCollector);
elementsFound.addAll(resultCollector.getSearchResults());
private void addProjectCompletions(IProject project, IRegion region, String frag, ArrayList completions) {
if((currentScope instanceof IMethod) || (currentScope instanceof IMethodDeclaration) ){
// add the methods and fields of the parent class
/* // use search with CElement Scope
ICElement[] classScopeElements = new ICElement[1];
classScopeElements[0] = currentScope.getParent();
ICSearchScope classScope = SearchEngine.createCSearchScope(classScopeElements);
OrPattern classOrPattern = new OrPattern();
classOrPattern.addPattern(SearchEngine.createSearchPattern(prefix, ICSearchConstants.FIELD, ICSearchConstants.DECLARATIONS, true));
classOrPattern.addPattern(SearchEngine.createSearchPattern(prefix, ICSearchConstants.METHOD, ICSearchConstants.DECLARATIONS, true));
classOrPattern.addPattern(SearchEngine.createSearchPattern(prefix, ICSearchConstants.METHOD, ICSearchConstants.DEFINITIONS, true));
searchEngine.search(CUIPlugin.getWorkspace(), classOrPattern, classScope, resultCollector);
elementsFound.addAll(resultCollector.getSearchResults());
*/
// Work around until CElement scope is implemented
IStructure parentClass = (IStructure) currentScope.getParent();
ArrayList children = new ArrayList();
children.addAll(parentClass.getChildrenOfType(ICElement.C_METHOD));
children.addAll(parentClass.getChildrenOfType(ICElement.C_METHOD_DECLARATION));
children.addAll(parentClass.getChildrenOfType(ICElement.C_FIELD));
Iterator c = children.iterator();
while (c.hasNext()){
IMember child = (IMember)c.next();
if (child.getElementName().startsWith(frag))
{
BasicSearchMatch childMatch = new BasicSearchMatch();
childMatch.setType(child.getElementType());
childMatch.setParentName(parentClass.getElementName());
if(child.getVisibility() == ASTAccessVisibility.PUBLIC )
childMatch.setVisibility(ICElement.CPP_PUBLIC);
else if(child.getVisibility() == ASTAccessVisibility.PROTECTED )
childMatch.setVisibility(ICElement.CPP_PROTECTED);
else if(child.getVisibility() == ASTAccessVisibility.PRIVATE )
childMatch.setVisibility(ICElement.CPP_PRIVATE);
childMatch.setConst(child.isConst());
childMatch.setVolatile(child.isVolatile());
childMatch.setStatic(child.isStatic());
if(child instanceof IMethodDeclaration){
childMatch.setName(((IMethodDeclaration)child).getSignature());
}
else {
childMatch.setName(child.getElementName());
}
elementsFound.add(childMatch);
}
}
}
Iterator i = elementsFound.iterator();
while (i.hasNext()){
CCompletionProposal proposal;
FunctionPrototypeSummary fproto = null;
String fname = "";
String displayString = "";
Image image = null;
StringBuffer infoString = new StringBuffer();
BasicSearchMatch match = (BasicSearchMatch)i.next();
fproto = getPrototype(match);
fname = (fproto == null) ? match.getName() : fproto.getName();
displayString = (fproto == null) ? fname : fproto.getPrototypeString(true);
image = labelProvider.getImage(match);
infoString.append(displayString);
if(match.getParentName().length() > 0) {
infoString.append(" - Parent: ");
infoString.append(match.getParentName());
}
proposal = new CCompletionProposal(
fname, // replacement string
region.getOffset(),
region.getLength(),
image,
displayString, // displayString
calculateRelevance(match)
);
completions.add(proposal);
// No summary information available yet
// context information is available for methods only
if(fproto != null){
String fargs = fproto.getArguments();
if(fargs != null && fargs.length() > 0) {
proposal.setContextInformation(new ContextInformation(fname, fargs));
}
}
// The info string could be populated with documentation info.
// For now, it has the name and the parent's name if available.
proposal.setAdditionalProposalInfo(infoString.toString());
}
}
// Search (and the new indexer) is used now instead of the old indexer and CTags
/* private void addProjectCompletions(IProject project, IRegion region, String frag, ArrayList completions) {
IndexModel model = IndexModel.getDefault();
ITagEntry[] tags = model.query(project, frag + "*", false, false);
@ -457,7 +682,7 @@ public class CCompletionProcessor implements IContentAssistProcessor {
}
}
}
*/
private Image getTagImage(int kind) {
switch (kind) {
case TagFlags.T_PROTOTYPE :

View file

@ -72,6 +72,7 @@ public class CSearchResultLabelProvider extends LabelProvider {
case ICElement.C_UNION: imageDescriptor = CPluginImages.DESC_OBJS_UNION; break;
case ICElement.C_NAMESPACE: imageDescriptor = CPluginImages.DESC_OBJS_CONTAINER; break;
case ICElement.C_ENUMERATION: imageDescriptor = CPluginImages.DESC_OBJS_ENUMERATION; break;
case ICElement.C_MACRO: imageDescriptor = CPluginImages.DESC_OBJS_MACRO; break;
case ICElement.C_FUNCTION: imageDescriptor = CPluginImages.DESC_OBJS_FUNCTION; break;
case ICElement.C_VARIABLE: imageDescriptor = CPluginImages.DESC_OBJS_FIELD; break;
case ICElement.C_ENUMERATOR: imageDescriptor = CPluginImages.DESC_OBJS_ENUMERATOR; break;

View file

@ -68,7 +68,7 @@ public class NewClassWizard extends BasicNewResourceWizard implements INewWizard
/**
* @see Wizard#performFinish
*/
public boolean performFinish() {
public boolean performFinish() {
IWorkspaceRunnable op= new IWorkspaceRunnable() {
public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
try {

View file

@ -15,10 +15,11 @@ import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.cdt.core.CConventions;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICElement;
@ -347,7 +348,7 @@ public class NewClassWizardPage extends WizardPage implements Listener {
if(fBaseClassDialogField.getText().length() > 0)
{
fAccessButtons.setEnabled(true);
fBaseClassStatus = baseClassChanged();
fBaseClassStatus = baseClassNameChanged();
}
else{
fAccessButtons.setEnabled(false);
@ -405,8 +406,9 @@ public class NewClassWizardPage extends WizardPage implements Listener {
private void searchForClasses(ICProject cProject, List elementsFound, IProgressMonitor monitor, int worked){
ICSearchPattern pattern = SearchEngine.createSearchPattern( "*", ICSearchConstants.CLASS, ICSearchConstants.DECLARATIONS, false );
// TODO: change that to project scope later
ICSearchScope scope = SearchEngine.createWorkspaceScope();;
ICElement[] elements = new ICElement[1];
elements[0] = cProject;
ICSearchScope scope = SearchEngine.createCSearchScope(elements, true);
searchEngine.search(CUIPlugin.getWorkspace(), pattern, scope, resultCollector);
elementsFound.addAll(resultCollector.getSearchResults());
@ -761,13 +763,13 @@ public class NewClassWizardPage extends WizardPage implements Listener {
{
extendingBase = true;
List classElements = findClassElementsInProject();
BasicSearchMatch baseClass = (BasicSearchMatch)findInList(baseClassName, classElements);
BasicSearchMatch baseClass = (BasicSearchMatch)findInList(baseClassName, null, classElements);
if(baseClass != null){
baseClassFileName = baseClass.getLocation().toString();
} else {
// if(baseClass != null){
// baseClassFileName = baseClass.getLocation().toString();
// } else {
baseClassFileName = baseClassName + HEADER_EXT;
}
// }
}
if(isIncludeGuard()){
@ -915,26 +917,30 @@ public class NewClassWizardPage extends WizardPage implements Listener {
*/
protected IStatus classNameChanged() {
StatusInfo status= new StatusInfo();
String typeName= getNewClassName();
// class name must not be empty
if (typeName.length() == 0) {
String className= getNewClassName();
// must not be empty
if (className.length() == 0) {
status.setError(NewWizardMessages.getString("NewClassWizardPage.error.EnterClassName")); //$NON-NLS-1$
return status;
}
// class name must not be qualified
if (typeName.indexOf('.') != -1) {
if (className.indexOf("::") != -1) {
status.setError(NewWizardMessages.getString("NewClassWizardPage.error.QualifiedName")); //$NON-NLS-1$
return status;
}
// class name must follow the C/CPP convensions
IStatus val= CConventions.validateClassName(className);
if (val.getSeverity() == IStatus.ERROR) {
status.setError(NewWizardMessages.getFormattedString("NewClassWizardPage.error.InvalidClassName", val.getMessage())); //$NON-NLS-1$
return status;
} else if (val.getSeverity() == IStatus.WARNING) {
status.setWarning(NewWizardMessages.getFormattedString("NewClassWizardPage.warning.ClassNameDiscouraged", val.getMessage())); //$NON-NLS-1$
// continue checking
}
// class must NOT exist
// ArrayList elementsFound = findClassElementsInProject();
// if(foundInList(getNewClassName(), elementsFound)){
// status.setWarning(NewWizardMessages.getString("NewClassWizardPage.error.ClassNameExists")); //$NON-NLS-1$
// }
// must not exist
List elementsFound = findClassElementsInProject();
if(foundInList(getNewClassName(), getContainerPath(linkedResourceGroupForHeader), elementsFound)){
status.setWarning(NewWizardMessages.getString("NewClassWizardPage.error.ClassNameExists")); //$NON-NLS-1$
}
return status;
}
/**
@ -946,32 +952,48 @@ public class NewClassWizardPage extends WizardPage implements Listener {
*
* @return the status of the validation
*/
protected IStatus baseClassChanged() {
protected IStatus baseClassNameChanged() {
String baseClassName = getBaseClassName();
StatusInfo status= new StatusInfo();
if (baseClassName.length() == 0) {
// accept the empty field (stands for java.lang.Object)
return status;
}
// class name must follow the C/CPP convensions
IStatus val= CConventions.validateClassName(baseClassName);
if (val.getSeverity() == IStatus.ERROR) {
status.setError(NewWizardMessages.getString("NewClassWizardPage.error.InvalidBaseClassName")); //$NON-NLS-1$
return status;
}
// if class does not exist, give warning
List elementsFound = findClassElementsInProject();
if(!foundInList(getBaseClassName(), elementsFound)){
if(!foundInList(baseClassName, null, elementsFound)){
status.setWarning(NewWizardMessages.getString("NewClassWizardPage.warning.BaseClassNotExists")); //$NON-NLS-1$
}
return status;
return status;
}
private Object findInList(String name, List elements){
private Object findInList(String name, IPath path, List elements){
Iterator i = elements.iterator();
while (i.hasNext()){
BasicSearchMatch element = (BasicSearchMatch)i.next();
if (name.equals(element.getName()))
return element;
if(path != null){
// check both the name and the path
if ((name.equals(element.getName())) && (path.makeAbsolute().equals(element.getLocation())))
return element;
} else {
// we don't care about the path
if (name.equals(element.getName()))
return element;
}
}
return null;
}
private boolean foundInList(String name, List elements){
if(findInList(name, elements) != null)
private boolean foundInList(String name, IPath path, List elements){
if(findInList(name, path, elements) != null)
return true;
else
return false;