Ajouter la mise en évidence de la syntaxe à IElisonBuffer
-
02-01-2020 - |
Question
Je travaille sur un projet dans lequel nous avons divisé le code C# en fonctions et stocké ces fonctions dans IElisonTampons.J'ai connecté Intellisense et les tampons interagissent correctement avec d'autres extensions, comme indiqué ci-dessous :
Cependant, je ne parviens pas à faire fonctionner la coloration syntaxique dans ces éditeurs.
J'intègre ces éditeurs via les étapes suivantes :
- Créé un
IVsInvisibleEditor
pour le fichier. - Obtenir le
IVsTextLines
pour çaIVsInvisibleEditor
- Créé un
IVsCodeWindow
et définissez le tampon pour celaIVsCodeWindow
être leIVsTextLines
duIVsInvisibleEditor
- Obtenez un
IWpfTextViewHost
à partir de cette fenêtre de code.Cela me ramène à « WPF Land » où je peux interagir avec les travées traditionnelles. - Créez un SnapshotSpan du
IWpfTextViewHost
la vue texte de.Ce SnapshotSpan contient une seule fonction. - Créé un
IElisionBuffer
contenant le SnapshotSpan. - Créé un
IVsTextBuffer
viaIVsEditorAdaptersFactoryService.CreateVsTextBufferAdapterForSecondaryBuffer()
passant dans leIElisionBuffer
. - Maintenant, je lance le
IVsTextBuffer
àIVsTextLines
et appelleSetLanguageServiceID()
en passant le GUID C# :694DD9B6-B865-4C5B-AD85-86356E9C88DC. - Je vérifie qu'il a été correctement réglé via
GetLanguageServiceID()
et tout semble bien. - Je crée un
IVsTextView
et initialisez-le avec le nouveauIVsTextBuffer
. - je reçois alors le
IWpfTextViewHost
pour çaIVsTextView
.
Y a-t-il des étapes particulières à suivre lors de la configuration de l'ID de service de langage pour un IElisionBuffer ?
Par souci d'exhaustivité, voici le code que j'utilise :
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);
}
La solution
Faites en sorte que le type de contenu de votre tampon d'élision soit ou dérive du type de contenu "projection".C'est l'indice que les tagueurs devraient projeter à travers cela.