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 212 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
212 commits
Select commit Hold shift + click to select a range
abd8ba1
v3.3.0: Logging updates and dropped EOL PowerShell
andyleejordan Nov 15, 2024
3f43311
adding in rename symbol service
Razmo99 Mar 26, 2023
53f6508
switched to using a task not sure if there is a better way
Razmo99 Mar 26, 2023
3d30492
completed rename function
Razmo99 Mar 26, 2023
6dcc91b
slight refactoring
Razmo99 Mar 26, 2023
6ca4a86
Adding in unit teste for refactoring functions
Razmo99 Mar 29, 2023
11cb655
test case for a function that is flat or inline
Razmo99 Mar 29, 2023
1e42f2d
added new test case
Razmo99 Sep 13, 2023
a072b33
initial commit
Razmo99 Sep 18, 2023
e2434ff
converted visitor class from powershell to C#
Razmo99 Sep 18, 2023
5a53f01
Updated to using ps1 file with a renamed varient
Razmo99 Sep 19, 2023
0b6b3d5
Bug Fixes now passing all tests
Razmo99 Sep 19, 2023
17a8cdf
Stripped un-needed functions now using FunctionRename class
Razmo99 Sep 19, 2023
f3b278f
small clean up of init of class
Razmo99 Sep 25, 2023
a5022c4
unneeded init of class var
Razmo99 Sep 25, 2023
2cea7c8
init commit of variable visitor class
Razmo99 Sep 25, 2023
11c69c7
piping for vscode rename symbol
Razmo99 Sep 25, 2023
42b9a1d
initial tests for variable visitor class
Razmo99 Sep 25, 2023
a24369a
fixing typo
Razmo99 Sep 25, 2023
105c8ec
adjusting scopestack to store Ast objects
Razmo99 Sep 25, 2023
0107233
added additional tests for variablerename visitor
Razmo99 Sep 26, 2023
81e6f07
adjusting so it finds the first variable definition within scope
Razmo99 Sep 26, 2023
bdd9ae0
added function to get variable top assignment
Razmo99 Sep 26, 2023
f8741db
renamed scriptfile to scriptast
Razmo99 Sep 26, 2023
246c9f5
can visit binary expressions now
Razmo99 Sep 26, 2023
cf3b522
can visit dountil and dowhile
Razmo99 Sep 26, 2023
d070546
logic to stop start shouldrename
Razmo99 Sep 26, 2023
99f8c84
can visit scriptexpressions now
Razmo99 Sep 26, 2023
95d9f08
start stop logic for if a redefinition is found
Razmo99 Sep 26, 2023
4caf80d
formatting
Razmo99 Sep 26, 2023
d87b701
implemented visithastable
Razmo99 Sep 27, 2023
5aa720b
function to determine if a node is within a targets scope
Razmo99 Sep 27, 2023
4e44ad8
adjusted get variable top assignment for better detection
Razmo99 Sep 27, 2023
bda9c44
additional test cases
Razmo99 Sep 27, 2023
44f897b
additional tests
Razmo99 Sep 27, 2023
bc1e124
added break for finding the top variable assignment
Razmo99 Sep 27, 2023
8c2082b
implemented visit command parameter
Razmo99 Sep 27, 2023
4a15b30
implemented visit member expression
Razmo99 Sep 27, 2023
18bcdd6
implemented visitparentexpression
Razmo99 Sep 27, 2023
f1aaa8f
altered logic for variable renamed to check operator is equals
Razmo99 Sep 27, 2023
d6a0cd9
removing examples file
Razmo99 Sep 27, 2023
1224ad4
formatting and additional test
Razmo99 Sep 28, 2023
d720cda
formatting and proper sorting for gettestscript
Razmo99 Sep 28, 2023
c9630b6
additional test data
Razmo99 Sep 28, 2023
219cec5
reworked class so that oldname is no longer needed
Razmo99 Sep 28, 2023
1178fbe
implemented some new visitors
Razmo99 Sep 28, 2023
db011fe
early start on commandparameter renaming
Razmo99 Sep 28, 2023
ee96944
more implementations and some formatting
Razmo99 Sep 28, 2023
78cdcf5
logic to determin if we are renaming a var or parameter
Razmo99 Sep 28, 2023
a7dc8fd
additional test
Razmo99 Sep 28, 2023
c714ee5
additional tests for parameters
Razmo99 Sep 30, 2023
7bf8cc9
adjusting checks for parameters
Razmo99 Sep 30, 2023
40f0748
case insensitive compare & adjustment for get ast parent scope to fav…
Razmo99 Sep 30, 2023
280dd8e
initial implentation of prepare rename provider
Razmo99 Oct 13, 2023
0797b1f
new test to handle detection for if the target function is a param ast
Razmo99 Oct 13, 2023
6e68024
new exception for when dot sourcing is detected
Razmo99 Oct 13, 2023
5819d6b
added more detection and errors for prepare rename symbol
Razmo99 Oct 13, 2023
a6831d7
new exception for when the function definition cannot be found
Razmo99 Oct 13, 2023
3fcc648
no longer using trygetsymbolatposition as it doesnt detect parameterA…
Razmo99 Oct 13, 2023
7201a91
further adjustments to detection
Razmo99 Oct 13, 2023
aeb9ce4
switched to processing using iteration to avoid stack overflow
Razmo99 Oct 14, 2023
3c14fd8
Fixing typo
Razmo99 Oct 14, 2023
b9171ff
Switching tests to use iterative class
Razmo99 Oct 14, 2023
c00bc57
init version of the variable rename iterative
Razmo99 Oct 14, 2023
53446c9
switched tests and vscode to use iterative class
Razmo99 Oct 14, 2023
c6ccec1
new test to check for method with the same parameter name
Razmo99 Oct 14, 2023
8af3137
fixing up tests for VariableParameterCommandWithSameName
Razmo99 Oct 14, 2023
bcc647d
fixing up tests
Razmo99 Oct 14, 2023
0efc203
adjusting tests for more complexity
Razmo99 Oct 14, 2023
a8da455
now adds Alias on commandParameterRenaming
Razmo99 Oct 14, 2023
be6effc
refactored alias creation for readability
Razmo99 Oct 15, 2023
61d612e
updated prepeare rename symbol to use iterative and added msg for if …
Razmo99 Oct 15, 2023
dc88444
renamed renamevariableiterative to IterativeVariableRename
Razmo99 Oct 15, 2023
0502e55
using switch instead of else if
Razmo99 Oct 15, 2023
3232fe3
formatting for rename symbol
Razmo99 Oct 15, 2023
b303f66
moved Function shared test content into its own folder
Razmo99 Oct 15, 2023
87a955d
New Test for splatted variable parameter renaming
Razmo99 Oct 15, 2023
b5a6573
first stage of supporting symbol renaming for splatted command Ast calls
Razmo99 Oct 15, 2023
cec73dd
split out exceptions into generic file
Razmo99 Oct 15, 2023
c64cef5
updated to use its own internal version
Razmo99 Oct 15, 2023
0ffa172
added functionality to reverse lookup the top variable from a splat
Razmo99 Oct 15, 2023
b1602a8
Detter symbol detection was timing out on larger files
Razmo99 Oct 15, 2023
e3932d0
added utilities for common renaming functions updated tests
Razmo99 Oct 15, 2023
78827bc
adjusted rename to use utilities
Razmo99 Oct 15, 2023
4c8bb80
added comments to NewSplattedModification
Razmo99 Oct 24, 2023
41d3ae8
adjusted test to use -username param instead of -password due to Alia…
Razmo99 Oct 24, 2023
334b91f
extracted method of LookForParentOfType from GetFuncDecFromCommAst
Razmo99 Oct 24, 2023
a5a716a
adjusted LookForParent so it accepts multiple types to look for
Razmo99 Oct 24, 2023
fcd6439
adjusting iterative functions to use LookForParentOfType method
Razmo99 Oct 24, 2023
321e8dd
Moved GetAstNodeByLineAndColumn to a generic method
Razmo99 Oct 24, 2023
bee4e00
updated visitors to use generic GetAstNodeByLineAndColumn
Razmo99 Oct 24, 2023
96d551e
formatting moved GetFunctionDefByCommandAst to Utilities
Razmo99 Oct 24, 2023
793ca95
Refactoring to use Utilities class for generic methods
Razmo99 Oct 24, 2023
266cb2b
Renaming methods to be clearer
Razmo99 Oct 24, 2023
3d18c28
Added a test to get a function definition ast
Razmo99 Mar 21, 2024
93f7c95
additional checks in getast for namedblock detection
Razmo99 Mar 21, 2024
f34bb2d
formatting an new test for finding a function
Razmo99 Oct 27, 2023
469a797
reworked GetAst for better detection
Razmo99 Oct 27, 2023
7c49e38
additional changes to getast utility method
Razmo99 Mar 21, 2024
b46206b
reverting changes from bad merge request pull
Razmo99 Mar 21, 2024
118ec83
removing unused properties of the class
Razmo99 Mar 22, 2024
4ff45b4
migrated FunctionDefinitionNotFoundException to exceptions.cs
Razmo99 Mar 22, 2024
350bc23
removed original recursive visitor classes
Razmo99 Mar 22, 2024
b9f1b2c
removed unsued class properties from function visitor
Razmo99 Mar 22, 2024
a6cb297
condensing if statements
Razmo99 Mar 22, 2024
e56f1be
Broke up Process node into 3 sub functions for readability
Razmo99 Mar 22, 2024
a5b874c
fixing comment grammar
Razmo99 Mar 22, 2024
a369822
New Method and tests to check if a script ast contains dot sourcing
Razmo99 Mar 24, 2024
458fd35
finalised dot source detection and notification
Razmo99 Mar 24, 2024
5da6d6b
fixing spelling / naming mistakes
Razmo99 Mar 24, 2024
fbe71a1
removing .vscode files
Razmo99 Mar 25, 2024
7d5aced
deleting package-lock.json
Razmo99 Mar 25, 2024
368867f
cleaning up comments and removed unused code
Razmo99 Mar 25, 2024
92b3d81
Adjusted refactoring Tests to use IAsyncLifetime instead of IDisposable
Razmo99 Jun 2, 2024
83e676d
Fix Path.Combine to be cross platform
JustinGrote Jun 3, 2024
7d0a945
Fixing an odd edge case, of not being to rename a variable directly u…
Razmo99 Jun 5, 2024
da41e40
added tests and logic for duplicate assignment cases for; foreach and…
Razmo99 Jun 5, 2024
75cef91
Adding in out of scope $i to test case which shouldnt be renamed
Razmo99 Jun 5, 2024
3fd29f8
.net 8 requires a newline at the start of the script to recognise the…
Razmo99 Jun 6, 2024
67c20a1
fixing type in test name
Razmo99 Jun 6, 2024
2255f2b
CommandAst input was being sent to variable renamer not function rena…
Razmo99 Jun 6, 2024
fbb8055
additional condition so that a function CommandAst will not be touche…
Razmo99 Jun 7, 2024
8da83a7
Making RenameSymbolParams public for xunit serializer
Razmo99 Jun 7, 2024
7ace177
Renamed Test .ps1 to match class property name, reworked test cases t…
Razmo99 Jun 7, 2024
e19c32b
consolidated tests as their are no special requirements
Razmo99 Jun 7, 2024
8f266bd
moved serializer and getmodifiedscriptcontent seperate file as it wil…
Razmo99 Jun 7, 2024
656a5f4
Adding missing test case renamed.ps1 varient
Razmo99 Jun 7, 2024
669e4ab
removing unused test case data
Razmo99 Jun 7, 2024
944e358
updated GetModifiedScript with changes from the VariableRenameTests, …
Razmo99 Jun 7, 2024
7a68edb
modified variable test cases to use parameterized test cases
Razmo99 Jun 7, 2024
09e9620
Added a new test case for renaming an inner variable leaking out the …
Razmo99 Jun 7, 2024
61c5cd0
reworked applicable utilities tests to be parameterized
Razmo99 Jun 7, 2024
74d99ad
Added test case for simple function parameter rename, added clause fo…
Razmo99 Jun 7, 2024
e23949a
adding plumbling for shouldgenerateAlias on server side
Razmo99 Jun 7, 2024
5cf761a
Passing through shouldGenerateAlias to VariableVisitor Class
Razmo99 Jun 7, 2024
5315334
added new test cases for functions with variables defined outside of …
Razmo99 Jun 7, 2024
3069e55
renaming ShouldGenerateAlias to create CreateAlias
Razmo99 Jun 10, 2024
f958896
Add Missing Disclaimer
JustinGrote Sep 12, 2024
75a3a63
Explicitly Show Unsaved Files as currently unsupported. It's probably…
JustinGrote Sep 12, 2024
8b2d69b
Move all Handling to OmniSharp LSP types
JustinGrote Sep 12, 2024
6f09f1a
Move HandlerError
JustinGrote Sep 14, 2024
aac9252
Rework initial AST filter
JustinGrote Sep 15, 2024
c0d32f5
Reorganize Tests under Handler folder
JustinGrote Sep 15, 2024
ee78005
Lots of removing of custom types. Currently broken until I create a S…
JustinGrote Sep 15, 2024
98d0c0e
Rework RenameHandler to use Adapters
JustinGrote Sep 15, 2024
2d40bde
Fix namespacing
JustinGrote Sep 15, 2024
78683d4
Remove Alias from tests for now, will have separate Alias test in future
JustinGrote Sep 15, 2024
e160ee8
Default CreateAlias to false per feedback
JustinGrote Sep 15, 2024
06dbfde
Reworked Visitor to use ScriptPositionAdapter
JustinGrote Sep 15, 2024
5f68141
Fixup tests with default PowerShell Formatting
JustinGrote Sep 15, 2024
5aedd97
Refine VariableVisitor to use ScriptExtentAdapter
JustinGrote Sep 15, 2024
392be0f
Extract some duplicate functions and enable NRT checking for RenameHa…
JustinGrote Sep 16, 2024
d13e5d7
Add initial EUA prompt scaffolding
JustinGrote Sep 16, 2024
65d463b
Move RenameHandler to TextDocument (more appropriate context)
JustinGrote Sep 17, 2024
a50153a
Remove Unnecessary code
JustinGrote Sep 17, 2024
1b81c23
Split out RenameService. **TESTS NEED FIXING**
JustinGrote Sep 17, 2024
c182fae
Introduce service to the Extension
JustinGrote Sep 17, 2024
a3830e0
Redo tests to be data-driven
JustinGrote Sep 17, 2024
27f231a
Fixup Tests, still a bug in rename logic with functions
JustinGrote Sep 18, 2024
cb797d5
Fixed all function Prepare tests
JustinGrote Sep 18, 2024
c4d4360
Fixed all variable PrepareRenameHandler Tests
JustinGrote Sep 18, 2024
77c7ebc
First Stage work to move to a more stateless AstVisitor for renames
JustinGrote Sep 23, 2024
d22f9b0
Separate out AstExtensions and continue Functions Reimplement. Functi…
JustinGrote Sep 24, 2024
65c0a38
Move renameservice
JustinGrote Sep 24, 2024
fd81afa
Breakout and simplify matching logic, fixes more test cases
JustinGrote Sep 24, 2024
b7b551f
SkipChildren doesn't work for nested function situations. More tests …
JustinGrote Sep 24, 2024
ccce304
All rename function tests fixed
JustinGrote Sep 24, 2024
d5e8bf3
Add disclaimer scaffolding (needs config)
JustinGrote Sep 24, 2024
bc69bfc
Add more config setup and lock down some classes
JustinGrote Sep 24, 2024
b903605
Add configuration and adjust opt-in message due to server-side LSP co…
JustinGrote Sep 24, 2024
03a21ba
Add mocks for configuration for testing, still need to fix some param…
JustinGrote Sep 25, 2024
02b6ffd
Bonus Assertions
JustinGrote Sep 25, 2024
b636f88
Fix all Tests
JustinGrote Sep 26, 2024
5c2e6ee
Actually fix tests (missing some pattern matching on AST types)
JustinGrote Sep 26, 2024
9f51c18
Small format adjust
JustinGrote Sep 26, 2024
934bf6f
Format Update
JustinGrote Sep 26, 2024
3629103
Move IterativeVariableVisitor into RenameService class
JustinGrote Sep 26, 2024
3efb876
Remove unnecessary intermediate tests
JustinGrote Sep 26, 2024
18c37a2
Internalize rename visitor into renameservice
JustinGrote Sep 26, 2024
4f29e10
Perform initial abstraction of visitors to a base class
JustinGrote Sep 26, 2024
a1b9524
Add disclaimer link
JustinGrote Sep 26, 2024
7657d97
Apply formatting to test fixtures
JustinGrote Sep 26, 2024
282b3e1
Fix issue with dependency injection weirdly setting Disclaimer to tru…
JustinGrote Sep 27, 2024
1a6b253
Change name of Alias Setting
JustinGrote Sep 27, 2024
5f39f5a
Make the PrepareRenameSymbol return more legible
JustinGrote Sep 27, 2024
f5abbb8
Add support for negative test cases
JustinGrote Sep 27, 2024
40b25ed
I thought I fixed all these...
JustinGrote Sep 27, 2024
b45b8c0
Small test fixes and naming updates
JustinGrote Sep 27, 2024
cc961dc
Move AstExtensions to Utility
JustinGrote Sep 27, 2024
82b947d
Remove utilities as they have been moved into RenameService or AstExt…
JustinGrote Sep 27, 2024
99aa202
Rewrote Variable Handler, all tests passing except a stringconstant s…
JustinGrote Sep 29, 2024
2653c8a
Fix up Splat function finding, all tests pass
JustinGrote Sep 29, 2024
826727d
Clean out some dead code
JustinGrote Sep 30, 2024
40ad3d0
Add name validation and related tests, also rearrange/rename some tes…
JustinGrote Sep 30, 2024
2294fda
Update readme with current unsupported scenarios
JustinGrote Oct 1, 2024
a75c04f
Rework function name check to be ast based
JustinGrote Oct 1, 2024
dbb6aed
Add Limitation about scriptblocks
JustinGrote Oct 1, 2024
4c80f4a
Cleanup and refactor a lot of logic out to the extension functions, r…
JustinGrote Oct 3, 2024
993e780
Simplify GetTopVariableAssignment using new extension methods
JustinGrote Oct 4, 2024
f2cc90e
Add Test case and more info
JustinGrote Oct 4, 2024
ec55b00
Add Rename Parameter Alias Support
JustinGrote Oct 5, 2024
a6a38ce
Add note about Get/Set variable limitation
JustinGrote Oct 7, 2024
8dc1e3c
Fix readme markdown formatting
JustinGrote Oct 7, 2024
3182e27
Rolyn Lint Fixes
JustinGrote Mar 2, 2025
8567f4a
Fix closing param
JustinGrote Mar 4, 2025
f6060fe
Remove unused function
JustinGrote Mar 4, 2025
fff0bbe
Fix Typo
JustinGrote Mar 4, 2025
dd7c824
Fix bad indentation
JustinGrote Mar 4, 2025
acdf0fa
Implement Roslynator Recommendation
JustinGrote Mar 4, 2025
8013ee2
Add back GetFullScript
JustinGrote Mar 4, 2025
1f98491
Remove unused script extensions
JustinGrote Mar 7, 2025
1897e7b
Fix minor script extension update
JustinGrote Mar 7, 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
Expand Up @@ -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
Expand Down
30 changes: 29 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,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:
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,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
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,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