PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/php.editor/src/org/netbeans/modules/php/editor/PHPDOCCodeCompletion.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 239 lines | 170 code | 22 blank | 47 comment | 26 complexity | 9a72076b1cb09430cb9744a197105a64 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 2008 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.php.editor;
  43. import java.util.Collections;
  44. import java.util.List;
  45. import java.util.Set;
  46. import javax.swing.ImageIcon;
  47. import org.netbeans.api.lexer.TokenHierarchy;
  48. import org.netbeans.api.lexer.TokenSequence;
  49. import org.netbeans.modules.csl.api.CompletionProposal;
  50. import org.netbeans.modules.csl.api.ElementHandle;
  51. import org.netbeans.modules.csl.api.ElementKind;
  52. import org.netbeans.modules.csl.api.HtmlFormatter;
  53. import org.netbeans.modules.csl.api.Modifier;
  54. import org.netbeans.modules.php.editor.PHPCompletionItem.CompletionRequest;
  55. import org.netbeans.modules.php.editor.index.PHPDOCTagElement;
  56. import org.netbeans.modules.php.editor.lexer.LexUtilities;
  57. import org.netbeans.modules.php.editor.lexer.PHPDocCommentTokenId;
  58. import org.netbeans.modules.php.editor.lexer.PHPTokenId;
  59. import org.netbeans.modules.php.editor.parser.api.Utils;
  60. import org.netbeans.modules.php.editor.parser.astnodes.ASTNode;
  61. import org.netbeans.modules.php.editor.parser.astnodes.FieldsDeclaration;
  62. import org.netbeans.modules.php.editor.parser.astnodes.FunctionDeclaration;
  63. import org.netbeans.modules.php.editor.parser.astnodes.MethodDeclaration;
  64. import org.netbeans.modules.php.editor.parser.astnodes.TypeDeclaration;
  65. import org.netbeans.modules.php.project.api.PhpAnnotations;
  66. import org.netbeans.modules.php.spi.annotation.AnnotationCompletionTag;
  67. import org.netbeans.modules.php.spi.annotation.AnnotationCompletionTagProvider;
  68. import org.openide.util.ImageUtilities;
  69. /**
  70. *
  71. * @author tomslot
  72. */
  73. public final class PHPDOCCodeCompletion {
  74. private static final String TAG_PREFIX = "@"; //NOI18N
  75. private PHPDOCCodeCompletion() {
  76. }
  77. static boolean isTypeCtx(PHPCompletionItem.CompletionRequest request) {
  78. TokenHierarchy<?> th = request.info.getSnapshot().getTokenHierarchy();
  79. TokenSequence<PHPTokenId> phpTS = (th != null) ? LexUtilities.getPHPTokenSequence(th, request.anchor) : null;
  80. if (phpTS != null) {
  81. phpTS.move(request.anchor);
  82. TokenSequence<PHPDocCommentTokenId> tokenSequence = phpTS.moveNext() ? phpTS.embedded(PHPDocCommentTokenId.language()) : null;
  83. if (tokenSequence == null) {
  84. return false;
  85. }
  86. tokenSequence.move(request.anchor);
  87. if (tokenSequence.movePrevious()) {
  88. int offset = tokenSequence.offset() + tokenSequence.token().length();
  89. if (tokenSequence.moveNext()) {
  90. CharSequence text = tokenSequence.token().text();
  91. String txt = text.subSequence(0, request.anchor - offset).toString();
  92. if (!txt.trim().isEmpty() && txt.charAt(txt.length() - 1) == '|') { //NOI18N
  93. // expect that user wants to complete mixed type
  94. txt = txt.trim();
  95. for (int i = 0; i < txt.length(); i++) {
  96. if (Character.isWhitespace(txt.charAt(i))) {
  97. return false;
  98. }
  99. }
  100. return true;
  101. }
  102. for (int i = 0; i < txt.length(); i++) {
  103. if (!Character.isWhitespace(txt.charAt(i))) {
  104. return false;
  105. }
  106. }
  107. }
  108. // text between PHPDoc directive and begining of the prefix, should only contain white spaces
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. public static void complete(final PHPCompletionResult completionResult, final CompletionRequest request) {
  115. if (request.prefix.startsWith(TAG_PREFIX)) {
  116. completeAnnotation(completionResult, request);
  117. }
  118. }
  119. private static void completeAnnotation(final PHPCompletionResult completionResult, final CompletionRequest request) {
  120. String prefix = request.prefix.substring(TAG_PREFIX.length());
  121. List<AnnotationCompletionTagProvider> providers = PhpAnnotations.getDefault().getCompletionTagProviders(request.info.getSnapshot().getSource().getFileObject());
  122. ASTNode nodeAfterOffset = Utils.getNodeAfterOffset(request.result, request.anchor);
  123. int priority = 0;
  124. for (AnnotationCompletionTagProvider annotationProvider : providers) {
  125. priority++;
  126. List<AnnotationCompletionTag> annotations;
  127. if (nodeAfterOffset instanceof TypeDeclaration) {
  128. annotations = annotationProvider.getTypeAnnotations();
  129. } else if (nodeAfterOffset instanceof MethodDeclaration) {
  130. annotations = annotationProvider.getMethodAnnotations();
  131. } else if (nodeAfterOffset instanceof FunctionDeclaration) {
  132. annotations = annotationProvider.getFunctionAnnotations();
  133. } else if (nodeAfterOffset instanceof FieldsDeclaration) {
  134. annotations = annotationProvider.getFieldAnnotations();
  135. } else {
  136. annotations = annotationProvider.getAnnotations();
  137. }
  138. for (AnnotationCompletionTag tag : annotations) {
  139. if (tag.getName().startsWith(prefix)) {
  140. completionResult.add(new PHPDOCCodeCompletionItem(request.anchor, tag, annotationProvider.getName(), priority));
  141. }
  142. }
  143. }
  144. }
  145. public static class PHPDOCCodeCompletionItem implements CompletionProposal {
  146. private static final String PHP_ANNOTATION_ICON = "org/netbeans/modules/php/editor/resources/annotation.png"; //NOI18N
  147. private static final ImageIcon ANNOTATION_ICON = new ImageIcon(ImageUtilities.loadImage(PHP_ANNOTATION_ICON));
  148. private final AnnotationCompletionTag tag;
  149. private final int anchorOffset;
  150. private final PHPDOCTagElement elem;
  151. private final String providerName;
  152. private final int priority;
  153. public PHPDOCCodeCompletionItem(int anchorOffset, AnnotationCompletionTag tag, String providerName, int priority) {
  154. this.tag = tag;
  155. this.anchorOffset = anchorOffset;
  156. this.providerName = providerName;
  157. this.priority = priority;
  158. elem = new PHPDOCTagElement(tag.getName(), tag.getDocumentation());
  159. }
  160. @Override
  161. public int getAnchorOffset() {
  162. return anchorOffset;
  163. }
  164. @Override
  165. public ElementHandle getElement() {
  166. return elem;
  167. }
  168. @Override
  169. public String getName() {
  170. return TAG_PREFIX + tag.getName(); //NOI18N
  171. }
  172. @Override
  173. public String getInsertPrefix() {
  174. return getName();
  175. }
  176. @Override
  177. public String getSortText() {
  178. return priority + providerName + getName();
  179. }
  180. @Override
  181. public int getSortPrioOverride() {
  182. return 0;
  183. }
  184. @Override
  185. public String getLhsHtml(HtmlFormatter formatter) {
  186. formatter.name(getKind(), true);
  187. formatter.appendText(getName());
  188. formatter.name(getKind(), false);
  189. tag.formatParameters(formatter);
  190. return formatter.getText();
  191. }
  192. @Override
  193. public String getRhsHtml(HtmlFormatter formatter) {
  194. return providerName;
  195. }
  196. @Override
  197. public ElementKind getKind() {
  198. return elem.getKind();
  199. }
  200. @Override
  201. public ImageIcon getIcon() {
  202. return ANNOTATION_ICON;
  203. }
  204. @Override
  205. public Set<Modifier> getModifiers() {
  206. return Collections.<Modifier>emptySet();
  207. }
  208. @Override
  209. public boolean isSmart() {
  210. return false;
  211. }
  212. @Override
  213. public String getCustomInsertTemplate() {
  214. return tag.getInsertTemplate();
  215. }
  216. }
  217. }