Exemple #1
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));
            }
        }
        public static KeyValuePair <string, string>?PromptDialog(string resFile, string defaultValue)
        {
            using (var esf = new ExtractStringCmdForm(resFile, defaultValue))
            {
                if (esf.ShowDialog() != DialogResult.OK)
                {
                    return(null);
                }

                return(new KeyValuePair <string, string>(esf.name, esf.value));
            }
        }