/// <summary> /// This function is the callback used to execute the command when the menu item is clicked. /// See the constructor to see how the menu item is associated with this function using /// OleMenuCommandService service and MenuCommand class. /// </summary> /// <param name="sender">Event sender.</param> /// <param name="e">Event args.</param> private void MenuItemCallback(object sender, EventArgs e) { var dte = (DTE2)ServiceProvider.GetService(typeof(DTE)); if (dte.ActiveDocument != null) { var selection = (TextSelection)dte.ActiveDocument.Selection; //Select hold line if not select text if (string.IsNullOrEmpty(selection.Text)) { selection.SelectLine(); } //Trim selected text var parser = CommentParserHelper.GetCommentParser(dte.ActiveDocument.Language); var text = selection.Text.Trim(); if (parser != null) { var regions = parser.GetCommentRegions(text, 0); if (regions.Count() > 0 && regions.First().Start == 0 && regions.Last().End == text.Length) { text = string.Join(Environment.NewLine, regions.Select(r => parser.GetComment(text.Substring(r.Start, r.Length)).Content.Trim())); } } //Check if selection text is still empty if (!string.IsNullOrEmpty(text)) { TranslatePopupConnector.Translate(GetWpfView(), text); } } }
/// <summary> /// This function is the callback used to execute the command when the menu item is clicked. /// See the constructor to see how the menu item is associated with this function using /// OleMenuCommandService service and MenuCommand class. /// </summary> /// <param name="sender">Event sender.</param> /// <param name="e">Event args.</param> private void MenuItemCallback(object sender, EventArgs e) { ThreadHelper.ThrowIfNotOnUIThread(); Settings settings = new Settings(); //获取配置值,赋值验证TKK settings.ReloadSetting((OptionPageGrid)package.GetDialogPage(typeof(OptionPageGrid))); if (string.IsNullOrWhiteSpace(settings.TKK)) { ShowMessageBox("请先设置TKK值"); return; } //获取DTE对象 var dte = (DTE2)ServiceProvider.GetService(typeof(DTE)); // EnvDTE80.DTE2 _dte2 = (EnvDTE80.DTE2)ServiceProvider.GlobalProvider.GetService(typeof(EnvDTE.DTE)); if (dte.ActiveDocument != null) { var selection = (TextSelection)dte.ActiveDocument.Selection; //如果不选择文本,自动选择光标所在行 if (string.IsNullOrEmpty(selection.Text)) { selection.SelectLine(); } //修剪所选文本 var parser = CommentParserHelper.GetCommentParser(dte.ActiveDocument.Language); var text = selection.Text.Trim(); if (parser != null) { var regions = parser.GetCommentRegions(text, 0); if (regions.Count() > 0 && regions.First().Start == 0 && regions.Last().End == text.Length) { text = string.Join(Environment.NewLine, regions.Select(r => parser.GetComment(text.Substring(r.Start, r.Length)).Content.Trim())); } } //Check if selection text is still empty if (!string.IsNullOrEmpty(text)) { TranslatePopupConnector.Translate(GetWpfView(), text); } } }