Skip to content

Remove LINQ usage in language service methods #737

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 9 commits into from
Sep 17, 2018

Conversation

rjmholt
Copy link
Contributor

@rjmholt rjmholt commented Sep 3, 2018

Removes most LINQ calls in the language service methods.

There are also some style changes. They should be contained to commits marked [Style].

Copy link
Member

@TylerLeonhardt TylerLeonhardt left a comment

Choose a reason for hiding this comment

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

LGTM with some minor nits and a question

foundSymbol,
false);

return
Copy link
Member

Choose a reason for hiding this comment

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

nit: put new FindOccurrencesResult on the same line as return?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I prefer that too

if (funcDefnAst.Body.Extent.StartLineNumber == lineNumber - 1)
// of all the function definitions found, return the innermost function
// definition that contains `lineNumber`
foreach (FunctionDefinitionAst foundAst in foundAsts.Cast<FunctionDefinitionAst>())
Copy link
Member

Choose a reason for hiding this comment

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

Do you need to use Cast? Could you just use something like as?

Copy link
Contributor Author

@rjmholt rjmholt Sep 5, 2018

Choose a reason for hiding this comment

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

I don't think as will work since it's an IEnumerable<Ast>. Even with covariant collections, every IEnumerable<Ast> is not an IEnumerable<FunctionDefintionAst>. As far as I know that means we need to use LINQ's Cast<T> or OrType<T>. Since we are expecting all the Asts returned, I chose Cast<T> in this instance. It all stems from the fact that Ast.FindAll() can't return a IEnumerable<T> where T : Ast, just IEnumerable<Ast>, which I suppose is fair enough -- the kind of limitation I constantly see in C#'s type system (trying to be clever about these types using generic constraints has only ever bitten me later on, sadly)

Copy link
Contributor

Choose a reason for hiding this comment

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

The as happens on line 642.

{
if (!_cmdletToAliasDictionary.ContainsKey(aliasInfo.Definition))
{
_cmdletToAliasDictionary.Add(aliasInfo.Definition, new List<String>() { aliasInfo.Name });
Copy link
Member

Choose a reason for hiding this comment

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

nit: don't need the () :) - actually if .Add can accept an array we can refactor this to:

new[] { aliasInfo.Name } I believe

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Personally I don't like omitting the () on the basis that () syntactically indicates a "call" and is consistent with other constructor usage, but happy to do whatever there.

Has to be a List though I think, since in the else branch we are adding to that list. The logic is basically:

if dictionary does not have entry:
    add new dictionary entry with singleton list
else:
    add new item to existing list

foundDefinition.FilePath = nestedModuleFiles[index].FilePath;
}
SymbolReference foundDefinition = null;
for (int i = 0; i < nestedModuleFiles.Length; i++)
Copy link
Member

Choose a reason for hiding this comment

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

did this need to be a for instead of a foreach?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you're right. I was changing from a while, but was probably too conservative

Copy link
Member

@TylerLeonhardt TylerLeonhardt left a comment

Choose a reason for hiding this comment

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

LGTM

@rjmholt
Copy link
Contributor Author

rjmholt commented Sep 6, 2018

Ooooh, I think the tests have actually picked up a breakage here!

@rjmholt
Copy link
Contributor Author

rjmholt commented Sep 6, 2018

Yay I fixed it! The fact that I fixed it makes me happy, but the fact that testing actually found it makes me much much happier!

@@ -55,14 +64,14 @@ public class LanguageService
PowerShellContext powerShellContext,
ILogger logger)
{
Validate.IsNotNull("powerShellContext", powerShellContext);
Validate.IsNotNull(nameof(powerShellContext), powerShellContext);
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@@ -426,8 +437,8 @@ public FindOccurrencesResult FindSymbolsInFile(ScriptFile scriptFile)
if (foundDefinition == null)
{
// Get a list of all powershell files in the workspace path
var allFiles = workspace.EnumeratePSFiles();
foreach (var file in allFiles)
IEnumerable<string> allFiles = workspace.EnumeratePSFiles();
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

{
// For some commands there are no paramsets (like applications). Until
// the valid command types are better understood, catch this exception
// which gets raised when there are no ParameterSets for the command type.
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should log this as a diagnostic level message - we expect this exception in some cases but just in case we're getting this for cases we didn't expect, it might be good to have a log entry when log level set to Diagnostic.

{
return ast is StatementAst && ast.Extent.Contains(lineNumber, columnNumber);
}, true);

// Find ast with the smallest extent
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be nice not to lose this comment.

Copy link
Contributor

@rkeithhill rkeithhill left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants