|
Re: getLineNumber [message #1060084 is a reply to message #1059833] |
Thu, 23 May 2013 03:13 |
Toshihiro Izumi Messages: 360 Registered: July 2009 Location: Japan |
Senior Member |
|
|
As you can see org.eclipse.php.internal.core.ast.nodes.Program.getLineNumber(int), the method needs org.eclipse.php.internal.core.ast.nodes.Program.lineEndTable and it is set by org.eclipse.php.internal.core.ast.nodes.Program.setLineEndTable(int[]) which is called from org.eclipse.php.internal.ui.editor.validation.PhpReconcilingStrategy.reconcile(ISourceModule, boolean) and org.eclipse.php.internal.ui.viewsupport.SelectionListenerWithASTManager.PartListenerGroup.calculateASTandInform(ISourceModule, ITextSelection, IProgressMonitor).
In short, Program.getLineNumber() is available on the php editor and the AST within it. It may not be available in your visitor implementation.
>public boolean visit(Program node) {
node must be the AST in the editor.
For example,
PHPStructuredEditor phpStructuredEditor = // get the instance somehow
IModelElement element = phpStructuredEditor.getModelElement();
if (element instanceof ISourceModule) {
try {
program = SharedASTProvider.getAST((ISourceModule) element, SharedASTProvider.WAIT_ACTIVE_ONLY, null);
...
} catch (ModelException | IOException e) {
}
}
I don't recommend this way because it may return unexpected(incorrect) value in a certain case.
Other way is using org.eclipse.jface.text.Document.
For example,
public boolean visit(Variable node) {
// TODO Auto-generated method stub
System.out.println(node.toString());
Document document = new Document(node.getProgramRoot().getSourceModule().getSource());
System.out.println(document.getLineOfOffset(node.getStart()) + 1);
return true;
}
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03066 seconds