void MainControl_InsertToEndExecuted(object sender, EventArgs e) { var c = sender as InsertText; var macaron = new VisualStudioMacaron(this); bool skip = c.Skip; var text = c.Text; try { macaron.ReplaceSelectionParagraphs(null, (a) => { if (skip && a.Text.EndsWith(text)) { a.IsCanceled = true; } else { a.Text = a.Text + text; } }); } catch (ActiveDocumentIsNullException) { macaron.ShowMessageBox(Resources.InsertTextCaption, Resources.MessageActivateTextEditorForInsertText); } // end try }
public void CreateWorkTextFile(object sender, EventArgs e) { var macaron = new VisualStudioMacaron(this); var file = FileMacro.GetNewWorkTextFile(); FileMacro.CreateFile(file); macaron.Dte.ItemOperations.OpenFile(file.FullName); macaron.Dte.ActiveDocument.Activate(); } // end function
// Overridden Package Implementation #region Package Members /// <summary> /// Initialization of the package; this method is called right after the package is sited, so this is the place where you can put all the initialization code that rely on services provided by VisualStudio. /// </summary> protected override void Initialize() { Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString())); base.Initialize(); this.InitializeCommands(GuidList.guidEditorPlusCmdSet); var macaron = new VisualStudioMacaron(this); macaron.Events.WindowEvents.WindowActivated += WindowEvents_WindowActivated; } // end sub
public void ToLowerCamel(object sender, EventArgs e) { var macaron = new VisualStudioMacaron(this); if (macaron.DocumentIsActive == false) { return; } macaron.ReplaceSelectionWords(null, (a) => { a.Text = TextMacro.CamelWords.GetLowerCamelWord(a.Text); }); } // end sub
public void ToVisualBasicText(object sender, EventArgs e) { var macaron = new VisualStudioMacaron(this); if (macaron.DocumentIsActive == false) { return; } macaron.ReplaceSelectionText(null, (a) => { a.Text = @"""" + a.Text.Replace(@"""", @"""""").Replace("\r\n", @""" & vbCrLf & " + "\r\n" + @"""") + @""""; }); } // end sub
} // end constructor #endregion #region MainControl(TextFormat)イベント処理 /// <summary> /// メインコントロール の TextFormat で、ボタンが押されたときの処理を実行します。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MainControl_Executed(object sender, EventArgs e) { var c = sender as TextFormat; var macaron = new VisualStudioMacaron(this); try { macaron.ReplaceSelectionParagraphs(null, (a) => { // c(ツールウィンドウ)Text をフォーマット、a(エディタ)選択行を値として、フォーマット変換を実行します。 a.Text = TextFormatMacro.Format(c.Text, a.Text); }); } catch (ActiveDocumentIsNullException) { macaron.ShowMessageBox(Resources.TextFormatCaption, Resources.MessageActivateTextEditorForFormatText); } // end try } // end sub
} // end function #endregion #region WindowEvents イベント処理 void WindowEvents_WindowActivated(EnvDTE.Window GotFocus, EnvDTE.Window LostFocus) { var macaron = new VisualStudioMacaron(this); var executable = macaron.DocumentIsActive; var menuCommandIsEnabled = macaron.DocumentIsActiveWindow; if (TextFormatToolWindow.MainControl != null) { TextFormatToolWindow.MainControl.Executable = executable; } if (InsertTextToolWindow.MainControl != null) { InsertTextToolWindow.MainControl.Executable = executable; } if (InsertSerialNumberToolWindow.MainControl != null) { InsertSerialNumberToolWindow.MainControl.Executable = executable; } // InsertCommentOnEndOfFunction MenuCoomand { var m = VisualStudioMacaron.MenuCommands[0x0102]; var enabled = false; if (menuCommandIsEnabled) { var project = macaron.Dte.ActiveDocument.ProjectItem.ContainingProject; enabled = (project != null && project.KnownKind() == ProjectKnownKind.CSharp); }// end if m.Enabled = enabled; } // WriteOutline MenuCommand { var m = VisualStudioMacaron.MenuCommands[0x0103]; m.Enabled = menuCommandIsEnabled; } } // end sub
void MainControl_Executed(object sender, InsertSerialNumberEventArgs e) { if (e.InsertPosition == InsertPosition.None) { return; } var macaron = new VisualStudioMacaron(this); long number = 0; if (long.TryParse(e.StartNumberText, out number) == false) { // TODO: ローカライズ macaron.ShowMessageBox(Resources.InsertSerialNumberCaption, Resources.MessageInputNumber); //this.MainControl.Focus(); return; } var numberLength = 0; long numberCounter = number; try { macaron.ReplaceSelectionParagraphs( (a) => { numberCounter += 1; numberLength = numberCounter.ToString().Length; }, (a) => { var text = number.ToString(); var paddingCount = numberLength - text.Length; if (paddingCount > 0) { switch (e.PaddingKind) { case PaddingKind.Zero: text = new string('0', paddingCount) + text; break; case PaddingKind.Space: text = new string(' ', paddingCount) + text; break; case PaddingKind.None: default: break; } } // end if if (e.Skip && (e.InsertPosition == InsertPosition.HeadOfLine && a.Text.StartsWith(text) || e.InsertPosition == InsertPosition.EndOfLine && a.Text.EndsWith(text)) ) { a.IsCanceled = true; } else { switch (e.InsertPosition) { case InsertPosition.HeadOfLine: a.Text = text + a.Text; break; case InsertPosition.EndOfLine: a.Text = a.Text + text; break; case InsertPosition.None: default: break; } } number += 1; }); } catch (ActiveDocumentIsNullException) { macaron.ShowMessageBox(Resources.InsertSerialNumberCaption, Resources.MessageActivateTextEditorForInsertText); } // end try } // end sub
public void WriteOutline(object sender, EventArgs e) { // 言語毎に異なる内容を調査・定義します。 // ブロック開始行の文字 var startRegionText = string.Empty; // ブロック終了行の文字 var endRegionText = string.Empty; // ブロック開始行のカーソルを合わせる位置 var offsetOfRegionExplanation = 0; // インデントの空白 var indentSpace = string.Empty; var macaron = new VisualStudioMacaron(this); if (macaron.DocumentIsActive == false) { return; } var project = macaron.Dte.ActiveDocument.ProjectItem.ContainingProject; if (project == null) { return; } var activeSelection = macaron.Dte.ActiveDocument.Selection as EnvDTE.TextSelection; var kind = project.KnownKind(); try { var extension = System.IO.Path.GetExtension(macaron.Dte.ActiveDocument.FullName); switch (extension) { case ".ts": kind = ProjectKnownKind.TypeScript; break; case ".js": kind = ProjectKnownKind.JavaScript; break; case ".vb": kind = ProjectKnownKind.VisualBasic; break; case ".cs": kind = ProjectKnownKind.CSharp; break; } } catch { } switch (kind) { case ProjectKnownKind.VisualBasic: startRegionText = "#Region \" \""; endRegionText = "#End Region"; offsetOfRegionExplanation = startRegionText.Length - 2; // Visual Basic はインデントの空白は "" です(常に左端に #Region が記述されます)。 indentSpace = String.Empty; break; case ProjectKnownKind.CSharp: startRegionText = "#region "; endRegionText = "#endregion"; offsetOfRegionExplanation = startRegionText.Length; // C# の時はインデントの空白を取得します(コードのインデントレベルに #region が記述されます)。 { var lineText = activeSelection.Text.Split('\n').FirstOrDefault(); if (string.IsNullOrEmpty(lineText) == false) { indentSpace = lineText.Substring(0, lineText.Length - lineText.TrimStart().Length); } } break; case ProjectKnownKind.JavaScript: case ProjectKnownKind.TypeScript: startRegionText = "// #region "; endRegionText = "// #endregion"; offsetOfRegionExplanation = startRegionText.Length; // TypeScript の時はインデントの空白を取得します(コードのインデントレベルに // #region が記述されます)。 { var lineText = activeSelection.Text.Split('\n').FirstOrDefault(); if (string.IsNullOrEmpty(lineText) == false) { indentSpace = lineText.Substring(0, lineText.Length - lineText.TrimStart().Length); } } break; case ProjectKnownKind.None: case ProjectKnownKind.VisualStudioInstaller: default: return; } // end switch // テキストを挿入します。 activeSelection.Insert( indentSpace + startRegionText + "\r\n" + "\r\n" + activeSelection.Text + "\r\n" + indentSpace + endRegionText + "\r\n" , (int)EnvDTE.vsInsertFlags.vsInsertFlagsContainNewText); //カーソル位置を移動します。 activeSelection.MoveToLineAndOffset(activeSelection.TopPoint.Line, indentSpace.Length + offsetOfRegionExplanation + 1); macaron.Dte.ActiveDocument.Activate(); } // end sub
public void InsertCommentOnEndOfFunction(object sender, EventArgs e) { var macaron = new VisualStudioMacaron(this); // アクティブドキュメントがない場合は処理を抜けます。 if (macaron.DocumentIsActive == false) { return; } // C# 以外の場合はコメントを挿入せずに、処理を抜けます。 var project = macaron.Dte.ActiveDocument.ProjectItem.ContainingProject; if (project == null || project.KnownKind() != ProjectKnownKind.CSharp) { return; } var textSelection = macaron.Dte.ActiveDocument.Selection as EnvDTE.TextSelection; textSelection.StartOfLine(EnvDTE.vsStartOfLineOptions.vsStartOfLineOptionsFirstText); var elements = new EnvDTE.vsCMElement[] { EnvDTE.vsCMElement.vsCMElementFunction, EnvDTE.vsCMElement.vsCMElementProperty, EnvDTE.vsCMElement.vsCMElementClass, EnvDTE.vsCMElement.vsCMElementInterface, EnvDTE.vsCMElement.vsCMElementStruct, EnvDTE.vsCMElement.vsCMElementEnum, EnvDTE.vsCMElement.vsCMElementModule, EnvDTE.vsCMElement.vsCMElementNamespace }; var comments = new string[] { " // end sub", " // end property", " // end class", " // end interface", " // end structure", " // end enum", " // end module", " // end namespace" }; var i = 0; foreach (var element in elements) { try { var codeElement = textSelection.ActivePoint.CodeElement[element]; if (codeElement != null) { textSelection.MoveToPoint(codeElement.EndPoint); textSelection.SelectLine(); if ((i != 0 && textSelection.Text.Contains(comments[i]) == false) || (i == 0 && ( textSelection.Text.Contains(comments[i]) == false && textSelection.Text.Contains(" // end function") == false && textSelection.Text.Contains(" // end constructor") == false )) ) { var isSub = false; var isConstructor = false; if (i == 0) { textSelection.MoveToPoint(codeElement.StartPoint); textSelection.SelectLine(); isSub = (textSelection.Text.Contains("void")); if (isSub == false) { var nameText = codeElement.FullName.Split('.'); try { isConstructor = nameText[nameText.Length - 2].Equals(nameText[nameText.Length - 1]); } catch (IndexOutOfRangeException) { } // end try } // end if } // end if textSelection.MoveToPoint(codeElement.EndPoint); if (i == 0 && isSub == false) { if (isConstructor) { textSelection.Insert(" // end constructor"); } else { textSelection.Insert(" // end function"); } // end if } else { textSelection.Insert(comments[i]); } // end if } else { textSelection.MoveToPoint(codeElement.EndPoint); } // end if break; // exit for } // end if } catch { } // end try i += 1; } // next element } // end sub
public void OpenActiveFileFolder(object sender, EventArgs e) { FileInfo file = null; DirectoryInfo folder = null; var macaron = new VisualStudioMacaron(this); if (macaron.Dte == null) { return; } if (macaron.Dte.ActiveDocument != null) { file = new FileInfo(macaron.Dte.ActiveDocument.FullName); if (file.Exists == false) { file = null; } } // end if var project = macaron.ActiveSolutionProjects.FirstOrDefault(); if (project != null && string.IsNullOrWhiteSpace(project.FullName) == false) { folder = new DirectoryInfo(Path.GetDirectoryName(project.FullName)); if (folder.Exists == false) { folder = null; } switch (project.KnownKind()) { case ProjectKnownKind.VisualStudioInstaller: file = null; break; default: break; } // end switch } // end if if (file == null && folder == null && macaron.Dte.Solution != null && string.IsNullOrWhiteSpace(macaron.Dte.Solution.FullName) == false) { file = new FileInfo(macaron.Dte.Solution.FullName); if (file.Exists == false) { file = null; } } // end if if (file != null) { FileMacro.OpenFolderAndSelectFile(file); } else if (folder != null) { FileMacro.OpenFolder(folder); } else { macaron.ShowMessageBox(Resources.OpenActiveFolderCaption, Resources.MessageCanNotOpenFolder); } // end if } // end function