/// <summary> /// this method should be called at the plugin's start and when we change the current database /// It refreshed the "static" items of the autocompletion : keywords, snippets, databases, tables, sequences /// </summary> public static void RefreshStaticItems() { if (_itemsListLock.TryEnterWriteLock(-1)) { try { _staticItems.Clear(); _staticItems = Keywords.GetList().ToList(); _staticItems.AddRange(Snippets.Keys.Select(x => new CompletionItem { DisplayText = x, Type = CompletionType.Snippet, Ranking = 0, FromParser = false, Flag = 0 }).ToList()); _staticItems.AddRange(DataBase.GetDbList()); _staticItems.AddRange(DataBase.GetSequencesList()); _staticItems.AddRange(DataBase.GetTablesList()); // we do the sorting (by type and then by ranking), doing it now will reduce the time for the next sort() _staticItems.Sort(new CompletionDataSortingClass()); } finally { _itemsListLock.ExitWriteLock(); } } // update parser? if (_initialized) { ParserHandler.ParseCurrentDocument(); } _initialized = true; // OnUpdatedStaticItems if (OnUpdatedStaticItems != null) { OnUpdatedStaticItems(); } // refresh the list of all the saved items (static + dynamic) RefreshDynamicItems(); }
/// <summary> /// Updates the CURRENT ITEMS LIST, /// handles the opening or the closing of the autocompletion form on key input, /// it is only called when the user adds or delete a char /// </summary> public static void UpdateAutocompletion() { if (!Config.Instance.AutoCompleteOnKeyInputShowSuggestions && !_openedFromShortCut) { return; } // dont show in string/comments..? if (!_openedFromShortCut && !IsVisible && !Config.Instance.AutoCompleteShowInCommentsAndStrings && !Style.IsCarretInNormalContext(Npp.CurrentPosition)) { return; } // get current word, current previous word (table or database name) int nbPoints; string previousWord = ""; var strOnLeft = Npp.GetTextOnLeftOfPos(Npp.CurrentPosition); var keyword = Abl.ReadAblWord(strOnLeft, false, out nbPoints); var splitted = keyword.Split('.'); string lastCharBeforeWord = ""; switch (nbPoints) { case 0: int startPos = strOnLeft.Length - 1 - keyword.Length; lastCharBeforeWord = startPos >= 0 ? strOnLeft.Substring(startPos, 1) : String.Empty; break; case 1: previousWord = splitted[0]; keyword = splitted[1]; break; case 2: previousWord = splitted[1]; keyword = splitted[2]; break; default: keyword = splitted[nbPoints]; break; } // list of fields or tables if (!string.IsNullOrEmpty(previousWord)) { // are we entering a field from a known table? var foundTable = ParserHandler.FindAnyTableOrBufferByName(previousWord); if (foundTable != null) { if (CurrentTypeOfList != TypeOfList.Fields) { CurrentTypeOfList = TypeOfList.Fields; SetCurrentItems(DataBase.GetFieldsList(foundTable).ToList()); } ShowSuggestionList(keyword); return; } // are we entering a table from a connected database? var foundDatabase = DataBase.FindDatabaseByName(previousWord); if (foundDatabase != null) { if (CurrentTypeOfList != TypeOfList.Tables) { CurrentTypeOfList = TypeOfList.Tables; SetCurrentItems(DataBase.GetTablesList(foundDatabase).ToList()); } ShowSuggestionList(keyword); return; } } // close if there is nothing to suggest if ((!_openedFromShortCut || _openedFromShortCutPosition != Npp.CurrentPosition) && (String.IsNullOrEmpty(keyword) || keyword != null && keyword.Length < Config.Instance.AutoCompleteStartShowingListAfterXChar)) { Close(); return; } // if the current is directly preceded by a :, we are entering an object field/method if (lastCharBeforeWord.Equals(":")) { if (CurrentTypeOfList != TypeOfList.KeywordObject) { CurrentTypeOfList = TypeOfList.KeywordObject; SetCurrentItems(SavedAllItems); } ShowSuggestionList(keyword); return; } // show normal complete list if (CurrentTypeOfList != TypeOfList.Complete) { CurrentTypeOfList = TypeOfList.Complete; SetCurrentItems(SavedAllItems); } ShowSuggestionList(keyword); }