Exemplo n.º 1
0
        private int GetOffsetFromIndentation(int indentation, IEditorOptions option)
        {
            int numberOfTabs   = 0;
            int numberOfSpaces = Math.Max(0, indentation);

            if (!option.IsConvertTabsToSpacesEnabled())
            {
                var tabSize = option.GetTabSize();

                numberOfTabs    = indentation / tabSize;
                numberOfSpaces -= numberOfTabs * tabSize;
            }

            return(numberOfTabs + numberOfSpaces);
        }
Exemplo n.º 2
0
        static string GetIndentString(int indent, IEditorOptions options)
        {
            if (options.IsConvertTabsToSpacesEnabled())
            {
                return(new string (' ', indent));
            }
            var tabSize   = options.GetTabSize();
            int tabs      = indent / tabSize;
            int spaces    = indent - tabs * tabSize;
            var indentStr = new string ('\t', tabs);

            if (spaces > 0)
            {
                indentStr += new string (' ', spaces);
            }
            return(indentStr);
        }
Exemplo n.º 3
0
        public bool TryGetDocumentOption(Document document, OptionKey option, OptionSet underlyingOptions, out object value)
        {
            // From the document, look up the ITextBuffer, and find its IEditorOptions.
            // Note that IEditorOptions are also available from an ITextView. Not sure if those options can differ from the ones on the buffer.
            var            workspace     = PrimaryWorkspace.Workspace as VisualStudioWorkspace;
            var            textBuffer    = workspace?.GetTextBufferForDocument(document.Id);
            IEditorOptions editorOptions = textBuffer?.Properties[typeof(IEditorOptions)] as IEditorOptions;

            if (editorOptions == null)
            {
                value = null;
                return(false);
            }

            // Check if the OptionKey is one of the ones we want to override with values from the IEditorOptions
            if (option.Option == FormattingOptions.UseTabs)
            {
                value = !editorOptions.IsConvertTabsToSpacesEnabled();
                return(true);
            }
            else if (option.Option == FormattingOptions.TabSize)
            {
                value = editorOptions.GetTabSize();
                return(true);
            }
            else if (option.Option == FormattingOptions.IndentationSize)
            {
                value = editorOptions.GetIndentSize();
                return(true);
            }
            else if (option.Option == FormattingOptions.NewLine)
            {
                value = editorOptions.GetNewLineCharacter();
                return(true);
            }
            else
            {
                value = null;
                return(false);
            }
        }
Exemplo n.º 4
0
        internal static FormattingOptions CreateFormattingOptions(IEditorOptions editorOptions, ITextSnapshot snapshot) {
            FormattingOptions res = new FormattingOptions();
            if (editorOptions.IsConvertTabsToSpacesEnabled()) {
                res.SpacesPerIndent = editorOptions.GetIndentSize();
            } else {
                res.SpacesPerIndent = null;
            }

            res.NewLine = VsExtensions.GetNewLineText(snapshot);

            res.SpaceAfterComma = NodejsPackage.Instance.FormattingSpacingOptionsPage.SpaceAfterComma;
            res.SpaceAfterSemiColonInFor = NodejsPackage.Instance.FormattingSpacingOptionsPage.SpaceAfterSemicolonInFor;
            res.SpaceBeforeAndAfterBinaryOperator = NodejsPackage.Instance.FormattingSpacingOptionsPage.SpaceBeforeAndAfterBinaryOperator;
            res.SpaceAfterKeywordsInControlFlowStatements = NodejsPackage.Instance.FormattingSpacingOptionsPage.SpaceAfterKeywordsInControlFlow;
            res.SpaceAfterFunctionInAnonymousFunctions = NodejsPackage.Instance.FormattingSpacingOptionsPage.SpaceAfterFunctionKeywordForAnonymousFunctions;
            res.SpaceAfterOpeningAndBeforeClosingNonEmptyParenthesis = NodejsPackage.Instance.FormattingSpacingOptionsPage.SpaceAfterOpeningAndBeforeClosingNonEmptyParens;

            res.OpenBracesOnNewLineForFunctions = NodejsPackage.Instance.FormattingBracesOptionsPage.BraceOnNewLineForFunctions;
            res.OpenBracesOnNewLineForControl = NodejsPackage.Instance.FormattingBracesOptionsPage.BraceOnNewLineForControlBlocks;

            return res;
        }
        public override bool Indent()
        {
            bool singleLineSelection = (GetStartPoint().LineNumber == GetEndPoint().LineNumber);
            bool entireLastLineSelected
                = (GetStartPoint().CurrentPosition != GetEndPoint().CurrentPosition&&
                   GetStartPoint().CurrentPosition == TextBuffer.GetEndPoint().StartOfLine&&
                   GetEndPoint().CurrentPosition == TextBuffer.GetEndPoint().EndOfLine);

            if (singleLineSelection && !entireLastLineSelected)
            {
                TextPoint endPoint = GetEndPoint();
                if (!Delete())
                {
                    return(false);
                }
                if (!endPoint.InsertIndent())
                {
                    return(false);
                }
                TextView.AdvancedTextView.Caret.MoveTo(endPoint.AdvancedTextPoint);
            }
            else // indent the selected lines
            {
                VirtualSnapshotPoint oldStartPoint = TextSelection.Start;
                VirtualSnapshotPoint oldEndPoint   = TextSelection.End;
                bool isReversed = TextSelection.IsReversed;

                ITextSnapshotLine startLine = AdvancedTextRange.Snapshot.GetLineFromPosition(oldStartPoint.Position);
                ITextSnapshotLine endLine   = AdvancedTextRange.Snapshot.GetLineFromPosition(oldEndPoint.Position);

                // If the selection span initially starts at the whitespace at the beginning of the line in the startLine or
                // ends at the whitespace at the beginning of the line in the endLine, restore selection and caret position,
                // *unless* the selection was in box mode.
                bool startAtStartLineWhitespace = oldStartPoint.Position <= _textView.GetTextPoint(startLine.Start).GetFirstNonWhiteSpaceCharacterOnLine().CurrentPosition;
                bool endAtEndLineWhitespace     = oldEndPoint.Position < _textView.GetTextPoint(endLine.Start).GetFirstNonWhiteSpaceCharacterOnLine().CurrentPosition;
                bool isBoxSelection             = AdvancedSelection.Mode == TextSelectionMode.Box;

                if (isBoxSelection)
                {
                    if (!this.BoxIndent())
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!TextRange.Indent())
                    {
                        return(false);
                    }
                }

                // Computing the new selection and caret position
                VirtualSnapshotPoint newStartPoint = TextSelection.Start;
                VirtualSnapshotPoint newEndPoint   = TextSelection.End;

                if (!isBoxSelection && (startAtStartLineWhitespace || endAtEndLineWhitespace))
                {
                    // After indent selection span should start at the start of startLine and end at the start of endLine
                    if (startAtStartLineWhitespace)
                    {
                        newStartPoint = new VirtualSnapshotPoint(AdvancedTextRange.Snapshot, oldStartPoint.Position.Position);
                    }

                    if (endAtEndLineWhitespace && oldEndPoint.Position.Position != endLine.Start && endLine.Length != 0)
                    {
                        int insertedTextSize = _editorOptions.IsConvertTabsToSpacesEnabled() ? _editorOptions.GetTabSize() : 1;
                        newEndPoint = new VirtualSnapshotPoint(AdvancedTextRange.Snapshot, newEndPoint.Position.Position - insertedTextSize);
                    }

                    if (!isReversed)
                    {
                        TextSelection.Select(newStartPoint, newEndPoint);
                    }
                    else
                    {
                        TextSelection.Select(newEndPoint, newStartPoint);
                    }

                    TextView.AdvancedTextView.Caret.MoveTo(TextSelection.ActivePoint, PositionAffinity.Successor);
                }
            }
            TextView.AdvancedTextView.Caret.EnsureVisible();
            return(true);
        }