PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.jdt.core.source_3.7.1.v_B76_R37x/org/eclipse/jdt/core/CompletionRequestor.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 358 lines | 83 code | 26 blank | 249 comment | 22 complexity | 6468f2c9d5a202d87db0690e13fa20c2 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2004, 2008 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *******************************************************************************/
  11. package org.eclipse.jdt.core;
  12. import org.eclipse.jdt.core.compiler.IProblem;
  13. /**
  14. * Abstract base class for a completion requestor which is passed completion
  15. * proposals as they are generated in response to a code assist request.
  16. * <p>
  17. * This class is intended to be subclassed by clients.
  18. * </p>
  19. * <p>
  20. * The code assist engine normally invokes methods on completion
  21. * requestor in the following sequence:
  22. * <pre>
  23. * requestor.beginReporting();
  24. * requestor.acceptContext(context);
  25. * requestor.accept(proposal_1);
  26. * requestor.accept(proposal_2);
  27. * ...
  28. * requestor.endReporting();
  29. * </pre>
  30. * If, however, the engine is unable to offer completion proposals
  31. * for whatever reason, <code>completionFailure</code> is called
  32. * with a problem object describing why completions were unavailable.
  33. * In this case, the sequence of calls is:
  34. * <pre>
  35. * requestor.beginReporting();
  36. * requestor.acceptContext(context);
  37. * requestor.completionFailure(problem);
  38. * requestor.endReporting();
  39. * </pre>
  40. * In either case, the bracketing <code>beginReporting</code>
  41. * <code>endReporting</code> calls are always made as well as
  42. * <code>acceptContext</code> call.
  43. * </p>
  44. * <p>
  45. * The class was introduced in 3.0 as a more evolvable replacement
  46. * for the <code>ICompletionRequestor</code> interface.
  47. * </p>
  48. *
  49. * @see ICodeAssist
  50. * @since 3.0
  51. */
  52. public abstract class CompletionRequestor {
  53. /**
  54. * The set of CompletionProposal kinds that this requestor
  55. * ignores; <code>0</code> means the set is empty.
  56. * 1 << completionProposalKind
  57. */
  58. private int ignoreSet = 0;
  59. private String[] favoriteReferences;
  60. /**
  61. * The set of CompletionProposal kinds that this requestor
  62. * allows for required proposals; <code>0</code> means the set is empty.
  63. * 1 << completionProposalKind
  64. */
  65. private int requiredProposalAllowSet[] = null;
  66. private boolean requireExtendedContext = false;
  67. /**
  68. * Creates a new completion requestor.
  69. * The requestor is interested in all kinds of completion
  70. * proposals; none will be ignored.
  71. *
  72. * Calls to this constructor are identical to calls to <code>CompletionRequestor(false)</code>
  73. */
  74. public CompletionRequestor() {
  75. this(false);
  76. }
  77. /**
  78. * Creates a new completion requestor.
  79. * If <code>ignoreAll</code> is <code>true</code> the requestor is not interested in
  80. * all kinds of completion proposals; all will be ignored. For each kind of completion proposals
  81. * that is of interest, <code>setIgnored(kind, false)</code> must be called.
  82. * If <code>ignoreAll</code> is <code>false</code> the requestor is interested in
  83. * all kinds of completion proposals; none will be ignored.
  84. *
  85. * @param ignoreAll <code>true</code> to ignore all kinds of completion proposals,
  86. * and <code>false</code> to propose all kinds
  87. *
  88. * @since 3.4
  89. */
  90. public CompletionRequestor(boolean ignoreAll) {
  91. this.ignoreSet = ignoreAll ? 0xffffffff : 0x00000000;
  92. }
  93. /**
  94. * Returns whether the given kind of completion proposal is ignored.
  95. *
  96. * @param completionProposalKind one of the kind constants declared
  97. * on <code>CompletionProposal</code>
  98. * @return <code>true</code> if the given kind of completion proposal
  99. * is ignored by this requestor, and <code>false</code> if it is of
  100. * interest
  101. * @see #setIgnored(int, boolean)
  102. * @see CompletionProposal#getKind()
  103. */
  104. public boolean isIgnored(int completionProposalKind) {
  105. if (completionProposalKind < CompletionProposal.FIRST_KIND
  106. || completionProposalKind > CompletionProposal.LAST_KIND) {
  107. throw new IllegalArgumentException("Unknown kind of completion proposal: "+completionProposalKind); //$NON-NLS-1$
  108. }
  109. return 0 != (this.ignoreSet & (1 << completionProposalKind));
  110. }
  111. /**
  112. * Sets whether the given kind of completion proposal is ignored.
  113. *
  114. * @param completionProposalKind one of the kind constants declared
  115. * on <code>CompletionProposal</code>
  116. * @param ignore <code>true</code> if the given kind of completion proposal
  117. * is ignored by this requestor, and <code>false</code> if it is of
  118. * interest
  119. * @see #isIgnored(int)
  120. * @see CompletionProposal#getKind()
  121. */
  122. public void setIgnored(int completionProposalKind, boolean ignore) {
  123. if (completionProposalKind < CompletionProposal.FIRST_KIND
  124. || completionProposalKind > CompletionProposal.LAST_KIND) {
  125. throw new IllegalArgumentException("Unknown kind of completion proposal: "+completionProposalKind); //$NON-NLS-1$
  126. }
  127. if (ignore) {
  128. this.ignoreSet |= (1 << completionProposalKind);
  129. } else {
  130. this.ignoreSet &= ~(1 << completionProposalKind);
  131. }
  132. }
  133. /**
  134. * Returns whether a proposal of a given kind with a required proposal
  135. * of the given kind is allowed.
  136. *
  137. * @param proposalKind one of the kind constants declared
  138. * @param requiredProposalKind one of the kind constants declared
  139. * on <code>CompletionProposal</code>
  140. * @return <code>true</code> if a proposal of a given kind with a required proposal
  141. * of the given kind is allowed by this requestor, and <code>false</code>
  142. * if it isn't of interest.
  143. * <p>
  144. * By default, all kinds of required proposals aren't allowed.
  145. * </p>
  146. * @see #setAllowsRequiredProposals(int, int, boolean)
  147. * @see CompletionProposal#getKind()
  148. * @see CompletionProposal#getRequiredProposals()
  149. *
  150. * @since 3.3
  151. */
  152. public boolean isAllowingRequiredProposals(int proposalKind, int requiredProposalKind) {
  153. if (proposalKind < CompletionProposal.FIRST_KIND
  154. || proposalKind > CompletionProposal.LAST_KIND) {
  155. throw new IllegalArgumentException("Unknown kind of completion proposal: "+requiredProposalKind); //$NON-NLS-1$
  156. }
  157. if (requiredProposalKind < CompletionProposal.FIRST_KIND
  158. || requiredProposalKind > CompletionProposal.LAST_KIND) {
  159. throw new IllegalArgumentException("Unknown required kind of completion proposal: "+requiredProposalKind); //$NON-NLS-1$
  160. }
  161. if (this.requiredProposalAllowSet == null) return false;
  162. return 0 != (this.requiredProposalAllowSet[proposalKind] & (1 << requiredProposalKind));
  163. }
  164. /**
  165. * Sets whether a proposal of a given kind with a required proposal
  166. * of the given kind is allowed.
  167. *
  168. * A required proposal of a given kind is proposed even if {@link #isIgnored(int)}
  169. * return <code>true</code> for that kind.
  170. *
  171. * Currently only a subset of kinds support required proposals. To see what combinations
  172. * are supported you must look at {@link CompletionProposal#getRequiredProposals()}
  173. * documentation.
  174. *
  175. * @param proposalKind one of the kind constants declared
  176. * @param requiredProposalKind one of the kind constants declared
  177. * on <code>CompletionProposal</code>
  178. * @param allow <code>true</code> if a proposal of a given kind with a required proposal
  179. * of the given kind is allowed by this requestor, and <code>false</code>
  180. * if it isn't of interest
  181. * @see #isAllowingRequiredProposals(int, int)
  182. * @see CompletionProposal#getKind()
  183. * @see CompletionProposal#getRequiredProposals()
  184. *
  185. * @since 3.3
  186. */
  187. public void setAllowsRequiredProposals(int proposalKind, int requiredProposalKind, boolean allow) {
  188. if (proposalKind < CompletionProposal.FIRST_KIND
  189. || proposalKind > CompletionProposal.LAST_KIND) {
  190. throw new IllegalArgumentException("Unknown kind of completion proposal: "+requiredProposalKind); //$NON-NLS-1$
  191. }
  192. if (requiredProposalKind < CompletionProposal.FIRST_KIND
  193. || requiredProposalKind > CompletionProposal.LAST_KIND) {
  194. throw new IllegalArgumentException("Unknown required kind of completion proposal: "+requiredProposalKind); //$NON-NLS-1$
  195. }
  196. if (this.requiredProposalAllowSet == null) {
  197. this.requiredProposalAllowSet = new int[CompletionProposal.LAST_KIND + 1];
  198. }
  199. if (allow) {
  200. this.requiredProposalAllowSet[proposalKind] |= (1 << requiredProposalKind);
  201. } else {
  202. this.requiredProposalAllowSet[proposalKind] &= ~(1 << requiredProposalKind);
  203. }
  204. }
  205. /**
  206. * Returns the favorite references which are used to compute some completion proposals.
  207. * <p>
  208. * A favorite reference is a qualified reference as it can be seen in an import statement.<br>
  209. * e.g. <code>{"java.util.Arrays"}</code><br>
  210. * It can be an on demand reference.<br>
  211. * e.g. <code>{"java.util.Arrays.*"}</code>
  212. * It can be a reference to a static method or field (as in a static import)<br>
  213. * e.g. <code>{"java.util.Arrays.equals"}</code>
  214. * </p>
  215. * <p>
  216. * Currently only on demand type references (<code>"java.util.Arrays.*"</code>),
  217. * references to a static method or a static field are used to compute completion proposals.
  218. * Other kind of reference could be used in the future.
  219. * </p>
  220. * @return favorite imports
  221. *
  222. * @since 3.3
  223. */
  224. public String[] getFavoriteReferences() {
  225. return this.favoriteReferences;
  226. }
  227. /**
  228. * Set the favorite references which will be used to compute some completion proposals.
  229. * A favorite reference is a qualified reference as it can be seen in an import statement.<br>
  230. *
  231. * @param favoriteImports
  232. *
  233. * @see #getFavoriteReferences()
  234. *
  235. * @since 3.3
  236. */
  237. public void setFavoriteReferences(String[] favoriteImports) {
  238. this.favoriteReferences = favoriteImports;
  239. }
  240. /**
  241. * Pro forma notification sent before reporting a batch of
  242. * completion proposals.
  243. * <p>
  244. * The default implementation of this method does nothing.
  245. * Clients may override.
  246. * </p>
  247. */
  248. public void beginReporting() {
  249. // do nothing
  250. }
  251. /**
  252. * Pro forma notification sent after reporting a batch of
  253. * completion proposals.
  254. * <p>
  255. * The default implementation of this method does nothing.
  256. * Clients may override.
  257. * </p>
  258. */
  259. public void endReporting() {
  260. // do nothing
  261. }
  262. /**
  263. * Notification of failure to produce any completions.
  264. * The problem object explains what prevented completing.
  265. * <p>
  266. * The default implementation of this method does nothing.
  267. * Clients may override to receive this kind of notice.
  268. * </p>
  269. *
  270. * @param problem the problem object
  271. */
  272. public void completionFailure(IProblem problem) {
  273. // default behavior is to ignore
  274. }
  275. /**
  276. * Proposes a completion. Has no effect if the kind of proposal
  277. * is being ignored by this requestor. Callers should consider
  278. * checking {@link #isIgnored(int)} before avoid creating proposal
  279. * objects that would only be ignored.
  280. * <p>
  281. * Similarly, implementers should check
  282. * {@link #isIgnored(int) isIgnored(proposal.getKind())}
  283. * and ignore proposals that have been declared as uninteresting.
  284. * The proposal object passed is only valid for the duration of
  285. * completion operation.
  286. *
  287. * @param proposal the completion proposal
  288. * @exception IllegalArgumentException if the proposal is null
  289. */
  290. public abstract void accept(CompletionProposal proposal);
  291. /**
  292. * Propose the context in which the completion occurs.
  293. * <p>
  294. * This method is called one and only one time before any call to
  295. * {@link #accept(CompletionProposal)}.
  296. * The default implementation of this method does nothing.
  297. * Clients may override.
  298. * </p>
  299. * @param context the completion context
  300. *
  301. * @since 3.1
  302. */
  303. public void acceptContext(CompletionContext context) {
  304. // do nothing
  305. }
  306. /**
  307. * Returns whether this requestor requires an extended context.
  308. *
  309. * By default this method return <code>false</code>.
  310. *
  311. * @return <code>true</code> if this requestor requires an extended context.
  312. *
  313. * @see CompletionContext#isExtended()
  314. *
  315. * @since 3.4
  316. */
  317. public boolean isExtendedContextRequired() {
  318. return this.requireExtendedContext;
  319. }
  320. /**
  321. * Sets whether this requestor requires an extended context.
  322. *
  323. * @param require <code>true</code> if this requestor requires an extended context.
  324. *
  325. * @see CompletionContext#isExtended()
  326. *
  327. * @since 3.4
  328. */
  329. public void setRequireExtendedContext(boolean require) {
  330. this.requireExtendedContext = require;
  331. }
  332. }