Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [cdt-dev] How to open a file at a particular function

You should not follow the approch below, the problem with it is that you have to compute an AST for every single file in your project.
Rather than that you should access the index database via org.eclipse.cdt.core.index.IIndex. (see my previous email).
 
Markus.


From: cdt-dev-bounces@xxxxxxxxxxx [mailto:cdt-dev-bounces@xxxxxxxxxxx] On Behalf Of Mariot Chauvin
Sent: Mittwoch, 17. Oktober 2007 23:56
To: CDT General developers list.
Subject: Re: [cdt-dev] How to open a file at a particular function
Importance: Low

Hi Derek,

Below you will find some code to find a method name in project files. I am not a CDT expert, so maybe it's not the best way to do it.

Regards,

Mariot

*****************************************************

public class MyVisitor extends ASTVisitor {

    {
        shouldVisitDeclarations = true;
    }
   
    public static final String LINE_NUMBER = "org.yourcompany.line_number "; //$NON-NLS-1$
    public static final String FILE = "org.yourcompany.file"; //$NON-NLS-1$
    public static final String FUNCTION = "org.yourcompany.function"; //$NON-NLS-1$
   
   
    private ArrayList result = new ArrayList();
    private String input;

    public ArrayList getResult() {
        return result;
    }
   

    public setInput(String input) {
        this.input = input;
    }

   
    public int visit(IASTDeclaration decl) {
       
        try {
                       
            if (decl instanceof IASTFunctionDefinition) {
               
                String function_signature = decl.getRawSignature();

                //check if the input is in  the function_signature
                if (function_signature.indexOf(input) > 0) {
                    //save the needed informations
                    Map b = new HashMap ();
                    b.put(MyVisitor.LINE_NUMBER,String.valueOf(decl.getFileLocation().getStartingLineNumber()));
                    b.put(MyVisitor.FILE,decl.getContainingFilename ());
                    b.put(MyVisitor.FUNCTION,((IASTFunctionDefinition) decl).getDeclarator().getRawSignature());
                    this.result.add(b);
                }
            }
            return PROCESS_CONTINUE;
        } catch (Throwable e) {
            return PROCESS_ABORT;
        }
    };
   
}


and in an another class :

private void visitFolders(ICContainer folders[], ASTVisitor astVisitor) throws CoreException {

        for (int i=0; i<folders.length; i++) {

            ITranslationUnit files[] = folders[i].getTranslationUnits();

            for (int j=0;j<files.length;j++) {
                IASTTranslationUnit ast = files[j].getAST();
                if (ast !=null) ast.accept(astVisitor);
            }

            this.visitFolders(folders[i].getCContainers(), astVisitor);
        }
    }

....

MyVisitor myVisitor = new MyVisitor();
myVisitor.set("the method name you search");

this.visitFolders(project.getSourceRoots(), myVisitor);
// then you get the results
...



2007/10/17, subs < dmsubs@xxxxxxxxxxxxx>:
Hi,

Given a function or method name (as a String), how can I locate the file it is
defined in and open that file at the function definition? Any pointers to code,
snippets or docs would be very useful.

Thanks

--
Derek

--
Derek
_______________________________________________
cdt-dev mailing list
cdt-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/cdt-dev



--
Mariot Chauvin

Back to the top