protected static IASTNode findEnclosingNode(IFortranAST ast, ITextSelection selection) { Token firstToken = findFirstTokenAfter(ast, selection.getOffset()); Token lastToken = findLastTokenBefore( ast, OffsetLength.getPositionPastEnd(selection.getOffset(), selection.getLength())); if (firstToken == null || lastToken == null) return null; for (IASTNode parent = lastToken.getParent(); parent != null; parent = parent.getParent()) if (contains(parent, firstToken)) return parent; return null; }
private static boolean contains(IASTNode target, Token token) { for (IASTNode node = token.getParent(); node != null; node = node.getParent()) if (node == target) return true; return false; }