Skip to content

Remove LINQ usage in language service methods - #737

Merged
Rob Holt (rjmholt) merged 9 commits into
PowerShell:masterfrom
rjmholt:linq-crackdown-language-service
Sep 17, 2018
Merged

Rob Holt (rjmholt) merged 9 commits into
PowerShell:masterfrom
rjmholt:linq-crackdown-language-service

Conversation

@rjmholt

Copy link
Copy Markdown
Contributor

Removes most LINQ calls in the language service methods.

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

@TylerLeonhardt TylerLeonhardt left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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?

@rjmholt Rob Holt (rjmholt) Sep 5, 2018 •

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

@TylerLeonhardt TylerLeonhardt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@rjmholt

Copy link
Copy Markdown
Contributor Author

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

@rjmholt

Copy link
Copy Markdown
Contributor Author

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!

ILogger logger)
{
Validate.IsNotNull("powerShellContext", powerShellContext);
Validate.IsNotNull(nameof(powerShellContext), powerShellContext);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍

// 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@rkeithhill Keith Hill (rkeithhill) left a comment

Copy link
Copy Markdown
Contributor

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