Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-ui-dev] How to parse the editor contents to get the required information using CDT?

>>>>Pls, Can anybody give any example How to parse the editor contents to
get the information like >>>>wherther the current selected text is
Declaration or Field or Class or Constructor etc. using CDT?

Try this piece of code. 
Here file.cc is the path to your source file (an object of IPath). 

This code will print all the includes, variables and function names in your source file. 
getElementType will return the type associated. Check org.eclipse.cdt.core.model.ICElement class for the values of constants. For eg...
type 74 is for C_FUNCTION
type 75 is for C_INCLUDE
type 76 is for C_VARIABLE and so on.....


ICElement cFile = CoreModel.getDefault().create(file.cc);
if(cFile != null)
{    
    ITranslationUnit trUnit = (ITranslationUnit) cFile;
    ICElement[] elts = trUnit.getChildren();
    if(elts != null)
     {    
         for(int i=0; i<elts.length; i++)
          {    
                System.out.println(elts[i].getElementName());
                 System.out.println(elts[i].getElementType());
           }    
      }        
}

If you want to find  the file from the selected text ....
Action class should implement IEditorActionDelegate. 
Use IEditorPart to get the selected Input
        IEditorInput currentInput = editorPart.getEditorInput();
        if (currentInput instanceof IFileEditorInput) {
            IFileEditorInput fileInput = (IFileEditorInput) currentInput;
            return fileInput.getFile();
        }
                
/Veenu





Back to the top