Skip to content

Remove unnecessary LINQ calls from LanguageServer #743

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 7 commits into from
Sep 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all looks great. Just curious what the Acc in symbolAcc is short for? Accumulator?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, accumulator, yes. Habit from writing tail-recursive folds in other languages. Maybe I should change it to something more obvious? Always conscious that having so many variables named essentially the same thing is a code smell, so open to suggestions :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal. Just wanted to make sure I understood what it meant.

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