Exemplo n.º 1
0
        public override TextSpan UncommentSpan(TextSpan span)
        {
            CommentInfo commentInfo = GetCommentFormat();

            using (new CompoundAction(this, "Uncomment this selection"))
            {
                // special case: empty span
                if (TextSpanHelper.IsEmpty(span))
                {
                    if (commentInfo.UseLineComments)
                    {
                        span = UncommentLines(span, commentInfo.LineStart);
                    }
                    return(span);
                }

                string textblock = GetText(span).Trim();

                if (!string.IsNullOrEmpty(commentInfo.BlockStart) &&
                    !string.IsNullOrEmpty(commentInfo.BlockEnd) &&
                    textblock.Length >= commentInfo.BlockStart.Length + commentInfo.BlockEnd.Length &&
                    textblock.StartsWith(commentInfo.BlockStart) &&
                    textblock.EndsWith(commentInfo.BlockEnd))
                {
                    TrimSpan(ref span);
                    span = UncommentBlock(span, commentInfo.BlockStart, commentInfo.BlockEnd);
                }
                else if (commentInfo.UseLineComments && !string.IsNullOrEmpty(commentInfo.LineStart))
                {
                    span = UncommentLines(span, commentInfo.LineStart);
                }
            }
            return(span);
        }
Exemplo n.º 2
0
        private void ApplyEdits()
        {
            var currentNode = _tokenEdits.First;

            while (currentNode != null)
            {
                if (TextSpanHelper.Intersects(_formattingSpan, currentNode.Value.TextSpan))
                {
                    if (currentNode.Value.EolType != EolType.Unknown)
                    {
                        switch (currentNode.Value.EolType)
                        {
                        case EolType.Eol:
                            currentNode.Value.EditSpan = new EditSpan(currentNode.Value.TextSpan, _eolText);
                            _indent = 0;
                            break;

                        case EolType.EolIndent:
                            currentNode.Value.EditSpan = new EditSpan(currentNode.Value.TextSpan, _eolText + IndentString());
                            break;

                        case EolType.EolIndentPlus:
                            _indent++;
                            currentNode.Value.EditSpan = new EditSpan(currentNode.Value.TextSpan, _eolText + IndentString());
                            break;

                        case EolType.EolIndentMinus:
                            _indent--;
                            Debug.Assert(_indent >= 0);
                            if (_indent < 0)
                            {
                                _indent = 0;
                            }
                            currentNode.Value.EditSpan = new EditSpan(currentNode.Value.TextSpan, _eolText + IndentString());
                            break;

                        case EolType.EolHardIndent1:
                            _indent = 1;
                            currentNode.Value.EditSpan = new EditSpan(currentNode.Value.TextSpan, _eolText + IndentString());
                            break;

                        case EolType.Unknown:
                        default:
                            Debug.Fail("Unreashable code");
                            break;
                        }
                    }

                    if (currentNode.Value.EditSpan != null)
                    {
                        _editManager.Add(currentNode.Value.EditSpan);
                    }
                }
                currentNode = currentNode.Next;
            }
        }
Exemplo n.º 3
0
        private void GoToLocation(RawSourceSpan loc, string caption, bool asReadonly)
        {
            // Code taken from Nemerle https://github.com/rsdn/nemerle/blob/master/snippets/VS2010/Nemerle.VisualStudio/LanguageService/NemerleLanguageService.cs#L565
            // TODO: Add licensing
            if (loc == null || loc.File == null)
            {
                return;
            }

            // Opens the document
            var span = new TextSpan {
                iStartLine = loc.Line - 1, iStartIndex = loc.Column - 1, iEndLine = loc.EndLine - 1, iEndIndex = loc.EndColumn - 1
            };
            uint           itemID;
            IVsUIHierarchy hierarchy;
            IVsWindowFrame docFrame;
            IVsTextView    textView;

            VsShell.OpenDocument(Site, loc.File, VSConstants.LOGVIEWID_Code, out hierarchy, out itemID, out docFrame, out textView);

            // If we need readonly, set the buffer to read-only
            if (asReadonly)
            {
                IVsTextLines buffer;
                ErrorHandler.ThrowOnFailure(textView.GetBuffer(out buffer));
                var stream = (IVsTextStream)buffer;
                stream.SetStateFlags((uint)BUFFERSTATEFLAGS.BSF_USER_READONLY);
            }

            // Need to use a different caption?
            if (caption != null)
            {
                ErrorHandler.ThrowOnFailure(docFrame.SetProperty((int)__VSFPROPID.VSFPROPID_OwnerCaption, caption));
            }

            // Show the frame
            ErrorHandler.ThrowOnFailure(docFrame.Show());

            // Go to the specific location
            if (textView != null && loc.Line != 0)
            {
                try
                {
                    ErrorHandler.ThrowOnFailure(textView.SetCaretPos(span.iStartLine, span.iStartIndex));
                    TextSpanHelper.MakePositive(ref span);
                    //ErrorHandler.ThrowOnFailure(textView.SetSelection(span.iStartLine, span.iStartIndex, span.iEndLine, span.iEndIndex));
                    ErrorHandler.ThrowOnFailure(textView.EnsureSpanVisible(span));
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                }
            }
        }
Exemplo n.º 4
0
        public override void GetPairExtents(IVsTextView view, int line, int col, out TextSpan span)
        {
            var spanAry = GetMatchingBraces(false, line, col);

            if (spanAry.Length == 2)
            {
                span = TextSpanHelper.ContainsInclusive(spanAry[0], line, col) ? spanAry[1] : spanAry[0];
            }
            else
            {
                span = new TextSpan();
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Handles the Goto-command.
        /// We have to do that synchronous as the default async-operation SOMETIMES doesn't work due to internal error (Win32Exception: "Invalid window handle")
        /// </summary>
        /// <param name="cmd">The command id.</param>
        public override void HandleGoto(VsCommands cmd)
        {
            //we handle only th goto-definition command
            if (cmd != VsCommands.GotoDefn)
            {
                base.HandleGoto(cmd);
                return;
            }

            int line, col;

            // Get the caret position
            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(TextView.GetCaretPos(out line, out col));

            //parse the source synchronous
            AuthoringScope scope = Source.LanguageService.ParseSource(new ParseRequest(line, col, new TokenInfo(), Source.GetText(), Source.GetFilePath(), ParseReason.Goto, TextView, Source.CreateAuthoringSink(ParseReason.Goto, line, col), true));

            //navigate to the found position

            string   url = null;
            TextSpan span;

            if (scope != null)
            {
                url = scope.Goto(cmd, TextView, line, col, out span);
            }
            else
            {
                return;
            }
            if (url == null || url.Trim().Length == 0)   // nothing to show
            {
                return;
            }

            // Open the referenced document, and scroll to the given location.
            IVsUIHierarchy hierarchy;
            uint           itemID;
            IVsWindowFrame frame;
            IVsTextView    view;

            Microsoft.VisualStudio.Shell.VsShellUtilities.OpenDocument(base.Source.LanguageService.Site, url, VSConstants.LOGVIEWID_Code, out hierarchy, out itemID, out frame, out view);
            if (view != null)
            {
                TextSpanHelper.MakePositive(ref span);
                Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(view.EnsureSpanVisible(span));
                Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(view.SetSelection(span.iStartLine, span.iStartIndex, span.iEndLine, span.iEndIndex));
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Match paired tokens. Run in GUI thread synchronously!
        /// </summary>
        /// <param name="view">Current view</param>
        /// <param name="line">zero based index of line</param>
        /// <param name="index">zero based index of char</param>
        public bool HighlightBraces(IVsTextView view, int line, int index)
        {
            try {
                var spanAry = GetMatchingBraces(false, line, index);
                if (spanAry.Length == 2 && TextSpanHelper.ValidSpan(this, spanAry[0]) && TextSpanHelper.ValidSpan(this, spanAry[1]))
                {
                    // No check result!
                    view.HighlightMatchingBrace((uint)Service.Preferences.HighlightMatchingBraceFlags, (uint)spanAry.Length, spanAry);
                    return(true);
                }

                return(false);
            } finally {
            }
        }
Exemplo n.º 7
0
        public override TextSpan CommentSpan(TextSpan span)
        {
            TextSpan    result      = span;
            CommentInfo commentInfo = GetCommentFormat();

            using (new CompoundAction(this, "Comment this selection"))
            {
                /*
                 * Use line comments if:
                 *  UseLineComments is true
                 *  AND LineStart is not null or empty
                 *  AND one of the following is true:
                 *
                 *  1. there is no selected text
                 *  2. on the line where the selection starts, there is only whitespace up to the selection start point
                 *     AND on the line where the selection ends, there is only whitespace up to the selection end point,
                 *         OR there is only whitespace from the selection end point to the end of the line
                 *
                 * Use block comments if:
                 *  We are not using line comments
                 *  AND some text is selected
                 *  AND BlockStart is not null or empty
                 *  AND BlockEnd is not null or empty
                 */
                if (commentInfo.UseLineComments &&
                    !string.IsNullOrEmpty(commentInfo.LineStart) &&
                    (TextSpanHelper.IsEmpty(span) ||
                     ((GetText(span.iStartLine, 0, span.iStartLine, span.iStartIndex).Trim().Length == 0) &&
                      ((GetText(span.iEndLine, 0, span.iEndLine, span.iEndIndex).Trim().Length == 0) ||
                       (GetText(span.iEndLine, span.iEndIndex, span.iEndLine, GetLineLength(span.iEndLine)).Trim().Length == 0))
                     )))
                {
                    result = CommentLines(span, commentInfo.LineStart);
                }
                else if (
                    TextSpanHelper.IsPositive(span) &&
                    !string.IsNullOrEmpty(commentInfo.BlockStart) &&
                    !string.IsNullOrEmpty(commentInfo.BlockEnd)
                    )
                {
                    result = CommentBlock(span, commentInfo.BlockStart, commentInfo.BlockEnd);
                }
            }
            return(result);
        }
Exemplo n.º 8
0
        public override TextSpan UncommentBlock(TextSpan span, string blockStart, string blockEnd)
        {
            int startLen = GetLineLength(span.iStartLine);
            int endLen   = GetLineLength(span.iEndLine);

            TextSpan result = span;

            //sp. case no selection, try and uncomment the current line.
            if (span.iStartIndex == span.iEndIndex &&
                span.iStartLine == span.iEndLine)
            {
                span.iStartIndex = ScanToNonWhitespaceChar(span.iStartLine);
                span.iEndIndex   = GetLineLength(span.iEndLine);
            }

            // Check that comment start and end blocks are possible.
            if (span.iStartIndex + blockStart.Length <= startLen && span.iEndIndex - blockStart.Length >= 0)
            {
                string startText = GetText(span.iStartLine, span.iStartIndex, span.iStartLine, span.iStartIndex + blockStart.Length);

                if (startText == blockStart)
                {
                    string   endText  = null;
                    TextSpan linespan = span;
                    linespan.iStartLine  = linespan.iEndLine;
                    linespan.iStartIndex = linespan.iEndIndex - blockEnd.Length;
                    System.Diagnostics.Debug.Assert(TextSpanHelper.IsPositive(linespan));
                    endText = GetText(linespan);
                    if (endText == blockEnd)
                    {
                        //yes, block comment selected; remove it
                        SetText(linespan.iStartLine, linespan.iStartIndex, linespan.iEndLine, linespan.iEndIndex, null);
                        SetText(span.iStartLine, span.iStartIndex, span.iStartLine, span.iStartIndex + blockStart.Length, null);
                        span.iEndIndex -= blockEnd.Length;
                        if (span.iStartLine == span.iEndLine)
                        {
                            span.iEndIndex -= blockStart.Length;
                        }
                        result = span;
                    }
                }
            }

            return(result);
        }
Exemplo n.º 9
0
        /// <summary>This method checks to see if the IVsDebugger is running, and if so,
        /// calls it to get additional information about the current token and returns a combined result.
        /// You can return an HRESULT here like TipSuccesses2.TIP_S_NODEFAULTTIP.</summary>
        public override int GetFullDataTipText(string textValue, TextSpan ts, out string fullTipText)
        {
            IVsTextLines textLines;

            fullTipText = textValue;

            ErrorHandler.ThrowOnFailure(this.TextView.GetBuffer(out textLines));

            // Now, check if the debugger is running and has anything to offer
            try
            {
                Microsoft.VisualStudio.Shell.Interop.IVsDebugger debugger = Source.LanguageService.GetIVsDebugger();

                if (debugger != null && Source.LanguageService.IsDebugging)
                {
                    var tsdeb = new TextSpan[1] {
                        new TextSpan()
                    };
                    if (!TextSpanHelper.IsEmpty(ts))
                    {
                        // While debugging we always want to evaluate the expression user is hovering over
                        ErrorHandler.ThrowOnFailure(TextView.GetWordExtent(ts.iStartLine, ts.iStartIndex, (uint)WORDEXTFLAGS.WORDEXT_FINDEXPRESSION, tsdeb));
                        // If it failed to find something, then it means their is no expression so return S_FALSE
                        if (TextSpanHelper.IsEmpty(tsdeb[0]))
                        {
                            return(NativeMethods.S_FALSE);
                        }
                    }
                    string debugTextTip = null;
                    int    hr           = debugger.GetDataTipValue(textLines, tsdeb, null, out debugTextTip);
                    fullTipText = debugTextTip;
                    if (hr == (int)TipSuccesses2.TIP_S_NODEFAULTTIP)
                    {
                        return(hr);
                    }
                    if (!string.IsNullOrEmpty(debugTextTip) && debugTextTip != textValue)
                    {
                        // The debugger in this case returns "=value [type]" which we can
                        // append to the variable name so we get "x=value[type]" as the full tip.
                        int i = debugTextTip.IndexOf('=');
                        if (i >= 0)
                        {
                            string spacer = (i < debugTextTip.Length - 1 && debugTextTip[i + 1] == ' ') ? " " : "";
                            fullTipText = textValue + spacer + debugTextTip.Substring(i);
                        }
                    }
                }
#if LANGTRACE
            } catch (COMException e) {
                Trace.WriteLine("COMException: GetDataTipValue, errorcode=" + e.ErrorCode);
#else
            }
            catch (System.Runtime.InteropServices.COMException)
            {
#endif
            }

            if (string.IsNullOrEmpty(fullTipText))
            {
                fullTipText = textValue;
            }

            return(NativeMethods.S_OK);
        }
Exemplo n.º 10
0
        public void GotoLocation(Location loc, string caption, bool asReadonly)
        {
            //TODO: VladD2: Разобраться почему этот код вызывает вылет
            //IVsUIShell uiShell = this.GetService(typeof(SVsUIShell)) as IVsUIShell;
            //if (uiShell != null)
            //{
            //  IVsWindowFrame frame;
            //  string data;
            //  object unknown;
            //  ErrorHandler.ThrowOnFailure(uiShell.GetCurrentBFNavigationItem(out frame, out data, out unknown));
            //  ErrorHandler.ThrowOnFailure(uiShell.AddNewBFNavigationItem(frame, data, unknown, 0));
            //}

            TextSpan span = new TextSpan();

            span.iStartLine  = loc.Line - 1;
            span.iStartIndex = loc.Column - 1;
            span.iEndLine    = loc.EndLine - 1;
            span.iEndIndex   = loc.EndColumn - 1;

            uint           itemID;
            IVsUIHierarchy hierarchy;
            IVsWindowFrame docFrame;
            IVsTextView    textView;

            if (loc.FileIndex == 0)
            {
                return;
            }

            VsShell.OpenDocument(Site, loc.File, VSConstants.LOGVIEWID_Code,
                                 out hierarchy, out itemID, out docFrame, out textView);

            if (asReadonly)
            {
                IVsTextLines buffer;
                ErrorHandler.ThrowOnFailure(textView.GetBuffer(out buffer));
                IVsTextStream stream = (IVsTextStream)buffer;
                stream.SetStateFlags((uint)BUFFERSTATEFLAGS.BSF_USER_READONLY);
            }

            if (caption != null)
            {
                ErrorHandler.ThrowOnFailure(docFrame.SetProperty((int)__VSFPROPID.VSFPROPID_OwnerCaption, caption));
            }

            ErrorHandler.ThrowOnFailure(docFrame.Show());

            if (textView != null && loc.Line != 0)
            {
                try
                {
                    ErrorHandler.ThrowOnFailure(textView.SetCaretPos(span.iStartLine, span.iStartIndex));
                    TextSpanHelper.MakePositive(ref span);
                    ErrorHandler.ThrowOnFailure(textView.SetSelection(span.iStartLine, span.iStartIndex, span.iEndLine, span.iEndIndex));
                    ErrorHandler.ThrowOnFailure(textView.EnsureSpanVisible(span));
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                }
            }
        }
Exemplo n.º 11
0
        //
        // Summary:
        //     Called to fill and synchronize all combo boxes.
        //
        // Parameters:
        //   languageService:
        //     [in] A Microsoft.VisualStudio.Package.LanguageService object representing
        //     the language service that uses the combo boxes.
        //
        //   textView:
        //     [in] An Microsoft.VisualStudio.TextManager.Interop.IVsTextView object representing
        //     the view the combo boxes are placed in and the view that shows the source
        //     file.
        //
        //   line:
        //     [in] The line number the caret is currently on.
        //
        //   col:
        //     [in] The character offset the caret is currently on.
        //
        //   dropDownTypes:
        //     [in, out] An System.Collections.ArrayList of Microsoft.VisualStudio.Package.DropDownMembers
        //     representing the types combo box.
        //
        //   dropDownMembers:
        //     [in, out] An System.Collections.ArrayList of Microsoft.VisualStudio.Package.DropDownMembers
        //     representing the members combo box.
        //
        //   selectedType:
        //     [in, out] The index of the entry to be selected in the types combo box. This
        //     can also be set if the current selection is invalid.
        //
        //   selectedMember:
        //     [in, out] The index of the entry to be selected in the members combo box.
        //     This can also be set if the current selection is invalid.
        //
        // Returns:
        //     If successful, returns true if the combo boxes have been changed; otherwise
        //     returns false.
        public override bool OnSynchronizeDropdowns(LanguageService languageService, IVsTextView textView, int line, int col, ArrayList dropDownTypes, ArrayList dropDownMembers, ref int selectedType, ref int selectedMember)
        {
            Source source = languageService.GetSource(textView);

            if (source == null)
            {
                return(false);
            }

            LibraryNode            filenode;
            SquirrelLibraryManager libraryManager = languageService.Site.GetService(typeof(ISquirrelLibraryManager)) as SquirrelLibraryManager;
            string currentfile = source.GetFilePath();

            lock (libraryManager.Library)
            {
                libraryManager.Library.FileNodes.TryGetValue(currentfile, out filenode);
            }

            if (previousfile != currentfile)
            {
                scopelist.Clear();
                dropDownTypes.Clear();
                PopulateTypeList(filenode, ref dropDownTypes);
                dropDownTypes.Sort();
                previousfile = currentfile;
            }

            DropDownMember scope = (from DropDownMember item in scopelist
                                    where TextSpanHelper.ContainsInclusive(item.Span, line, col)
                                    orderby(item.Span.iStartLine << 16) + item.Span.iStartIndex
                                    select item).LastOrDefault();

            string currenttype = "";

            if (scope != null)
            {
                bool found = false;
                foreach (DropDownMember type in dropDownTypes)
                {
                    if (scope.Label == type.Label)
                    {
                        selectedType = dropDownTypes.IndexOf(type);
                        currenttype  = type.Label;
                        found        = true;
                        break;
                    }
                }

                if (!found)
                {
                    int    lastidx = scope.Label.LastIndexOf('.');
                    string typeLabel;
                    if (lastidx != -1)
                    {
                        typeLabel = scope.Label.Substring(0, lastidx);
                        foreach (DropDownMember type in dropDownTypes)
                        {
                            if (typeLabel == type.Label)
                            {
                                selectedType = dropDownTypes.IndexOf(type);
                                currenttype  = type.Label;
                                break;
                            }
                        }
                    }
                }

                if (previoustype != currenttype)
                {
                    dropDownMembers.Clear();
                    LibraryNode merge = new LibraryNode("merge");
                    MergeFileNode(filenode, ref merge);
                    RemoveGlobalNode(ref merge);
                    LibraryNode typenode = null;
                    GetTypeNode(currenttype, merge, ref typenode);
                    PopulateMemberList(typenode, ref dropDownMembers);
                    dropDownMembers.Sort();
                    previoustype = currenttype;
                }

                selectedMember = -1;
                foreach (DropDownMember member in dropDownMembers)
                {
                    if (scope.Label.Split('.').Last() == member.Label)
                    {
                        selectedMember = dropDownMembers.IndexOf(member);
                        break;
                    }
                }
            }

            return(true);
        }