-
Notifications
You must be signed in to change notification settings - Fork 235
Provide pester functions (it, context, describe) in document symbols #490
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
97ffcf0
Provide symbols in a pester file
93bf996
Use document provider interface to provide symbols
32f2804
Add provider for *.psd1 files
6273be1
Update IsPowerShellDataFileAst method
74cbd87
Update IDocumentSymbolProvider interface
f1cc069
Add document symbol provider for pester files
31821b3
Update document symbol providers' class names
8116e81
Update pester document provider implementation
81395d4
Fix psd1 document symbol provider
e5f3fbd
Add new lines at the end of new files
9ed6af3
Convert DocumentSymbolProvider interface to abstract class
df7a645
Implicitly iterate over document symbol providers
0611d4a
Remove redundant case from switch statement
ce741d1
Add tests for psd1 and pester document symbols
86753fe
Add xml documentation to a public method
c627165
Add relevant test files
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/PowerShellEditorServices/Language/DocumentSymbolProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Microsoft.PowerShell.EditorServices | ||
{ | ||
internal abstract class DocumentSymbolProvider | ||
{ | ||
public IEnumerable<SymbolReference> GetSymbols(ScriptFile scriptFile, Version psVersion = null) | ||
{ | ||
if (CanProvideFor(scriptFile)) | ||
{ | ||
return GetSymbolsImpl(scriptFile, psVersion); | ||
} | ||
|
||
return Enumerable.Empty<SymbolReference>(); | ||
} | ||
|
||
protected abstract IEnumerable<SymbolReference> GetSymbolsImpl(ScriptFile scriptFile, Version psVersion); | ||
|
||
protected abstract bool CanProvideFor(ScriptFile scriptFile); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/PowerShellEditorServices/Language/PSDDocumentSymbolProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.PowerShell.EditorServices | ||
{ | ||
internal class PSDDocumentSymbolProvider : DocumentSymbolProvider | ||
{ | ||
protected override bool CanProvideFor(ScriptFile scriptFile) | ||
{ | ||
return (scriptFile.FilePath != null && | ||
scriptFile.FilePath.EndsWith(".psd1", StringComparison.OrdinalIgnoreCase)) || | ||
AstOperations.IsPowerShellDataFileAst(scriptFile.ScriptAst); | ||
} | ||
protected override IEnumerable<SymbolReference> GetSymbolsImpl(ScriptFile scriptFile, Version psVersion) | ||
{ | ||
var findHashtableSymbolsVisitor = new FindHashtableSymbolsVisitor(); | ||
scriptFile.ScriptAst.Visit(findHashtableSymbolsVisitor); | ||
return findHashtableSymbolsVisitor.SymbolReferences; | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/PowerShellEditorServices/Language/PesterDocumentSymbolProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Management.Automation.Language; | ||
|
||
namespace Microsoft.PowerShell.EditorServices | ||
{ | ||
internal class PesterDocumentSymbolProvider : DocumentSymbolProvider | ||
{ | ||
protected override bool CanProvideFor(ScriptFile scriptFile) | ||
{ | ||
return scriptFile.FilePath.EndsWith("tests.ps1", StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
protected override IEnumerable<SymbolReference> GetSymbolsImpl(ScriptFile scriptFile, Version psVersion) | ||
{ | ||
var commandAsts = scriptFile.ScriptAst.FindAll(ast => | ||
{ | ||
switch ((ast as CommandAst)?.GetCommandName().ToLower()) | ||
{ | ||
case "describe": | ||
case "context": | ||
case "it": | ||
return true; | ||
|
||
default: | ||
return false; | ||
} | ||
}, | ||
true); | ||
|
||
return commandAsts.Select(ast => new SymbolReference( | ||
SymbolType.Function, | ||
ast.Extent, | ||
scriptFile.FilePath, | ||
scriptFile.GetLine(ast.Extent.StartLineNumber))); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/PowerShellEditorServices/Language/ScriptDocumentSymbolProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.PowerShell.EditorServices | ||
{ | ||
internal class ScriptDocumentSymbolProvider : DocumentSymbolProvider | ||
{ | ||
protected override bool CanProvideFor(ScriptFile scriptFile) | ||
{ | ||
return scriptFile != null && | ||
scriptFile.FilePath != null && | ||
(scriptFile.FilePath.EndsWith(".ps1", StringComparison.OrdinalIgnoreCase) || | ||
scriptFile.FilePath.EndsWith(".psm1", StringComparison.OrdinalIgnoreCase)); | ||
} | ||
|
||
protected override IEnumerable<SymbolReference> GetSymbolsImpl( | ||
ScriptFile scriptFile, | ||
Version psVersion) | ||
{ | ||
return AstOperations.FindSymbolsInDocument( | ||
scriptFile.ScriptAst, | ||
psVersion); | ||
|
||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
test/PowerShellEditorServices.Test.Shared/Symbols/FindSymbolsInPSDFile.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
// | ||
|
||
namespace Microsoft.PowerShell.EditorServices.Test.Shared.Symbols | ||
{ | ||
public class FindSymbolsInPSDFile | ||
{ | ||
public static readonly ScriptRegion SourceDetails = | ||
new ScriptRegion | ||
{ | ||
File = @"Symbols\PowerShellDataFile.psd1" | ||
}; | ||
} | ||
} | ||
|
17 changes: 17 additions & 0 deletions
17
test/PowerShellEditorServices.Test.Shared/Symbols/FindSymbolsInPesterFile.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
// | ||
|
||
namespace Microsoft.PowerShell.EditorServices.Test.Shared.Symbols | ||
{ | ||
public class FindSymbolsInPesterFile | ||
{ | ||
public static readonly ScriptRegion SourceDetails = | ||
new ScriptRegion | ||
{ | ||
File = @"Symbols\PesterFile.tests.ps1" | ||
}; | ||
} | ||
} | ||
|
15 changes: 15 additions & 0 deletions
15
test/PowerShellEditorServices.Test.Shared/Symbols/PesterFile.tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Describe "A dummy test" { | ||
Context "When a pester file is given" { | ||
It "Should return it symbols" { | ||
|
||
} | ||
|
||
It "Should return context symbols" { | ||
|
||
} | ||
|
||
It "Should return describe symbols" { | ||
|
||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
test/PowerShellEditorServices.Test.Shared/Symbols/PowerShellDataFile.psd1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@{ | ||
property1 = "value1" | ||
property2 = "value2" | ||
property3 = "value3" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you reading my mind? I was just about to implement a generic "feature provider" model in certain parts of PSES :)