diff --git a/components/autocomplete/init.js b/components/autocomplete/init.js index ea8c5a3..a08c73a 100755 --- a/components/autocomplete/init.js +++ b/components/autocomplete/init.js @@ -50,20 +50,24 @@ this.addListenerToOnDocumentChange(); - this.updateSuggestions(); + var foundSuggestions = this.updateSuggestions(); - // Show the completion popup. - this.show(); - - // handle click-out autoclosing. - var fn = function () { - _this.hide(); - $(window).off('click', fn); - }; - $(window).on('click', fn); + if (foundSuggestions) { + // Show the completion popup. + this.show(); + console.log($('.suggestion')); + // handle click-out autoclosing. + var fn = function () { + _this.hide(); + $(window).off('click', fn); + }; + $(window).on('click', fn); + } }, + /* Update the suggestions for the word under the cursor. Return true if + * some suitable suggestions could be found, false if not. */ updateSuggestions: function () { var _this = this; @@ -74,23 +78,42 @@ /* Extract the word being typed. It is somehow the prefix of the * wanted full word. Make sure we only keep one word. */ - var prefix = session.getTokenAt(position.row, position.column).value; + var token = session.getTokenAt(position.row, position.column); + if (!token) { + /* Not word at the cursor position. */ + this.removeSuggestions(); + return false; + } + + var prefix = token.value; prefix = prefix.split(this.wordRegex).slice(-1)[0]; + if (prefix === '') { + /* Not word at the cursor position. */ + this.removeSuggestions(); + return false; + } /* Build and order the suggestions themselves. */ // TODO cache suggestions and augment them incrementally. var suggestionsAndDistance = this.getSuggestions(position); var suggestions = this.rankSuggestions(prefix, suggestionsAndDistance); + if (suggestions.length < 1) { + /* No suitable suggestions found. */ + this.removeSuggestions(); + return false; + } /* Remove the existing suggestions and populate the popup with the * updated ones. */ - $('.suggestion').remove(); + this.removeSuggestions(); var popupContent = $('#autocomplete #suggestions'); $.each(suggestions, function (index, suggestion) { popupContent.append('