locked
How to highlight one line (isolated mode) RRS feed

  • Question

  • Hello everybody,

    I have managed to build my own editor classifier, which color keywords, comments, etc. But I’m stuck with the apparently simple task of highlighting one single line from another package.

    I would like to move the caret to a concrete line and highlight it. I can get the open document by DTE object, but I don’t find any useful method in this object. I don’t see the way to implement this in the editor classifier either.

    I would appreciate your advice.

    Pedro.


    Tuesday, May 19, 2015 3:11 PM

Answers

  • Well, the code below does the job for me. Next I need to scroll the screen to center the line, but that will be easy to find, I guess.

    // Get WPF text view.
    IComponentModel componentModel = Package.GetGlobalService(typeof(SComponentModel)) as IComponentModel;
    IVsEditorAdaptersFactoryService adaptersFactory = componentModel.GetService<IVsEditorAdaptersFactoryService>();
    IVsTextManager textManager = (IVsTextManager)Package.GetGlobalService(typeof(SVsTextManager));
    IVsTextView textView;
    textManager.GetActiveView(1, null, out textView);
    IWpfTextView wpfView = adaptersFactory.GetWpfTextView(textView);
    
    // Set line and column and move caret.
    ITextSnapshotLine line = wpfView.TextSnapshot.GetLineFromLineNumber(LineNumber - 1);
    SnapshotPoint point = new SnapshotPoint(line.Snapshot, line.Start.Position);
    wpfView.Caret.MoveTo(point);
    

    Finally I haven't highlighted the line because the "normal" highlight, that is, the soft gray rectangle around the caret line, is enough for me.

    Pedro.

    • Marked as answer by pintope Tuesday, May 19, 2015 6:00 PM
    • Edited by pintope Tuesday, May 19, 2015 6:01 PM
    Tuesday, May 19, 2015 6:00 PM

All replies

  • Well, the code below does the job for me. Next I need to scroll the screen to center the line, but that will be easy to find, I guess.

    // Get WPF text view.
    IComponentModel componentModel = Package.GetGlobalService(typeof(SComponentModel)) as IComponentModel;
    IVsEditorAdaptersFactoryService adaptersFactory = componentModel.GetService<IVsEditorAdaptersFactoryService>();
    IVsTextManager textManager = (IVsTextManager)Package.GetGlobalService(typeof(SVsTextManager));
    IVsTextView textView;
    textManager.GetActiveView(1, null, out textView);
    IWpfTextView wpfView = adaptersFactory.GetWpfTextView(textView);
    
    // Set line and column and move caret.
    ITextSnapshotLine line = wpfView.TextSnapshot.GetLineFromLineNumber(LineNumber - 1);
    SnapshotPoint point = new SnapshotPoint(line.Snapshot, line.Start.Position);
    wpfView.Caret.MoveTo(point);
    

    Finally I haven't highlighted the line because the "normal" highlight, that is, the soft gray rectangle around the caret line, is enough for me.

    Pedro.

    • Marked as answer by pintope Tuesday, May 19, 2015 6:00 PM
    • Edited by pintope Tuesday, May 19, 2015 6:01 PM
    Tuesday, May 19, 2015 6:00 PM
  • Hi Pedro,

    Thank you for sharing the answer here. That'll help those who have similar problems.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 20, 2015 6:33 AM
  • You are welcome. And this is the code for scrolling:

    // Center the line in the view.
    point = (SnapshotPoint)wpfView.Caret.Position.Point.GetPoint(wpfView.TextSnapshot.TextBuffer, PositionAffinity.Successor);
    if (point != null)
    {
        SnapshotSpan span = new SnapshotSpan(point, 1);
        wpfView.ViewScroller.EnsureSpanVisible(span);
    }

    Pedro.

    Thursday, May 21, 2015 7:35 AM