Exemplo n.º 1
0
        protected override string GetFormatted(bool isSignatureFile, string source, Fantomas.FormatConfig.FormatConfig config)
        {
            Range.pos   startPos = TextUtils.GetFSharpPos(TextView.Selection.Start);
            Range.pos   endPos   = TextUtils.GetFSharpPos(TextView.Selection.End);
            Range.range range    = Range.mkRange("fsfile", startPos, endPos);

            return(Fantomas.CodeFormatter.formatSelectionFromString(isSignatureFile, range, source, config));
        }
        protected override string GetFormatted(bool isSignatureFile, string source, Fantomas.FormatConfig.FormatConfig config)
        {
            // This still seems to give "The indent level cannot be negative"
            // in a lot of cases that feel like they should work, e.g. 'let' forms

            Range.pos   startPos = TextUtils.GetFSharpPos(TextView.Selection.Start);
            Range.pos   endPos   = TextUtils.GetFSharpPos(TextView.Selection.End);
            Range.range range    = Range.mkRange("fsfile", startPos, endPos);

            return(Fantomas.CodeFormatter.formatSelectionFromString(isSignatureFile, range, source, config));
        }
        protected override string GetFormatted(bool isSignatureFile, string source, Fantomas.FormatConfig.FormatConfig config)
        {
            if (isFormattingCursor)
            {
                var       caretPos = new VirtualSnapshotPoint(TextView.TextBuffer.CurrentSnapshot, TextView.Caret.Position.BufferPosition);
                Range.pos pos      = TextUtils.GetFSharpPos(caretPos);
                return(Fantomas.CodeFormatter.formatAroundCursor(isSignatureFile, pos, source, config));
            }

            Range.pos   startPos = TextUtils.GetFSharpPos(TextView.Selection.Start);
            Range.pos   endPos   = TextUtils.GetFSharpPos(TextView.Selection.End);
            Range.range range    = Range.mkRange("fsfile", startPos, endPos);

            return(Fantomas.CodeFormatter.formatSelectionFromString(isSignatureFile, range, source, config));
        }
Exemplo n.º 4
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;
            }
        }
Exemplo n.º 5
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;
            }
        }
        public override void Execute()
        {
            string text = TextView.TextSnapshot.GetText();

            ITextBuffer buffer = TextView.TextBuffer;
            //ITextSnapshot snapshot = buffer.CurrentSnapshot;
            //IEditorOptions editorOptions = _controller._provider.EditorOptionsFactory.GetOptions(buffer);
            //int tabSize = editorOptions.GetOptionValue<int>(new TabSize().Key);
            //int indentSize = editorOptions.GetOptionValue<int>(new IndentSize().Key);

            string source;

            using (var writer = new StringWriter())
            {
                buffer.CurrentSnapshot.Write(writer);
                writer.Flush();
                source = writer.ToString();
            }

            var def = Fantomas.FormatConfig.FormatConfig.Default;
            var config = new Fantomas.FormatConfig.FormatConfig(
                def.IndentSpaceNum,
                130,
                def.SemicolonAtEndOfLine,
                def.SpaceBeforeArgument,
                false, //def.SpaceBeforeColon,
                def.SpaceAfterComma,
                def.SpaceAfterSemicolon,
                def.IndentOnTryWith);

            var formatted = Fantomas.CodeFormatter.formatSourceString(false /* TODO: handle FSI */, source, config);

            using (var edit = buffer.CreateEdit())
            {
                edit.Replace(0, source.Length, formatted);
                edit.Apply();
            }
        }
Exemplo n.º 7
0
 protected abstract string GetFormatted(bool isSignatureFile, string source, Fantomas.FormatConfig.FormatConfig config);
Exemplo n.º 8
0
 protected override string GetFormatted(bool isSignatureFile, string source, Fantomas.FormatConfig.FormatConfig config)
 {
     return(Fantomas.CodeFormatter.formatSourceString(isSignatureFile, source, config));
 }