연습: 여백 문자 모양 만들기
편집기 사용자 지정 확장을 사용 하 여 여백 편집기의 모양을 사용자 지정할 수 있습니다. 코드 주석을 "todo" 이라는 단어가 나타날 때마다이 연습에서는 사용자 지정 문자 모양을 표시기 여백에 배치 합니다.
사전 요구 사항
이 연습을 완료 하려면 설치 해야 해당 Visual Studio 2010 SDK.
참고
Visual Studio SDK에 대 한 자세한 내용은 참조 하십시오. Visual Studio 개요를 확장합니다..Visual Studio SDK를 다운로드 하는 방법를 참조 하십시오. Visual Studio 확장성 개발자 센터 MSDN 웹 사이트에서.
관리 되는 확장성 프레임 워크 (MEF) 프로젝트 만들기
MEF 프로젝트를 만들려면
C# 또는 Visual Basic 편집기 분류자 프로젝트를 만듭니다. 솔루션의 이름을 TodoGlyphTest.
VSIX 매니페스트 편집기에서 Source.extension.vsixmanifest 파일을 엽니다.
Content MEF 구성 요소 콘텐츠 형식 및 해당 제목 포함은 Path Todoglyphtest.dll으로 설정 됩니다.
저장 하 고 source.extension.vsixmanifest를 닫습니다.
기존 클래스 파일을 제거 합니다.
기호를 정의합니다.
구현 하 여 문자 모양을 정의 IGlyphFactory 인터페이스입니다.
기호를 정의 하려면
클래스 파일을 추가 하 고 이름을 TodoGlyphFactory.
다음 imports 추가 합니다.
Imports System.ComponentModel.Composition Imports System.Windows Imports System.Windows.Shapes Imports System.Windows.Media Imports System.Windows.Controls Imports Microsoft.VisualStudio.Text Imports Microsoft.VisualStudio.Text.Editor Imports Microsoft.VisualStudio.Text.Formatting Imports Microsoft.VisualStudio.Text.Tagging Imports Microsoft.VisualStudio.Utilities
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;
라는 클래스를 추가 합니다. TodoGlyphFactory 는 구현 IGlyphFactory.
Friend Class TodoGlyphFactory Implements IGlyphFactory
internal class TodoGlyphFactory : IGlyphFactory
치수 기호를 정의 하는 전용 필드를 추가 합니다.
Const m_glyphSize As Double = 16.0
const double m_glyphSize = 16.0;
구현 GenerateGlyph 문자 사용자 인터페이스 (UI) 요소를 정의 합니다. TodoTag이 연습의 뒷부분에서 정의 합니다.
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 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; }
라는 클래스를 추가 합니다. TodoGlyphFactoryProvider 는 구현 IGlyphFactoryProvider. 이 클래스를 내보낼는 NameAttribute 의 "TodoGlyph"는 OrderAttribute 의 VsTextMarker 후,는 ContentTypeAttribute "코드"를 하는 TagTypeAttribute Todotag의.
<Export(GetType(IGlyphFactoryProvider)), Name("TodoGlyph"), Order(After:="VsTextMarker"), ContentType("code"), TagType(GetType(TodoTag))> Friend NotInheritable Class TodoGlyphFactoryProvider Implements IGlyphFactoryProvider
[Export(typeof(IGlyphFactoryProvider))] [Name("TodoGlyph")] [Order(After = "VsTextMarker")] [ContentType("code")] [TagType(typeof(TodoTag))] internal sealed class TodoGlyphFactoryProvider : IGlyphFactoryProvider
구현은 GetGlyphFactory 메서드에 의해 인스턴스화의 TodoGlyphFactory.
Public Function GetGlyphFactory(ByVal view As IWpfTextView, ByVal margin As IWpfTextViewMargin) As IGlyphFactory Implements IGlyphFactoryProvider.GetGlyphFactory Return New TodoGlyphFactory() End Function
public IGlyphFactory GetGlyphFactory(IWpfTextView view, IWpfTextViewMargin margin) { return new TodoGlyphFactory(); }
Todo 태그 및 Tagger 정의
태그 종류 및 tagger, 만들고 tagger 공급자를 사용 하 여 내보내기 하 여 이전 단계에서 정의 된 UI 요소에서 표시기 여백 사이의 관계를 정의 합니다.
Todo 태그 및 tagger 정의 하기
프로젝트에 새 클래스를 추가 하 고 이름을 TodoTagger.
다음 imports 추가 합니다.
Imports System Imports System.Collections.Generic Imports System.ComponentModel.Composition Imports Microsoft.VisualStudio.Text Imports Microsoft.VisualStudio.Text.Tagging Imports Microsoft.VisualStudio.Text.Editor Imports Microsoft.VisualStudio.Text.Classification Imports Microsoft.VisualStudio.Utilities
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;
라는 클래스를 추가 합니다. TodoTag.
Friend Class TodoTag Implements IGlyphTag Public Sub New() MyBase.New() End Sub End Class
internal class TodoTag : IGlyphTag
명명 된 클래스를 수정 TodoTagger 는 구현 ITagger 형식 TodoTag.
Friend Class TodoTagger Implements ITagger(Of TodoTag)
internal class TodoTagger : ITagger<TodoTag>
하는 TodoTagger 클래스를 private 필드를 추가 IClassifier 분류에서 찾을 텍스트를 포함 하 고 있습니다.
Private m_classifier As IClassifier Private Const m_searchText As String = "todo"
private IClassifier m_classifier; private const string m_searchText = "todo";
분류자를 설정 하는 생성자를 추가 합니다.
Friend Sub New(ByVal classifier As IClassifier) m_classifier = classifier End Sub
internal TodoTagger(IClassifier classifier) { m_classifier = classifier; }
구현에서 GetTags 가 모든 분류를 찾는 방법에 걸쳐 이름에 "의견" 이라는 단어가 포함 한 텍스트가 검색 텍스트를 포함 합니다. 검색 텍스트가 발견 되 면 다시 새로운 yield TagSpan 형식 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
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()); } } } } }
선언 된 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 event EventHandler<SnapshotSpanEventArgs> TagsChanged;
라는 클래스를 추가 TodoTaggerProvider 는 구현 ITaggerProvider, 함께 내보낼는 ContentTypeAttribute "코드"와 a TagTypeAttribute Todotag의.
<Export(GetType(ITaggerProvider)), ContentType("code"), TagType(GetType(TodoTag))> Friend Class TodoTaggerProvider Implements ITaggerProvider
[Export(typeof(ITaggerProvider))] [ContentType("code")] [TagType(typeof(TodoTag))] class TodoTaggerProvider : ITaggerProvider
가져오기는 IClassifierAggregatorService.
<Import()> Friend AggregatorService As IClassifierAggregatorService
[Import] internal IClassifierAggregatorService AggregatorService;
구현은 CreateTagger``1 메서드에 의해 인스턴스화의 TodoTagger.
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
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>; }
빌드 및 코드 테스트
이 코드를 테스트 하려면 TodoGlyphTest 솔루션을 빌드하고 실험 인스턴스를 실행 합니다.
빌드 및 TodoGlyphTest 솔루션을 테스트 하려면
솔루션을 빌드합니다.
F5 키를 눌러 프로젝트를 실행합니다. Visual Studio 두 번째 인스턴스를 인스턴스화합니다.
표시기 여백에 표시 되어 있는지 확인 하십시오. (에 있는 도구 메뉴를 클릭 옵션. 에 텍스트 편집기 페이지에서 확인 표시기 여백 을 선택 합니다.)
설명이 포함 된 코드 파일을 엽니다. 단어 "todo" 주석 섹션 중 하나에 추가 합니다.
진한 파랑 윤곽선 연한 파랑 원을 왼쪽 코드 창에 있는 표시기 여백에 나타납니다.