11/*
22 * Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
3- *
3+ *
44 * Permission is hereby granted, free of charge, to any person obtaining a
5- * copy of this software and associated documentation files (the "Software"),
6- * to deal in the Software without restriction, including without limitation
7- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8- * and/or sell copies of the Software, and to permit persons to whom the
5+ * copy of this software and associated documentation files (the "Software"),
6+ * to deal in the Software without restriction, including without limitation
7+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+ * and/or sell copies of the Software, and to permit persons to whom the
99 * Software is furnished to do so, subject to the following conditions:
10- *
10+ *
1111 * The above copyright notice and this permission notice shall be included in
1212 * all copies or substantial portions of the Software.
13- *
13+ *
1414 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1616 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2020 * DEALINGS IN THE SOFTWARE.
21- *
21+ *
2222 */
2323
2424/*
9191 * to show hints. Because implicit hints can be triggered by every
9292 * character insertion, hasHints may be called frequently; consequently,
9393 * the provider should endeavor to return a value as quickly as possible.
94- *
94+ *
9595 * Because calls to hasHints imply that a hinting session is about to
9696 * begin, a provider may wish to clean up cached data from previous
9797 * sessions in this method. Similarly, if the provider returns true, it
9898 * may wish to prepare to cache data suitable for the current session. In
9999 * particular, it should keep a reference to the editor object so that it
100100 * can access the editor in future calls to getHints and insertHints.
101- *
102- * param {Editor} editor
101+ *
102+ * param {Editor} editor
103103 * A non-null editor object for the active window.
104104 *
105- * param {string} implicitChar
105+ * param {string} implicitChar
106106 * Either null, if the hinting request was explicit, or a single character
107107 * that represents the last insertion and that indicates an implicit
108108 * hinting request.
109109 *
110- * return {boolean}
110+ * return {boolean}
111111 * Determines whether the current provider is able to provide hints for
112112 * the given editor context and, in case implicitChar is non- null,
113113 * whether it is appropriate to do so.
114- *
115- *
114+ *
115+ *
116116 * # CodeHintProvider.getHints(implicitChar)
117- *
117+ *
118118 * The method by which a provider provides hints for the editor context
119119 * associated with the current session. The getHints method is called only
120120 * if the provider asserted its willingness to provide hints in an earlier
121- * call to hasHints. The provider may return null or false, which indicates
121+ * call to hasHints. The provider may return null or false, which indicates
122122 * that the manager should end the current hinting session and close the hint
123- * list window; or true, which indicates that the manager should end the
123+ * list window; or true, which indicates that the manager should end the
124124 * current hinting session but immediately attempt to begin a new hinting
125125 * session by querying registered providers. Otherwise, the provider should
126126 * return a response object that contains the following properties:
156156 * deferred object from the current call has resolved or been rejected. If
157157 * the provider rejects the deferred, the manager will end the hinting
158158 * session.
159- *
159+ *
160160 * The getHints method may be called by the manager repeatedly during a
161161 * hinting session. Providers may wish to cache information for efficiency
162162 * that may be useful throughout these sessions. The same editor context
177177 * match: string,
178178 * selectInitial: boolean,
179179 * handleWideResults: boolean}}
180- *
180+ *
181181 * Null if the provider wishes to end the hinting session. Otherwise, a
182182 * response object, possibly deferred, that provides 1. a sorted array
183183 * hints that consists either of strings or jQuery objects; 2. a string
184184 * match, possibly null, that is used by the manager to emphasize
185185 * matching substrings when rendering the hint list; and 3. a boolean that
186186 * indicates whether the first result, if one exists, should be selected
187187 * by default in the hint list window. If match is non-null, then the
188- * hints should be strings.
189- *
190- * If the match is null, the manager will not
191- * attempt to emphasize any parts of the hints when rendering the hint
192- * list; instead the provider may return strings or jQuery objects for
188+ * hints should be strings.
189+ *
190+ * If the match is null, the manager will not
191+ * attempt to emphasize any parts of the hints when rendering the hint
192+ * list; instead the provider may return strings or jQuery objects for
193193 * which emphasis is self-contained. For example, the strings may contain
194- * substrings that wrapped in bold tags. In this way, the provider can
194+ * substrings that wrapped in bold tags. In this way, the provider can
195195 * choose to let the manager handle emphasis for the simple and common case
196- * of prefix matching, or can provide its own emphasis if it wishes to use
196+ * of prefix matching, or can provide its own emphasis if it wishes to use
197197 * a more sophisticated matching algorithm.
198- *
198+ *
199199 *
200200 * # CodeHintProvider.insertHint(hint)
201201 *
209209 * explicit hinting request, which may result in a new hinting session
210210 * being opened with some provider, but not necessarily the current one.
211211 *
212- * param {string} hint
212+ * param {string} hint
213213 * The hint to be inserted into the editor context for the current session.
214- *
215- * return {boolean}
214+ *
215+ * return {boolean}
216216 * Indicates whether the manager should follow hint insertion with an
217217 * explicit hint request.
218218 *
219219 *
220220 * # CodeHintProvider.insertHintOnTab
221- *
221+ *
222222 * type {?boolean} insertHintOnTab
223- * Indicates whether the CodeHintManager should request that the provider of
223+ * Indicates whether the CodeHintManager should request that the provider of
224224 * the current session insert the currently selected hint on tab key events,
225225 * or if instead a tab character should be inserted into the editor. If omitted,
226226 * the fallback behavior is determined by the CodeHintManager. The default
@@ -275,12 +275,12 @@ define(function (require, exports, module) {
275275 return b . priority - a . priority ;
276276 }
277277
278- /**
278+ /**
279279 * The method by which a CodeHintProvider registers its willingness to
280280 * providing hints for editors in a given language.
281281 *
282282 * @param {!CodeHintProvider } provider
283- * The hint provider to be registered, described below.
283+ * The hint provider to be registered, described below.
284284 *
285285 * @param {!Array.<string> } languageIds
286286 * The set of language ids for which the provider is capable of
@@ -353,7 +353,7 @@ define(function (require, exports, module) {
353353 } ) ;
354354 }
355355
356- /**
356+ /**
357357 * Return the array of hint providers for the given language id.
358358 * This gets called (potentially) on every keypress. So, it should be fast.
359359 *
@@ -384,14 +384,14 @@ define(function (require, exports, module) {
384384 }
385385 }
386386
387- /**
387+ /**
388388 * Is there a hinting session active for a given editor?
389- *
389+ *
390390 * NOTE: the sessionEditor, sessionProvider and hintList objects are
391- * only guaranteed to be initialized during an active session.
392- *
391+ * only guaranteed to be initialized during an active session.
392+ *
393393 * @param {Editor } editor
394- * @return boolean
394+ * @return boolean
395395 */
396396 function _inSession ( editor ) {
397397 if ( sessionEditor ) {
@@ -433,7 +433,7 @@ define(function (require, exports, module) {
433433 _beginSession ( previousEditor ) ;
434434 } else if ( response . hasOwnProperty ( "hints" ) ) { // a synchronous response
435435 if ( hintList . isOpen ( ) ) {
436- // the session is open
436+ // the session is open
437437 hintList . update ( response ) ;
438438 } else {
439439 hintList . open ( response ) ;
@@ -442,7 +442,7 @@ define(function (require, exports, module) {
442442 deferredHints = response ;
443443 response . done ( function ( hints ) {
444444 if ( hintList . isOpen ( ) ) {
445- // the session is open
445+ // the session is open
446446 hintList . update ( hints ) ;
447447 } else {
448448 hintList . open ( hints ) ;
@@ -453,7 +453,7 @@ define(function (require, exports, module) {
453453 }
454454
455455 /**
456- * Try to begin a new hinting session.
456+ * Try to begin a new hinting session.
457457 * @param {Editor } editor
458458 */
459459 _beginSession = function ( editor ) {
@@ -497,7 +497,7 @@ define(function (require, exports, module) {
497497 } ;
498498
499499 /**
500- * Explicitly start a new session. If we have an existing session,
500+ * Explicitly start a new session. If we have an existing session,
501501 * then close the current one and restart a new one.
502502 * @param {Editor } editor
503503 */
@@ -517,18 +517,19 @@ define(function (require, exports, module) {
517517 }
518518
519519 /**
520- * Handles keys related to displaying, searching, and navigating the hint list.
520+ * Handles keys related to displaying, searching, and navigating the hint list.
521521 * This gets called before handleChange.
522522 *
523523 * TODO: Ideally, we'd get a more semantic event from the editor that told us
524524 * what changed so that we could do all of this logic without looking at
525525 * key events. Then, the purposes of handleKeyEvent and handleChange could be
526526 * combined. Doing this well requires changing CodeMirror.
527527 *
528+ * @param {Event } jqEvent
528529 * @param {Editor } editor
529530 * @param {KeyboardEvent } event
530531 */
531- function handleKeyEvent ( editor , event ) {
532+ function _handleKeyEvent ( jqEvent , editor , event ) {
532533 keyDownEditor = editor ;
533534 if ( event . type === "keydown" ) {
534535 if ( ! ( event . ctrlKey || event . altKey || event . metaKey ) &&
@@ -560,19 +561,23 @@ define(function (require, exports, module) {
560561 }
561562
562563 /**
563- * Start a new implicit hinting session, or update the existing hint list.
564+ * Start a new implicit hinting session, or update the existing hint list.
564565 * Called by the editor after handleKeyEvent, which is responsible for setting
565566 * the lastChar.
567+ *
568+ * @param {Event } event
569+ * @param {Editor } editor
570+ * @param {{from: Pos, to: Pos, text: Array, origin: string} } changeList
566571 */
567- function handleChange ( editor , changeList ) {
572+ function _handleChange ( event , editor , changeList ) {
568573 if ( lastChar && editor === keyDownEditor ) {
569574 keyDownEditor = null ;
570575 if ( _inSession ( editor ) ) {
571576 var charToRetest = lastChar ;
572577 _updateHintList ( ) ;
573578
574- // _updateHintList() may end a hinting session and clear lastChar, but a
575- // different provider may want to start a new session with the same character.
579+ // _updateHintList() may end a hinting session and clear lastChar, but a
580+ // different provider may want to start a new session with the same character.
576581 // So check whether current provider terminates the current hinting
577582 // session. If so, then restore lastChar and restart a new session.
578583 if ( ! _inSession ( editor ) ) {
@@ -618,9 +623,26 @@ define(function (require, exports, module) {
618623 return hintList ;
619624 }
620625
626+ function activeEditorChangeHandler ( event , current , previous ) {
627+ if ( current ) {
628+ $ ( current ) . on ( "editorChange" , _handleChange ) ;
629+ $ ( current ) . on ( "keyEvent" , _handleKeyEvent ) ;
630+ }
631+
632+ if ( previous ) {
633+ //Removing all old Handlers
634+ $ ( previous ) . off ( "editorChange" , _handleChange ) ;
635+ $ ( previous ) . off ( "keyEvent" , _handleKeyEvent ) ;
636+ }
637+ }
638+
639+ activeEditorChangeHandler ( null , EditorManager . getActiveEditor ( ) , null ) ;
640+
641+ $ ( EditorManager ) . on ( "activeEditorChange" , activeEditorChangeHandler ) ;
642+
621643 // Dismiss code hints before executing any command since the command
622- // may make the current hinting session irrevalent after execution.
623- // For example, when the user hits Ctrl+K to open Quick Doc, it is
644+ // may make the current hinting session irrevalent after execution.
645+ // For example, when the user hits Ctrl+K to open Quick Doc, it is
624646 // pointless to keep the hint list since the user wants to view the Quick Doc.
625647 $ ( CommandManager ) . on ( "beforeExecuteCommand" , _endSession ) ;
626648
@@ -631,8 +653,6 @@ define(function (require, exports, module) {
631653
632654 // Define public API
633655 exports . isOpen = isOpen ;
634- exports . handleKeyEvent = handleKeyEvent ;
635- exports . handleChange = handleChange ;
636656 exports . registerHintProvider = registerHintProvider ;
637657 exports . hasValidExclusion = hasValidExclusion ;
638658 exports . setInsertHintOnTab = setInsertHintOnTab ;
0 commit comments