Ejemplo n.º 1
0
        // return true if it the ial of a string were found
        bool searchRange()
        {
            var tv = VsUtils.GetActiveTextView();

            if (tv == null)
            {
                return(false);
            }

            // scan the current text for strings
            var ranges = Scanner.ScanVs(VsUtils.GetTextForTextView(tv));

            // find the positions
            int caretLine, caretCol;

            tv.GetCaretPos(out caretLine, out caretCol);

            foreach (var range in ranges)
            {
                if (range.ContainsPos(caretLine, caretCol))
                { // if this range was found set it and notify we found it
                    csRange = range;
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
        // executed when the menu command is clicked
        void MenuItemCallback(object sender, EventArgs e)
        {
            if (!csRange.HasValue)
            {
                return;
            }

            var range = csRange.Value; // get the text in the range
            var text  = VsUtils.GetTextForTextView(VsUtils.GetActiveTextView(),
                                                   range.topLine, range.topCol, range.bottomLine, range.bottomCol);

            // default values resource file
            var resFile = Resourcer.GetResourcePath(Resourcer.GetStringsResName());

            string normalized      = Strings.Normalize(text);
            bool   resourceExisted = StringsXMLEditor.ContainsValue(resFile, normalized);

            var result = resourceExisted ?
                         // if a key with this value already exists, use it
                         new KeyValuePair <string, string>(StringsXMLEditor.GetKeyByValue(resFile, normalized), normalized) :
                         // else, prompt a new dialog asking for a key
                         ExtractStringCmdForm.PromptDialog(resFile, normalized);

            // if the result has value, replace the string with the method call
            if (result.HasValue) // replace the old string with the new method call
            {
                ((TextDocument)VsUtils.GetDTE().ActiveDocument.Object())
                .ReplacePattern(text, Resourcer.AddString(
                                    result.Value,
                                    VsUtils.GetActiveDocumentLanguage() == LANGUAGE_XAML,
                                    resourceExisted));
            }
        }