예제 #1
0
        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);
            }
        }
    public Image statusIconPrefab; // prefab to instantiate

    // Use this for initialization
    void Awake()
    {
        instance = this;
    }
예제 #3
0
 public HttpManager(LogHandler logMessage, StatusBarHandler ShowStatusBar)
 {
     this.logMessage    = logMessage;
     this.ShowStatusBar = ShowStatusBar;
 }