Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-dev] Problem when I want to find the source range of a function in .c file!

I am trying to open a .c file in CEditor and select the source code of a certain function. The code are as follows:

		        try{
		        	IFile file = fFile.getProject().getFile("main.c");//fFile list the files' names in the same CProject
		
		            Object element = CoreModel.getDefault().create(file);
		            if ( element instanceof ITranslationUnit ) {
                       try{
							ICElement element2 = ((ITranslationUnit)element).getElement("main");
							if (element2 instanceof IFunction || element2 instanceof IMethod){
									ISourceRange sourceRange = ((IDeclaration)element2).getSourceRange();
									if ( sourceRange != null ) {
										charStart = sourceRange.getStartPos();
										charEnd = charStart + sourceRange.getLength();
										if ( charEnd <= 0 ) {
											charStart = 0;
											charEnd = 0;
										}
									}
							}
						}
						catch( CModelException ce ) {ce.printStackTrace();}
			            (ITranslationUnit)element;
                        }
		        	
		        	IEditorPart iep = IDE.openEditor(getPage(), file, "org.eclipse.cdt.ui.editor.CEditor");

		        	if(iep instanceof CEditor){
		        		((CEditor)iep).selectAndReveal(charStart, charEnd);
					    }
		        	
		        	}
		        catch(PartInitException pie){
		        	pie.printStackTrace();
		        	}

but when I execute this code, I get:

Unhandled event loop exception
Reason:
org/eclipse/cdt/core/model/CoreModel

I wonder how to deal with this problem. Thank you!



Back to the top