private bool ExecuteFormatCore()
        {
            string text = TextView.TextSnapshot.GetText();

            ITextBuffer buffer = TextView.TextBuffer;

            IEditorOptions      editorOptions = Services.EditorOptionsFactory.GetOptions(buffer);
            int                 indentSize    = editorOptions.GetOptionValue <int>(new IndentSize().Key);
            FantomasOptionsPage customOptions = (FantomasOptionsPage)(Package.GetGlobalService(typeof(FantomasOptionsPage)));

            string source = GetAllText(buffer);

            var isSignatureFile = IsSignatureFile(buffer);

            var config = Fantomas.FormatConfig.FormatConfig.create(
                indentSize,
                customOptions.PageWidth,
                customOptions.SemicolonAtEndOfLine,
                customOptions.SpaceBeforeArgument,
                customOptions.SpaceBeforeColon,
                customOptions.SpaceAfterComma,
                customOptions.SpaceAfterSemicolon,
                customOptions.IndentOnTryWith,
                customOptions.ReorderOpenDeclaration,
                customOptions.SpaceAroundDelimiter
                );

            try
            {
                var formatted = GetFormatted(isSignatureFile, source, config);

                using (var edit = buffer.CreateEdit())
                {
                    var setCaretPosition = GetNewCaretPositionSetter();

                    edit.Replace(0, text.Length, formatted);
                    edit.Apply();

                    setCaretPosition();

                    return(true);
                }
            }
            catch (Fantomas.FormatConfig.FormatException ex)
            {
                MessageBox.Show(ex.Message, "Fantomas");
                return(false);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to format. " + ex.Message, "Fantomas");
                return(false);
            }
        }
示例#2
0
        protected void ExecuteFormat()
        {
            string text = TextView.TextSnapshot.GetText();

            ITextBuffer buffer = TextView.TextBuffer;

            IEditorOptions      editorOptions = Services.EditorOptionsFactory.GetOptions(buffer);
            int                 indentSize    = editorOptions.GetOptionValue <int>(new IndentSize().Key);
            FantomasOptionsPage customOptions = (FantomasOptionsPage)(Package.GetGlobalService(typeof(FantomasOptionsPage)));

            string source = GetAllText(buffer);

            var isSignatureFile = IsSignatureFile(buffer);

            var config = new Fantomas.FormatConfig.FormatConfig(
                indentSpaceNum: indentSize,
                pageWidth: customOptions.PageWidth,
                semicolonAtEndOfLine: customOptions.SemicolonAtEndOfLine,
                spaceBeforeArgument: customOptions.SpaceBeforeArgument,
                spaceBeforeColon: customOptions.SpaceBeforeColon,
                spaceAfterComma: customOptions.SpaceAfterComma,
                spaceAfterSemicolon: customOptions.SpaceAfterSemicolon,
                indentOnTryWith: customOptions.IndentOnTryWith
                );

            try
            {
                var formatted = GetFormatted(isSignatureFile, source, config);

                using (var edit = buffer.CreateEdit())
                {
                    var setCaretPosition = GetNewCaretPositionSetter();

                    edit.Replace(0, text.Length, formatted);
                    edit.Apply();

                    // TODO: return cursor to the correct position
                    setCaretPosition();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to format.  " + ex.Message);
                return;
            }
        }