Je vais vous en donner le plus.
private IVsEditorAdaptersFactoryService GetEditorAdaptersFactoryService()
{
IComponentModel componentModel =(IComponentModel)GetService(typeof(SComponentModel));
return componentModel.GetService<IVsEditorAdaptersFactoryService>();
}
private Microsoft.VisualStudio.Text.Editor.IWpfTextView GetTextView()
{
IVsTextManager textManager = (IVsTextManager)GetService(typeof(SVsTextManager));
if (textManager == null)
return null;
IVsTextView textView = null;
textManager.GetActiveView(1, null, out textView);
if (textView == null)
return null;
return GetEditorAdaptersFactoryService().GetWpfTextView(textView);
}
public void SomeFUnction()
{
Microsoft.VisualStudio.Text.Editor.IWpfTextView textView = GetTextView();
if (textView != null)
{
SnapshotPoint caretPosition = textView.Caret.Position.BufferPosition;
}
}
Maintenant vous avez la position du curseur sur vous pour comprendre ce qu'il y a. Quelque chose comme textView.GetTextElementSpan (caretPosition) .GetText()
selected = (chaîne) this.ServiceProvider.GetService (typeof (TextSelection)); c'est le problème. La chaîne est null et c'est pourquoi la méthode Contains ne peut pas être appelée. Que retourne .GetService() et retourne-t-il réellement quelque chose? Comme c'est nul. Peut-être pas une conversion naturelle –
À ce stade, je veux juste voir un exemple de travail pour obtenir le code sélectionné par l'utilisateur. GetService() renvoyait une méthode. @PetterPettersson – Christmas
Utilisez DTE.ActiveDocument.Selection http://stackoverflow.com/questions/24402325/how-to-add-text-in-active-document-using-c-sharp –