Fix #17: Add go to definition support for dot sourced file paths#786
Fix #17: Add go to definition support for dot sourced file paths#786rjmholt merged 9 commits intoPowerShell:masterfrom dee-see:go-to-dot-sourced
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 😄 |
|
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. :) |
TylerLeonhardt
left a comment
There was a problem hiding this comment.
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 |
| 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.
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.
Ooop yes definitely, that was sloppy.
| { | ||
| path = path.Replace(variableExpressionAst.ToString(), _psScriptRoot); | ||
| } | ||
| else |
There was a problem hiding this comment.
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. |
Fix #17: Add go to definition support for dot sourced file paths
I would have liked to implement this with
FindDeclarationVisitorbut it didn't really make sense as the definition of a dot-sourcing is the file itself and there is noVisitFileAsAWholemethod 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-Functionand 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.