}
private FormulaFunctionElement getFunction(final int offset)
{
FormulaFunctionElement function = null;
final FastStack functionsStack = new FastStack();
final int count = rootElement.getElementCount();
boolean haveCloseParentheses = false;
for (int i = 0; i < count; i++)
{
final FormulaElement node = (FormulaElement) rootElement.getElement(i);
if ((node != null) && (node.getStartOffset() > offset))
{
if (function == null)
{
return null;
}
return function;
}
if (haveCloseParentheses)
{
if (functionsStack.isEmpty() == false)
{
functionsStack.pop();
}
if (functionsStack.isEmpty())
{
function = null;
}
else
{
function = (FormulaFunctionElement) functionsStack.peek();
}
haveCloseParentheses = false;
}
if (node instanceof FormulaFunctionElement)
{
function = (FormulaFunctionElement) node;
}
if (node instanceof FormulaOpenParenthesisElement)
{
functionsStack.push(function);
}
if (node instanceof FormulaClosingParenthesisElement)
{
haveCloseParentheses = true;
}
}
if (functionsStack.isEmpty() == false)
{
final FormulaElement lastElement = (count >= 1) ? (FormulaElement)rootElement.getElement(count - 1) : null;
if ((lastElement != null) && (lastElement.getEndOffset() >= offset))
{
return (FormulaFunctionElement)functionsStack.get(0);
}
else
{
return (FormulaFunctionElement)functionsStack.peek();
}
}
return function;
}