From 180b4c4c3792eb02e24925e89b555cd37127b59b Mon Sep 17 00:00:00 2001 From: David Wilson Date: Thu, 6 Apr 2017 16:44:11 -0700 Subject: [PATCH] Fix crash when performing symbol operations in untitled file This change fixes a host crash which occurs when the user tries to perform a symbol operation (like Go to Definition or Find References) in an untitled script file. The fix is to not try and convert an untitled file's path to a Uri. Resolves PowerShell/vscode-powershell#645 --- .../Server/LanguageServer.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index cbd7db859..ca988c6f9 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -653,7 +653,7 @@ await editorSession.LanguageService.GetDefinitionOfSymbol( definitionLocations.Add( new Location { - Uri = new Uri("file://" + definition.FoundDefinition.FilePath).AbsoluteUri, + Uri = GetFileUri(definition.FoundDefinition.FilePath), Range = GetRangeFromScriptRegion(definition.FoundDefinition.ScriptRegion) }); } @@ -692,7 +692,7 @@ await editorSession.LanguageService.FindReferencesOfSymbol( { return new Location { - Uri = new Uri("file://" + r.FilePath).AbsoluteUri, + Uri = GetFileUri(r.FilePath), Range = GetRangeFromScriptRegion(r.ScriptRegion) }; }) @@ -936,7 +936,7 @@ protected async Task HandleDocumentSymbolRequest( Kind = GetSymbolKind(r.SymbolType), Location = new Location { - Uri = new Uri("file://" + r.FilePath).AbsolutePath, + Uri = GetFileUri(r.FilePath), Range = GetRangeFromScriptRegion(r.ScriptRegion) }, Name = GetDecoratedSymbolName(r) @@ -1009,7 +1009,7 @@ protected async Task HandleWorkspaceSymbolRequest( Kind = r.SymbolType == SymbolType.Variable ? SymbolKind.Variable : SymbolKind.Function, Location = new Location { - Uri = new Uri("file://" + r.FilePath).AbsoluteUri, + Uri = GetFileUri(r.FilePath), Range = GetRangeFromScriptRegion(r.ScriptRegion) }, Name = GetDecoratedSymbolName(r) @@ -1194,6 +1194,15 @@ await this.SendEvent( #region Helper Methods + private static string GetFileUri(string filePath) + { + // If the file isn't untitled, return a URI-style path + return + !filePath.StartsWith("untitled") + ? new Uri("file://" + filePath).AbsoluteUri + : filePath; + } + private static Range GetRangeFromScriptRegion(ScriptRegion scriptRegion) { return new Range