-
Notifications
You must be signed in to change notification settings - Fork 223
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
Fix #17: Add go to definition support for dot sourced file paths #786
Conversation
Also, it's a bit strange that a dot-sourcing |
Fix build issues (net451 compatibility and public method comments) Fix test Make sure test is not platform-specific
@dee-see thanks for this PR! Sorry for the delay. I was on vacation for a bit 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks pretty good @dee-see. Left a couple of comments. Thanks for your contribution!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me. Thanks @dee-see!
question - does this handle things like |
ping @dee-see :) |
Sorry I missed the notification! It searches the dot-sourced file relative to the file where the dot-sourcing happens (relative to Is that what you meant? Or are you asking if it handles |
I think the second case. But if that's a significant work item in its own respect, I'm all for incremental improvement. |
Ok, I'll look into it anyway. Up to you guys to decide if you prefer having it as a second pull request or as part of this one in the meantime. :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good! Just the one comment 🙂
Done! I'll probably be able to handle |
As far as I know, automatic variables are handled like any other variables -- they come from the object x = null;
using (var pwsh = PowerShell.Create())
{
x = pwsh.AddScript("$variable").Invoke();
} BUT, The nice thing is that |
@dee-see We can probably merge this as-is if you're happy? And then come back to |
If you don't mind I'd keep this open until the next week-end. I'll continue working on |
Yeah iirc it's set in the locals tuple every time a new script block invocation is started. It's set to the equivalent of something like |
Okie doke. We're overdue for a release and given this spate of PRs, I want to run a build at the end of this week, so it would be a good opportunity to test and get a fix out. But I don't want to force anything — just let me know when you're ready for review again :) |
Ok I see! I have some free time tonight, I'll try to complete the |
@rjmholt There you go. I had a cool solution with an Those One cool side effect of this is that Go to definition on functions now works across files that were dot-sourced using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks really great @dee-see. Left a couple of small comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-approving 😉
private static string GetDotSourcedPath(SymbolReference symbol, Workspace workspace, ScriptFile scriptFile) | ||
{ | ||
string cleanedUpSymbol = PathUtils.NormalizePathSeparators(symbol.SymbolName.Trim('\'', '"')); | ||
return workspace.ResolveRelativeScriptPath(Path.GetDirectoryName(scriptFile.FilePath), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth factoring out the Path.GetDirectoryName(scriptFile.FilePath)
as a variable here -- saves an allocation at least.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooop yes definitely, that was sloppy.
{ | ||
path = path.Replace(variableExpressionAst.ToString(), _psScriptRoot); | ||
} | ||
else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than have an else
clause, I would negate the top condition:
if (!(nestedExpression is VariableExpressionAst variableAst
&& variableAst.VariablePath.UserPath.Equals("PSScriptRoot", StringComparison.OrdinalIgnoreCase)))
{
return null;
}
path = path.Replace(variableAst.ToString(), _psScriptRoot);
Just want @TylerLeonhardt to approve before we merge |
This may aid PowerShell/vscode-powershell#144 btw. That issue wants full intellisense I think (which would essentially require us to pre-emptively run the script or otherwise reimplement our completions), but will still help. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍 left a nit
Fix #17: Add go to definition support for dot sourced file paths
I would have liked to implement this with
FindDeclarationVisitor
but it didn't really make sense as the definition of a dot-sourcing is the file itself and there is noVisitFileAsAWhole
method on the visitor!I think the current implementation is a decent compromise though, let me know what you think.
As always, I'm working on Linux and I can't run release build/tests on my machine so I'll keep an eye on the AppVeyor builds.
Side note: I discovered an existing issue with Go To Definition when references were dot-sourced using Windows directory separators when running on Linux.
In this file:
You could F12 on
My-Function
and it would work, but in fact PSES didn't find anything in the "ReferencedFiles" because of the Windows directory separator and had to go over every file in the workspace to find the definition. This bug happens to be fixed by my implementation of Go-To-Dotsourced-Files.