Skip to content

Commit 58742a0

Browse files
authored
Remove unnecessary LINQ calls from LanguageServer (#743)
* Remove reference request select call * Remove completion request select call * Remove signature request select calls * Remove highlight request select call * Remove select call in document symbol request * Remove linq in workspace symbol request
1 parent f45c631 commit 58742a0

File tree

1 file changed

+100
-117
lines changed

1 file changed

+100
-117
lines changed

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

+100-117
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ public class LanguageServer
2828
{
2929
private static CancellationTokenSource existingRequestCancellation;
3030

31+
private static readonly Location[] s_emptyLocationResult = new Location[0];
32+
33+
private static readonly CompletionItem[] s_emptyCompletionResult = new CompletionItem[0];
34+
35+
private static readonly SignatureInformation[] s_emptySignatureResult = new SignatureInformation[0];
36+
37+
private static readonly DocumentHighlight[] s_emptyHighlightResult = new DocumentHighlight[0];
38+
39+
private static readonly SymbolInformation[] s_emptySymbolResult = new SymbolInformation[0];
40+
3141
private ILogger Logger;
3242
private bool profilesLoaded;
3343
private bool consoleReplStarted;
@@ -692,26 +702,20 @@ await editorSession.LanguageService.FindReferencesOfSymbol(
692702
editorSession.Workspace.ExpandScriptReferences(scriptFile),
693703
editorSession.Workspace);
694704

695-
Location[] referenceLocations = null;
705+
Location[] referenceLocations = s_emptyLocationResult;
696706

697707
if (referencesResult != null)
698708
{
699-
referenceLocations =
700-
referencesResult
701-
.FoundReferences
702-
.Select(r =>
703-
{
704-
return new Location
705-
{
706-
Uri = GetFileUri(r.FilePath),
707-
Range = GetRangeFromScriptRegion(r.ScriptRegion)
708-
};
709-
})
710-
.ToArray();
711-
}
712-
else
713-
{
714-
referenceLocations = new Location[0];
709+
var locations = new List<Location>();
710+
foreach (SymbolReference foundReference in referencesResult.FoundReferences)
711+
{
712+
locations.Add(new Location
713+
{
714+
Uri = GetFileUri(foundReference.FilePath),
715+
Range = GetRangeFromScriptRegion(foundReference.ScriptRegion)
716+
});
717+
}
718+
referenceLocations = locations.ToArray();
715719
}
716720

717721
await requestContext.SendResult(referenceLocations);
@@ -734,24 +738,18 @@ await editorSession.LanguageService.GetCompletionsInFile(
734738
cursorLine,
735739
cursorColumn);
736740

737-
CompletionItem[] completionItems = null;
741+
CompletionItem[] completionItems = s_emptyCompletionResult;
738742

739743
if (completionResults != null)
740744
{
741745
int sortIndex = 1;
742-
completionItems =
743-
completionResults
744-
.Completions
745-
.Select(
746-
c => CreateCompletionItem(
747-
c,
748-
completionResults.ReplacedRange,
749-
sortIndex++))
750-
.ToArray();
751-
}
752-
else
753-
{
754-
completionItems = new CompletionItem[0];
746+
var completions = new List<CompletionItem>();
747+
foreach (CompletionDetails completion in completionResults.Completions)
748+
{
749+
CompletionItem completionItem = CreateCompletionItem(completion, completionResults.ReplacedRange, sortIndex);
750+
sortIndex++;
751+
}
752+
completionItems = completions.ToArray();
755753
}
756754

757755
await requestContext.SendResult(completionItems);
@@ -796,40 +794,35 @@ await editorSession.LanguageService.FindParameterSetsInFile(
796794
textDocumentPositionParams.Position.Line + 1,
797795
textDocumentPositionParams.Position.Character + 1);
798796

799-
SignatureInformation[] signatures = null;
800-
int? activeParameter = null;
801-
int? activeSignature = 0;
797+
SignatureInformation[] signatures = s_emptySignatureResult;
802798

803799
if (parameterSets != null)
804800
{
805-
signatures =
806-
parameterSets
807-
.Signatures
808-
.Select(s =>
809-
{
810-
return new SignatureInformation
811-
{
812-
Label = parameterSets.CommandName + " " + s.SignatureText,
813-
Documentation = null,
814-
Parameters =
815-
s.Parameters
816-
.Select(CreateParameterInfo)
817-
.ToArray()
818-
};
819-
})
820-
.ToArray();
821-
}
822-
else
823-
{
824-
signatures = new SignatureInformation[0];
801+
var sigs = new List<SignatureInformation>();
802+
foreach (ParameterSetSignature sig in parameterSets.Signatures)
803+
{
804+
var parameters = new List<ParameterInformation>();
805+
foreach (ParameterInfo paramInfo in sig.Parameters)
806+
{
807+
parameters.Add(CreateParameterInfo(paramInfo));
808+
}
809+
810+
var signature = new SignatureInformation
811+
{
812+
Label = parameterSets.CommandName + " " + sig.SignatureText,
813+
Documentation = null,
814+
Parameters = parameters.ToArray(),
815+
};
816+
}
817+
signatures = sigs.ToArray();
825818
}
826819

827820
await requestContext.SendResult(
828821
new SignatureHelp
829822
{
830823
Signatures = signatures,
831-
ActiveParameter = activeParameter,
832-
ActiveSignature = activeSignature
824+
ActiveParameter = null,
825+
ActiveSignature = 0
833826
});
834827
}
835828

@@ -847,26 +840,20 @@ protected async Task HandleDocumentHighlightRequest(
847840
textDocumentPositionParams.Position.Line + 1,
848841
textDocumentPositionParams.Position.Character + 1);
849842

850-
DocumentHighlight[] documentHighlights = null;
843+
DocumentHighlight[] documentHighlights = s_emptyHighlightResult;
851844

852845
if (occurrencesResult != null)
853846
{
854-
documentHighlights =
855-
occurrencesResult
856-
.FoundOccurrences
857-
.Select(o =>
858-
{
859-
return new DocumentHighlight
860-
{
861-
Kind = DocumentHighlightKind.Write, // TODO: Which symbol types are writable?
862-
Range = GetRangeFromScriptRegion(o.ScriptRegion)
863-
};
864-
})
865-
.ToArray();
866-
}
867-
else
868-
{
869-
documentHighlights = new DocumentHighlight[0];
847+
var highlights = new List<DocumentHighlight>();
848+
foreach (SymbolReference foundOccurrence in occurrencesResult.FoundOccurrences)
849+
{
850+
highlights.Add(new DocumentHighlight
851+
{
852+
Kind = DocumentHighlightKind.Write, // TODO: Which symbol types are writable?
853+
Range = GetRangeFromScriptRegion(foundOccurrence.ScriptRegion)
854+
});
855+
}
856+
documentHighlights = highlights.ToArray();
870857
}
871858

872859
await requestContext.SendResult(documentHighlights);
@@ -933,34 +920,29 @@ protected async Task HandleDocumentSymbolRequest(
933920
editorSession.LanguageService.FindSymbolsInFile(
934921
scriptFile);
935922

936-
SymbolInformation[] symbols = null;
937-
938923
string containerName = Path.GetFileNameWithoutExtension(scriptFile.FilePath);
939924

925+
SymbolInformation[] symbols = s_emptySymbolResult;
940926
if (foundSymbols != null)
941927
{
942-
symbols =
943-
foundSymbols
944-
.FoundOccurrences
945-
.Select(r =>
946-
{
947-
return new SymbolInformation
948-
{
949-
ContainerName = containerName,
950-
Kind = GetSymbolKind(r.SymbolType),
951-
Location = new Location
952-
{
953-
Uri = GetFileUri(r.FilePath),
954-
Range = GetRangeFromScriptRegion(r.ScriptRegion)
955-
},
956-
Name = GetDecoratedSymbolName(r)
957-
};
958-
})
959-
.ToArray();
960-
}
961-
else
962-
{
963-
symbols = new SymbolInformation[0];
928+
var symbolAcc = new List<SymbolInformation>();
929+
foreach (SymbolReference foundOccurrence in foundSymbols.FoundOccurrences)
930+
{
931+
var location = new Location
932+
{
933+
Uri = GetFileUri(foundOccurrence.FilePath),
934+
Range = GetRangeFromScriptRegion(foundOccurrence.ScriptRegion)
935+
};
936+
937+
symbolAcc.Add(new SymbolInformation
938+
{
939+
ContainerName = containerName,
940+
Kind = GetSymbolKind(foundOccurrence.SymbolType),
941+
Location = location,
942+
Name = GetDecoratedSymbolName(foundOccurrence)
943+
});
944+
}
945+
symbols = symbolAcc.ToArray();
964946
}
965947

966948
await requestContext.SendResult(symbols);
@@ -1011,26 +993,27 @@ protected async Task HandleWorkspaceSymbolRequest(
1011993

1012994
if (foundSymbols != null)
1013995
{
1014-
var matchedSymbols =
1015-
foundSymbols
1016-
.FoundOccurrences
1017-
.Where(r => IsQueryMatch(workspaceSymbolParams.Query, r.SymbolName))
1018-
.Select(r =>
1019-
{
1020-
return new SymbolInformation
1021-
{
1022-
ContainerName = containerName,
1023-
Kind = r.SymbolType == SymbolType.Variable ? SymbolKind.Variable : SymbolKind.Function,
1024-
Location = new Location
1025-
{
1026-
Uri = GetFileUri(r.FilePath),
1027-
Range = GetRangeFromScriptRegion(r.ScriptRegion)
1028-
},
1029-
Name = GetDecoratedSymbolName(r)
1030-
};
1031-
});
1032-
1033-
symbols.AddRange(matchedSymbols);
996+
foreach (SymbolReference foundOccurrence in foundSymbols.FoundOccurrences)
997+
{
998+
if (!IsQueryMatch(workspaceSymbolParams.Query, foundOccurrence.SymbolName))
999+
{
1000+
continue;
1001+
}
1002+
1003+
var location = new Location
1004+
{
1005+
Uri = GetFileUri(foundOccurrence.FilePath),
1006+
Range = GetRangeFromScriptRegion(foundOccurrence.ScriptRegion)
1007+
};
1008+
1009+
symbols.Add(new SymbolInformation
1010+
{
1011+
ContainerName = containerName,
1012+
Kind = foundOccurrence.SymbolType == SymbolType.Variable ? SymbolKind.Variable : SymbolKind.Function,
1013+
Location = location,
1014+
Name = GetDecoratedSymbolName(foundOccurrence)
1015+
});
1016+
}
10341017
}
10351018
}
10361019

0 commit comments

Comments
 (0)