Anda dapat menyesuaikan tampilan margin editor dengan menggunakan ekstensi editor kustom. Panduan ini menempatkan glyph kustom pada margin indikator setiap kali kata "todo" muncul dalam komentar kode.
Membuat proyek MEF
Buat proyek C# VSIX. (Dalam Dialog Proyek Baru, pilih Visual C# / Ekstensibilitas, lalu Proyek VSIX.) Beri nama solusi TodoGlyphTest.
Tambahkan item proyek Pengklasifikasi Editor. Untuk informasi selengkapnya, lihat Membuat ekstensi dengan templat item editor.
Hapus file kelas yang ada.
Tentukan glyph
Tentukan glyph dengan menjalankan IGlyphFactory antarmuka.
Untuk menentukan glyph
Tambahkan file kelas dan beri nama TodoGlyphFactory.
Tambahkan kode berikut dengan menggunakan deklarasi.
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Controls;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Formatting;
using Microsoft.VisualStudio.Text.Tagging;
using Microsoft.VisualStudio.Utilities;
public UIElement GenerateGlyph(IWpfTextViewLine line, IGlyphTag tag)
{
// Ensure we can draw a glyph for this marker.
if (tag == null || !(tag is TodoTag))
{
return null;
}
System.Windows.Shapes.Ellipse ellipse = new Ellipse();
ellipse.Fill = Brushes.LightBlue;
ellipse.StrokeThickness = 2;
ellipse.Stroke = Brushes.DarkBlue;
ellipse.Height = m_glyphSize;
ellipse.Width = m_glyphSize;
return ellipse;
}
Public Function GenerateGlyph(ByVal line As IWpfTextViewLine, ByVal tag As IGlyphTag) As System.Windows.UIElement Implements IGlyphFactory.GenerateGlyph
' Ensure we can draw a glyph for this marker.
If tag Is Nothing OrElse Not (TypeOf tag Is TodoTag) Then
Return Nothing
End If
Dim ellipse As Ellipse = New Ellipse()
ellipse.Fill = Brushes.LightBlue
ellipse.StrokeThickness = 2
ellipse.Stroke = Brushes.DarkBlue
ellipse.Height = m_glyphSize
ellipse.Width = m_glyphSize
Return ellipse
End Function
public IGlyphFactory GetGlyphFactory(IWpfTextView view, IWpfTextViewMargin margin)
{
return new TodoGlyphFactory();
}
Public Function GetGlyphFactory(ByVal view As IWpfTextView, ByVal margin As IWpfTextViewMargin) As IGlyphFactory Implements IGlyphFactoryProvider.GetGlyphFactory
Return New TodoGlyphFactory()
End Function
Menentukan tag dan tagger Todo
Tentukan hubungan antara elemen UI yang Anda tentukan di langkah sebelumnya dan margin indikator. Buat jenis tag dan tagger dan ekspor dengan menggunakan penyedia tagger.
Untuk menentukan tag dan tagger todo
Tambahkan file kelas baru ke proyek dan beri nama TodoTagger.
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Tagging;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Utilities;
Friend Sub New(ByVal classifier As IClassifier)
m_classifier = classifier
End Sub
Terapkan GetTags metode dengan menemukan semua rentang klasifikasi yang namanya menyertakan kata "komentar" dan yang teksnya menyertakan teks pencarian. Setiap kali teks pencarian ditemukan, hasilkan kembali jenis TodoTagbaru TagSpan<T> .
IEnumerable<ITagSpan<TodoTag>> ITagger<TodoTag>.GetTags(NormalizedSnapshotSpanCollection spans)
{
foreach (SnapshotSpan span in spans)
{
//look at each classification span \
foreach (ClassificationSpan classification in m_classifier.GetClassificationSpans(span))
{
//if the classification is a comment
if (classification.ClassificationType.Classification.ToLower().Contains("comment"))
{
//if the word "todo" is in the comment,
//create a new TodoTag TagSpan
int index = classification.Span.GetText().ToLower().IndexOf(m_searchText);
if (index != -1)
{
yield return new TagSpan<TodoTag>(new SnapshotSpan(classification.Span.Start + index, m_searchText.Length), new TodoTag());
}
}
}
}
}
Private Function GetTags(ByVal spans As NormalizedSnapshotSpanCollection) As IEnumerable(Of ITagSpan(Of TodoTag)) Implements ITagger(Of TodoTag).GetTags
Dim list As List(Of ITagSpan(Of TodoTag))
list = New List(Of ITagSpan(Of TodoTag))()
For Each span As SnapshotSpan In spans
'look at each classification span \
For Each classification As ClassificationSpan In m_classifier.GetClassificationSpans(span)
'if the classification is a comment
If classification.ClassificationType.Classification.ToLower().Contains("comment") Then
'if the word "todo" is in the comment,
'create a new TodoTag TagSpan
Dim index As Integer = classification.Span.GetText().ToLower().IndexOf(m_searchText)
If index <> -1 Then
list.Add(New TagSpan(Of TodoTag)(New SnapshotSpan(classification.Span.Start + index, m_searchText.Length), New TodoTag()))
End If
End If
Next classification
Next span
Return list
End Function
public event EventHandler<SnapshotSpanEventArgs> TagsChanged;
Public Event TagsChanged(ByVal sender As Object, ByVal e As Microsoft.VisualStudio.Text.SnapshotSpanEventArgs) Implements Microsoft.VisualStudio.Text.Tagging.ITagger(Of TodoTag).TagsChanged
public ITagger<T> CreateTagger<T>(ITextBuffer buffer) where T : ITag
{
if (buffer == null)
{
throw new ArgumentNullException("buffer");
}
return new TodoTagger(AggregatorService.GetClassifier(buffer)) as ITagger<T>;
}
Public Function CreateTagger(Of T As Microsoft.VisualStudio.Text.Tagging.ITag)(ByVal buffer As Microsoft.VisualStudio.Text.ITextBuffer) As Microsoft.VisualStudio.Text.Tagging.ITagger(Of T) Implements Microsoft.VisualStudio.Text.Tagging.ITaggerProvider.CreateTagger
If buffer Is Nothing Then
Throw New ArgumentNullException("buffer")
End If
Return TryCast(New TodoTagger(AggregatorService.GetClassifier(buffer)), ITagger(Of T))
End Function
Membangun dan menguji kode
Untuk menguji kode ini, buat solusi TodoGlyphTest dan jalankan dalam instans eksperimental.
Untuk membangun dan menguji solusi TodoGlyphTest
Bangun solusinya.
Jalankan proyek dengan menekan F5. Instans kedua Visual Studio dimulai.
Pastikan margin indikator ditampilkan. (Pada Menu Alat, klik Opsi. Pada halaman Editor Teks, pastikan margin Indikator dipilih.)
Buka file kode yang memiliki komentar. Tambahkan kata "todo" ke salah satu bagian komentar.
Lingkaran biru muda dengan kerangka biru tua muncul di margin indikator di sebelah kiri jendela kode.