List of usage examples for org.eclipse.jdt.core.dom TextElement TEXT_PROPERTY
SimplePropertyDescriptor TEXT_PROPERTY
To view the source code for org.eclipse.jdt.core.dom TextElement TEXT_PROPERTY.
Click Source Link
From source file:org.eclipse.pde.api.tools.ui.internal.markers.UpdateSinceTagOperation.java
License:Open Source License
public void run(IProgressMonitor monitor) { if (monitor != null && monitor.isCanceled()) { return;/*from w w w .j a va 2 s . c o m*/ } if (monitor != null) { monitor.beginTask(MarkerMessages.UpdateSinceTagOperation_title, 3); } // retrieve the AST node compilation unit try { Integer charStartAttribute = (Integer) this.fMarker.getAttribute(IMarker.CHAR_START); int intValue = charStartAttribute.intValue(); IJavaElement javaElement = null; IJavaElement handleElement = null; if (intValue > 0) { IResource resource = this.fMarker.getResource(); javaElement = JavaCore.create(resource); } else { // this is a case where the marker is reported against the // MANIFEST.MF file String handle = (String) fMarker.getAttribute(IApiMarkerConstants.MARKER_ATTR_HANDLE_ID); if (handle != null) { handleElement = JavaCore.create(handle); } if (handleElement != null && handleElement.exists()) { javaElement = handleElement.getAncestor(IJavaElement.COMPILATION_UNIT); } } if (javaElement != null && javaElement.getElementType() == IJavaElement.COMPILATION_UNIT) { ICompilationUnit compilationUnit = (ICompilationUnit) javaElement; if (!compilationUnit.isWorkingCopy()) { // open an editor of the corresponding unit to "show" the // quickfix change JavaUI.openInEditor(compilationUnit); } ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setSource(compilationUnit); if (intValue <= 0) { // try to use the name range of the corresponding element if (handleElement instanceof IMember) { IMember member = (IMember) handleElement; ISourceRange range = member.getNameRange(); if (range != null) { intValue = range.getOffset(); } else { range = member.getSourceRange(); if (range != null && range.getOffset() > 0) { intValue = range.getOffset(); } else { return; } } } else { return; } } parser.setFocalPosition(intValue); parser.setResolveBindings(true); Map<String, String> options = compilationUnit.getJavaProject().getOptions(true); options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); parser.setCompilerOptions(options); final CompilationUnit unit = (CompilationUnit) parser.createAST(new NullProgressMonitor()); BodyDeclaration node = null; NodeFinder nodeFinder = new NodeFinder(intValue); unit.accept(nodeFinder); if (monitor != null) { monitor.worked(1); } node = nodeFinder.getNode(); if (node != null) { unit.recordModifications(); AST ast = unit.getAST(); ASTRewrite rewrite = ASTRewrite.create(ast); if (IApiProblem.SINCE_TAG_MISSING == this.sinceTagType) { Javadoc docnode = node.getJavadoc(); if (docnode == null) { docnode = ast.newJavadoc(); // we do not want to create a new empty Javadoc node // in // the AST if there are no missing tags rewrite.set(node, node.getJavadocProperty(), docnode, null); } else { List<TagElement> tags = docnode.tags(); boolean found = false; loop: for (Iterator<TagElement> iterator = tags.iterator(); iterator.hasNext();) { TagElement element = iterator.next(); String tagName = element.getTagName(); if (TagElement.TAG_SINCE.equals(tagName)) { found = true; break loop; } } if (found) { return; } } ListRewrite lrewrite = rewrite.getListRewrite(docnode, Javadoc.TAGS_PROPERTY); // check the existing tags list TagElement newtag = ast.newTagElement(); newtag.setTagName(TagElement.TAG_SINCE); TextElement textElement = ast.newTextElement(); textElement.setText(this.sinceTagVersion); newtag.fragments().add(textElement); lrewrite.insertLast(newtag, null); } else { Javadoc docnode = node.getJavadoc(); List<TagElement> tags = docnode.tags(); TagElement sinceTag = null; for (Iterator<TagElement> iterator = tags.iterator(); iterator.hasNext();) { TagElement tagElement = iterator.next(); if (TagElement.TAG_SINCE.equals(tagElement.getTagName())) { sinceTag = tagElement; break; } } if (sinceTag != null) { List<TextElement> fragments = sinceTag.fragments(); if (fragments.size() >= 1) { TextElement textElement = fragments.get(0); StringBuffer buffer = new StringBuffer(); buffer.append(' ').append(this.sinceTagVersion); rewrite.set(textElement, TextElement.TEXT_PROPERTY, String.valueOf(buffer), null); } else { ListRewrite lrewrite = rewrite.getListRewrite(docnode, Javadoc.TAGS_PROPERTY); // check the existing tags list TagElement newtag = ast.newTagElement(); newtag.setTagName(TagElement.TAG_SINCE); TextElement textElement = ast.newTextElement(); textElement.setText(this.sinceTagVersion); newtag.fragments().add(textElement); lrewrite.replace(sinceTag, newtag, null); } } } try { if (monitor != null) { monitor.worked(1); } TextEdit edit = rewrite.rewriteAST(); compilationUnit.applyTextEdit(edit, monitor); if (monitor != null) { monitor.worked(1); } } finally { compilationUnit.reconcile(ICompilationUnit.NO_AST, false /* * don * 't * force * problem * detection */, null /* * use * primary * owner */, null /* * no * progress * monitor */); } } } } catch (CoreException e) { ApiUIPlugin.log(e); } finally { if (monitor != null) { monitor.done(); } } }
From source file:org.moe.natjgen.XcodeFullDocumentation.java
License:Apache License
private void addTag(Javadoc doc, ASTRewrite rewrite, String tag, String string) { if (string == null) return;/*w ww. j av a 2 s.co m*/ ListRewrite lrw = rewrite.getListRewrite(doc, Javadoc.TAGS_PROPERTY); TagElement t = rewrite.getAST().newTagElement(); lrw.insertLast(t, editGroup); if (tag != null) { rewrite.set(t, TagElement.TAG_NAME_PROPERTY, tag, editGroup); } ListRewrite fragments = rewrite.getListRewrite(t, TagElement.FRAGMENTS_PROPERTY); TextElement text = rewrite.getAST().newTextElement(); fragments.insertLast(text, editGroup); rewrite.set(text, TextElement.TEXT_PROPERTY, string, editGroup); }