22using System . Collections . Generic ;
33using System . Linq ;
44using FineCodeCoverage . Core . Utilities ;
5+ using FineCodeCoverage . Editor . DynamicCoverage . TrackedLinesImpl . Construction ;
56using FineCodeCoverage . Engine ;
67using FineCodeCoverage . Engine . Model ;
78using FineCodeCoverage . Impl ;
1112
1213namespace FineCodeCoverage . Editor . DynamicCoverage
1314{
15+ internal interface ICoverageContentTypes
16+ {
17+ bool IsApplicable ( string contentTypeName ) ;
18+ }
19+
1420 internal class BufferLineCoverage :
1521 IBufferLineCoverage , IListener < NewCoverageLinesMessage > , IListener < TestExecutionStartingMessage >
1622 {
@@ -19,12 +25,14 @@ internal class BufferLineCoverage :
1925 private readonly ITrackedLinesFactory trackedLinesFactory ;
2026 private readonly IDynamicCoverageStore dynamicCoverageStore ;
2127 private readonly IAppOptionsProvider appOptionsProvider ;
28+ private readonly ICoverageContentTypes coverageContentTypes ;
2229 private readonly ILogger logger ;
2330 private readonly ITextBuffer2 textBuffer ;
2431 private bool ? editorCoverageModeOff ;
2532 private IFileLineCoverage fileLineCoverage ;
2633 private Nullable < DateTime > lastChanged ;
2734 private DateTime lastTestExecutionStarting ;
35+ private bool applicableContentType = true ;
2836
2937 public ITrackedLines TrackedLines { get ; set ; }
3038
@@ -40,6 +48,7 @@ public BufferLineCoverage(
4048 ITrackedLinesFactory trackedLinesFactory ,
4149 IDynamicCoverageStore dynamicCoverageStore ,
4250 IAppOptionsProvider appOptionsProvider ,
51+ ICoverageContentTypes coverageContentTypes ,
4352 ILogger logger
4453 )
4554 {
@@ -50,11 +59,13 @@ ILogger logger
5059 }
5160
5261 this . textBuffer = textInfo . TextBuffer ;
62+ this . textBuffer . ContentTypeChanged += this . ContentTypeChanged ;
5363 this . textInfo = textInfo ;
5464 this . eventAggregator = eventAggregator ;
5565 this . trackedLinesFactory = trackedLinesFactory ;
5666 this . dynamicCoverageStore = dynamicCoverageStore ;
5767 this . appOptionsProvider = appOptionsProvider ;
68+ this . coverageContentTypes = coverageContentTypes ;
5869 this . logger = logger ;
5970 void AppOptionsChanged ( IAppOptions appOptions )
6071 {
@@ -79,6 +90,7 @@ void textViewClosedHandler(object s, EventArgs e)
7990 {
8091 this . UpdateDynamicCoverageStore ( ( s as ITextView ) . TextSnapshot ) ;
8192 this . textBuffer . Changed -= this . TextBuffer_ChangedOnBackground ;
93+ this . textBuffer . ContentTypeChanged -= this . ContentTypeChanged ;
8294 textInfo . TextView . Closed -= textViewClosedHandler ;
8395 appOptionsProvider . OptionsChanged -= AppOptionsChanged ;
8496 _ = eventAggregator . RemoveListener ( this ) ;
@@ -87,6 +99,25 @@ void textViewClosedHandler(object s, EventArgs e)
8799 textInfo . TextView . Closed += textViewClosedHandler ;
88100 }
89101
102+ private void ContentTypeChanged ( object sender , ContentTypeChangedEventArgs args )
103+ {
104+ // purpose is so do not create tracked lines for a content type that is not applicable when new coverage
105+ this . applicableContentType = this . coverageContentTypes . IsApplicable ( args . AfterContentType . TypeName ) ;
106+ if ( this . applicableContentType )
107+ {
108+ // this currently does not work as Roslyn is not ready.
109+ // could fallback to single lines but would have to look at other uses of IFileCodeSpanRangeService
110+ // this is low priority
111+ this . CreateTrackedLinesIfRequired ( true ) ;
112+ }
113+ else
114+ {
115+ this . TrackedLines = null ;
116+ }
117+
118+ this . SendCoverageChangedMessage ( ) ;
119+ }
120+
90121 private void UpdateDynamicCoverageStore ( ITextSnapshot textSnapshot )
91122 {
92123 if ( this . TrackedLines != null )
@@ -116,23 +147,23 @@ private void UpdateDynamicCoverageStore(ITextSnapshot textSnapshot)
116147 private bool FileSystemReflectsTrackedLines ( string snapshotText )
117148 => this . textInfo . GetFileText ( ) == snapshotText ;
118149
119- private void CreateTrackedLinesIfRequired ( bool initial )
150+ private void CreateTrackedLinesIfRequired ( bool fromStore )
120151 {
121152 if ( this . EditorCoverageColouringModeOff ( ) )
122153 {
123154 this . TrackedLines = null ;
124155 }
125156 else
126157 {
127- this . TryCreateTrackedLines ( initial ) ;
158+ this . TryCreateTrackedLines ( fromStore ) ;
128159 }
129160 }
130161
131- private void TryCreateTrackedLines ( bool initial )
162+ private void TryCreateTrackedLines ( bool fromStore )
132163 {
133164 try
134165 {
135- this . CreateTrackedLines ( initial ) ;
166+ this . CreateTrackedLines ( fromStore ) ;
136167 }
137168 catch ( Exception e )
138169 {
@@ -182,11 +213,11 @@ as When is written when the text view is closed it is always - LastWriteTime < W
182213 : ( SerializedCoverageState . Ok , serializedCoverageWhen . Serialized ) ;
183214 }
184215
185- private void CreateTrackedLines ( bool initial )
216+ private void CreateTrackedLines ( bool fromStore )
186217 {
187218 string filePath = this . textInfo . FilePath ;
188219 ITextSnapshot currentSnapshot = this . textBuffer . CurrentSnapshot ;
189- if ( initial )
220+ if ( fromStore )
190221 {
191222 SerializedCoverageWhen serializedCoverageWhen = this . dynamicCoverageStore . GetSerializedCoverage (
192223 filePath
@@ -266,6 +297,8 @@ public IEnumerable<IDynamicLine> GetLines(int startLineNumber, int endLineNumber
266297
267298 public void Handle ( NewCoverageLinesMessage message )
268299 {
300+ if ( ! this . applicableContentType ) return ;
301+
269302 this . fileLineCoverage = message . CoverageLines ;
270303
271304 bool hadTrackedLines = this . TrackedLines != null ;
0 commit comments