Example #1
0
        public static void FormatCurrentScope(IEditorView editorView, IEditorBuffer textBuffer, IServiceContainer services, bool indentCaret)
        {
            // Figure out caret position in the document text buffer
            var document = textBuffer.GetEditorDocument <IREditorDocument>();

            if (document == null)
            {
                return;
            }
            var caretPoint = editorView.GetCaretPosition(textBuffer);

            if (caretPoint == null)
            {
                return;
            }

            // Make sure AST is up to date
            document.EditorTree.EnsureTreeReady();
            var ast = document.EditorTree.AstRoot;
            // Find scope to format
            var scope = ast.GetNodeOfTypeFromPosition <IScope>(caretPoint.Position);

            var es = services.GetService <IEditorSupport>();

            using (var undoAction = es.CreateUndoAction(editorView)) {
                var settings = services.GetService <IREditorSettings>();
                undoAction.Open(Resources.AutoFormat);
                // Now format the scope
                var formatter = new RangeFormatter(services);
                var changed   = formatter.FormatRange(editorView, textBuffer, scope);
                if (indentCaret)
                {
                    // Formatting may change AST and the caret position so we need to reacquire both
                    caretPoint = editorView.GetCaretPosition(textBuffer);
                    if (caretPoint != null)
                    {
                        document.EditorTree.EnsureTreeReady();
                        ast   = document.EditorTree.AstRoot;
                        scope = ast.GetNodeOfTypeFromPosition <IScope>(caretPoint.Position);
                        IndentCaretInNewScope(editorView, scope, caretPoint, settings.FormatOptions);
                    }
                }
                if (changed)
                {
                    undoAction.Commit();
                }
            }
        }
Example #2
0
        /// <summary>
        /// Formats statement that the caret is at
        /// </summary>
        public void FormatCurrentStatement(bool limitAtCaret = false, int caretOffset = 0)
        {
            var caretPoint = _editorView.GetCaretPosition(_editorBuffer);

            if (caretPoint == null)
            {
                return;
            }
            var document = _editorBuffer.GetEditorDocument <IREditorDocument>();

            if (document != null)
            {
                var ast  = document.EditorTree.AstRoot;
                var node = ast.GetNodeOfTypeFromPosition <IStatement>(Math.Max(0, caretPoint.Position + caretOffset)) as IAstNode;
                FormatNode(node, limit: caretPoint.Position);
            }
        }
        public static bool IsCaretInNamespace(this IEditorView view, IEditorBuffer editorBuffer = null)
        {
            editorBuffer = editorBuffer ?? view.EditorBuffer;
            var bufferPosition = view.GetCaretPosition(editorBuffer);

            if (bufferPosition != null)
            {
                return(bufferPosition.Snapshot.IsPositionInNamespace(bufferPosition.Position));
            }
            return(false);
        }
Example #4
0
        /// <summary>
        /// Formats line the caret is currently at
        /// </summary>
        public static void FormatViewLine(IEditorView editorView, IEditorBuffer textBuffer, int offset, IServiceContainer services)
        {
            var caretPoint = editorView.GetCaretPosition(textBuffer);

            if (caretPoint == null)
            {
                return;
            }

            var snapshot    = textBuffer.CurrentSnapshot;
            var lineNumber  = snapshot.GetLineNumberFromPosition(caretPoint.Position);
            var line        = snapshot.GetLineFromLineNumber(lineNumber + offset);
            var formatRange = new TextRange(line.Start, line.Length);

            UndoableFormatRange(editorView, textBuffer, formatRange, services);
        }
Example #5
0
        /// <summary>
        /// Formats statement that the caret is at
        /// </summary>
        public static void FormatCurrentStatement(IEditorView editorView, IEditorBuffer textBuffer, IServiceContainer services, bool limitAtCaret = false, int caretOffset = 0)
        {
            var caretPoint = editorView.GetCaretPosition(textBuffer);

            if (caretPoint == null)
            {
                return;
            }
            var document = textBuffer.GetEditorDocument <IREditorDocument>();

            if (document != null)
            {
                var ast  = document.EditorTree.AstRoot;
                var node = ast.GetNodeOfTypeFromPosition <IStatement>(Math.Max(0, caretPoint.Position + caretOffset)) as IAstNode;
                FormatNode(editorView, textBuffer, services, node, limit: caretPoint.Position);
            }
        }
        public static bool IsCaretInLibraryStatement(this IEditorView view, IEditorBuffer editorBuffer = null)
        {
            try {
                editorBuffer = editorBuffer ?? view.EditorBuffer;
                var bufferPosition = view.GetCaretPosition(editorBuffer);
                if (bufferPosition != null)
                {
                    var snapshot      = bufferPosition.Snapshot;
                    int caretPosition = bufferPosition.Position;
                    var line          = snapshot.GetLineFromPosition(caretPosition);

                    if (line.Length < 8 || caretPosition < line.Start + 8 || snapshot[caretPosition - 1] != '(')
                    {
                        return(false);
                    }

                    int start = -1;
                    int end   = -1;

                    for (int i = caretPosition - 2; i >= 0; i--)
                    {
                        if (!char.IsWhiteSpace(snapshot[i]))
                        {
                            end = i + 1;
                            break;
                        }
                    }

                    if (end <= 0)
                    {
                        return(false);
                    }

                    for (int i = end - 1; i >= 0; i--)
                    {
                        if (char.IsWhiteSpace(snapshot[i]))
                        {
                            start = i + 1;
                            break;
                        }
                        else if (i == 0)
                        {
                            start = 0;
                            break;
                        }
                    }

                    if (start < 0 || end <= start)
                    {
                        return(false);
                    }

                    start -= line.Start;
                    end   -= line.Start;

                    string s = line.GetText().Substring(start, end - start);
                    if (s == "library" || s == "require")
                    {
                        return(true);
                    }
                }
            } catch (ArgumentException) { }

            return(false);
        }