سؤال

أنا أعمل في مشروع حيث قمنا بتقسيم كود C# إلى وظائف وقمنا بتخزين هذه الوظائف بداخلها IElisonBuffers.لقد قمت بتوصيل Intellisense، وتتفاعل المخازن المؤقتة مع الامتدادات الأخرى بشكل جيد كما هو موضح أدناه:

enter image description here

ومع ذلك، لا يمكنني الحصول على تمييز بناء الجملة للعمل ضمن هؤلاء المحررين.

أقوم بتضمين هؤلاء المحررين عبر الخطوات التالية:

  1. يخترع IVsInvisibleEditor للملف.
  2. احصل على ال IVsTextLines لهذا IVsInvisibleEditor
  3. يخترع IVsCodeWindow وتعيين المخزن المؤقت لهذا IVsCodeWindow ليكون IVsTextLines من IVsInvisibleEditor
  4. احصل على IWpfTextViewHost من نافذة التعليمات البرمجية هذه.يعيدني هذا إلى "WPF Land" حيث أستطيع التفاعل مع المسافات التقليدية.
  5. قم بإنشاء SnapshotSpan لـ IWpfTextViewHostعرض النص.يحتوي SnapshotSpan على وظيفة واحدة.
  6. يخترع IElisionBuffer يحتوي على SnapshotSpan.
  7. يخترع IVsTextBuffer عبر IVsEditorAdaptersFactoryService.CreateVsTextBufferAdapterForSecondaryBuffer() تمر في IElisionBuffer.
  8. الآن ألقيت IVsTextBuffer ل IVsTextLines و اتصل SetLanguageServiceID() المرور في C# GUID:694DD9B6-B865-4C5B-AD85-86356E9C88DC.
  9. أتحقق مرة أخرى من أنه تم ضبطه بشكل صحيح عبر GetLanguageServiceID() وكل شيء يبدو على ما يرام.
  10. أقوم بإنشاء IVsTextView وتهيئته بالجديد IVsTextBuffer.
  11. ثم أحصل على IWpfTextViewHost لهذا IVsTextView.

هل هناك أي خطوات خاصة يجب الاهتمام بها عند إعداد معرف خدمة اللغة لـ IElisionBuffer؟

من أجل الاكتمال هذا هو الكود الذي أستخدمه:

public CustomEditorViewModel CreateEditor(string filePath, int start, int end) {

IVsInvisibleEditor invisibleEditor;
ErrorHandler.ThrowOnFailure(this._InvisibleEditorManager.RegisterInvisibleEditor(
    filePath
    , pProject: null
    , dwFlags: (uint)_EDITORREGFLAGS.RIEF_ENABLECACHING
    , pFactory: null
    , ppEditor: out invisibleEditor));

var docDataPointer = IntPtr.Zero;
Guid guidIVsTextLines = typeof(IVsTextLines).GUID;

ErrorHandler.ThrowOnFailure(
  invisibleEditor.GetDocData(
  fEnsureWritable: 1
  , riid: ref guidIVsTextLines
  , ppDocData: out docDataPointer));

IVsTextLines docData = (IVsTextLines)Marshal.GetObjectForIUnknown(docDataPointer);

//Createa a code window adapter
var codeWindow = _EditorAdapterFactory.CreateVsCodeWindowAdapter(VisualStudioServices.OLEServiceProvider);

//Associate our IVsTextLines with our new code window
ErrorHandler.ThrowOnFailure(codeWindow.SetBuffer(docData));

//Get our text view for our editor which we will use to get the WPF control that hosts that editor.
IVsTextView textView;
ErrorHandler.ThrowOnFailure(codeWindow.GetPrimaryView(out textView));

//This is our TextViewHost
//It transports us back into the land of WPF 
IWpfTextViewHost textViewHost = _EditorAdapterFactory.GetWpfTextViewHost(textView);

  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  //Now we need to subset TextBuffer somehow... 
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int length = end - start;
SnapshotSpan subsetSnapshot = new SnapshotSpan(textViewHost.TextView.TextSnapshot, start, length);

var CSharpType = _contentTypeRegistry.GetContentType("CSharp");

var projBuffer = _ProjectionBufferFactory.CreateElisionBuffer(
  null
  , new NormalizedSnapshotSpanCollection(subsetSnapshot)
  , ElisionBufferOptions.None
  ,CSharpType);

IVsTextBuffer bufferAdapter = _EditorAdapterFactory.CreateVsTextBufferAdapterForSecondaryBuffer(VisualStudioServices.OLEServiceProvider, projBuffer);

//My attempt at getting syntax coloring to work:
Guid CSharpLanguageServiceId = new Guid("694DD9B6-B865-4C5B-AD85-86356E9C88DC");
IVsTextLines buffer = (IVsTextLines)bufferAdapter;
buffer.SetLanguageServiceID(ref CSharpLanguageServiceId);


IVsTextView projTextView = _EditorAdapterFactory.CreateVsTextViewAdapter(VisualStudioServices.OLEServiceProvider);

projTextView.Initialize(
            (IVsTextLines)bufferAdapter
            , IntPtr.Zero
            , (uint)TextViewInitFlags.VIF_HSCROLL | (uint)TextViewInitFlags.VIF_VSCROLL | (uint)TextViewInitFlags3.VIF_NO_HWND_SUPPORT,
                    new[] { new INITVIEW { fSelectionMargin = 0, fWidgetMargin = 0, fVirtualSpace = 0, fDragDropMove = 0 } }
                );


 return _EditorAdapterFactory.GetWpfTextViewHost(projTextView);
}
هل كانت مفيدة؟

المحلول

اجعل نوع محتوى المخزن المؤقت للحذف الخاص بك هو نوع المحتوى "الإسقاط" أو مشتق منه.هذا هو التلميح الذي يجب على أصحاب العلامات عرضه من خلال ذلك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top