Skip to content

Commit

Permalink
Moved processing step files onto background work thread
Browse files Browse the repository at this point in the history
  • Loading branch information
BenHall committed May 9, 2010
1 parent 04298a5 commit f1f0710
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
59 changes: 43 additions & 16 deletions src/EditorExtension/Interaction/Processors/StepDefinitionFinder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;

namespace Meerkatalyst.Lonestar.EditorExtension.Interaction.Processors
Expand All @@ -10,34 +11,60 @@ internal class StepDefinitionFinder
internal event NewStepsFoundHandler NewStepsFound;

public void ProcessSteps(string getFilePath)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (s,e) =>
{
e.Result = CreateStepsForAllFilesInPath(e.Argument.ToString());
};

worker.RunWorkerCompleted += (s, e) => RaiseNewStepsFound(e.Result as List<StepDefinition>);
worker.RunWorkerAsync(getFilePath);
}

private List<StepDefinition> CreateStepsForAllFilesInPath(string getFilePath)
{
string directory = Path.GetDirectoryName(getFilePath);
string[] rubyFilesToScan = Directory.GetFiles(directory, "*.rb", SearchOption.AllDirectories);
string[] rubyFilesToScan = Directory.GetFiles(directory, "*.rb",
SearchOption.AllDirectories);
var stepDefinitions = new List<StepDefinition>();

foreach (var stepFile in rubyFilesToScan)
{
string[] readAllLines = File.ReadAllLines(stepFile);
for (int index = 0; index < readAllLines.Length; index++)
{
var readAllLine = readAllLines[index];
string line = readAllLine.Trim();
if (line.StartsWith("Given") || line.StartsWith("When") || line.StartsWith("Then"))
stepDefinitions.Add(new StepDefinition
{
File = Path.GetFileName(stepFile),
LineNumber = index,
Name = line
});
}
stepDefinitions.AddRange(GetStepsForFile(stepFile));
}

RaiseNewStepsFound(stepDefinitions);
return stepDefinitions;
}

private IEnumerable<StepDefinition> GetStepsForFile(string stepFile)
{
List<StepDefinition> stepDefinitions = new List<StepDefinition>();
string[] readAllLines = File.ReadAllLines(stepFile);
for (int index = 0; index < readAllLines.Length; index++)
{
var readAllLine = readAllLines[index];
string line = readAllLine.Trim();
if (StepDefinition(line))
stepDefinitions.Add(new StepDefinition
{
File = Path.GetFileName(stepFile),
LineNumber = index,
Name = line
});
}

return stepDefinitions;
}

private bool StepDefinition(string line)
{
return line.StartsWith("Given") || line.StartsWith("When") || line.StartsWith("Then");
}

private void RaiseNewStepsFound(List<StepDefinition> stepDefinitions)
{
if (NewStepsFound == null)
if (NewStepsFound == null || stepDefinitions == null || stepDefinitions.Count == 0)
return;

NewStepsFound(this, new NewStepsFoundHandlerArgs{NewDefinitions = stepDefinitions});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public StepsIntellisenseProcessor(IWpfTextView view)
_layer = view.GetAdornmentLayer(AdornmentLayerNames.StepsIntellisense);
_intellisenseWindow = new IntelliSenseControl();
_view.Caret.PositionChanged += ResetCaretIfIntellisenseWindowOpen;

_finder = new StepDefinitionFinder();
_finder.NewStepsFound += OnNewStepsFound;
_finder.ProcessSteps(_view.GetFilePath());
Expand Down

0 comments on commit f1f0710

Please sign in to comment.