Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide pester functions (it, context, describe) in document symbols #490

Merged
merged 16 commits into from
Jun 5, 2017
Merged
Prev Previous commit
Next Next commit
Use document provider interface to provide symbols
  • Loading branch information
Kapil Borle committed Jun 3, 2017
commit 93bf9964f53d70fc270a7c4388520c135512d59f
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;

namespace Microsoft.PowerShell.EditorServices
{
internal class GenericDocumentSymbolProvider : IDocumentSymbolProvider
{
bool IDocumentSymbolProvider.CanProvideFor(ScriptFile scriptFile)
{
return scriptFile != null &&
scriptFile.FilePath != null &&
(scriptFile.FilePath.EndsWith(".ps1", StringComparison.OrdinalIgnoreCase) ||
scriptFile.FilePath.EndsWith(".psm1", StringComparison.OrdinalIgnoreCase));
}

IEnumerable<SymbolReference> IDocumentSymbolProvider.GetSymbols(ScriptFile scriptFile, Version psVersion)
{
return AstOperations.FindSymbolsInDocument(
scriptFile.ScriptAst,
psVersion);
}
}
}
11 changes: 11 additions & 0 deletions src/PowerShellEditorServices/Language/IDocumentSymbolProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;

namespace Microsoft.PowerShell.EditorServices
{
internal interface IDocumentSymbolProvider
{
IEnumerable<SymbolReference> GetSymbols(ScriptFile scriptFile, Version psVersion = null);
bool CanProvideFor(ScriptFile scriptFile);
}
}
58 changes: 42 additions & 16 deletions src/PowerShellEditorServices/Language/LanguageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class LanguageService
private string mostRecentRequestFile;
private Dictionary<String, List<String>> CmdletToAliasDictionary;
private Dictionary<String, String> AliasToCmdletDictionary;
private IDocumentSymbolProvider[] documentSymbolProviders;

const int DefaultWaitTimeoutMilliseconds = 5000;

Expand All @@ -54,6 +55,12 @@ public LanguageService(PowerShellContext powerShellContext)

this.CmdletToAliasDictionary = new Dictionary<String, List<String>>(StringComparer.OrdinalIgnoreCase);
this.AliasToCmdletDictionary = new Dictionary<String, String>(StringComparer.OrdinalIgnoreCase);
this.documentSymbolProviders = new IDocumentSymbolProvider[]
{
new GenericDocumentSymbolProvider()
// new PSDataFileDocumentSymbolProvider(),
// new PesterFileDocumentSymboleProvider()
};
}

#endregion
Expand Down Expand Up @@ -227,30 +234,49 @@ public FindOccurrencesResult FindSymbolsInFile(ScriptFile scriptFile)
{
Validate.IsNotNull("scriptFile", scriptFile);

// todo create a document symbol provider interface
// todo create document symbol providers for generic symbols, psd1 file symbols, pester file symbols
IEnumerable<SymbolReference> symbolReferencesinFile =
AstOperations
.FindSymbolsInDocument(scriptFile.ScriptAst, this.powerShellContext.LocalPowerShellVersion.Version)
.Select(
var symbolReferences = Enumerable.Empty<SymbolReference>();
foreach (var provider in documentSymbolProviders)
{
symbolReferences = symbolReferences.Concat(provider.GetSymbols(scriptFile));
}

return new FindOccurrencesResult
{
FoundOccurrences = symbolReferences.Select(
reference =>
{
reference.SourceLine =
scriptFile.GetLine(reference.ScriptRegion.StartLineNumber);
reference.FilePath = scriptFile.FilePath;
return reference;
});
})
};

if (IsPesterFile(scriptFile))
{
symbolReferencesinFile = symbolReferencesinFile.Concat(GetPesterSymbols(scriptFile));
}
// todo create a document symbol provider interface
// todo create document symbol providers for generic symbols, psd1 file symbols, pester file symbols

return
new FindOccurrencesResult
{
FoundOccurrences = symbolReferencesinFile
};
// IEnumerable<SymbolReference> symbolReferencesinFile =
// AstOperations
// .FindSymbolsInDocument(scriptFile.ScriptAst, this.powerShellContext.LocalPowerShellVersion.Version)
// .Select(
// reference =>
// {
// reference.SourceLine =
// scriptFile.GetLine(reference.ScriptRegion.StartLineNumber);
// reference.FilePath = scriptFile.FilePath;
// return reference;
// });

// if (IsPesterFile(scriptFile))
// {
// symbolReferencesinFile = symbolReferencesinFile.Concat(GetPesterSymbols(scriptFile));
// }

// return
// new FindOccurrencesResult
// {
// FoundOccurrences = symbolReferencesinFile
// };
}

private IEnumerable<SymbolReference> GetPesterSymbols(ScriptFile scriptFile)
Expand Down