Commit 57dec9a
Modernize alias expansion to use the language parser
The `powerShell/expandAlias` handler built an embedded PowerShell
here-string that called `[System.Management.Automation.PsParser]::Tokenize`
to find command tokens. That legacy tokenizer is Windows PowerShell /
full-framework only and carries the parsing limitations that come with it.
This rewrites the logic in C# against the modern, cross-edition APIs:
- Tokenize with `Parser.ParseInput` and select command-name tokens via
`TokenFlags.CommandName` instead of the legacy `PSTokenType.Command`.
- Resolve every distinct name to its alias definition in a single
`Get-Command -CommandType Alias` round-trip rather than re-defining a
helper function and querying one token at a time.
- Escape wildcard metacharacters with `WildcardPattern.Escape` so aliases
whose names are patterns -- `?` (Where-Object) and `%` (ForEach-Object)
-- resolve to themselves. The old script did this by hand with a leading
backtick.
- Rebuild the text by substituting from the highest offset down so the
earlier extents stay valid as the length changes, preserving the existing
behavior for multiple aliases on a line and multi-token definitions.
Scripts with no aliases are returned unchanged. The JSON-RPC contract is
untouched. I extended the E2E tests with a multi-alias line that includes
the wildcard-named `?` alias, which the naive modern approach would
mishandle.
Fixes #2108
Drafted by Copilot (Claude Opus 4.8).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 6ad4f46 commit 57dec9a
2 files changed
Lines changed: 73 additions & 26 deletions
File tree
- src/PowerShellEditorServices/Services/PowerShell/Handlers
- test/PowerShellEditorServices.Test.E2E
Lines changed: 50 additions & 26 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
4 | 7 | | |
| 8 | + | |
| 9 | + | |
5 | 10 | | |
6 | 11 | | |
7 | 12 | | |
| |||
31 | 36 | | |
32 | 37 | | |
33 | 38 | | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
| 39 | + | |
38 | 40 | | |
39 | | - | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
40 | 47 | | |
41 | | - | |
42 | | - | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
43 | 52 | | |
44 | | - | |
45 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
46 | 61 | | |
47 | | - | |
48 | | - | |
49 | | - | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
50 | 67 | | |
51 | | - | |
52 | | - | |
53 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
54 | 71 | | |
55 | | - | |
56 | | - | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
57 | 77 | | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
66 | 90 | | |
67 | 91 | | |
68 | 92 | | |
69 | | - | |
| 93 | + | |
70 | 94 | | |
71 | 95 | | |
72 | 96 | | |
| |||
Lines changed: 23 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1274 | 1274 | | |
1275 | 1275 | | |
1276 | 1276 | | |
| 1277 | + | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
| 1287 | + | |
| 1288 | + | |
| 1289 | + | |
| 1290 | + | |
| 1291 | + | |
| 1292 | + | |
| 1293 | + | |
| 1294 | + | |
| 1295 | + | |
| 1296 | + | |
| 1297 | + | |
| 1298 | + | |
| 1299 | + | |
1277 | 1300 | | |
1278 | 1301 | | |
1279 | 1302 | | |
| |||
0 commit comments