0

I was investigating this example and found the problem: the syntax highlighting did not work for a C# file after calling the method

_BufferAdapter.GetLanguageServiceID("694dd9b6-b865-4c5b-ad85-86356e9c88dc")

This problem reproduces in VS 2015 and does not reproduce in VS 2013. For a VB file I have the same problem. I think that the problem is related to the new Roslyn engine for C#/VB editors in VS 2015. How can I enable syntax highlighting for the C# and VB files? Generally, I want to create a simple VS editor and host its WPF content into my WPF control.

The method InitializeEditor in my code:

private void InitializeEditor(string path)
{
  var text = File.ReadAllText(path); // path to the .cs file

  var componentModel = (IComponentModel)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SComponentModel));
  _OleServiceProvider = (IOleServiceProvider)GetService(typeof(IOleServiceProvider));
  _BufferFactory = componentModel.GetService<ITextBufferFactoryService>();

  var contentType = componentModel.GetService<IContentTypeRegistryService>().GetContentType("CSharp");

  _EditorAdapterFactory = componentModel.GetService<IVsEditorAdaptersFactoryService>();
  _BufferAdapter = _EditorAdapterFactory.CreateVsTextBufferAdapter(_OleServiceProvider, contentType);
  _BufferAdapter.InitializeContent(text, text.Length);
  _BufferAdapter.SetLanguageServiceID("694dd9b6-b865-4c5b-ad85-86356e9c88dc")

  _ViewAdapter = _EditorAdapterFactory.CreateVsTextViewAdapter(_OleServiceProvider);
  ((IVsWindowPane)_ViewAdapter).SetSite(_OleServiceProvider);

  var initView = new[] { new INITVIEW() };
  initView[0].fSelectionMargin = 0;
  initView[0].fWidgetMargin = 0;
  initView[0].fVirtualSpace = 0;
  initView[0].fDragDropMove = 1;

  _ViewAdapter.Initialize(_BufferAdapter as IVsTextLines, IntPtr.Zero,
    (uint)TextViewInitFlags.VIF_HSCROLL |
    (uint)TextViewInitFlags.VIF_VSCROLL |
    (uint)TextViewInitFlags3.VIF_NO_HWND_SUPPORT, initView);
}

I replaced _BufferFactory.TextContentType on the content type 'CSharp'. I hoped it will resolve the problem. But new content type did not resolve the highlighting problem.

0 Answers0