ReplaceKeywordWrapped() public static method

Replaces the left part of the keyword found at (CurrentPosition + offset) by the keyword given (all wrapped in an undo action + handles multiselection)
public static ReplaceKeywordWrapped ( string keyword, int offset ) : void
keyword string
offset int offset relative to the current carret position
return void
Exemplo n.º 1
0
Arquivo: Plug.cs Projeto: devjerome/3P
        /// <summary>
        /// Called when the user has finished entering a word
        /// Called after the UI has updated, allows to correctly read the text style, to correct
        /// the indentation w/o it being erased and so on...
        /// </summary>
        /// <param name="c"></param>
        public static void OnCharAddedWordEnd(char c)
        {
            try {
                // we finished entering a keyword
                var curPos = Npp.CurrentPosition;
                int offset;
                if (c == '\n')
                {
                    offset  = curPos - Npp.GetLine().Position;
                    offset += (Npp.GetTextOnLeftOfPos(curPos - offset, 2).Equals("\r\n")) ? 2 : 1;
                }
                else
                {
                    offset = 1;
                }
                var searchWordAt    = curPos - offset;
                var keyword         = Npp.GetKeyword(searchWordAt);
                var isNormalContext = Style.IsCarretInNormalContext(searchWordAt);

                if (!String.IsNullOrWhiteSpace(keyword) && isNormalContext)
                {
                    string replacementWord = null;

                    // automatically insert selected keyword of the completion list
                    if (Config.Instance.AutoCompleteInsertSelectedSuggestionOnWordEnd && keyword.ContainsAtLeastOneLetter())
                    {
                        if (AutoComplete.IsVisible)
                        {
                            var lastSugg = AutoComplete.GetCurrentSuggestion();
                            if (lastSugg != null)
                            {
                                replacementWord = lastSugg.DisplayText;
                            }
                        }
                    }

                    // replace abbreviation by completekeyword
                    if (Config.Instance.CodeReplaceAbbreviations)
                    {
                        var fullKeyword = Keywords.GetFullKeyword(replacementWord ?? keyword);
                        if (fullKeyword != null)
                        {
                            replacementWord = fullKeyword;
                        }
                    }

                    // replace the last keyword by the correct case
                    var casedKeyword = AutoComplete.CorrectKeywordCase(replacementWord ?? keyword, searchWordAt);
                    if (casedKeyword != null)
                    {
                        replacementWord = casedKeyword;
                    }

                    if (replacementWord != null)
                    {
                        Npp.ReplaceKeywordWrapped(replacementWord, -offset);
                    }
                }


                // replace semicolon by a point
                if (c == ';' && Config.Instance.CodeReplaceSemicolon && isNormalContext)
                {
                    Npp.ModifyTextAroundCaret(-1, 0, ".");
                }

                // handles the autocompletion
                AutoComplete.UpdateAutocompletion();
            } catch (Exception e) {
                ErrorHandler.ShowErrors(e, "Error in OnCharAddedWordEnd");
            }
        }