private bool ValidExecution(out IWpfTextView view) { SettingsProvider settingsProvider = new SettingsProvider(); FormatSettingsModel formatSettings = settingsProvider.GetFormatSettingsModel(); view = Vsix.GetDocumentView(mDocument); if (view == null) { return(false); } if (false == FileHasExtension(mDocument.FullName, formatSettings.FileExtensions)) { return(false); } if (true == SkipFile(mDocument.FullName, formatSettings.FilesToIgnore)) { return(false); } if (ScriptConstants.kCMakeConfigFile == mDocument.Name.ToLower()) { return(false); } if (IsFileFormatSelected(formatSettings)) { var filePath = Vsix.GetDocumentParent(view); if (DoesClangFormatFileExist(filePath) == false) { OnFormatFile(new FormatCommandEventArgs() { CanFormat = false }); return(false); } } return(true); }
private void ExecuteFormatCommand() { try { if (ValidExecution(out IWpfTextView view) == false) { return; } var dirPath = string.Empty; var filePath = Vsix.GetDocumentPath(view); var text = view.TextBuffer.CurrentSnapshot.GetText(); var startPosition = 0; var length = text.Length; if (false == view.Selection.StreamSelectionSpan.IsEmpty) { // get the necessary elements for format selection FindStartPositionAndLengthOfSelectedText(view, text, out startPosition, out length); dirPath = Vsix.GetDocumentParent(view); } else { // format the end of the file for format document text = FormatEndOfFile(view, filePath, out dirPath); } string output = RunFormatProcess(dirPath, filePath, text, startPosition, length); ApplyClangFormat(output, view); } catch (Exception exception) { VsShellUtilities.ShowMessageBox(AsyncPackage, exception.Message, "Error while running clang-format", OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST); } }
private void ExecuteFormatCommand() { try { if (ValidExecution(out IWpfTextView view) == false) { return; } var dirPath = string.Empty; var filePath = Vsix.GetDocumentPath(view); var text = view.TextBuffer.CurrentSnapshot.GetText(); var startPosition = 0; var length = text.Length; if (false == view.Selection.StreamSelectionSpan.IsEmpty) { // get the necessary elements for format selection FindStartPositionAndLengthOfSelectedText(view, text, out startPosition, out length); dirPath = Vsix.GetDocumentParent(view); } else { // format the end of the file for format document text = FormatEndOfFile(view, filePath, out dirPath); } var process = CreateProcess(text, startPosition, length, dirPath, filePath); try { process.Start(); } catch (Exception exception) { throw new Exception( $"Cannot execute {process.StartInfo.FileName}.\n{exception.Message}."); } process.StandardInput.Write(text); process.StandardInput.Close(); var output = process.StandardOutput.ReadToEnd(); process.WaitForExit(); if (0 != process.ExitCode) { throw new Exception(process.StandardError.ReadToEnd()); } ApplyClangFormat(output, view); } catch (Exception exception) { VsShellUtilities.ShowMessageBox(AsyncPackage, exception.Message, "Error while running clang-format", OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST); } OnFormatFile(new FormatCommandEventArgs() { CanFormat = true }); }
/// <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 RunClangFormat(object sender, EventArgs e) { try { if (null == mClangFormatView) { FormatAllSelectedDocuments(); return; } var view = Vsix.GetDocumentView(mDocument); if (view == null) { return; } System.Diagnostics.Process process; var dirPath = string.Empty; var filePath = Vsix.GetDocumentPath(view); var text = view.TextBuffer.CurrentSnapshot.GetText(); var startPosition = 0; var length = text.Length; if (false == view.Selection.StreamSelectionSpan.IsEmpty) { // get the necessary elements for format selection FindStartPositionAndLengthOfSelectedText(view, text, out startPosition, out length); dirPath = Vsix.GetDocumentParent(view); mClangFormatView = GetUserOptions(); } else { // format the end of the file for format document text = FormatEndOfFile(view, filePath, out dirPath); } process = CreateProcess(text, startPosition, length, dirPath, filePath, mClangFormatView); try { process.Start(); } catch (Exception exception) { throw new Exception( $"Cannot execute {process.StartInfo.FileName}.\n{exception.Message}."); } process.StandardInput.Write(text); process.StandardInput.Close(); var output = process.StandardOutput.ReadToEnd(); process.WaitForExit(); if (0 != process.ExitCode) { throw new Exception(process.StandardError.ReadToEnd()); } ApplyClangFormat(output, view); } catch (Exception exception) { VsShellUtilities.ShowMessageBox(Package, exception.Message, "Error while running clang-format", OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST); } finally { mDocument = null; mClangFormatView = null; } }
private bool ValidExecution(out IWpfTextView view) { var formatSettings = SettingsProvider.FormatSettingsModel; view = Vsix.GetDocumentView(mDocument); if (view == null) { return(false); } if (IsFileStyleSelected(formatSettings)) { var fileToFormatPath = Vsix.GetDocumentParent(view); if (!FileSystem.SearchAllTopDirectories(fileToFormatPath, FileSystem.ConfigClangFormatFileTypes)) { OnFormatFile(new FormatCommandEventArgs() { Clear = clearOutput, FormatConfigFound = false }); if (clearOutput) { clearOutput = false; } return(false); } } if (FileHasExtension(mDocument.FullName, formatSettings.FileExtensions) == false) { OnFormatFile(new FormatCommandEventArgs() { IgnoreExtension = true, FileName = mDocument.Name, Clear = clearOutput }); if (clearOutput) { clearOutput = false; } return(false); } var ignoreItem = new IgnoreItem(); if (ignoreItem.Check(mDocument)) { OnFormatFile(new FormatCommandEventArgs() { IgnoreFile = true, FileName = mDocument.Name, Clear = clearOutput }); if (clearOutput) { clearOutput = false; } OnIgnoreItem(new ClangCommandMessageEventArgs(ignoreItem.IgnoreFormatMessage, false)); return(false); } if (ScriptConstants.kCMakeConfigFile == mDocument.Name.ToLower()) { return(false); } OnFormatFile(new FormatCommandEventArgs() { Clear = true }); return(true); }
public void RunClangFormat(CommandUILocation commandUILocation) { try { if (mClangFormatView == null) { FormatAllSelectedDocuments(commandUILocation); return; } if (ScriptConstants.kCMakeConfigFile == mDocument.Name.ToLower()) { return; } var view = Vsix.GetDocumentView(mDocument); if (view == null) { return; } StatusBarHandler.Status("Clang-Format started...", 1, vsStatusAnimation.vsStatusAnimationBuild, 1); System.Diagnostics.Process process; var dirPath = string.Empty; var filePath = Vsix.GetDocumentPath(view); var text = view.TextBuffer.CurrentSnapshot.GetText(); var startPosition = 0; var length = text.Length; if (false == view.Selection.StreamSelectionSpan.IsEmpty) { // get the necessary elements for format selection FindStartPositionAndLengthOfSelectedText(view, text, out startPosition, out length); dirPath = Vsix.GetDocumentParent(view); mClangFormatView = SettingsProvider.ClangFormatSettings; } else { // format the end of the file for format document text = FormatEndOfFile(view, filePath, out dirPath); } process = CreateProcess(text, startPosition, length, dirPath, filePath, mClangFormatView); try { process.Start(); } catch (Exception exception) { throw new Exception( $"Cannot execute {process.StartInfo.FileName}.\n{exception.Message}."); } process.StandardInput.Write(text); process.StandardInput.Close(); var output = process.StandardOutput.ReadToEnd(); process.WaitForExit(); if (0 != process.ExitCode) { throw new Exception(process.StandardError.ReadToEnd()); } ApplyClangFormat(output, view); } catch (Exception exception) { VsShellUtilities.ShowMessageBox(AsyncPackage, exception.Message, "Error while running clang-format", OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST); } finally { mDocument = null; mClangFormatView = null; StatusBarHandler.Status("Clang-Format finished", 0, vsStatusAnimation.vsStatusAnimationBuild, 0); } }