Skip to content

Commit

Permalink
Merge pull request #68 from GeorgeAlexandria/fix_caching
Browse files Browse the repository at this point in the history
Avoid caching of semantic model
  • Loading branch information
GeorgeAlexandria authored Oct 26, 2019
2 parents 26f2c27 + d1b35c7 commit 81d5b4f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
26 changes: 12 additions & 14 deletions src/vs14/CoCo_vs14/CoCo_vs14.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,18 @@
</ItemGroup>

<ItemGroup>
<Compile Include="..\..\vs16\CoCo_vs16\Editor\ClassificationChangingService.cs" Link="Editor\ClassificationChangingService.cs" />
<Compile Include="..\..\vs16\CoCo_vs16\Migrations\MigrationService_3_1_0.cs" Link="Migrations\MigrationService_3_1_0.cs" />
<Compile Include="..\..\vs16\CoCo_vs16\Paths.cs" Link="Paths.cs" />
<Compile Include="..\..\vs16\CoCo_vs16\Editor\ResetValuesProvider.cs" Link="Editor\ResetValuesProvider.cs" />
<Compile Include="..\..\vs16\CoCo_vs16\GeneralChangedEventArgs.cs" Link="GeneralChangedEventArgs.cs" />
<Compile Include="..\..\vs16\CoCo_vs16\GeneralChangingService.cs" Link="GeneralChangingService.cs" />
<Compile Include="..\..\vs16\CoCo_vs16\ServicesProvider.cs" Link="ServicesProvider.cs" />
<Compile Include="..\..\vs16\CoCo_vs16\Providers\VisualBasicClassifierProvider.cs" Link="Providers\VisualBasicClassifierProvider.cs" />
<Compile Include="..\..\vs16\CoCo_vs16\VsPackage.cs" Link="VsPackage.cs" />
<Compile Include="..\..\vs16\CoCo_vs16\Editor\ClassificationManager.cs" Link="Editor\ClassificationManager.cs" />
<Compile Include="..\..\vs16\CoCo_vs16\Providers\CSharpClassifierProvider.cs" Link="Providers\CSharpClassifierProvider.cs" />
<Compile Include="..\..\vs16\CoCo_vs16\Extensions.cs" Link="Extensions.cs" />
<Compile Include="..\..\vs16\CoCo_vs16\Editor\FormattingService.cs" Link="Editor\FormattingService.cs" />
<Compile Include="..\..\vs16\CoCo_vs16\OptionService.cs" Link="OptionService.cs" />
<LinkedItemsToExclude Include="..\..\vs16\CoCo_vs16\obj\**" />
<LinkedItemsToExclude Include="..\..\vs16\CoCo_vs16\Properties\AssemblyInfo.cs" />
<LinkedItemsToExclude Include="..\..\vs16\CoCo_vs16\Guids.cs" />
<LinkedItemsToExclude Include="..\..\vs16\CoCo_vs16\Editor\ClassificationManager.Classifications.cs" />
<LinkedItemsToExclude Include="..\..\vs16\CoCo_vs16\Editor\PresetService.cs" />
<LinkedItemsToExclude Include="..\..\vs16\CoCo_vs16\Migrations\MigrationService_2_0_0.cs" />
<LinkedItemsToExclude Include="..\..\vs16\CoCo_vs16\Migrations\MigrationService_2_3_0.cs" />
<LinkedItemsToExclude Include="..\..\vs16\CoCo_vs16\Providers\QuickInfoProvider.cs" />
<LinkedItemsToExclude Include="..\..\vs16\CoCo_vs16\Providers\ToolTipPresenterFactory.cs" />
<LinkedItemsToExclude Include="..\..\vs16\CoCo_vs16\QuickInfo\**\*.cs" />

<Compile Include="..\..\vs16\CoCo_vs16\**\*.cs" Link="%(RecursiveDir)%(Filename)%(Extension)" Exclude="@(LinkedItemsToExclude)" />
<Compile Include="..\..\vs15\CoCo_vs15\Editor\ClassificationManager.Classifications.cs" Link="Editor\ClassificationManager.Classifications.cs" />
<Compile Include="..\AssemblyInfoCommon.cs" Link="Properties\AssemblyInfoCommon.cs" />
<None Include="source.extension.vsixmanifest" />
Expand Down
23 changes: 15 additions & 8 deletions src/vs16/CoCo.Analyser_vs16/Editor/RoslynTextBufferClassifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ internal abstract class RoslynTextBufferClassifier : IClassifier

private static readonly List<ClassificationSpan> _emptyClassifications = new List<ClassificationSpan>();

private SemanticModel _semanticModel;
private bool _isEnable;

protected RoslynTextBufferClassifier()
Expand All @@ -40,7 +39,6 @@ protected RoslynTextBufferClassifier(
_editorChangingService = editorChangingService;

_editorChangingService.EditorOptionsChanged += OnEditorOptionsChanged;
_textBuffer.Changed += OnTextBufferChanged;
_textDocumentFactoryService.TextDocumentDisposed += OnTextDocumentDisposed;
}

Expand Down Expand Up @@ -70,9 +68,22 @@ public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
return _emptyClassifications;
}

var document = workspace.GetDocument(span.Snapshot.AsText());
var semanticModel = _semanticModel ?? (_semanticModel = document.GetSemanticModelAsync().Result);
// NOTE: to use the previously classified result as a cached result classifier must correctly determine
// when it can use it or not. To correctly determine it, classifier must handle all bellow events:
//
// * All source code files changing
// * Source file appending/removing/including/excluding to projects
// * All project files changing
// * Project file appending/removing/including/excluding to solution
// * Solution changing
// * Solution/projects current configuration changing
//
// because all of them may affect to a semantic model of code. Subscribing and unsubscribing on all of them is expensive
// and all of them are raised very often that make subscribing non effective. So much better way
// is the get actual data to the everyone requests

var document = workspace.GetDocument(span.Snapshot.AsText());
var semanticModel = document.GetSemanticModelAsync().Result;
return GetClassificationSpans(workspace, semanticModel, span);
}

Expand All @@ -83,8 +94,6 @@ internal abstract List<ClassificationSpan> GetClassificationSpans(

protected abstract string Language { get; }

private void OnTextBufferChanged(object sender, TextContentChangedEventArgs e) => _semanticModel = null;

private void OnEditorOptionsChanged(EditorChangedEventArgs args)
{
// NOTE: if the state of editor option was changed => raise that classifications were changed for the current buffer
Expand All @@ -103,8 +112,6 @@ private void OnTextDocumentDisposed(object sender, TextDocumentEventArgs e)
{
if (e.TextDocument.TextBuffer == _textBuffer)
{
_semanticModel = null;
_textBuffer.Changed -= OnTextBufferChanged;
_textDocumentFactoryService.TextDocumentDisposed -= OnTextDocumentDisposed;
_editorChangingService.EditorOptionsChanged -= OnEditorOptionsChanged;
}
Expand Down

0 comments on commit 81d5b4f

Please sign in to comment.