Skip to content

Commit

Permalink
Remove unnecessary LINQ calls from LanguageServer (#743)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rjmholt authored Sep 17, 2018
1 parent f45c631 commit 58742a0
Showing 1 changed file with 100 additions and 117 deletions.
217 changes: 100 additions & 117 deletions src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public class LanguageServer
{
private static CancellationTokenSource existingRequestCancellation;

private static readonly Location[] s_emptyLocationResult = new Location[0];

private static readonly CompletionItem[] s_emptyCompletionResult = new CompletionItem[0];

private static readonly SignatureInformation[] s_emptySignatureResult = new SignatureInformation[0];

private static readonly DocumentHighlight[] s_emptyHighlightResult = new DocumentHighlight[0];

private static readonly SymbolInformation[] s_emptySymbolResult = new SymbolInformation[0];

private ILogger Logger;
private bool profilesLoaded;
private bool consoleReplStarted;
Expand Down Expand Up @@ -692,26 +702,20 @@ await editorSession.LanguageService.FindReferencesOfSymbol(
editorSession.Workspace.ExpandScriptReferences(scriptFile),
editorSession.Workspace);

Location[] referenceLocations = null;
Location[] referenceLocations = s_emptyLocationResult;

if (referencesResult != null)
{
referenceLocations =
referencesResult
.FoundReferences
.Select(r =>
{
return new Location
{
Uri = GetFileUri(r.FilePath),
Range = GetRangeFromScriptRegion(r.ScriptRegion)
};
})
.ToArray();
}
else
{
referenceLocations = new Location[0];
var locations = new List<Location>();
foreach (SymbolReference foundReference in referencesResult.FoundReferences)
{
locations.Add(new Location
{
Uri = GetFileUri(foundReference.FilePath),
Range = GetRangeFromScriptRegion(foundReference.ScriptRegion)
});
}
referenceLocations = locations.ToArray();
}

await requestContext.SendResult(referenceLocations);
Expand All @@ -734,24 +738,18 @@ await editorSession.LanguageService.GetCompletionsInFile(
cursorLine,
cursorColumn);

CompletionItem[] completionItems = null;
CompletionItem[] completionItems = s_emptyCompletionResult;

if (completionResults != null)
{
int sortIndex = 1;
completionItems =
completionResults
.Completions
.Select(
c => CreateCompletionItem(
c,
completionResults.ReplacedRange,
sortIndex++))
.ToArray();
}
else
{
completionItems = new CompletionItem[0];
var completions = new List<CompletionItem>();
foreach (CompletionDetails completion in completionResults.Completions)
{
CompletionItem completionItem = CreateCompletionItem(completion, completionResults.ReplacedRange, sortIndex);
sortIndex++;
}
completionItems = completions.ToArray();
}

await requestContext.SendResult(completionItems);
Expand Down Expand Up @@ -796,40 +794,35 @@ await editorSession.LanguageService.FindParameterSetsInFile(
textDocumentPositionParams.Position.Line + 1,
textDocumentPositionParams.Position.Character + 1);

SignatureInformation[] signatures = null;
int? activeParameter = null;
int? activeSignature = 0;
SignatureInformation[] signatures = s_emptySignatureResult;

if (parameterSets != null)
{
signatures =
parameterSets
.Signatures
.Select(s =>
{
return new SignatureInformation
{
Label = parameterSets.CommandName + " " + s.SignatureText,
Documentation = null,
Parameters =
s.Parameters
.Select(CreateParameterInfo)
.ToArray()
};
})
.ToArray();
}
else
{
signatures = new SignatureInformation[0];
var sigs = new List<SignatureInformation>();
foreach (ParameterSetSignature sig in parameterSets.Signatures)
{
var parameters = new List<ParameterInformation>();
foreach (ParameterInfo paramInfo in sig.Parameters)
{
parameters.Add(CreateParameterInfo(paramInfo));
}

var signature = new SignatureInformation
{
Label = parameterSets.CommandName + " " + sig.SignatureText,
Documentation = null,
Parameters = parameters.ToArray(),
};
}
signatures = sigs.ToArray();
}

await requestContext.SendResult(
new SignatureHelp
{
Signatures = signatures,
ActiveParameter = activeParameter,
ActiveSignature = activeSignature
ActiveParameter = null,
ActiveSignature = 0
});
}

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

DocumentHighlight[] documentHighlights = null;
DocumentHighlight[] documentHighlights = s_emptyHighlightResult;

if (occurrencesResult != null)
{
documentHighlights =
occurrencesResult
.FoundOccurrences
.Select(o =>
{
return new DocumentHighlight
{
Kind = DocumentHighlightKind.Write, // TODO: Which symbol types are writable?
Range = GetRangeFromScriptRegion(o.ScriptRegion)
};
})
.ToArray();
}
else
{
documentHighlights = new DocumentHighlight[0];
var highlights = new List<DocumentHighlight>();
foreach (SymbolReference foundOccurrence in occurrencesResult.FoundOccurrences)
{
highlights.Add(new DocumentHighlight
{
Kind = DocumentHighlightKind.Write, // TODO: Which symbol types are writable?
Range = GetRangeFromScriptRegion(foundOccurrence.ScriptRegion)
});
}
documentHighlights = highlights.ToArray();
}

await requestContext.SendResult(documentHighlights);
Expand Down Expand Up @@ -933,34 +920,29 @@ protected async Task HandleDocumentSymbolRequest(
editorSession.LanguageService.FindSymbolsInFile(
scriptFile);

SymbolInformation[] symbols = null;

string containerName = Path.GetFileNameWithoutExtension(scriptFile.FilePath);

SymbolInformation[] symbols = s_emptySymbolResult;
if (foundSymbols != null)
{
symbols =
foundSymbols
.FoundOccurrences
.Select(r =>
{
return new SymbolInformation
{
ContainerName = containerName,
Kind = GetSymbolKind(r.SymbolType),
Location = new Location
{
Uri = GetFileUri(r.FilePath),
Range = GetRangeFromScriptRegion(r.ScriptRegion)
},
Name = GetDecoratedSymbolName(r)
};
})
.ToArray();
}
else
{
symbols = new SymbolInformation[0];
var symbolAcc = new List<SymbolInformation>();
foreach (SymbolReference foundOccurrence in foundSymbols.FoundOccurrences)
{
var location = new Location
{
Uri = GetFileUri(foundOccurrence.FilePath),
Range = GetRangeFromScriptRegion(foundOccurrence.ScriptRegion)
};

symbolAcc.Add(new SymbolInformation
{
ContainerName = containerName,
Kind = GetSymbolKind(foundOccurrence.SymbolType),
Location = location,
Name = GetDecoratedSymbolName(foundOccurrence)
});
}
symbols = symbolAcc.ToArray();
}

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

if (foundSymbols != null)
{
var matchedSymbols =
foundSymbols
.FoundOccurrences
.Where(r => IsQueryMatch(workspaceSymbolParams.Query, r.SymbolName))
.Select(r =>
{
return new SymbolInformation
{
ContainerName = containerName,
Kind = r.SymbolType == SymbolType.Variable ? SymbolKind.Variable : SymbolKind.Function,
Location = new Location
{
Uri = GetFileUri(r.FilePath),
Range = GetRangeFromScriptRegion(r.ScriptRegion)
},
Name = GetDecoratedSymbolName(r)
};
});

symbols.AddRange(matchedSymbols);
foreach (SymbolReference foundOccurrence in foundSymbols.FoundOccurrences)
{
if (!IsQueryMatch(workspaceSymbolParams.Query, foundOccurrence.SymbolName))
{
continue;
}

var location = new Location
{
Uri = GetFileUri(foundOccurrence.FilePath),
Range = GetRangeFromScriptRegion(foundOccurrence.ScriptRegion)
};

symbols.Add(new SymbolInformation
{
ContainerName = containerName,
Kind = foundOccurrence.SymbolType == SymbolType.Variable ? SymbolKind.Variable : SymbolKind.Function,
Location = location,
Name = GetDecoratedSymbolName(foundOccurrence)
});
}
}
}

Expand Down

0 comments on commit 58742a0

Please sign in to comment.