Extending VS2005

Hi all,

I have a custom tool pane to aid in the documenting of our source code, but i have no idea how to read the line where the cursor is at in the editor, i need this to see what Methode or type i have selected..

Could anyone point me in the right direction

Greetings


Answer this question

Extending VS2005

  • mancer

    Thanks this was exactly what i was looking for, i am going to try it out!
    If i have any questions ill be sure to let you know!

    Greetings

  • Mehdi_Becario

    Dylan,

    I was aware of that and i am infact using the VSIP package!

    I am awaiting your response Big Smile

    Greetings,
    Paul

  • Sreenivas Kairi

    Hmm, looks as though the colorization isn't right.  It looks like lexical items that are not colored specially are getting the previous "special" color. 

  • angstela

    Well i finally figured it out. If anyone was wondering how to do this:

    -> using Microsoft.VisualStudio.TextManager.Interop;

    IVsTextManager textManager = (IVsTextManager)GetService(typeof(SVsTextManager));
    IVsTextView activeView;
    int hr = textManager.GetActiveView(1, null, out activeView);

    Greetings,
    Paul


  • Fabiods

    Paul,

    What are you using to extend Visual Studio   Macros, Add-ins, or creating a VSIP package

    Only if you are creating a VSIP package will you be able to use the last option.

    Thanks,

    Dylan

  • SavitaHK

    Sounds like what I am after.  However when I search google or other for IVsIntellisenseBuilder interface I get no results.  In fact I don't even get results just searching for IntellisenseBuilder or Intellisense Builder that make any sense.
    Can you please elaborate

  • StringMon

    CoderJames,

    Check out the IVsIntellisenseBuilder interface.  That way you can have a picker always stay visible, and then pop up a file picking box or something similar in the OnCommit method.

    Thanks,

    Dylan

    Note: IVsIntellisenseBuilder is actually called IVsCompletionSetBuilder.

  • Bart.NET

    Paul,

    How are you extending Visual Studio   Through a macro, an Add-in or the VSIP interfaces   There are different ways to do this depending on which you are using.  I'll lay out how to do this each way so you can pick the solution that is right for you.

    Macros:



    Dim sel As TextSelection = DTE.ActiveDocument.Selection
    Dim oldAnchorPoint As EditPoint = sel.AnchorPoint.CreateEditPoint()
    Dim oldActivePoint As EditPoint = sel.ActivePoint.CreateEditPoint()
    Dim oldMode As vsSelectionMode = sel.Mode

    sel.SelectLine()

    Dim lineText As String = sel.Text()

    sel.Mode = oldMode
    sel.MoveToPoint(oldAnchorPoint,
    False) ' False = Don't extend selection
    sel.MoveToPoint(oldActivePoint,
    True) ' True = Extend selection

     


    If you don't care about restoring the selection, you could just keep the active point around, and then just do this:



    sel.MoveToPoint(oldActivePoint, False)

     


    Addins (This is similar, but I wanted to give an example in C# as well):



    TextSelection sel = _applicationObject.ActiveDocument.Selection as TextSelection;
    EditPoint oldActivePoint = sel.ActivePoint.CreateEditPoint();
    EditPoint oldAnchorPoint = sel.AnchorPoint.CreateEditPoint();

    sel.SelectLine();

    string lineText = sel.Text;

    sel.MoveToPoint(oldAnchorPoint,
    false); // false = Don't extend
    sel.MoveToPoint(oldActivePoint, true); // true = Extend selection to recreate it

     


    Note that I am assuming _applicationObject is your DTE2 instance variable.  If you don't care about restoring the selection, you can do as I mentioned above, and only restore the active point.

    VSIP interfaces:

    I am going to assume here that you can get a pointer to the IVsTextView interface.  Please respond back if you need help getting a pointer to that interface.

    In unmanaged C++:



    CComPtr<IVsTextView> view;

    // Get the view

    long iLine = 0;
    ViewCol iColumn = 0;

    if (SUCCEEDED(view->GetCaretPos(&iLine, &iColumn))
    {
       CComPtr<IVsTextLines> buffer;
       
    if (SUCCEEDED(view->GetBuffer(&buffer) && buffer)
       {
          CComBSTR lineText;
          
          // Note, this could cause problems if iLine is the last line in the buffer.
          
    if (SUCCEEDED(buffer->GetLineText(iLine, 0 /* start index */, iLine + 1, 0 /* end index */, &lineText)))
          {
             // Use the line text
          }
       }
    }

     


    As I mentioned in the comments above, you could potentially get this wrong if you are on the last line of the file.  To do it correctly, you would need to QueryInterface for IVsTextBuffer and get the length of the line you are on.  I am going to leave that example out for brevity here, please let me know if you need it.  The colorization looks wrong because I don't think the forums handle C++ code well.

    In C# using the managed PIA's and again assuming you are able to get an IVsTextView object:



    public string GetCurrentLineText(IVsTextView view)
    {
       
    int line = 0;
       
    int column = 0;
       
       if (Microsoft.VisualStudio.ErrorHandler.Succeeded(view.GetCaretPos(out line, out column)))
       {
          
    IVsTextLines buffer;
          
    if (Microsoft.VisualStudio.ErrorHandler.Succeeded(view.GetBuffer(out buffer)))
          {
             
    IVsTextBuffer textBuffer = buffer as IVsTextBuffer;
             
    if (textBuffer != null)
             {
                
    int lineLength = 0;
                
    if (Microsoft.VisualStudio.ErrorHandler.Succeeded(textBuffer.GetLengthOfLine(line, out lineLength)))
                {
                   
    string lineText = "";
                   
    if (Microsoft.VisualStudio.ErrorHandler.Succeeded(buffer.GetLineText(line, 0, line, lineLength, out lineText)))
                   
    {
                      return
    lineText;
                   }
                }
             }
          }
       }
    }

     


    Feel free to let me know if this doesn't work for you.

    Thanks,

    Dylan



  • Martin Susil

    but how can we link IVsCompletionSetBuilder to the IVsTextView

  • Joey Lau

    That is the correct way to do it.  You may want to check the return value of hr here by using the Microsoft.VisualStudio.ErrorHandler.Succeeded method.  That way you will know if you should use the activeView object or not.  You can see how to use it in my above example.

    Thanks,

    Dylan



  • antimatty

    That is what i did just a minute after posting here! Thanks for all your help!
  • ZM

    Hi Dylan,
     
    I have no problems getting your first examples to run, thanks for that, but.. in the C# example where you where assuming i would be able to get the IVsTextView object, i am sorry to say that i am not able to get the IVsTextView object, Could you point me in the right direction on how to do that once more

    Greetings,
    Paul

  • melack

    Well that makes sense because I put the interface name down wrong.  Try searching for IVsCompletionSetBuilder.  That will teach me to just toss out an interface name without actually verifying it is the correct one :-). 

    Sorry for the confusion,

    Dylan



  • Ramana Gali

    I think I am trying to do about the same thing as you two...  But I am a few steps behind.  My basic goal is to respond to a developer working in the tool.  In html view actually.  In the exact same manor as the FILE picker critter.  Can you help me understand how to get the behavior of the lil File picker image . that shows up when you type in file="    [...]".  You click it, you find your file, you return the filepath to the IDE / Code Editor.

    Thanks, -CoderJames

  • Extending VS2005