Commits

Nerzhul500 committed 9615ce4

Lazy suggest generation for performance

  • Participants
  • Parent commits b2fd239
  • Branches resharper7

Comments (0)

Files changed (5)

File ReSpeller/Bulbs/AdvancedTypoRenameBulbItem.cs

   class AdvancedTypoRenameBulbItem : IBulbItem
   {
     private readonly IDeclaredElement myElement;
-    private readonly List<string> mySuggestedNames; 
+    private readonly Lazy<List<string>> mySuggestedNames; 
 
-    public AdvancedTypoRenameBulbItem(IDeclaredElement element, List<string> suggestedNames)
+    public AdvancedTypoRenameBulbItem(IDeclaredElement element, Lazy<List<string>> suggestedNames)
     {
       myElement = element;
       mySuggestedNames = suggestedNames;
     {
       var service = solution.GetComponent<RenameRefactoringService>();
       IRefactoringWorkflow workflow;
-      if (myElement is INamespace)
+        List<string> suggests = mySuggestedNames.Value;
+        if (myElement is INamespace)
       {
-        workflow = new RenameNamespaceWorkflowDecorator("NamespaceTypoRename", mySuggestedNames);
+        workflow = new RenameNamespaceWorkflowDecorator("NamespaceTypoRename", suggests);
       }
       else if (service.IsLocal(myElement))
       {
-        workflow = new TypoInlineRenameWorkflowDecorator("InlineTypoRename", mySuggestedNames);
+        workflow = new TypoInlineRenameWorkflowDecorator("InlineTypoRename", suggests);
       }
       else
       {
-        workflow = new TypoRenameWorkflow(mySuggestedNames, Shell.Instance.Locks, 
+        workflow = new TypoRenameWorkflow(suggests, Shell.Instance.Locks, 
                                           SearchDomainFactory.Instance, 
                                           RenameRefactoringService.Instance, 
                                           solution, 

File ReSpeller/Bulbs/TextRangeTypoReplaceBulbItem.cs

   class TextRangeTypoReplaceBulbItem : IBulbItem
   {
     private DocumentRange myRange;
-    private readonly List<string> mySuggestions;
+    private readonly Lazy<List<string>> mySuggestions;
 
-    public TextRangeTypoReplaceBulbItem(DocumentRange range, List<string> suggestions)
+    public TextRangeTypoReplaceBulbItem(DocumentRange range, Lazy<List<string>> suggestions)
     {
       myRange = range;
       mySuggestions = suggestions;
         return;
       var allWords = new List<string>();
       string text = myRange.GetText();
-      if (!mySuggestions.Contains(text))
+        List<string> suggests = mySuggestions.Value;
+        if (!suggests.Contains(text))
         allWords.Add(text);
-      allWords.AddRange(mySuggestions);
+      allWords.AddRange(suggests);
       var templateField = new TemplateField("TEXT_RANGE_TYPO", new TextHotspotExpression(allWords), 0);
       var fieldInfo = new HotspotInfo(templateField, myRange.TextRange);
       textControl.Caret.MoveTo(fieldInfo.Ranges[0].StartOffset, CaretVisualPlacement.Generic);

File ReSpeller/Lazy.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading;
+
+namespace ReSpeller
+{
+    class Lazy<T>
+    {
+        private T myValue;
+ 
+        private readonly ManualResetEvent myReset = new ManualResetEvent(false);
+
+        public Lazy(Func<T> evaluator)
+        {
+            new Thread(() => 
+                {
+                    myValue = evaluator();
+                    myReset.Set();
+                }).Start();
+        }
+
+        public T Value
+        {
+            get 
+            {
+                myReset.WaitOne();
+                return myValue;
+            }
+        }
+    }
+}

File ReSpeller/QuickFixes/TypoQuickFix.cs

       myHighlighting = highlighting;
       string prefix;
       string suffix;
-      List<TextPart> decomposedName = AnalyzerHelper.DecomposeName(highlighting.TreeNode, highlighting.Element, out prefix, out suffix);
-      List<string> suggests = mySuggestionGenerator.Generate(decomposedName, prefix, suffix);
-      myBulbItems.Add(new AdvancedTypoRenameBulbItem(highlighting.Element, suggests));
+        Lazy<List<string>> suggests = new Lazy<List<string>>(() =>
+                                              {
+                                                  List<TextPart> decomposedName = AnalyzerHelper.DecomposeName(highlighting.TreeNode, highlighting.Element, out prefix, out suffix);
+                                                  return mySuggestionGenerator.Generate(decomposedName, prefix, suffix);
+                                              });
+
+        myBulbItems.Add(new AdvancedTypoRenameBulbItem(highlighting.Element, suggests));
       string word = highlighting.WrongPart.Text;
       myBulbItems.Add(new AddToDictionaryBulbItem(word));
       myBulbItems.Add(new EditAddToDictionaryBulb(word));
     public TypoQuickFix([NotNull]TextRangeTypoHighlighting highlighting)
     {
       myHighlighting = highlighting;
-      List<string> suggestions = myChecker.Suggestions(highlighting.Range.GetText());
-      myBulbItems.Add(new TextRangeTypoReplaceBulbItem(highlighting.Range, suggestions));
+        Lazy<List<string>> suggests = new Lazy<List<string>>(() => myChecker.Suggestions(highlighting.Range.GetText()));
+      myBulbItems.Add(new TextRangeTypoReplaceBulbItem(highlighting.Range, suggests));
       string word = highlighting.Range.GetText();
       myBulbItems.Add(new AddToDictionaryBulbItem(word));
       myBulbItems.Add(new EditAddToDictionaryBulb(word));

File ReSpeller/ReSpeller.csproj

     <Compile Include="Highlightings\TextRangeTypoHighlighting.cs" />
     <Compile Include="Highlightings\MarkupTextTypoHighlighting.cs" />
     <Compile Include="Img\ThemedIcons.Img.Generated.cs" />
+    <Compile Include="Lazy.cs" />
     <Compile Include="QuickFixes\TypoQuickFix.cs" />
     <Compile Include="Settings\ReSpellerSettings.cs" />
     <Compile Include="Settings\ReSpellerSettingsControl.xaml.cs">
     <ThemedIconPng Include="Img\icon.png" />
   </ItemGroup>
   <PropertyGroup>
-    <ReSharperSdkTargets Condition=" '$(ReSharperSdkTargets)' == '' ">$(MSBuildExtensionsPath)\JetBrains\ReSharper.SDK\v7.0</ReSharperSdkTargets>
+    <ReSharperSdkTargets Condition=" '$(ReSharperSdkTargets)' == '' ">$(MSBuildExtensionsPath)\JetBrains\ReSharper.SDK\v7.1</ReSharperSdkTargets>
   </PropertyGroup>
   <Import Project="$(ReSharperSdkTargets)\Plugin.Targets" />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />