@Override protected List<INode> getLocationNodes(EObject obj) { if (obj instanceof XMemberFeatureCall || obj instanceof XAssignment || obj instanceof XFeatureCall) { List<INode> resultNodes = Lists.newArrayList(); final ICompositeNode startNode = findNodeFor(obj); boolean crossRefConsumed = false; for (INode child : startNode.getChildren()) { if (crossRefConsumed) { resultNodes.add(child); } else { EObject grammarElement = child.getGrammarElement(); if (grammarElement instanceof CrossReference) { // We don't use the grammar access to be more robust against // overwriting grammars Assignment assignment = GrammarUtil.containingAssignment(grammarElement); if (XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE .getName() .equals(assignment.getFeature())) { crossRefConsumed = true; resultNodes.add(child); } } } } if (!resultNodes.isEmpty()) return resultNodes; } return super.getLocationNodes(obj); }
private INode findNode( EObject source, boolean sourceFound, INode root, Keyword keyword, int[] index) { if (sourceFound && root.getSemanticElement() != source) { return null; } if (root.getSemanticElement() == source) { sourceFound = true; } EObject grammarElement = root.getGrammarElement(); // .equals or == does not work because sub grammars use their own // Modules with custom // grammarAccess instance and .equals is not overwritten. if (grammarElement instanceof Keyword && keyword.getValue().equals(((Keyword) grammarElement).getValue())) { if (index[0] != INSIGNIFICANT_INDEX) { index[0]--; } if (index[0] == 0 || index[0] == INSIGNIFICANT_INDEX) { return root; } } if (root instanceof ICompositeNode) { ICompositeNode node = (ICompositeNode) root; for (INode child : node.getChildren()) { INode result = findNode(source, sourceFound, child, keyword, index); if (result != null) { return result; } } } return null; }
public TextRegion getImportRegion(XtextResource xtextResource) { XPackage xPackage = getXPackage(xtextResource); if (xPackage != null) { List<INode> xPackageNodes = NodeModelUtils.findNodesForFeature(xPackage, XcorePackage.Literals.XNAMED_ELEMENT__NAME); int begin; int size = xPackageNodes.size(); if (size == 0) { begin = 0; } else { INode lastNode = xPackageNodes.get(size - 1); begin = lastNode.getOffset() + lastNode.getLength(); } int end; EList<XAnnotationDirective> annotationDirectives = xPackage.getAnnotationDirectives(); if (!annotationDirectives.isEmpty()) { ICompositeNode node = NodeModelUtils.getNode(annotationDirectives.get(0)); end = node.getTotalOffset(); } else { EList<XClassifier> classifiers = xPackage.getClassifiers(); if (!classifiers.isEmpty()) { ICompositeNode node = NodeModelUtils.getNode(classifiers.get(0)); end = node.getTotalOffset(); } else { end = xtextResource.getParseResult().getRootNode().getTotalEndOffset(); } } return new TextRegion(begin, end - begin); } else { return null; } }
public List<ReplaceRegion> rewrite() { removeObsoleteStaticImports(); final List<ReplaceRegion> replaceRegions = newArrayList(); if (isSort) { List<XImportDeclaration> allImportDeclarations = newArrayList(); allImportDeclarations.addAll(originalImportDeclarations); allImportDeclarations.addAll(addedImportDeclarations); allImportDeclarations.removeAll(removedImportDeclarations); String newImportSection = serializeImports(allImportDeclarations); importRegion = regionUtil.addLeadingWhitespace(importRegion, resource); importRegion = regionUtil.addTrailingWhitespace(importRegion, resource); return singletonList(new ReplaceRegion(importRegion, newImportSection)); } else { for (XImportDeclaration removedImportDeclaration : removedImportDeclarations) { ICompositeNode node = NodeModelUtils.findActualNodeFor(removedImportDeclaration); if (node != null) { ITextRegion textRegion = node.getTextRegion(); textRegion = regionUtil.addTrailingSingleWhitespace(textRegion, lineSeparator, resource); replaceRegions.add(new ReplaceRegion(textRegion, "")); } } addSectionToAppend( new IAcceptor<ReplaceRegion>() { @Override public void accept(ReplaceRegion t) { replaceRegions.add(t); } }); } return replaceRegions; }
protected String serialize(final EObject obj) { final ICompositeNode node = NodeModelUtils.getNode(obj); boolean _equals = Objects.equal(node, null); if (_equals) { return ""; } return node.getText(); }
public String serialize(final EObject obj) { ICompositeNode _node = NodeModelUtils.getNode(obj); String _text = null; if (_node != null) { _text = _node.getText(); } return _text; }
protected void moveUp( ICompositeNode node, ICompositeNode previousNode, IModificationContext context) throws BadLocationException { IXtextDocument document = context.getXtextDocument(); String text = node.getText() + previousNode.getText(); text = text.trim(); remove(document, node); document.replace(previousNode.getOffset(), previousNode.getLength(), text); }
/** * @return true, if the parent node could contain cross references to the same semantic element as * the given node. */ protected boolean shouldCheckParentNode(INode node) { EObject grammarElement = node.getGrammarElement(); if (grammarElement instanceof AbstractElement) { ICompositeNode parent = node.getParent(); if (parent != null && !parent.hasDirectSemanticElement()) { Assignment assignment = GrammarUtil.containingAssignment(grammarElement); return assignment == null; } } return false; }
protected void remove(IXtextDocument document, ICompositeNode node) throws BadLocationException { int offset = node.getOffset(); int length = node.getLength(); if (node.hasPreviousSibling()) { INode previousSibling = node.getPreviousSibling(); int endOffset = previousSibling.getEndOffset(); length = length + (offset - endOffset); offset = endOffset; } document.replace(offset, length, ""); }
public void assertNonSmoking(final CharSequence input) throws Exception { final String string = input.toString(); final XtendFile file = this.file(string, false); boolean _notEquals = (!Objects.equal(file, null)); if (_notEquals) { Resource _eResource = file.eResource(); final XtextResource resource = ((XtextResource) _eResource); IParseResult _parseResult = resource.getParseResult(); final ICompositeNode rootNode = _parseResult.getRootNode(); ReplaceRegion region = null; BidiTreeIterable<INode> _asTreeIterable = rootNode.getAsTreeIterable(); for (final INode node : _asTreeIterable) { { final int offset = node.getTotalOffset(); final int length = node.getTotalLength(); boolean _notEquals_1 = (length != 0); if (_notEquals_1) { boolean _or = false; boolean _or_1 = false; boolean _equals = Objects.equal(region, null); if (_equals) { _or_1 = true; } else { int _offset = region.getOffset(); boolean _notEquals_2 = (_offset != offset); _or_1 = (_equals || _notEquals_2); } if (_or_1) { _or = true; } else { int _length = region.getLength(); boolean _notEquals_3 = (_length != length); _or = (_or_1 || _notEquals_3); } if (_or) { ReplaceRegion _replaceRegion = new ReplaceRegion(offset, length, ""); region = _replaceRegion; String _text = rootNode.getText(); StringBuilder _stringBuilder = new StringBuilder(_text); final StringBuilder builder = _stringBuilder; region.applyTo(builder); String _string = builder.toString(); this.processFile(_string); } } } } } }
protected void highlightSpecialIdentifiers( IHighlightedPositionAcceptor acceptor, ICompositeNode root) { TerminalRule idRule = getIDRule(); for (ILeafNode leaf : root.getLeafNodes()) { if (!leaf.isHidden()) { highlightSpecialIdentifiers(leaf, acceptor, idRule); } } }
protected void highlightAnnotation( XAnnotation annotation, IHighlightedPositionAcceptor acceptor, String highlightingConfiguration) { JvmType annotationType = annotation.getAnnotationType(); if (annotationType != null && !annotationType.eIsProxy() && annotationType instanceof JvmAnnotationType) { ICompositeNode xannotationNode = NodeModelUtils.findActualNodeFor(annotation); if (xannotationNode != null) { ILeafNode firstLeafNode = NodeModelUtils.findLeafNodeAtOffset(xannotationNode, xannotationNode.getOffset()); if (firstLeafNode != null) highlightNode(acceptor, firstLeafNode, highlightingConfiguration); } highlightReferenceJvmType( acceptor, annotation, XAnnotationsPackage.Literals.XANNOTATION__ANNOTATION_TYPE, annotationType, highlightingConfiguration); } }
protected int[] getOffsetAndLength(XIfExpression ifExpression, ICompositeNode node) { int offset = node.getOffset(); int length = node.getLength(); if (ifExpression.getElse() != null) { ICompositeNode elseNode = NodeModelUtils.findActualNodeFor(ifExpression.getElse()); if (elseNode != null) { length = elseNode.getOffset() - offset; } } else { XIfExpression parentIfExpression = EcoreUtil2.getContainerOfType(ifExpression.eContainer(), XIfExpression.class); if (parentIfExpression != null && parentIfExpression.getElse() == ifExpression) { ICompositeNode thenNode = NodeModelUtils.findActualNodeFor(parentIfExpression.getThen()); if (thenNode != null) { int endOffset = thenNode.getEndOffset(); length = length + (offset - endOffset); offset = endOffset; } } } return new int[] {offset, length}; }
protected void highlightNumberLiterals( XNumberLiteral literal, IHighlightedPositionAcceptor acceptor) { ICompositeNode node = NodeModelUtils.findActualNodeFor(literal); ITextRegion textRegion = node.getTextRegion(); acceptor.addPosition(textRegion.getOffset(), textRegion.getLength(), NUMBER_ID); }
private IRegion[] regionOf(Content content) { ICompositeNode node = NodeModelUtils.getNode(content); int length = node.getLength(); int offset = node.getOffset(); return new Region[] {new Region(offset, length)}; }
private void calculateVersionProposals( EObject model, ContentAssistContext context, ICompletionProposalAcceptor acceptor, boolean majorVersionsOnly) { ICompositeNode parentNode = NodeModelUtils.findActualNodeFor(model).getParent(); Iterable<ILeafNode> leafs = parentNode.getLeafNodes(); Iterable<ILeafNode> nonHidden = Iterables.filter( leafs, new Predicate<ILeafNode>() { public boolean apply(ILeafNode node) { return !node.isHidden(); } }); Model bizModel = null; EObject curObj = model; while (!(curObj instanceof Model) && curObj.eContainer() != null) { curObj = curObj.eContainer(); } if (curObj instanceof Model) { bizModel = (Model) curObj; EList<Import> imports = bizModel.getImports(); List<String> importedNamespaces = Lists.newArrayList( Lists.transform( imports, new Function<Import, String>() { public String apply(Import from) { return from.getImportedNamespace().replaceAll("\\.\\*", ""); } })); Iterator<ILeafNode> leafIt = nonHidden.iterator(); if (model.eContainer() instanceof CapabilityRef) { CapabilityRef capRef = (CapabilityRef) model.eContainer(); boolean versionConstraintFound = false; StringBuilder nameParts = new StringBuilder(); while (leafIt.hasNext() && !versionConstraintFound) { ILeafNode curNode = leafIt.next(); if (curNode.getSemanticElement() instanceof VersionRef) versionConstraintFound = true; else nameParts.append(curNode.getText()); } String typeName = nameParts.toString().trim().replaceAll("\\[\\]", "").trim(); final List<String> classNames = Lists.newArrayList( BusinessDslPackage.Literals.SIMPLE_CAPABILITY.getName(), BusinessDslPackage.Literals.CAPABILITY_VARIANT.getName()); Iterable<String> canditateVersions = getCanditateVersions(typeName, classNames, importedNamespaces, majorVersionsOnly); for (String version : canditateVersions) { acceptor.accept(createCompletionProposal(version, context)); } } else if (model.eContainer() instanceof ServiceRef) { ServiceRef capRef = (ServiceRef) model.eContainer(); boolean versionConstraintFound = false; StringBuilder nameParts = new StringBuilder(); while (leafIt.hasNext() && !versionConstraintFound) { ILeafNode curNode = leafIt.next(); if (curNode.getSemanticElement() instanceof VersionRef) versionConstraintFound = true; else nameParts.append(curNode.getText()); } String typeName = nameParts.toString().trim().replaceAll("\\[\\]", "").trim(); Iterable<String> canditateVersions = getCanditateVersions( typeName, ServiceDslPackage.Literals.SERVICE.getName(), importedNamespaces, majorVersionsOnly); for (String version : canditateVersions) { acceptor.accept(createCompletionProposal(version, context)); } } else if (model.eContainer() instanceof EventRef) { EventRef eventRef = (EventRef) model.eContainer(); boolean versionConstraintFound = false; StringBuilder nameParts = new StringBuilder(); while (leafIt.hasNext() && !versionConstraintFound) { ILeafNode curNode = leafIt.next(); if (curNode.getSemanticElement() instanceof VersionRef) versionConstraintFound = true; else nameParts.append(curNode.getText()); } String typeName = nameParts.toString().trim().replaceAll("\\[\\]", "").trim(); final List<String> classNames = Lists.newArrayList( ServiceDslPackage.Literals.EVENT.getName(), ServiceDslPackage.Literals.EVENT.getName()); Iterable<String> canditateVersions = getCanditateVersions(typeName, classNames, importedNamespaces, majorVersionsOnly); for (String version : canditateVersions) { acceptor.accept(createCompletionProposal(version, context)); } } else if (model.eContainer() instanceof OperationRef) { boolean versionConstraintFound = false; StringBuilder nameParts = new StringBuilder(); while (leafIt.hasNext() && !versionConstraintFound) { ILeafNode curNode = leafIt.next(); if (curNode.getSemanticElement() instanceof VersionRef) versionConstraintFound = true; else nameParts.append(curNode.getText()); } String typeName = nameParts.toString().trim().replaceAll("\\[\\]", "").trim(); String[] opNameParts = typeName.split("\\."); if (opNameParts.length > 1) { typeName = typeName.replaceAll("\\." + opNameParts[opNameParts.length - 1], ""); } String className = ServiceDslPackage.Literals.SERVICE.getName(); Iterable<String> canditateVersions = getCanditateVersions(typeName, className, importedNamespaces, majorVersionsOnly); for (String version : canditateVersions) { acceptor.accept(createCompletionProposal(version, context)); } } else { if (majorVersionsOnly) acceptor.accept(createCompletionProposal("1", context)); else acceptor.accept(createCompletionProposal("1.0", context)); } } }