Skip to content
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

RenameProvider for variable/function renaming #2152

Open
wants to merge 203 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
203 commits
Select commit Hold shift + click to select a range
824debe
v3.3.0: Logging updates and dropped EOL PowerShell
andyleejordan Nov 15, 2024
8b270f9
adding in rename symbol service
Razmo99 Mar 26, 2023
8b66704
switched to using a task not sure if there is a better way
Razmo99 Mar 26, 2023
7d958b6
completed rename function
Razmo99 Mar 26, 2023
1e4d0bf
slight refactoring
Razmo99 Mar 26, 2023
ac1b1c8
Adding in unit teste for refactoring functions
Razmo99 Mar 29, 2023
2d7bec4
test case for a function that is flat or inline
Razmo99 Mar 29, 2023
0863aa7
added new test case
Razmo99 Sep 13, 2023
77f4693
initial commit
Razmo99 Sep 18, 2023
f458287
converted visitor class from powershell to C#
Razmo99 Sep 18, 2023
d765fda
Updated to using ps1 file with a renamed varient
Razmo99 Sep 19, 2023
bb9847c
Bug Fixes now passing all tests
Razmo99 Sep 19, 2023
64e89bd
Stripped un-needed functions now using FunctionRename class
Razmo99 Sep 19, 2023
38c8e73
small clean up of init of class
Razmo99 Sep 25, 2023
655bb96
unneeded init of class var
Razmo99 Sep 25, 2023
4e32529
init commit of variable visitor class
Razmo99 Sep 25, 2023
4eb9b78
piping for vscode rename symbol
Razmo99 Sep 25, 2023
1a3fe4e
initial tests for variable visitor class
Razmo99 Sep 25, 2023
3e6da81
fixing typo
Razmo99 Sep 25, 2023
d57822d
adjusting scopestack to store Ast objects
Razmo99 Sep 25, 2023
e5d0875
added additional tests for variablerename visitor
Razmo99 Sep 26, 2023
9ea0f0a
adjusting so it finds the first variable definition within scope
Razmo99 Sep 26, 2023
dad669d
added function to get variable top assignment
Razmo99 Sep 26, 2023
deeacdb
renamed scriptfile to scriptast
Razmo99 Sep 26, 2023
f5bb1d5
can visit binary expressions now
Razmo99 Sep 26, 2023
a266d61
can visit dountil and dowhile
Razmo99 Sep 26, 2023
e95e56d
logic to stop start shouldrename
Razmo99 Sep 26, 2023
7580d67
can visit scriptexpressions now
Razmo99 Sep 26, 2023
1b0dc31
start stop logic for if a redefinition is found
Razmo99 Sep 26, 2023
9f3cac8
formatting
Razmo99 Sep 26, 2023
47d1716
implemented visithastable
Razmo99 Sep 27, 2023
b4cc857
function to determine if a node is within a targets scope
Razmo99 Sep 27, 2023
dc9af0d
adjusted get variable top assignment for better detection
Razmo99 Sep 27, 2023
72eb38b
additional test cases
Razmo99 Sep 27, 2023
d364d40
additional tests
Razmo99 Sep 27, 2023
1f52e77
added break for finding the top variable assignment
Razmo99 Sep 27, 2023
219d01c
implemented visit command parameter
Razmo99 Sep 27, 2023
fe2d359
implemented visit member expression
Razmo99 Sep 27, 2023
6df0fa0
implemented visitparentexpression
Razmo99 Sep 27, 2023
8af56bc
altered logic for variable renamed to check operator is equals
Razmo99 Sep 27, 2023
fa563a6
removing examples file
Razmo99 Sep 27, 2023
160f8ba
formatting and additional test
Razmo99 Sep 28, 2023
62350e6
formatting and proper sorting for gettestscript
Razmo99 Sep 28, 2023
3d85a64
additional test data
Razmo99 Sep 28, 2023
734601a
reworked class so that oldname is no longer needed
Razmo99 Sep 28, 2023
35e834b
implemented some new visitors
Razmo99 Sep 28, 2023
ae6a052
early start on commandparameter renaming
Razmo99 Sep 28, 2023
9cc8eff
more implementations and some formatting
Razmo99 Sep 28, 2023
314a665
logic to determin if we are renaming a var or parameter
Razmo99 Sep 28, 2023
b49898e
additional test
Razmo99 Sep 28, 2023
92d429a
additional tests for parameters
Razmo99 Sep 30, 2023
ff09c6f
adjusting checks for parameters
Razmo99 Sep 30, 2023
56dd927
case insensitive compare & adjustment for get ast parent scope to fav…
Razmo99 Sep 30, 2023
b097c4b
initial implentation of prepare rename provider
Razmo99 Oct 13, 2023
849920e
new test to handle detection for if the target function is a param ast
Razmo99 Oct 13, 2023
9384b3a
new exception for when dot sourcing is detected
Razmo99 Oct 13, 2023
8e9fffe
added more detection and errors for prepare rename symbol
Razmo99 Oct 13, 2023
682be29
new exception for when the function definition cannot be found
Razmo99 Oct 13, 2023
8def8bc
no longer using trygetsymbolatposition as it doesnt detect parameterA…
Razmo99 Oct 13, 2023
7a9aa03
further adjustments to detection
Razmo99 Oct 13, 2023
e9f990f
switched to processing using iteration to avoid stack overflow
Razmo99 Oct 14, 2023
5ee72cc
Fixing typo
Razmo99 Oct 14, 2023
65dc4e7
Switching tests to use iterative class
Razmo99 Oct 14, 2023
8b1de29
init version of the variable rename iterative
Razmo99 Oct 14, 2023
eea9369
switched tests and vscode to use iterative class
Razmo99 Oct 14, 2023
8b55eac
new test to check for method with the same parameter name
Razmo99 Oct 14, 2023
3c0364e
fixing up tests for VariableParameterCommandWithSameName
Razmo99 Oct 14, 2023
4a59385
fixing up tests
Razmo99 Oct 14, 2023
0fd2601
adjusting tests for more complexity
Razmo99 Oct 14, 2023
8cc9ba0
now adds Alias on commandParameterRenaming
Razmo99 Oct 14, 2023
d6da799
refactored alias creation for readability
Razmo99 Oct 15, 2023
828a1f5
updated prepeare rename symbol to use iterative and added msg for if …
Razmo99 Oct 15, 2023
3328497
renamed renamevariableiterative to IterativeVariableRename
Razmo99 Oct 15, 2023
d90a5ea
using switch instead of else if
Razmo99 Oct 15, 2023
8cc30d6
formatting for rename symbol
Razmo99 Oct 15, 2023
8037082
moved Function shared test content into its own folder
Razmo99 Oct 15, 2023
22f0eb7
New Test for splatted variable parameter renaming
Razmo99 Oct 15, 2023
de41454
first stage of supporting symbol renaming for splatted command Ast calls
Razmo99 Oct 15, 2023
56187f6
split out exceptions into generic file
Razmo99 Oct 15, 2023
b119d5a
updated to use its own internal version
Razmo99 Oct 15, 2023
2195998
added functionality to reverse lookup the top variable from a splat
Razmo99 Oct 15, 2023
664ffdb
Detter symbol detection was timing out on larger files
Razmo99 Oct 15, 2023
c332955
added utilities for common renaming functions updated tests
Razmo99 Oct 15, 2023
acb9e0a
adjusted rename to use utilities
Razmo99 Oct 15, 2023
44a0499
added comments to NewSplattedModification
Razmo99 Oct 24, 2023
95c4540
adjusted test to use -username param instead of -password due to Alia…
Razmo99 Oct 24, 2023
0320723
extracted method of LookForParentOfType from GetFuncDecFromCommAst
Razmo99 Oct 24, 2023
5ca399c
adjusted LookForParent so it accepts multiple types to look for
Razmo99 Oct 24, 2023
4b26780
adjusting iterative functions to use LookForParentOfType method
Razmo99 Oct 24, 2023
2e704d2
Moved GetAstNodeByLineAndColumn to a generic method
Razmo99 Oct 24, 2023
d683f70
updated visitors to use generic GetAstNodeByLineAndColumn
Razmo99 Oct 24, 2023
869414d
formatting moved GetFunctionDefByCommandAst to Utilities
Razmo99 Oct 24, 2023
0dd2f16
Refactoring to use Utilities class for generic methods
Razmo99 Oct 24, 2023
5311641
Renaming methods to be clearer
Razmo99 Oct 24, 2023
ea75f46
Added a test to get a function definition ast
Razmo99 Mar 21, 2024
b8859fe
additional checks in getast for namedblock detection
Razmo99 Mar 21, 2024
2ae405a
formatting an new test for finding a function
Razmo99 Oct 27, 2023
dc9005a
reworked GetAst for better detection
Razmo99 Oct 27, 2023
e817c0e
additional changes to getast utility method
Razmo99 Mar 21, 2024
e3ce343
reverting changes from bad merge request pull
Razmo99 Mar 21, 2024
6daf20e
removing unused properties of the class
Razmo99 Mar 22, 2024
828c3a5
migrated FunctionDefinitionNotFoundException to exceptions.cs
Razmo99 Mar 22, 2024
700c5fe
removed original recursive visitor classes
Razmo99 Mar 22, 2024
b04a20e
removed unsued class properties from function visitor
Razmo99 Mar 22, 2024
8a9ede2
condensing if statements
Razmo99 Mar 22, 2024
ab11522
Broke up Process node into 3 sub functions for readability
Razmo99 Mar 22, 2024
0c94c65
fixing comment grammar
Razmo99 Mar 22, 2024
d10d900
New Method and tests to check if a script ast contains dot sourcing
Razmo99 Mar 24, 2024
1adec8b
finalised dot source detection and notification
Razmo99 Mar 24, 2024
7f5eb44
fixing spelling / naming mistakes
Razmo99 Mar 24, 2024
434805b
removing .vscode files
Razmo99 Mar 25, 2024
8195125
deleting package-lock.json
Razmo99 Mar 25, 2024
a5b6f2e
cleaning up comments and removed unused code
Razmo99 Mar 25, 2024
982bfdf
Adjusted refactoring Tests to use IAsyncLifetime instead of IDisposable
Razmo99 Jun 2, 2024
c89ed16
Fix Path.Combine to be cross platform
JustinGrote Jun 3, 2024
7d50476
Fixing an odd edge case, of not being to rename a variable directly u…
Razmo99 Jun 5, 2024
d66397c
added tests and logic for duplicate assignment cases for; foreach and…
Razmo99 Jun 5, 2024
1269cc3
Adding in out of scope $i to test case which shouldnt be renamed
Razmo99 Jun 5, 2024
d2090cc
.net 8 requires a newline at the start of the script to recognise the…
Razmo99 Jun 6, 2024
e51fb49
fixing type in test name
Razmo99 Jun 6, 2024
a436dcc
CommandAst input was being sent to variable renamer not function rena…
Razmo99 Jun 6, 2024
cef1696
additional condition so that a function CommandAst will not be touche…
Razmo99 Jun 7, 2024
54497e1
Making RenameSymbolParams public for xunit serializer
Razmo99 Jun 7, 2024
7a2b76f
Renamed Test .ps1 to match class property name, reworked test cases t…
Razmo99 Jun 7, 2024
95c677b
consolidated tests as their are no special requirements
Razmo99 Jun 7, 2024
3d90b41
moved serializer and getmodifiedscriptcontent seperate file as it wil…
Razmo99 Jun 7, 2024
dd0194a
Adding missing test case renamed.ps1 varient
Razmo99 Jun 7, 2024
bb9d277
removing unused test case data
Razmo99 Jun 7, 2024
8b7ff3a
updated GetModifiedScript with changes from the VariableRenameTests, …
Razmo99 Jun 7, 2024
0c778db
modified variable test cases to use parameterized test cases
Razmo99 Jun 7, 2024
83ab5b7
Added a new test case for renaming an inner variable leaking out the …
Razmo99 Jun 7, 2024
b239392
reworked applicable utilities tests to be parameterized
Razmo99 Jun 7, 2024
e1e2112
Added test case for simple function parameter rename, added clause fo…
Razmo99 Jun 7, 2024
b3cb27f
adding plumbling for shouldgenerateAlias on server side
Razmo99 Jun 7, 2024
b465f6e
Passing through shouldGenerateAlias to VariableVisitor Class
Razmo99 Jun 7, 2024
895c360
added new test cases for functions with variables defined outside of …
Razmo99 Jun 7, 2024
ae10c37
renaming ShouldGenerateAlias to create CreateAlias
Razmo99 Jun 10, 2024
08a8685
Add Missing Disclaimer
JustinGrote Sep 12, 2024
fec5319
Explicitly Show Unsaved Files as currently unsupported. It's probably…
JustinGrote Sep 12, 2024
70af863
Move all Handling to OmniSharp LSP types
JustinGrote Sep 12, 2024
bdfec36
Move HandlerError
JustinGrote Sep 14, 2024
d196582
Rework initial AST filter
JustinGrote Sep 15, 2024
1d86dcc
Reorganize Tests under Handler folder
JustinGrote Sep 15, 2024
d2d060c
Lots of removing of custom types. Currently broken until I create a S…
JustinGrote Sep 15, 2024
7ef4dfa
Rework RenameHandler to use Adapters
JustinGrote Sep 15, 2024
36df0f0
Fix namespacing
JustinGrote Sep 15, 2024
b5035c1
Remove Alias from tests for now, will have separate Alias test in future
JustinGrote Sep 15, 2024
2d15151
Default CreateAlias to false per feedback
JustinGrote Sep 15, 2024
0505be7
Reworked Visitor to use ScriptPositionAdapter
JustinGrote Sep 15, 2024
554d4c1
Fixup tests with default PowerShell Formatting
JustinGrote Sep 15, 2024
58fab45
Refine VariableVisitor to use ScriptExtentAdapter
JustinGrote Sep 15, 2024
7254b6f
Extract some duplicate functions and enable NRT checking for RenameHa…
JustinGrote Sep 16, 2024
57ca684
Add initial EUA prompt scaffolding
JustinGrote Sep 16, 2024
6e196be
Move RenameHandler to TextDocument (more appropriate context)
JustinGrote Sep 17, 2024
d87ae85
Remove Unnecessary code
JustinGrote Sep 17, 2024
690119f
Split out RenameService. **TESTS NEED FIXING**
JustinGrote Sep 17, 2024
209f2ef
Introduce service to the Extension
JustinGrote Sep 17, 2024
e6d3933
Redo tests to be data-driven
JustinGrote Sep 17, 2024
2bcabba
Fixup Tests, still a bug in rename logic with functions
JustinGrote Sep 18, 2024
fa10f68
Fixed all function Prepare tests
JustinGrote Sep 18, 2024
46119c7
Fixed all variable PrepareRenameHandler Tests
JustinGrote Sep 18, 2024
05e0b55
First Stage work to move to a more stateless AstVisitor for renames
JustinGrote Sep 23, 2024
5b4f54d
Separate out AstExtensions and continue Functions Reimplement. Functi…
JustinGrote Sep 24, 2024
3f81bcd
Move renameservice
JustinGrote Sep 24, 2024
00ea69f
Breakout and simplify matching logic, fixes more test cases
JustinGrote Sep 24, 2024
8b8f485
SkipChildren doesn't work for nested function situations. More tests …
JustinGrote Sep 24, 2024
d187a39
All rename function tests fixed
JustinGrote Sep 24, 2024
089d611
Add disclaimer scaffolding (needs config)
JustinGrote Sep 24, 2024
b92debf
Add more config setup and lock down some classes
JustinGrote Sep 24, 2024
db22ff2
Add configuration and adjust opt-in message due to server-side LSP co…
JustinGrote Sep 24, 2024
ddd65b3
Add mocks for configuration for testing, still need to fix some param…
JustinGrote Sep 25, 2024
7bb4693
Bonus Assertions
JustinGrote Sep 25, 2024
0f30aca
Fix all Tests
JustinGrote Sep 26, 2024
6a0ca49
Actually fix tests (missing some pattern matching on AST types)
JustinGrote Sep 26, 2024
35c24dd
Small format adjust
JustinGrote Sep 26, 2024
31fcf32
Format Update
JustinGrote Sep 26, 2024
abadcc0
Move IterativeVariableVisitor into RenameService class
JustinGrote Sep 26, 2024
37538b6
Remove unnecessary intermediate tests
JustinGrote Sep 26, 2024
9467aa6
Internalize rename visitor into renameservice
JustinGrote Sep 26, 2024
90cf02e
Perform initial abstraction of visitors to a base class
JustinGrote Sep 26, 2024
1af2a87
Add disclaimer link
JustinGrote Sep 26, 2024
8575c41
Apply formatting to test fixtures
JustinGrote Sep 26, 2024
1d8e57a
Fix issue with dependency injection weirdly setting Disclaimer to tru…
JustinGrote Sep 27, 2024
fdf05a9
Change name of Alias Setting
JustinGrote Sep 27, 2024
dbc6f69
Make the PrepareRenameSymbol return more legible
JustinGrote Sep 27, 2024
a3e8557
Add support for negative test cases
JustinGrote Sep 27, 2024
a8d72e6
I thought I fixed all these...
JustinGrote Sep 27, 2024
0621173
Small test fixes and naming updates
JustinGrote Sep 27, 2024
0abc713
Move AstExtensions to Utility
JustinGrote Sep 27, 2024
703c804
Remove utilities as they have been moved into RenameService or AstExt…
JustinGrote Sep 27, 2024
2800ab2
Rewrote Variable Handler, all tests passing except a stringconstant s…
JustinGrote Sep 29, 2024
81bd16e
Fix up Splat function finding, all tests pass
JustinGrote Sep 29, 2024
26fe7d9
Clean out some dead code
JustinGrote Sep 30, 2024
1042ab8
Add name validation and related tests, also rearrange/rename some tes…
JustinGrote Sep 30, 2024
d06bb51
Update readme with current unsupported scenarios
JustinGrote Oct 1, 2024
627365c
Rework function name check to be ast based
JustinGrote Oct 1, 2024
1076229
Add Limitation about scriptblocks
JustinGrote Oct 1, 2024
a0eb123
Cleanup and refactor a lot of logic out to the extension functions, r…
JustinGrote Oct 3, 2024
5973349
Simplify GetTopVariableAssignment using new extension methods
JustinGrote Oct 4, 2024
5d6a2f3
Add Test case and more info
JustinGrote Oct 4, 2024
2609d3c
Add Rename Parameter Alias Support
JustinGrote Oct 5, 2024
7051803
Add note about Get/Set variable limitation
JustinGrote Oct 7, 2024
63bfac1
Fix readme markdown formatting
JustinGrote Oct 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ Please update to PowerShell 7.4 LTS going forward.
This release contains a logging overhaul which purposely removes our
dependency on Serilog and should lead to improved stability with
PowerShell 5.1 (by avoiding a major GAC assembly conflict).
## v3.3.0
### Friday, November 15, 2024

See more details at the GitHub Release for [v3.3.0](https://github.com/PowerShell/PowerShellEditorServices/releases/tag/v3.3.0).

Logging updates and dropped EOL PowerShell

## v3.21.0
### Wednesday, October 30, 2024
Expand Down
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
functionality needed to enable a consistent and robust PowerShell development
experience in almost any editor or integrated development environment (IDE).

## [Language Server Protocol](https://microsoft.github.io/language-server-protocol/) clients using PowerShell Editor Services:
## [Language Server Protocol](https://microsoft.github.io/language-server-protocol/) clients using PowerShell Editor Services

- [PowerShell for Visual Studio Code](https://github.com/PowerShell/vscode-powershell)
> [!NOTE]
Expand Down Expand Up @@ -143,6 +143,33 @@ The debugging functionality in PowerShell Editor Services is available in the fo
- [powershell.nvim for Neovim](https://github.com/TheLeoP/powershell.nvim)
- [intellij-powershell](https://github.com/ant-druha/intellij-powershell)

### Rename Disclaimer

PowerShell is not a statically typed language. As such, the renaming of functions, parameters, and other symbols can only be done on a best effort basis. While this is sufficient for the majority of use cases, it cannot be relied upon to find all instances of a symbol and rename them across an entire code base such as in C# or TypeScript.

There are several edge case scenarios which may exist where rename is difficult or impossible, or unable to be determined due to the dynamic scoping nature of PowerShell.

The focus of the rename support is on quick updates to variables or functions within a self-contained script file. It is not intended for module developers to find and rename a symbol across multiple files, which is very difficult to do as the relationships are primarily only computed at runtime and not possible to be statically analyzed.
#### 👍 [Implemented and Tested Rename Scenarios](https://github.com/PowerShell/PowerShellEditorServices/blob/main/test/PowerShellEditorServices.Test.Shared/Refactoring)

#### 🤚 Unsupported Scenarios

- ❌ Renaming can only be done within a single file. Renaming symbols across multiple files is not supported, even if those are dotsourced from the source file.
- ❌ Functions or variables must have a corresponding definition within their scope or above to be renamed. If we cannot find the original definition of a variable or function, the rename will not be supported.
- ❌ Dynamic Parameters are not supported
- ❌ Dynamically constructed splat parameters will not be renamed/updated (e.g. `$splat = @{};$splat.a = 5;Do-Thing @a`)
- ❌ Scoped variables (e.g. $SCRIPT:test) are not currently supported
- ❌ Renaming a variable inside of a scriptblock that is used in unscoped operations like `Foreach-Parallel` or `Start-Job` and the variable is not defined within the scriptblock is not supported and can have unexpected results.
- ❌ Scriptblocks part of an assignment are considered isolated scopes. For example `$a = 5; $x = {$a}; & $x` does not consider the two $a to be related, even though in execution this reference matches.
- ❌ Scriptblocks that are part of a parameter are assumed to not be executing in a different runspace. For example, the renaming behavior will treat `ForEach-Object -Parallel {$x}` the same as `Foreach-Object {$x}` for purposes of finding scope definitions. To avoid unexpected renaming, define/redefine all your variables in the scriptblock using a param block.
- ❌ A lot of the logic relies on the position of items, so for example, defining a variable in a `begin` block and placing it after a `process` block, while technically correct in PowerShell, will not rename as expected.
- ❌ Similarly, defining a function, and having the function rely on a variable that is assigned outside the function and after the function definition, will not find the outer variable reference.
- ❌ `Get-Variable` and `Set-Variable` are not considered and not currently searched for renames

#### 📄 Filing a Rename Issue

If there is a rename scenario you feel can be reasonably supported in PowerShell, please file a bug report in the PowerShellEditorServices repository with the "Expected" and "Actual" being the before and after rename. We will evaluate it and accept or reject it and give reasons why. Items that fall under the Unsupported Scenarios above will be summarily rejected, however that does not mean that they may not be supported in the future if we come up with a reasonably safe way to implement a scenario.

## API Usage

Please note that we only consider the following as stable APIs that can be relied on:
Expand Down
4 changes: 4 additions & 0 deletions src/PowerShellEditorServices/Server/PsesLanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public async Task StartAsync()
.ClearProviders()
.AddPsesLanguageServerLogging()
.SetMinimumLevel(_minimumLogLevel))
// TODO: Consider replacing all WithHandler with AddSingleton
.WithConfigurationSection("powershell.rename")
.WithHandler<PsesWorkspaceSymbolsHandler>()
.WithHandler<PsesTextDocumentHandler>()
.WithHandler<GetVersionHandler>()
Expand Down Expand Up @@ -123,6 +125,8 @@ public async Task StartAsync()
.WithHandler<ExpandAliasHandler>()
.WithHandler<PsesSemanticTokensHandler>()
.WithHandler<DidChangeWatchedFilesHandler>()
.WithHandler<PrepareRenameHandler>()
.WithHandler<RenameHandler>()
// NOTE: The OnInitialize delegate gets run when we first receive the
// _Initialize_ request:
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#initialize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public static IServiceCollection AddPsesLanguageServices(
extensionService.InitializeAsync();
return extensionService;
})
.AddSingleton<AnalysisService>();
.AddSingleton<AnalysisService>()
.AddSingleton<RenameService>();
}

public static IServiceCollection AddPsesDebugServices(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;

namespace Microsoft.PowerShell.EditorServices.Handlers;

/// <summary>
/// A convenience exception for handlers to throw when a request fails for a normal reason,
/// and to communicate that reason to the user without a full internal stacktrace.
/// </summary>
/// <param name="message">The message describing the reason for the request failure.</param>
/// <param name="logDetails">Additional details to be logged regarding the failure. It should be serializable to JSON.</param>
/// <param name="severity">The severity level of the message. This is only shown in internal logging.</param>
public class HandlerErrorException
(
string message,
object logDetails = null,
MessageType severity = MessageType.Error
) : RpcErrorException((int)severity, logDetails!, message)
{ }
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
namespace Microsoft.PowerShell.EditorServices.Refactoring
{

public class TargetSymbolNotFoundException : Exception
{
public TargetSymbolNotFoundException()
{
}

public TargetSymbolNotFoundException(string message)
: base(message)
{
}

public TargetSymbolNotFoundException(string message, Exception inner)
: base(message, inner)
{
}
}

public class FunctionDefinitionNotFoundException : Exception
{
public FunctionDefinitionNotFoundException()
{
}

public FunctionDefinitionNotFoundException(string message)
: base(message)
{
}

public FunctionDefinitionNotFoundException(string message, Exception inner)
: base(message, inner)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

// using System.Management.Automation.Language;
// using Microsoft.PowerShell.EditorServices.Services;

// namespace PowerShellEditorServices.Services.PowerShell.Utility
// {
// public static class IScriptExtentExtensions
// {
// public static bool Contains(this IScriptExtent extent, IScriptExtent position)
// => ScriptExtentAdapter.ContainsPosition(extent, position);
// }
// }
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public override async Task<TextEditContainer> Handle(DocumentFormattingParams re
return s_emptyTextEditContainer;
}

return new TextEditContainer(new TextEdit
return new TextEditContainer(new OmniSharp.Extensions.LanguageServer.Protocol.Models.TextEdit
{
NewText = formattedScript,
Range = editRange
Expand Down Expand Up @@ -184,7 +184,7 @@ public override async Task<TextEditContainer> Handle(DocumentRangeFormattingPara
return s_emptyTextEditContainer;
}

return new TextEditContainer(new TextEdit
return new TextEditContainer(new OmniSharp.Extensions.LanguageServer.Protocol.Models.TextEdit
{
NewText = formattedScript,
Range = editRange
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#nullable enable


using System.Threading;
using System.Threading.Tasks;
using Microsoft.PowerShell.EditorServices.Services;

using OmniSharp.Extensions.LanguageServer.Protocol.Document;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;

namespace Microsoft.PowerShell.EditorServices.Handlers;

/// <summary>
/// A handler for <a href="https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_prepareRename">textDocument/prepareRename</a>
/// </summary>
internal class PrepareRenameHandler
(
RenameService renameService
) : IPrepareRenameHandler
{
public RenameRegistrationOptions GetRegistrationOptions(RenameCapability capability, ClientCapabilities clientCapabilities) => capability.PrepareSupport ? new() { PrepareProvider = true } : new();

public async Task<RangeOrPlaceholderRange?> Handle(PrepareRenameParams request, CancellationToken cancellationToken)
=> await renameService.PrepareRenameSymbol(request, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// A handler for <a href="https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_rename">textDocument/rename</a>
/// </summary>
internal class RenameHandler(
RenameService renameService
) : IRenameHandler
{
// RenameOptions may only be specified if the client states that it supports prepareSupport in its initial initialize request.
public RenameRegistrationOptions GetRegistrationOptions(RenameCapability capability, ClientCapabilities clientCapabilities) => capability.PrepareSupport ? new() { PrepareProvider = true } : new();

public async Task<WorkspaceEdit?> Handle(RenameParams request, CancellationToken cancellationToken)
=> await renameService.RenameSymbol(request, cancellationToken).ConfigureAwait(false);
}
Loading