Add code in specific point with Roslyn - c#

Is there a way to add a code in a specific point with Roslyn (also without roslyn)? This is my problem: I develop a vspackage that add a command in the context menu (in CODE WINDOW). When I right click and I select this command it should add some code in that point.
How can I resolve this problem?

You have to:
receive the current text window of visual studio
get the position in the textbuffer (clicking the right mouse button will set the caret position)
insert your text
First things first; receive the text view:
public static IWpfTextView GetCurrentTextView(Package package)
{
try
{
var serviceProvider = package as IServiceProvider;
IVsTextManager textManager = (IVsTextManager)serviceProvider.GetService(typeof(SVsTextManager));
IVsTextView textView;
textManager.GetActiveView(1, null, out textView);
IComponentModel componentModel = (IComponentModel)serviceProvider.GetService(typeof(SComponentModel));
var factoryService = componentModel.GetService<IVsEditorAdaptersFactoryService>();
return factoryService.GetWpfTextView(textView);
}
catch
{
return null;
}
}
Get the caret position from that and insert your text:
IWpfTextView textView = GetCurrentTextView(package);
SnapshotPoint caretPosition = textView.Caret.Position.BufferPosition;
textView.TextBuffer.Insert(caretPosition, "HELLO WORLD");
Don't forget to add error handling.

Related

VSIX: IErrorTag tooltip content not displaying

I am trying to write a code analysis extension for visual studio using MEF. I have implemented the ITagger interface for an IErrorTag along with the required ITaggerProvider. As a result, i get the expected squiggles in the editor window for the issues my code analysis finds. However, when hovering above the squiggles with the mouse, the respective tooltip content is never displayed.
Here is a minimalistic example which has the same problem:
using Microsoft.VisualStudio.Text.Adornments;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Tagging;
using System;
using System.Collections.Generic;
namespace CodeAnalyzer
{
struct DummyIssue
{
public int Line; // one based line
public string ToolTip;
public DummyIssue(int line, string toolTip)
{
Line = line;
ToolTip = toolTip;
}
}
internal class DummyCodeCheckTagger : ITagger<IErrorTag>
{
readonly List<DummyIssue> mIssues;
readonly ITextView TextView;
public DummyCodeCheckTagger(ITextView textView)
{
TextView = textView;
mIssues = new List<DummyIssue>
{
new DummyIssue(1, "asldfjoqwet"),
new DummyIssue(7, "ASASDAER")
};
textView.LayoutChanged += Update;
}
public event EventHandler<SnapshotSpanEventArgs> TagsChanged;
private void Update(object sender, TextViewLayoutChangedEventArgs args)
{
TagsChanged?.Invoke(this, new SnapshotSpanEventArgs(new SnapshotSpan(args.NewSnapshot, 0, args.NewSnapshot.Length)));
}
IEnumerable<ITagSpan<IErrorTag>> ITagger<IErrorTag>.GetTags(NormalizedSnapshotSpanCollection spans)
{
var issues = mIssues;
foreach (var span in spans)
{
foreach (var issue in issues)
{
int zeroBasedLine = issue.Line - 1;
ITextSnapshotLine snapshotLine = TextView.TextSnapshot.GetLineFromLineNumber(zeroBasedLine);
SnapshotSpan snapshotSpan = snapshotLine.Extent;
if (spans.IntersectsWith(snapshotSpan))
{
yield return new TagSpan<IErrorTag>(snapshotSpan, new ErrorTag(PredefinedErrorTypeNames.SyntaxError, issue.ToolTip));
}
}
}
}
}
}
The result looks like this:
tooltip not displaying
What am i missing to get the tooltip displayed?
Fater's comment above led me to think about the problem again. Since i already tried the suggestions in the document posted by fater without success, i started thinking if the problem could be somewhere else.
It turns out that the ITagger implementation was not the problem, but the ITaggerProvider implementation caused the strange behavior. For that,
I pretty much followed the VSIX ErrorList example implementing a SpellChecker, which contains the following code
/// <summary>
/// Create a tagger that does spell checking on the view/buffer combination.
/// </summary>
public ITagger<T> CreateTagger<T>(ITextView textView, ITextBuffer buffer) where T : ITag
{
ITagger<T> tagger = null;
// Only attempt to spell check on the view's edit buffer (and multiple views could have that buffer open simultaneously so
// only create one instance of the spell checker.
if ((buffer == textView.TextBuffer) && (typeof(T) == typeof(IErrorTag)))
{
var spellChecker = buffer.Properties.GetOrCreateSingletonProperty(typeof(SpellChecker), () => new SpellChecker(this, textView, buffer));
// This is a thin wrapper around the SpellChecker that can be disposed of without shutting down the SpellChecker
// (unless it was the last tagger on the spell checker).
tagger = new SpellCheckerTagger(spellChecker) as ITagger<T>;
}
return tagger;
}
The point is, that the code above only creates an ITagger for a certain view. In that case the created tagger is used only for providing the squiggles in the editor window view. Visual Studio uses a different tagger instance for providing the tooltips for the squiggles and another tagger instance for coloring the scroll bar in the editor window. I had assumed that this would be done by one single tagger instance.

How can I get the caret position from a textbox in another application? (Not the coordinates, but the actual index inside of the textbox)

I need to retrieve the index of the caret inside a textbox in the focused window, maybe using UI Automation, or maybe a Win32 API function, if there's any function that cat do that.
And I emphasize, I don't mean the x,y coordinates, but the index of the caret inside the text of the textbox. How can I do that?
Also see this similar question.
You can use UI Automation for that, and especially the IUIAutomationTextPattern2 interface that has a GetCaretRange method.
Here are two sample Console app (C++ and C# code) that run continuously and display the caret position for the current element under the mouse:
C++ version
int main()
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);
{
CComPtr<IUIAutomation> automation;
// make sure you use CLSID_CUIAutomation8, *not* CLSID_CUIAutomation
automation.CoCreateInstance(CLSID_CUIAutomation8);
do
{
POINT pt;
if (GetCursorPos(&pt))
{
CComPtr<IUIAutomationElement> element;
automation->ElementFromPoint(pt, &element);
if (element)
{
CComBSTR name;
element->get_CurrentName(&name);
wprintf(L"Watched element %s\n", name);
CComPtr<IUIAutomationTextPattern2> text;
element->GetCurrentPatternAs(UIA_TextPattern2Id, IID_PPV_ARGS(&text));
if (text)
{
// get document range
CComPtr<IUIAutomationTextRange> documentRange;
text->get_DocumentRange(&documentRange);
// get caret range
BOOL active = FALSE;
CComPtr<IUIAutomationTextRange> range;
text->GetCaretRange(&active, &range);
if (range)
{
// compare caret start with document start
int caretPos = 0;
range->CompareEndpoints(TextPatternRangeEndpoint_Start, documentRange, TextPatternRangeEndpoint_Start, &caretPos);
wprintf(L" caret is at %i\n", caretPos);
}
}
}
}
Sleep(500);
} while (TRUE);
}
CoUninitialize();
return 0;
}
C# version
static void Main(string[] args)
{
// needs 'using UIAutomationClient;'
// to reference UIA, don't use the .NET assembly
// but instead, reference the UIAutomationClient dll as a COM object
// and set Embed Interop Types to False for the UIAutomationClient reference in the C# project
var automation = new CUIAutomation8();
do
{
var cursor = System.Windows.Forms.Cursor.Position;
var element = automation.ElementFromPoint(new tagPOINT { x = cursor.X, y = cursor.Y });
if (element != null)
{
Console.WriteLine("Watched element " + element.CurrentName);
var guid = typeof(IUIAutomationTextPattern2).GUID;
var ptr = element.GetCurrentPatternAs(UIA_PatternIds.UIA_TextPattern2Id, ref guid);
if (ptr != IntPtr.Zero)
{
var pattern = (IUIAutomationTextPattern2)Marshal.GetObjectForIUnknown(ptr);
if (pattern != null)
{
var documentRange = pattern.DocumentRange;
var caretRange = pattern.GetCaretRange(out _);
if (caretRange != null)
{
var caretPos = caretRange.CompareEndpoints(
TextPatternRangeEndpoint.TextPatternRangeEndpoint_Start,
documentRange,
TextPatternRangeEndpoint.TextPatternRangeEndpoint_Start);
Console.WriteLine(" caret is at " + caretPos);
}
}
}
}
Thread.Sleep(500);
}
while (true);
}
The trick is to use the IUIAutomationTextRange::CompareEndpoints method that allows you to compare the caret range with another range, for example the whole document range.
Note there are drawbacks:
some apps don't support any MSAA/UIA introspection at all, or don't support the text pattern. For these, there are simply no solution (even using Windows API I think)
some apps report the caret incorrectly, especially when you select text (so, with a moving caret). For example with Notepad, moving LEFT while pressing SHIFT will select backwards but for some reason UIA doesn't update caret pos. I think it's a problem with Notepad because it also has caret issue with IME (Input Method Editor, like the Emoji editor you can summon using Win+; keyboard combination) which also uses the global caret position. There's no problem with Wordpad for example.

Visual Studio - inserting multi-line expressions into Watch Window while debugging

While debugging in Visual Studio, how can I insert multi-line expressions into Watch Window, so that each line is not broken into a separate INVALID watch expression. This is really frustrating because I have many expressions spanning multiple lines that I need to watch. Note that both Pin to Source and Immediate Window do not work for tracking multiple values from many places in source code.
e.g.
PyFunc1(Py.kw("var1", var1),
Py.kw("var2", var2))
gets broken to:
PyFunc1(Py.kw("var1", var1),
and
Py.kw("var2", var2))
Repro
I dont think this is "By-Design", its just unavailable "out-of-the-box".
I agree, it'd be better behaviour for multi-line calls to be added to the Watch Window using line terminators instead of new lines:
Research
I found this similar question with a few "workarounds" to choose from:
Multi-Line Watch Window in Visual Studio 2010?
I also found this comment in the MSDN Forums by a MSFT Engineer:
I’m afraid that it is not supported, we often edit them one by one. Maybe you could submit this feature request: http://visualstudio.uservoice.com/forums/121579-visual-studio
Roll your own Visual Studio Add-In
So I had a go at it myself, this is by no means production code but it shows you how to do it:
(click image to enlarge)
namespace AddinMultiLineWatch
{
public class Connect : IDTExtensibility2, IDTCommandTarget
{
//ADD THESE MEMBER VARIABLES
//private DebuggerEvents _debuggerEvents = null;
//private _dispDebuggerEvents_OnEnterBreakModeEventHandler DebuggerEvents_OnEnterBreakMode;
private Window _watchWindow = null;
private CommandEvents _objCommandEvents;
private bool _isRecursive = false;
public Connect()
{
}
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
//SET THE MEMBER VARIABLES
//_debuggerEvents = _applicationObject.Events.DebuggerEvents;
//_debuggerEvents.OnEnterBreakMode += new _dispDebuggerEvents_OnEnterBreakModeEventHandler(BreakHandler);
//var watchWindow = _applicationObject.Windows.Item(EnvDTE.Constants.vsWindowKindWatch);
_objCommandEvents = _applicationObject.Events.CommandEvents;
_objCommandEvents.BeforeExecute += new _dispCommandEvents_BeforeExecuteEventHandler(BeforeExecute);
if(connectMode == ext_ConnectMode.ext_cm_UISetup)
{
object []contextGUIDS = new object[] { };
Commands2 commands = (Commands2)_applicationObject.Commands;
string toolsMenuName = "Tools";
Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];
ar:
CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;
try
{
Command command = commands.AddNamedCommand2(_addInInstance, "AddinMultiLineWatch", "AddinMultiLineWatch", "Executes the command for AddinMultiLineWatch", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
if((command != null) && (toolsPopup != null))
{
command.AddControl(toolsPopup.CommandBar, 1);
}
}
catch(System.ArgumentException)
{
}
}
}
//ADD THIS METHOD TO INTERCEPT THE DEBUG.ADDWATCH COMMAND
public void BeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
{
EnvDTE.Command objCommand = default(EnvDTE.Command);
try
{
objCommand = _applicationObject.Commands.Item(Guid, ID);
}
catch (Exception ex)
{
}
if ((objCommand != null))
{
if (objCommand.Name == "Debug.AddWatch")
{
//if (_isRecursive) return;
//_isRecursive = true;
TextSelection selection = (TextSelection)_applicationObject.ActiveDocument.Selection;
//TODO make selection goto next semi-colon/Line Terminator...
var selText = selection.Text;
if (string.IsNullOrEmpty(selText)) return;
//Only intercept multi-line Add Watch commands
if (selText.Contains(Environment.NewLine))
{
//THE BLACK MAGIC: make it fit in one line! lol
selText = selText.Replace(Environment.NewLine, string.Empty);
//THIS CALL IS RECURSIVE, I'LL LEAVE IT TO THE READER AS AN EXERCISE TO SOLVE..
_applicationObject.ExecuteCommand("Debug.AddWatch", selText);
}
}
}
}
Create a New Project > Other Project Types > Extensibility > Visual Studio Add-In > name it AddinMultiLineWatch
Go through the wizard
Add the code above to the Connect.cs class - see my //UPPERCASE comments with what stuff to add.
Put a break point on the line TextSelection selection = (TextSelection)_applicationObject.ActiveDocument.Selection;
Press F5 and a new instance of VS will launch > choose New Project > Console App > name it TestMultilineAddWatch
In the program.cs of the Console App, specify a code call over 2 lines and put a break point on it, as shown in the screenshot, eg:
Add(1, //Breakpoint here and select both lines
2);
}
static int Add(int i, int j)
{
return i + j;
}
F5 in the TestMultilineAddWatch solution and when the code control halts on the break point > select/highlight the two lines Add(1, \r\n 2) > right click > Add Watch
Clicking Add Watch in the VS IDE debugging context menu causes the VS AddinMultiLineWatch solution to intercept the call and activate, halting on the break point.... where you will see the black magic of replacing multi lined code in to a single line sent to the Watch Window.
The Visual Studio EXEC command calling itself makes this method recursive, if you debug it, exiting out of the recursion manually you will see the results as per my screenshot.
Happy debugging!
You could do it using autohotkey and a custom key binding ( e.g. Alt+Shift+V)
!+v means Alt+Shift+v
The macro below:
If in devenv.exe, and you press Alt+Shift+V, edit the clipboard contents, removing /r/n and replace them with nothing, then press Ctrl+V to paste
I tested this out cutting and pasting in a text document in visual studio.
#IfWinActive ahk_exe devenv.exe
!+v::
FixString = %clipboard%
StringReplace, FixString, FixString,`r`n,,A
Clipboard := FixString
Send, ^v

Sync Navigation Drawer menu on Back stack

I followed the https://github.com/jamesmontemagno/Xam.NavDrawer example and Im able to successfully use the drawer layout with fragments(infact nested fragments with view pager). I have one issue, when I click the back button the navigation drawer menu item on the left side is not synced with the fragment that is shown.
This is how I navigate to other fragments
SupportFragmentManager.BeginTransaction().Replace(Resource.Id.content, fragment).AddToBackStack(fragment.Name).Commit();
I tried the OnAttachFragment method, but its not called on back stack. I also tried the SupportFragmentManager BackStackChanged method, but I could not get the current fragment that is in the view to update the navigation drawer menu title.
I had the same issue and couldn't find any solution as well. Although this question is kinda old, my solution may help someone. I'm not sure if it's the best solution but it works for me.
So first you need to add variable to store ID of previously checked item:
private int previousItemChecked;
set it initially to your default checked item:
if (savedInstanceState == null) {
selectItem(0);
previousItemChecked = 0;
}
then edit the fragment transaction so that the transaction title in backstack contains position of the previously checked item converted to string and after the transaction is done set the variable previousItemChecked to the currently checked item id:
fragmentManager.beginTransaction().replace(R.id.content_frame, selectedFragment).addToBackStack(""+previousItemChecked).commit();
previousItemChecked = mDrawerList.getCheckedItemPosition();
and finally in method onBackPressed you need to get the string previously assigned to fragment transaction, parse it to int and update the drawer according to the obtained id:
#Override
public void onBackPressed() {
if(fragmentManager.getBackStackEntryCount() > 0) {
String title = fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount()-1).getName();
int pos = Integer.parseInt(title);
previousItemChecked = pos;
mDrawerList.setItemChecked(pos, true);
}
super.onBackPressed();
}
I took the code from my app created in Android Studio so it isn't for Xamarin but if you update the code it should work with it too. What's important here is the idea how it's done. I hope the answer is understandable and will help someone.
I had the same issue and I solved like this:
Inside selectItem we are passing the position Item;
So if position is 0 (or whatever is fragment we want it appears as first indipendently from it's position on the menu) we have to avoid to save the first transaction. So...
private void selectItem(position){
//...
if (position != 0)
{
SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.content_frame, fragment)
.AddToBackStack(fragment.Name)
.Commit();
}
else
{
SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.content_frame, fragment)
.Commit();
}
}
Managed to achieve this by injecting a DestinationChangedListener into the NavController like this:
NavController navController.addOnDestinationChangedListener(this);
and then:
#Override
public void onDestinationChanged(#NonNull NavController controller,
#NonNull NavDestination destination,
#Nullable Bundle arguments) {
NavigationView navigationView = findViewById(R.id.nav_view);
if(navigationView != null){
navigationView.setCheckedItem(destination.getId());
}
}

get the selected text of the editor window..visual studio extension

hi i'm making a extension for visual studio and the specific thing that i need is get the selected text of the editor windows for further processing. Someone know what interface or service has this?
Previously i need to locate the path of the open solution and for that i ask for a service that implements IVsSolution, so for this other problem I thing that there must be some service that provides me this information.
To clarify "just get the viewhost" in Stacker's answer, here is the full code for how you can get the current editor view, and from there the ITextSelection, from anywhere else in a Visual Studio 2010 VSPackage. In particular, I used this to get the current selection from a menu command callback.
IWpfTextViewHost GetCurrentViewHost()
{
// code to get access to the editor's currently selected text cribbed from
// http://msdn.microsoft.com/en-us/library/dd884850.aspx
IVsTextManager txtMgr = (IVsTextManager)GetService(typeof(SVsTextManager));
IVsTextView vTextView = null;
int mustHaveFocus = 1;
txtMgr.GetActiveView(mustHaveFocus, null, out vTextView);
IVsUserData userData = vTextView as IVsUserData;
if (userData == null)
{
return null;
}
else
{
IWpfTextViewHost viewHost;
object holder;
Guid guidViewHost = DefGuidList.guidIWpfTextViewHost;
userData.GetData(ref guidViewHost, out holder);
viewHost = (IWpfTextViewHost)holder;
return viewHost;
}
}
/// Given an IWpfTextViewHost representing the currently selected editor pane,
/// return the ITextDocument for that view. That's useful for learning things
/// like the filename of the document, its creation date, and so on.
ITextDocument GetTextDocumentForView( IWpfTextViewHost viewHost )
{
ITextDocument document;
viewHost.TextView.TextDataModel.DocumentBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document);
return document;
}
/// Get the current editor selection
ITextSelection GetSelection( IWpfTextViewHost viewHost )
{
return viewHost.TextView.Selection;
}
Here's MSDN's docs for IWpfTextViewHost, ITextDocument, and ITextSelection.
Inside of the OnlayoutChanged, the following code would pop up a message with the code selected:
if (_view.Selection.IsEmpty) return;
else
{
string selectedText = _view.Selection.StreamSelectionSpan.GetText();
MessageBox.Show(selectedText);
}
Anywhere else, just get the viewhost and its _view of typeIWpfTextView

Categories

Resources