void calculateLineNumbers(SourceCodePositioner positioner) { int startOffset = node.getAbsolutePosition(); int endOffset = startOffset + node.getLength(); this.beginLine = positioner.lineNumberFromOffset(startOffset); this.beginColumn = positioner.columnFromOffset(this.beginLine, startOffset); this.endLine = positioner.lineNumberFromOffset(endOffset); // end column is inclusive this.endColumn = positioner.columnFromOffset(this.endLine, endOffset) - 1; if (this.endColumn < 0) { this.endColumn = 0; } }
private void handleParseProblems(EcmascriptNode<? extends AstNode> node) { if (node instanceof TrailingCommaNode) { TrailingCommaNode trailingCommaNode = (TrailingCommaNode) node; int nodeStart = node.getNode().getAbsolutePosition(); int nodeEnd = nodeStart + node.getNode().getLength() - 1; for (ParseProblem parseProblem : parseProblems) { // The node overlaps the comma (i.e. end of the problem)? int problemStart = parseProblem.getFileOffset(); int commaPosition = problemStart + parseProblem.getLength() - 1; if (nodeStart <= commaPosition && commaPosition <= nodeEnd) { if ("Trailing comma is not legal in an ECMA-262 object initializer" .equals(parseProblem.getMessage())) { // Report on the shortest code block containing the // problem (i.e. inner most code in nested structures). EcmascriptNode<?> currentNode = (EcmascriptNode<?>) parseProblemToNode.get(parseProblem); if (currentNode == null || node.getNode().getLength() < currentNode.getNode().getLength()) { parseProblemToNode.put(parseProblem, trailingCommaNode); } } } } } }
private boolean canProcessNode(AstNode node) { int start = node.getAbsolutePosition(); int offset = start + node.getLength(); return dot >= start && dot < offset; }
private boolean containsOffs(AstNode node) { int start = node.getAbsolutePosition(); return start<=offs && start+node.getLength()>offs; }
/** * Permits AST nodes to be sorted based on start position and length. * This makes it easy to sort Comment and Error nodes into a set of * other AST nodes: just put them all into a {@link java.util.SortedSet}, * for instance. * @param other another node * @return -1 if this node's start position is less than {@code other}'s * start position. If tied, -1 if this node's length is less than * {@code other}'s length. If the lengths are equal, sorts abitrarily * on hashcode unless the nodes are the same per {@link #equals}. */ public int compareTo(AstNode other) { if (this.equals(other)) return 0; int abs1 = this.getAbsolutePosition(); int abs2 = other.getAbsolutePosition(); if (abs1 < abs2) return -1; if (abs2 < abs1) return 1; int len1 = this.getLength(); int len2 = other.getLength(); if (len1 < len2) return -1; if (len2 < len1) return 1; return this.hashCode() - other.hashCode(); }
/** * Permits AST nodes to be sorted based on start position and length. * This makes it easy to sort Comment and Error nodes into a set of * other AST nodes: just put them all into a {@link java.util.SortedSet}, * for instance. * @param other another node * @return -1 if this node's start position is less than {@code other}'s * start position. If tied, -1 if this node's length is less than * {@code other}'s length. If the lengths are equal, sorts abitrarily * on hashcode unless the nodes are the same per {@link #equals}. */ public int compareTo(AstNode other) { if (this.equals(other)) return 0; int abs1 = this.getAbsolutePosition(); int abs2 = other.getAbsolutePosition(); if (abs1 < abs2) return -1; if (abs2 < abs1) return 1; int len1 = this.getLength(); int len2 = other.getLength(); if (len1 < len2) return -1; if (len2 < len1) return 1; return this.hashCode() - other.hashCode(); }
/** * Permits AST nodes to be sorted based on start position and length. * This makes it easy to sort Comment and Error nodes into a set of * other AST nodes: just put them all into a {@link java.util.SortedSet}, * for instance. * @param other another node * @return -1 if this node's start position is less than {@code other}'s * start position. If tied, -1 if this node's length is less than * {@code other}'s length. If the lengths are equal, sorts abitrarily * on hashcode unless the nodes are the same per {@link #equals}. */ public int compareTo(AstNode other) { if (this.equals(other)) return 0; int abs1 = this.getAbsolutePosition(); int abs2 = other.getAbsolutePosition(); if (abs1 < abs2) return -1; if (abs2 < abs1) return 1; int len1 = this.getLength(); int len2 = other.getLength(); if (len1 < len2) return -1; if (len2 < len1) return 1; return this.hashCode() - other.hashCode(); }
/** * Permits AST nodes to be sorted based on start position and length. * This makes it easy to sort Comment and Error nodes into a set of * other AST nodes: just put them all into a {@link java.util.SortedSet}, * for instance. * @param other another node * @return -1 if this node's start position is less than {@code other}'s * start position. If tied, -1 if this node's length is less than * {@code other}'s length. If the lengths are equal, sorts abitrarily * on hashcode unless the nodes are the same per {@link #equals}. */ public int compareTo(AstNode other) { if (this.equals(other)) return 0; int abs1 = this.getAbsolutePosition(); int abs2 = other.getAbsolutePosition(); if (abs1 < abs2) return -1; if (abs2 < abs1) return 1; int len1 = this.getLength(); int len2 = other.getLength(); if (len1 < len2) return -1; if (len2 < len1) return 1; return this.hashCode() - other.hashCode(); }
public boolean visit(AstNode node) { int tt = node.getType(); String name = Token.typeToName(tt); buffer.append(node.getAbsolutePosition()).append("\t"); buffer.append(makeIndent(node.depth())); buffer.append(name).append(" "); buffer.append(node.getPosition()).append(" "); buffer.append(node.getLength()); if (tt == Token.NAME) { buffer.append(" ").append(((Name)node).getIdentifier()); } buffer.append("\n"); return true; // process kids } }
public boolean visit(AstNode node) { int tt = node.getType(); String name = Token.typeToName(tt); buffer.append(node.getAbsolutePosition()).append("\t"); buffer.append(makeIndent(node.depth())); buffer.append(name).append(" "); buffer.append(node.getPosition()).append(" "); buffer.append(node.getLength()); if (tt == Token.NAME) { buffer.append(" ").append(((Name)node).getIdentifier()); } buffer.append("\n"); return true; // process kids } }
public boolean visit(AstNode node) { int tt = node.getType(); String name = Token.typeToName(tt); buffer.append(node.getAbsolutePosition()).append("\t"); buffer.append(makeIndent(node.depth())); buffer.append(name).append(" "); buffer.append(node.getPosition()).append(" "); buffer.append(node.getLength()); if (tt == Token.NAME) { buffer.append(" ").append(((Name)node).getIdentifier()); } buffer.append("\n"); return true; // process kids } }
public boolean visit(AstNode node) { int tt = node.getType(); String name = Token.typeToName(tt); buffer.append(node.getAbsolutePosition()).append("\t"); buffer.append(makeIndent(node.depth())); buffer.append(name).append(" "); buffer.append(node.getPosition()).append(" "); buffer.append(node.getLength()); if (tt == Token.NAME) { buffer.append(" ").append(((Name)node).getIdentifier()); } buffer.append("\n"); return true; // process kids } }
private Position makePosition(AstNode n) { if (n != null) { URL url = ((SourceModule)sourceModule).getURL(); int line = n.getLineno(); Position pos = new RangePosition(url, line, n.getAbsolutePosition(), n.getAbsolutePosition()+n.getLength()); if (sourceModule instanceof MappedSourceModule) { Position np = ((MappedSourceModule) sourceModule).getMapping().getIncludedPosition(pos); if (np != null) { return np; } } return pos; } else { return null; } }
/** * Creates and returns a node for the outline tree that corresponds to * specific AST nodes. * * @param nodes The AST nodes. * @return The outline tree node. */ private JavaScriptTreeNode createTreeNode(List<AstNode> nodes) { JavaScriptTreeNode tn = new JavaScriptTreeNode(nodes); try { int offs = nodes.get(0).getAbsolutePosition(); tn.setOffset(textArea.getDocument().createPosition(offs)); } catch (BadLocationException ble) { // Never happens ble.printStackTrace(); } return tn; }
private ITree buildTree(AstNode node) { ITree t = context.createTree(node.getType(), ITree.NO_LABEL, Token.typeToName(node.getType())); t.setPos(node.getAbsolutePosition()); t.setLength(node.getLength()); trees.put(node, t); return t; }
/** * Creates and returns a node for the outline tree that corresponds to * a specific AST node. * * @param node The AST node. * @return The outline tree node. */ private JavaScriptTreeNode createTreeNode(AstNode node) { JavaScriptTreeNode tn = new JavaScriptTreeNode(node); try { int offs = node.getAbsolutePosition(); tn.setOffset(textArea.getDocument().createPosition(offs)); } catch (BadLocationException ble) { // Never happens ble.printStackTrace(); } return tn; }
/** * for each child of parent AstNode add a new code block and add completions * for each block of code * * @param parent AstNode to iterate children * @param set completions set to add to * @param entered Text entered * @param codeBlock parent CodeBlock * @param offset codeblock offset */ private void addCodeBlock(Node parent, Set<Completion> set, String entered, CodeBlock codeBlock, int offset) { if(parent == null) return; Node child = parent.getFirstChild(); while (child != null) { CodeBlock childBlock = codeBlock; if (child instanceof AstNode) { AstNode node = (AstNode) child; int start = node.getAbsolutePosition(); childBlock = codeBlock.addChildCodeBlock(start); childBlock.setEndOffset(offset); } iterateNode((AstNode) child, set, entered, childBlock, offset); child = child.getNext(); } }
@Override public void actionPerformed(ActionEvent e) { JavaScriptParser parser = getParser(textArea); if (parser==null) { return; // Shouldn't happen } AstRoot astRoot = parser.getAstRoot(); if (astRoot!=null) { int dot = textArea.getCaretPosition(); visitor.reset(dot); astRoot.visit(visitor); AstNode scope = visitor.getDeepestScope(); if (scope!=null && scope!=astRoot) { int start = scope.getAbsolutePosition(); int end = Math.min(start + scope.getLength() - 1, textArea.getDocument().getLength()); try { int startLine = textArea.getLineOfOffset(start); int endLine = end<0 ? textArea.getLineCount() : textArea.getLineOfOffset(end); textArea.setActiveLineRange(startLine, endLine); } catch (BadLocationException ble) { ble.printStackTrace(); // Never happens } } else { textArea.setActiveLineRange(-1, -1); } } }
/** * Extract variables from if/else node(s) */ private void processIfThenElse(Node child, CodeBlock block, Set<Completion> set, String entered, int offset) { IfStatement ifStatement = (IfStatement) child; if (canProcessNode(ifStatement)) { offset = ifStatement.getAbsolutePosition() + ifStatement.getLength(); addCodeBlock(ifStatement.getThenPart(), set, entered, block, offset); AstNode elseNode = ifStatement.getElsePart(); if (elseNode != null) { int start = elseNode.getAbsolutePosition(); CodeBlock childBlock = block.addChildCodeBlock(start); offset = start + elseNode.getLength(); iterateNode(elseNode, set, entered, childBlock, offset); childBlock.setEndOffset(offset); } } }
TryStatement tryStatement = (TryStatement) child; if (canProcessNode(tryStatement)) { offset = tryStatement.getTryBlock().getAbsolutePosition() + tryStatement.getTryBlock().getLength(); addCodeBlock(tryStatement.getTryBlock(), set, entered, block, AstNode finallyNode = tryStatement.getFinallyBlock(); if (canProcessNode(finallyNode)) { offset = finallyNode.getAbsolutePosition() + finallyNode.getLength(); CodeBlock finallyBlock = block.getParent() .addChildCodeBlock( tryStatement.getFinallyBlock() .getAbsolutePosition()); addCodeBlock(finallyNode, set, entered, finallyBlock, offset);