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 215 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
215 commits
Select commit Hold shift + click to select a range
b52eaea
v3.3.0: Logging updates and dropped EOL PowerShell
andyleejordan Nov 15, 2024
e2e032b
adding in rename symbol service
Razmo99 Mar 26, 2023
6c673d7
switched to using a task not sure if there is a better way
Razmo99 Mar 26, 2023
1b0c292
completed rename function
Razmo99 Mar 26, 2023
5a33847
slight refactoring
Razmo99 Mar 26, 2023
c5b2c26
Adding in unit teste for refactoring functions
Razmo99 Mar 29, 2023
d496419
test case for a function that is flat or inline
Razmo99 Mar 29, 2023
dbdd78f
added new test case
Razmo99 Sep 13, 2023
e4fb46b
initial commit
Razmo99 Sep 18, 2023
0b9a562
converted visitor class from powershell to C#
Razmo99 Sep 18, 2023
6e1dfd9
Updated to using ps1 file with a renamed varient
Razmo99 Sep 19, 2023
55ec303
Bug Fixes now passing all tests
Razmo99 Sep 19, 2023
49bd609
Stripped un-needed functions now using FunctionRename class
Razmo99 Sep 19, 2023
1840171
small clean up of init of class
Razmo99 Sep 25, 2023
eeebe4e
unneeded init of class var
Razmo99 Sep 25, 2023
dc410b0
init commit of variable visitor class
Razmo99 Sep 25, 2023
4fd1b0c
piping for vscode rename symbol
Razmo99 Sep 25, 2023
2293294
initial tests for variable visitor class
Razmo99 Sep 25, 2023
f9c8753
fixing typo
Razmo99 Sep 25, 2023
3024dca
adjusting scopestack to store Ast objects
Razmo99 Sep 25, 2023
29b65e4
added additional tests for variablerename visitor
Razmo99 Sep 26, 2023
2b1e640
adjusting so it finds the first variable definition within scope
Razmo99 Sep 26, 2023
327c814
added function to get variable top assignment
Razmo99 Sep 26, 2023
7ab6d17
renamed scriptfile to scriptast
Razmo99 Sep 26, 2023
2611cd8
can visit binary expressions now
Razmo99 Sep 26, 2023
426a81c
can visit dountil and dowhile
Razmo99 Sep 26, 2023
369ba75
logic to stop start shouldrename
Razmo99 Sep 26, 2023
3bf4e17
can visit scriptexpressions now
Razmo99 Sep 26, 2023
1a57413
start stop logic for if a redefinition is found
Razmo99 Sep 26, 2023
c4339ef
formatting
Razmo99 Sep 26, 2023
6d64038
implemented visithastable
Razmo99 Sep 27, 2023
dccb672
function to determine if a node is within a targets scope
Razmo99 Sep 27, 2023
8cbead4
adjusted get variable top assignment for better detection
Razmo99 Sep 27, 2023
ce0af4e
additional test cases
Razmo99 Sep 27, 2023
953cff7
additional tests
Razmo99 Sep 27, 2023
0903de3
added break for finding the top variable assignment
Razmo99 Sep 27, 2023
bb94a62
implemented visit command parameter
Razmo99 Sep 27, 2023
89a7687
implemented visit member expression
Razmo99 Sep 27, 2023
2185545
implemented visitparentexpression
Razmo99 Sep 27, 2023
5103a79
altered logic for variable renamed to check operator is equals
Razmo99 Sep 27, 2023
1829bb2
removing examples file
Razmo99 Sep 27, 2023
8f7ecba
formatting and additional test
Razmo99 Sep 28, 2023
61f7891
formatting and proper sorting for gettestscript
Razmo99 Sep 28, 2023
7edadea
additional test data
Razmo99 Sep 28, 2023
00a5639
reworked class so that oldname is no longer needed
Razmo99 Sep 28, 2023
7aea9ca
implemented some new visitors
Razmo99 Sep 28, 2023
6b786c0
early start on commandparameter renaming
Razmo99 Sep 28, 2023
c561062
more implementations and some formatting
Razmo99 Sep 28, 2023
2da9afc
logic to determin if we are renaming a var or parameter
Razmo99 Sep 28, 2023
f654b9e
additional test
Razmo99 Sep 28, 2023
72d8a08
additional tests for parameters
Razmo99 Sep 30, 2023
3419910
adjusting checks for parameters
Razmo99 Sep 30, 2023
950b3e5
case insensitive compare & adjustment for get ast parent scope to fav…
Razmo99 Sep 30, 2023
b409867
initial implentation of prepare rename provider
Razmo99 Oct 13, 2023
dfa10e0
new test to handle detection for if the target function is a param ast
Razmo99 Oct 13, 2023
e520f0d
new exception for when dot sourcing is detected
Razmo99 Oct 13, 2023
c712665
added more detection and errors for prepare rename symbol
Razmo99 Oct 13, 2023
374b890
new exception for when the function definition cannot be found
Razmo99 Oct 13, 2023
340b40b
no longer using trygetsymbolatposition as it doesnt detect parameterA…
Razmo99 Oct 13, 2023
cdd45f1
further adjustments to detection
Razmo99 Oct 13, 2023
5c71254
switched to processing using iteration to avoid stack overflow
Razmo99 Oct 14, 2023
4294b69
Fixing typo
Razmo99 Oct 14, 2023
fa5ce76
Switching tests to use iterative class
Razmo99 Oct 14, 2023
7c511be
init version of the variable rename iterative
Razmo99 Oct 14, 2023
3024204
switched tests and vscode to use iterative class
Razmo99 Oct 14, 2023
e8785fc
new test to check for method with the same parameter name
Razmo99 Oct 14, 2023
73b7121
fixing up tests for VariableParameterCommandWithSameName
Razmo99 Oct 14, 2023
b8f5c9a
fixing up tests
Razmo99 Oct 14, 2023
b22e7ef
adjusting tests for more complexity
Razmo99 Oct 14, 2023
3d47904
now adds Alias on commandParameterRenaming
Razmo99 Oct 14, 2023
d3985d0
refactored alias creation for readability
Razmo99 Oct 15, 2023
ff593a7
updated prepeare rename symbol to use iterative and added msg for if …
Razmo99 Oct 15, 2023
9160cda
renamed renamevariableiterative to IterativeVariableRename
Razmo99 Oct 15, 2023
d842e76
using switch instead of else if
Razmo99 Oct 15, 2023
7ecbd68
formatting for rename symbol
Razmo99 Oct 15, 2023
80b668c
moved Function shared test content into its own folder
Razmo99 Oct 15, 2023
e05f794
New Test for splatted variable parameter renaming
Razmo99 Oct 15, 2023
8646dd4
first stage of supporting symbol renaming for splatted command Ast calls
Razmo99 Oct 15, 2023
5ec6d30
split out exceptions into generic file
Razmo99 Oct 15, 2023
d3f7148
updated to use its own internal version
Razmo99 Oct 15, 2023
a688a19
added functionality to reverse lookup the top variable from a splat
Razmo99 Oct 15, 2023
123d2eb
Detter symbol detection was timing out on larger files
Razmo99 Oct 15, 2023
3f28bdb
added utilities for common renaming functions updated tests
Razmo99 Oct 15, 2023
e883d5c
adjusted rename to use utilities
Razmo99 Oct 15, 2023
94f26c2
added comments to NewSplattedModification
Razmo99 Oct 24, 2023
88fe7b2
adjusted test to use -username param instead of -password due to Alia…
Razmo99 Oct 24, 2023
699aa64
extracted method of LookForParentOfType from GetFuncDecFromCommAst
Razmo99 Oct 24, 2023
b183682
adjusted LookForParent so it accepts multiple types to look for
Razmo99 Oct 24, 2023
a152c3a
adjusting iterative functions to use LookForParentOfType method
Razmo99 Oct 24, 2023
9905c4b
Moved GetAstNodeByLineAndColumn to a generic method
Razmo99 Oct 24, 2023
343ed7c
updated visitors to use generic GetAstNodeByLineAndColumn
Razmo99 Oct 24, 2023
6477f43
formatting moved GetFunctionDefByCommandAst to Utilities
Razmo99 Oct 24, 2023
20ab7bb
Refactoring to use Utilities class for generic methods
Razmo99 Oct 24, 2023
fa854b5
Renaming methods to be clearer
Razmo99 Oct 24, 2023
3af5ebd
Added a test to get a function definition ast
Razmo99 Mar 21, 2024
a7b6cb3
additional checks in getast for namedblock detection
Razmo99 Mar 21, 2024
9f7dafa
formatting an new test for finding a function
Razmo99 Oct 27, 2023
65a6940
reworked GetAst for better detection
Razmo99 Oct 27, 2023
8c854f1
additional changes to getast utility method
Razmo99 Mar 21, 2024
be90d8c
reverting changes from bad merge request pull
Razmo99 Mar 21, 2024
c67c4f4
removing unused properties of the class
Razmo99 Mar 22, 2024
8811154
migrated FunctionDefinitionNotFoundException to exceptions.cs
Razmo99 Mar 22, 2024
9170b05
removed original recursive visitor classes
Razmo99 Mar 22, 2024
7e7ccbf
removed unsued class properties from function visitor
Razmo99 Mar 22, 2024
8c2c31e
condensing if statements
Razmo99 Mar 22, 2024
cb10656
Broke up Process node into 3 sub functions for readability
Razmo99 Mar 22, 2024
9550af2
fixing comment grammar
Razmo99 Mar 22, 2024
7a41db9
New Method and tests to check if a script ast contains dot sourcing
Razmo99 Mar 24, 2024
8b61cf8
finalised dot source detection and notification
Razmo99 Mar 24, 2024
e7f9491
fixing spelling / naming mistakes
Razmo99 Mar 24, 2024
baa2af1
removing .vscode files
Razmo99 Mar 25, 2024
1a100e7
deleting package-lock.json
Razmo99 Mar 25, 2024
13f2b65
cleaning up comments and removed unused code
Razmo99 Mar 25, 2024
de02ede
Adjusted refactoring Tests to use IAsyncLifetime instead of IDisposable
Razmo99 Jun 2, 2024
117ac8b
Fix Path.Combine to be cross platform
JustinGrote Jun 3, 2024
7300a82
Fixing an odd edge case, of not being to rename a variable directly u…
Razmo99 Jun 5, 2024
bb04380
added tests and logic for duplicate assignment cases for; foreach and…
Razmo99 Jun 5, 2024
f49add8
Adding in out of scope $i to test case which shouldnt be renamed
Razmo99 Jun 5, 2024
3d4def9
.net 8 requires a newline at the start of the script to recognise the…
Razmo99 Jun 6, 2024
a4c3388
fixing type in test name
Razmo99 Jun 6, 2024
79f22b1
CommandAst input was being sent to variable renamer not function rena…
Razmo99 Jun 6, 2024
2be446c
additional condition so that a function CommandAst will not be touche…
Razmo99 Jun 7, 2024
a47913f
Making RenameSymbolParams public for xunit serializer
Razmo99 Jun 7, 2024
a452988
Renamed Test .ps1 to match class property name, reworked test cases t…
Razmo99 Jun 7, 2024
25d1f01
consolidated tests as their are no special requirements
Razmo99 Jun 7, 2024
145dccd
moved serializer and getmodifiedscriptcontent seperate file as it wil…
Razmo99 Jun 7, 2024
ca323b4
Adding missing test case renamed.ps1 varient
Razmo99 Jun 7, 2024
56c93ed
removing unused test case data
Razmo99 Jun 7, 2024
41412ea
updated GetModifiedScript with changes from the VariableRenameTests, …
Razmo99 Jun 7, 2024
30ca8e8
modified variable test cases to use parameterized test cases
Razmo99 Jun 7, 2024
c4d273b
Added a new test case for renaming an inner variable leaking out the …
Razmo99 Jun 7, 2024
9b6201f
reworked applicable utilities tests to be parameterized
Razmo99 Jun 7, 2024
ac4064f
Added test case for simple function parameter rename, added clause fo…
Razmo99 Jun 7, 2024
aaabc80
adding plumbling for shouldgenerateAlias on server side
Razmo99 Jun 7, 2024
c713a87
Passing through shouldGenerateAlias to VariableVisitor Class
Razmo99 Jun 7, 2024
7b8cc20
added new test cases for functions with variables defined outside of …
Razmo99 Jun 7, 2024
29d38a6
renaming ShouldGenerateAlias to create CreateAlias
Razmo99 Jun 10, 2024
75a3336
Add Missing Disclaimer
JustinGrote Sep 12, 2024
5b59774
Explicitly Show Unsaved Files as currently unsupported. It's probably…
JustinGrote Sep 12, 2024
72ea7b0
Move all Handling to OmniSharp LSP types
JustinGrote Sep 12, 2024
9d04533
Move HandlerError
JustinGrote Sep 14, 2024
b63d7c8
Rework initial AST filter
JustinGrote Sep 15, 2024
5006645
Reorganize Tests under Handler folder
JustinGrote Sep 15, 2024
54c6224
Lots of removing of custom types. Currently broken until I create a S…
JustinGrote Sep 15, 2024
3036b57
Rework RenameHandler to use Adapters
JustinGrote Sep 15, 2024
403077c
Fix namespacing
JustinGrote Sep 15, 2024
19172ea
Remove Alias from tests for now, will have separate Alias test in future
JustinGrote Sep 15, 2024
9ef7207
Default CreateAlias to false per feedback
JustinGrote Sep 15, 2024
955f6eb
Reworked Visitor to use ScriptPositionAdapter
JustinGrote Sep 15, 2024
5baa192
Fixup tests with default PowerShell Formatting
JustinGrote Sep 15, 2024
e9508fa
Refine VariableVisitor to use ScriptExtentAdapter
JustinGrote Sep 15, 2024
26ebf6e
Extract some duplicate functions and enable NRT checking for RenameHa…
JustinGrote Sep 16, 2024
1324e9d
Add initial EUA prompt scaffolding
JustinGrote Sep 16, 2024
ab1dbab
Move RenameHandler to TextDocument (more appropriate context)
JustinGrote Sep 17, 2024
c3c79e0
Remove Unnecessary code
JustinGrote Sep 17, 2024
f444eac
Split out RenameService. **TESTS NEED FIXING**
JustinGrote Sep 17, 2024
cac1533
Introduce service to the Extension
JustinGrote Sep 17, 2024
ba48b19
Redo tests to be data-driven
JustinGrote Sep 17, 2024
6e3c72a
Fixup Tests, still a bug in rename logic with functions
JustinGrote Sep 18, 2024
cdcb746
Fixed all function Prepare tests
JustinGrote Sep 18, 2024
9b13345
Fixed all variable PrepareRenameHandler Tests
JustinGrote Sep 18, 2024
70a485d
First Stage work to move to a more stateless AstVisitor for renames
JustinGrote Sep 23, 2024
4910ab8
Separate out AstExtensions and continue Functions Reimplement. Functi…
JustinGrote Sep 24, 2024
43c719a
Move renameservice
JustinGrote Sep 24, 2024
785d1d3
Breakout and simplify matching logic, fixes more test cases
JustinGrote Sep 24, 2024
8acf310
SkipChildren doesn't work for nested function situations. More tests …
JustinGrote Sep 24, 2024
5cc72ce
All rename function tests fixed
JustinGrote Sep 24, 2024
2a9c6ad
Add disclaimer scaffolding (needs config)
JustinGrote Sep 24, 2024
2503906
Add more config setup and lock down some classes
JustinGrote Sep 24, 2024
f21ba79
Add configuration and adjust opt-in message due to server-side LSP co…
JustinGrote Sep 24, 2024
2142aa1
Add mocks for configuration for testing, still need to fix some param…
JustinGrote Sep 25, 2024
d1f2a63
Bonus Assertions
JustinGrote Sep 25, 2024
c5d10c5
Fix all Tests
JustinGrote Sep 26, 2024
b7034a1
Actually fix tests (missing some pattern matching on AST types)
JustinGrote Sep 26, 2024
7113b11
Small format adjust
JustinGrote Sep 26, 2024
e40fe6c
Format Update
JustinGrote Sep 26, 2024
2d745d8
Move IterativeVariableVisitor into RenameService class
JustinGrote Sep 26, 2024
7d1f3b7
Remove unnecessary intermediate tests
JustinGrote Sep 26, 2024
84a3dc4
Internalize rename visitor into renameservice
JustinGrote Sep 26, 2024
ffb7aef
Perform initial abstraction of visitors to a base class
JustinGrote Sep 26, 2024
5599e05
Add disclaimer link
JustinGrote Sep 26, 2024
44be8dd
Apply formatting to test fixtures
JustinGrote Sep 26, 2024
1c8a2ce
Fix issue with dependency injection weirdly setting Disclaimer to tru…
JustinGrote Sep 27, 2024
fc066e0
Change name of Alias Setting
JustinGrote Sep 27, 2024
0304587
Make the PrepareRenameSymbol return more legible
JustinGrote Sep 27, 2024
ff75355
Add support for negative test cases
JustinGrote Sep 27, 2024
9e05678
I thought I fixed all these...
JustinGrote Sep 27, 2024
13e99eb
Small test fixes and naming updates
JustinGrote Sep 27, 2024
93c449e
Move AstExtensions to Utility
JustinGrote Sep 27, 2024
edcadeb
Remove utilities as they have been moved into RenameService or AstExt…
JustinGrote Sep 27, 2024
8af302e
Rewrote Variable Handler, all tests passing except a stringconstant s…
JustinGrote Sep 29, 2024
27f1a3b
Fix up Splat function finding, all tests pass
JustinGrote Sep 29, 2024
87fee89
Clean out some dead code
JustinGrote Sep 30, 2024
c8d26c6
Add name validation and related tests, also rearrange/rename some tes…
JustinGrote Sep 30, 2024
6832e0c
Update readme with current unsupported scenarios
JustinGrote Oct 1, 2024
46ccae3
Rework function name check to be ast based
JustinGrote Oct 1, 2024
7607f46
Add Limitation about scriptblocks
JustinGrote Oct 1, 2024
5c34173
Cleanup and refactor a lot of logic out to the extension functions, r…
JustinGrote Oct 3, 2024
9f611ba
Simplify GetTopVariableAssignment using new extension methods
JustinGrote Oct 4, 2024
bdb40b7
Add Test case and more info
JustinGrote Oct 4, 2024
102c69d
Add Rename Parameter Alias Support
JustinGrote Oct 5, 2024
d3bb1b9
Add note about Get/Set variable limitation
JustinGrote Oct 7, 2024
706c9b1
Fix readme markdown formatting
JustinGrote Oct 7, 2024
25f74a5
Rolyn Lint Fixes
JustinGrote Mar 2, 2025
4da6079
Fix closing param
JustinGrote Mar 4, 2025
0abbb25
Remove unused function
JustinGrote Mar 4, 2025
9f1aaa2
Fix Typo
JustinGrote Mar 4, 2025
e643423
Fix bad indentation
JustinGrote Mar 4, 2025
8821310
Implement Roslynator Recommendation
JustinGrote Mar 4, 2025
38fe329
Add back GetFullScript
JustinGrote Mar 4, 2025
87753ed
Remove unused script extensions
JustinGrote Mar 7, 2025
981f6e9
Fix minor script extension update
JustinGrote Mar 7, 2025
35eea13
Remove placeholders/comments and clean up in-progress items
JustinGrote Mar 20, 2025
d430459
Remove and cleanup TODO comments
JustinGrote Mar 20, 2025
153dd51
Fix placeholder not being a valid variable name
JustinGrote Mar 20, 2025
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
@@ -28,6 +28,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
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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]
@@ -143,6 +143,34 @@ 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:
4 changes: 4 additions & 0 deletions src/PowerShellEditorServices/Server/PsesLanguageServer.cs
Original file line number Diff line number Diff line change
@@ -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>()
@@ -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
Original file line number Diff line number Diff line change
@@ -50,7 +50,8 @@ public static IServiceCollection AddPsesLanguageServices(
extensionService.InitializeAsync();
return extensionService;
})
.AddSingleton<AnalysisService>();
.AddSingleton<AnalysisService>()
.AddSingleton<RenameService>();
}

public static IServiceCollection AddPsesDebugServices(
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,40 @@
// 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
@@ -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
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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