Skip to content

Commit e7d3afe

Browse files
committed
Make DocumentSymbolHandler more readable
While we're debugging it.
1 parent 4bffeb8 commit e7d3afe

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

src/PowerShellEditorServices/Services/CodeLens/PesterCodeLensProvider.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public CodeLens[] ProvideCodeLenses(ScriptFile scriptFile, CancellationToken can
119119
continue;
120120
}
121121

122-
// Skip codelense for setup/teardown block
122+
// Skip CodeLens for setup/teardown block
123123
if (!PesterSymbolReference.IsPesterTestCommand(pesterSymbol.Command))
124124
{
125125
continue;

src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs

+2
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ static string[] GetIdentifiers(string symbolName, SymbolType symbolType, Command
231231

232232
symbolReferences.AddRange(references);
233233

234+
// This async method is pretty dense with synchronous code
235+
// so it's helpful to add some yields.
234236
await Task.Yield();
235237
cancellationToken.ThrowIfCancellationRequested();
236238
}

src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs

+30-30
Original file line numberDiff line numberDiff line change
@@ -44,45 +44,45 @@ public PsesDocumentSymbolHandler(ILoggerFactory factory, WorkspaceService worksp
4444
DocumentSelector = LspUtils.PowerShellDocumentSelector
4545
};
4646

47-
public override Task<SymbolInformationOrDocumentSymbolContainer> Handle(DocumentSymbolParams request, CancellationToken cancellationToken)
47+
public override async Task<SymbolInformationOrDocumentSymbolContainer> Handle(DocumentSymbolParams request, CancellationToken cancellationToken)
4848
{
4949
ScriptFile scriptFile = _workspaceService.GetFile(request.TextDocument.Uri);
5050

51-
IEnumerable<SymbolReference> foundSymbols =
52-
ProvideDocumentSymbols(scriptFile);
53-
54-
SymbolInformationOrDocumentSymbol[] symbols = null;
51+
IEnumerable<SymbolReference> foundSymbols = ProvideDocumentSymbols(scriptFile);
52+
if (foundSymbols is null)
53+
{
54+
return null;
55+
}
5556

5657
string containerName = Path.GetFileNameWithoutExtension(scriptFile.FilePath);
58+
List<SymbolInformationOrDocumentSymbol> symbols = new();
59+
foreach (SymbolReference r in foundSymbols)
60+
{
61+
// This async method is pretty dense with synchronous code
62+
// so it's helpful to add some yields.
63+
await Task.Yield();
64+
cancellationToken.ThrowIfCancellationRequested();
65+
66+
// TODO: This should be a DocumentSymbol now as SymbolInformation is deprecated.
67+
symbols.Add(new SymbolInformationOrDocumentSymbol(new SymbolInformation
68+
{
69+
ContainerName = containerName,
70+
Kind = SymbolTypeUtils.GetSymbolKind(r.SymbolType),
71+
Location = new Location
72+
{
73+
Uri = DocumentUri.From(r.FilePath),
74+
Range = r.ScriptRegion.ToRange()
75+
},
76+
Name = SymbolTypeUtils.GetDecoratedSymbolName(r)
77+
}));
78+
}
5779

58-
symbols = foundSymbols != null
59-
? foundSymbols
60-
.Select(r =>
61-
{
62-
// TODO: This should be a DocumentSymbol now as SymbolInformation is deprecated.
63-
return new SymbolInformationOrDocumentSymbol(new SymbolInformation
64-
{
65-
ContainerName = containerName,
66-
Kind = SymbolTypeUtils.GetSymbolKind(r.SymbolType),
67-
Location = new Location
68-
{
69-
Uri = DocumentUri.From(r.FilePath),
70-
Range = r.ScriptRegion.ToRange()
71-
},
72-
Name = SymbolTypeUtils.GetDecoratedSymbolName(r)
73-
});
74-
})
75-
.ToArray()
76-
: Array.Empty<SymbolInformationOrDocumentSymbol>();
77-
78-
return Task.FromResult(new SymbolInformationOrDocumentSymbolContainer(symbols));
80+
return new SymbolInformationOrDocumentSymbolContainer(symbols);
7981
}
8082

81-
private IEnumerable<SymbolReference> ProvideDocumentSymbols(
82-
ScriptFile scriptFile)
83+
private IEnumerable<SymbolReference> ProvideDocumentSymbols(ScriptFile scriptFile)
8384
{
84-
return
85-
InvokeProviders(p => p.ProvideDocumentSymbols(scriptFile))
85+
return InvokeProviders(p => p.ProvideDocumentSymbols(scriptFile))
8686
.SelectMany(r => r);
8787
}
8888

0 commit comments

Comments
 (0)