2013-09-27 5 views
1

MS Visual Studio 편집기의 확장으로 C++에 강조 표시를 추가했습니다.표준 분류 유형에 대한 IClassifier를 얻는 방법은 무엇입니까?

제공된 SnapshotSpan에 표준 분류 유형 ("의견")이 있는지 확인하고 싶습니다. 이를 수행하는 방법에는 여러 가지가 있습니다.

. 주석 범위를 찾으려면 C++ 코드를 수동으로 구문 분석 할 수 있습니다. 이것은 내가 사용하고자하는 마지막 옵션입니다.

.

this.colorer = buffer.Properties.PropertyList // <-- buffer is a ITextBuffer 
    .Select(p => p.Value as IClassifier) // Get all classifiers someone put into the properies of the current text buffer 
    .Where(i => i != null) 
    .Where(i => i.GetType().FullName == "Microsoft.VisualC.CppColorer") // <-- Hack 
    .FirstOrDefault(); 

지금 나는이 colorer를 사용할 수있는 다음과 같은 방법으로합니다 (C++ 분류의 Microsoft 내부에서 구현 한 것입니다) :

this.colorer.GetClassificationSpans(span) 
    .Where(s => s.ClassificationType.Classification == FormatNames.Comment || 
       s.ClassificationType.Classification == FormatNames.XmlDocComment) 

타다 나는 해킹을 사용할 수 있습니다! 텍스트 버퍼에 주석에 대한 정보가 있습니다. 이해 하시겠지만,이 해킹과 나는 이것을 피하고 싶습니다. :)

. 표준 분류 유형 (예 : "comment")에 대해 (어쨌든) 분류자를 얻으려고 할 수 있습니다.


그래서 제 질문은 : 분류 유형 이름으로 IClassifier 얻을이 가능합니까?

답변

1

공식적인 방법이없는 것처럼 보입니다. 그래서 코드 주석을위한 분류자를 직접 구현했습니다.

[Import] 
internal IClassifierAggregatorService classifierAggregatorService = null; 

그리고 모든 분류 범위 유형 "comment"의 것을 확인 ClassificationSpan 반복 :

1

당신은 IClassifierAggregatorService 가져올 수 IClassifierAggregatorService, 당신이 할 수있는지고의 대안으로

IClassifier classifier = classifierAggregatorService.GetClassifier(textBuffer); 
IList<ClassificationSpan> classificationSpanList = _classifier.GetClassificationSpans(span); 
foreach (ClassificationSpan classificationSpan in classificationSpanList) 
{ 
    if (classificationSpan.ClassificationType.IsOfType("comment")) 
    { 
     // ... 
    } 
} 

IBufferTagAggregatorFactoryService에서 ITagAggregator<IClassificationTag>을 얻으십시오. 이전 분류에 따라 분류를 추가하려는 경우 특히 유용합니다 (this answer 참조).