diff --git a/src/PowerShellEditorServices/Server/PsesLanguageServer.cs b/src/PowerShellEditorServices/Server/PsesLanguageServer.cs index caddc5e5f..ed62b206d 100644 --- a/src/PowerShellEditorServices/Server/PsesLanguageServer.cs +++ b/src/PowerShellEditorServices/Server/PsesLanguageServer.cs @@ -74,8 +74,8 @@ public async Task StartAsync() _additionalModules)) .ConfigureLogging(builder => builder .AddSerilog(Log.Logger) + .AddLanguageServer(LogLevel.Trace) .SetMinimumLevel(LogLevel.Trace)) - .AddDefaultLoggingProvider() .WithHandler() .WithHandler() .WithHandler() diff --git a/src/PowerShellEditorServices/Services/CodeLens/ReferencesCodeLensProvider.cs b/src/PowerShellEditorServices/Services/CodeLens/ReferencesCodeLensProvider.cs index 5d1a55ae6..1ef194a56 100644 --- a/src/PowerShellEditorServices/Services/CodeLens/ReferencesCodeLensProvider.cs +++ b/src/PowerShellEditorServices/Services/CodeLens/ReferencesCodeLensProvider.cs @@ -45,8 +45,7 @@ public ReferencesCodeLensProvider(WorkspaceService workspaceService, SymbolsServ _workspaceService = workspaceService; _symbolsService = symbolsService; // TODO: Pull this from components - _symbolProvider = new ScriptDocumentSymbolProvider( - VersionUtils.PSVersion); + _symbolProvider = new ScriptDocumentSymbolProvider(); } /// diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetVersionHandler.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetVersionHandler.cs index ad8fd62d1..a1c738ee1 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetVersionHandler.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetVersionHandler.cs @@ -39,7 +39,7 @@ public Task Handle(GetVersionParams request, CancellationToke return Task.FromResult(new PowerShellVersion { - Version = VersionUtils.PSVersion.ToString(), + Version = VersionUtils.PSVersionString, Edition = VersionUtils.PSEdition, DisplayVersion = VersionUtils.PSVersion.ToString(2), Architecture = architecture.ToString() diff --git a/src/PowerShellEditorServices/Services/Symbols/ScriptDocumentSymbolProvider.cs b/src/PowerShellEditorServices/Services/Symbols/ScriptDocumentSymbolProvider.cs index 0ad1f20ea..1ba81b180 100644 --- a/src/PowerShellEditorServices/Services/Symbols/ScriptDocumentSymbolProvider.cs +++ b/src/PowerShellEditorServices/Services/Symbols/ScriptDocumentSymbolProvider.cs @@ -17,18 +17,6 @@ namespace Microsoft.PowerShell.EditorServices.Services.Symbols /// public class ScriptDocumentSymbolProvider : IDocumentSymbolProvider { - private Version powerShellVersion; - - /// - /// Creates an instance of the ScriptDocumentSymbolProvider to - /// target the specified PowerShell version. - /// - /// The target PowerShell version. - public ScriptDocumentSymbolProvider(Version powerShellVersion) - { - this.powerShellVersion = powerShellVersion; - } - IEnumerable IDocumentSymbolProvider.ProvideDocumentSymbols( ScriptFile scriptFile) { @@ -37,10 +25,7 @@ IEnumerable IDocumentSymbolProvider.ProvideDocumentSymbols( (scriptFile.FilePath.EndsWith(".ps1", StringComparison.OrdinalIgnoreCase) || scriptFile.FilePath.EndsWith(".psm1", StringComparison.OrdinalIgnoreCase))) { - return - FindSymbolsInDocument( - scriptFile.ScriptAst, - this.powerShellVersion); + return FindSymbolsInDocument(scriptFile.ScriptAst); } return Enumerable.Empty(); @@ -52,7 +37,7 @@ IEnumerable IDocumentSymbolProvider.ProvideDocumentSymbols( /// The abstract syntax tree of the given script /// The PowerShell version the Ast was generated from /// A collection of SymbolReference objects - static public IEnumerable FindSymbolsInDocument(Ast scriptAst, Version powerShellVersion) + static public IEnumerable FindSymbolsInDocument(Ast scriptAst) { IEnumerable symbolReferences = null; diff --git a/src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs b/src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs index f3c9c53b0..a9a36923d 100644 --- a/src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs +++ b/src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs @@ -54,7 +54,7 @@ public SymbolsService( _workspaceService = workspaceService; _documentSymbolProviders = new IDocumentSymbolProvider[] { - new ScriptDocumentSymbolProvider(VersionUtils.PSVersion), + new ScriptDocumentSymbolProvider(), new PsdDocumentSymbolProvider(), new PesterDocumentSymbolProvider() }; diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs index 52ba0660b..3a2af8838 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs @@ -44,8 +44,7 @@ public DocumentSymbolHandler(ILoggerFactory factory, ConfigurationService config _workspaceService = workspaceService; _providers = new IDocumentSymbolProvider[] { - new ScriptDocumentSymbolProvider( - VersionUtils.PSVersion), + new ScriptDocumentSymbolProvider(), new PsdDocumentSymbolProvider(), new PesterDocumentSymbolProvider() }; diff --git a/src/PowerShellEditorServices/Utility/VersionUtils.cs b/src/PowerShellEditorServices/Utility/VersionUtils.cs index 5406ba1b6..e2f2be54d 100644 --- a/src/PowerShellEditorServices/Utility/VersionUtils.cs +++ b/src/PowerShellEditorServices/Utility/VersionUtils.cs @@ -20,15 +20,20 @@ internal static class VersionUtils public static bool IsNetCore { get; } = RuntimeInformation.FrameworkDescription.StartsWith(".NET Core", StringComparison.Ordinal); /// - /// Get's the Version of PowerShell being used. + /// Gets the Version of PowerShell being used. /// public static Version PSVersion { get; } = PowerShellReflectionUtils.PSVersion; /// - /// Get's the Edition of PowerShell being used. + /// Gets the Edition of PowerShell being used. /// public static string PSEdition { get; } = PowerShellReflectionUtils.PSEdition; + /// + /// Gets the string of the PSVersion including prerelease tags if it applies. + /// + public static string PSVersionString { get; } = PowerShellReflectionUtils.PSVersionString; + /// /// True if we are running in Windows PowerShell, false otherwise. /// @@ -49,8 +54,16 @@ internal static class PowerShellReflectionUtils { private static readonly Type s_psVersionInfoType = typeof(System.Management.Automation.Runspaces.Runspace).Assembly.GetType("System.Management.Automation.PSVersionInfo"); + + // This property is a Version type in PowerShell. It's existed since 5.1, but it was only made public in 6.2. private static readonly PropertyInfo s_psVersionProperty = s_psVersionInfoType .GetProperty("PSVersion", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); + + // This property is a SemanticVersion in PowerShell that contains the prerelease tag as well. + // It was added in 6.2 so we can't depend on it for anything before. + private static readonly PropertyInfo s_psCurrentVersionProperty = s_psVersionInfoType + .GetProperty("PSCurrentVersion", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); + private static readonly PropertyInfo s_psEditionProperty = s_psVersionInfoType .GetProperty("PSEdition", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); @@ -64,5 +77,12 @@ internal static class PowerShellReflectionUtils /// Get's the Edition of PowerShell being used. /// public static string PSEdition { get; } = s_psEditionProperty.GetValue(null) as string; + + /// + /// Gets the stringified version of PowerShell including prerelease tags if it applies. + /// + public static string PSVersionString { get; } = s_psCurrentVersionProperty != null + ? s_psCurrentVersionProperty.GetValue(null).ToString() + : s_psVersionProperty.GetValue(null).ToString(); } }