Skip to content

Commit fd0e9cc

Browse files
authored
Fix PSES Commands module (#1211)
1 parent 35fd46d commit fd0e9cc

File tree

7 files changed

+182
-44
lines changed

7 files changed

+182
-44
lines changed

module/PowerShellEditorServices/Commands/Private/BuiltInCommands.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Register-EditorCommand `
33
-DisplayName 'Open Editor Profile' `
44
-SuppressOutput `
55
-ScriptBlock {
6-
param([Microsoft.PowerShell.EditorServices.Services.PowerShellContext.EditorContext]$context)
6+
param([Microsoft.PowerShell.EditorServices.Extensions.EditorContext, Microsoft.PowerShell.EditorServices]$context)
77
If (!(Test-Path -Path $Profile)) { New-Item -Path $Profile -ItemType File }
88
$psEditor.Workspace.OpenFile($Profile)
99
}
@@ -13,7 +13,7 @@ Register-EditorCommand `
1313
-DisplayName 'Open Profile from List (Current User)' `
1414
-SuppressOutput `
1515
-ScriptBlock {
16-
param([Microsoft.PowerShell.EditorServices.Services.PowerShellContext.EditorContext]$context)
16+
param([Microsoft.PowerShell.EditorServices.Extensions.EditorContext, Microsoft.PowerShell.EditorServices]$context)
1717

1818
$Current = Split-Path -Path $profile -Leaf
1919
$List = @($Current,'Microsoft.VSCode_profile.ps1','Microsoft.PowerShell_profile.ps1','Microsoft.PowerShellISE_profile.ps1','Profile.ps1') | Select-Object -Unique

module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,17 @@ function Register-EditorCommand {
3636
{
3737
$commandArgs = @($Name, $DisplayName, $SuppressOutput.IsPresent)
3838

39-
if ($ScriptBlock -ne $null)
39+
$editorCommand = if ($ScriptBlock -ne $null)
4040
{
4141
Write-Verbose "Registering command '$Name' which executes a ScriptBlock"
42-
$commandArgs += $ScriptBlock
42+
[Microsoft.PowerShell.EditorServices.Extensions.EditorCommand, Microsoft.PowerShell.EditorServices]::new($Name, $DisplayName, $SuppressOutput, $ScriptBlock)
4343
}
4444
else
4545
{
4646
Write-Verbose "Registering command '$Name' which executes a function"
47-
$commandArgs += $Function
47+
[Microsoft.PowerShell.EditorServices.Extensions.EditorCommand, Microsoft.PowerShell.EditorServices]::new($Name, $DisplayName, $SuppressOutput, $Function)
4848
}
4949

50-
$editorCommand = New-Object Microsoft.PowerShell.EditorServices.Extensions.EditorCommand -ArgumentList $commandArgs
5150
if ($psEditor.RegisterCommand($editorCommand))
5251
{
5352
Write-Verbose "Registered new command '$Name'"

module/PowerShellEditorServices/Commands/Public/ConvertFrom-ScriptExtent.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ function ConvertFrom-ScriptExtent {
88
.EXTERNALHELP ..\PowerShellEditorServices.Commands-help.xml
99
#>
1010
[CmdletBinding()]
11-
[OutputType([Microsoft.PowerShell.EditorServices.BufferRange], ParameterSetName='BufferRange')]
12-
[OutputType([Microsoft.PowerShell.EditorServices.BufferPosition], ParameterSetName='BufferPosition')]
11+
[OutputType([Microsoft.PowerShell.EditorServices.Extensions.IFileRange, Microsoft.PowerShell.EditorServices], ParameterSetName='BufferRange')]
12+
[OutputType([Microsoft.PowerShell.EditorServices.Extensions.IFilePosition, Microsoft.PowerShell.EditorServices], ParameterSetName='BufferPosition')]
1313
param(
1414
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
1515
[ValidateNotNullOrEmpty()]
@@ -37,7 +37,7 @@ function ConvertFrom-ScriptExtent {
3737
switch ($PSCmdlet.ParameterSetName) {
3838
BufferRange {
3939
# yield
40-
New-Object Microsoft.PowerShell.EditorServices.BufferRange @(
40+
[Microsoft.PowerShell.EditorServices.Extensions.FileRange, Microsoft.PowerShell.EditorServices]::new(
4141
$aExtent.StartLineNumber,
4242
$aExtent.StartColumnNumber,
4343
$aExtent.EndLineNumber,
@@ -52,7 +52,7 @@ function ConvertFrom-ScriptExtent {
5252
$column = $aExtent.StartLineNumber
5353
}
5454
# yield
55-
New-Object Microsoft.PowerShell.EditorServices.BufferPosition @(
55+
[Microsoft.PowerShell.EditorServices.Extensions.FileRange, Microsoft.PowerShell.EditorServices]::new(
5656
$line,
5757
$column)
5858
}

module/PowerShellEditorServices/Commands/Public/ConvertTo-ScriptExtent.ps1

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ function ConvertTo-ScriptExtent {
4949

5050
[Parameter(ValueFromPipelineByPropertyName, ParameterSetName='ByBuffer')]
5151
[Alias('Start')]
52-
[Microsoft.PowerShell.EditorServices.BufferPosition]
52+
[Microsoft.PowerShell.EditorServices.Extensions.IFilePosition, Microsoft.PowerShell.EditorServices]
5353
$StartBuffer,
5454

5555
[Parameter(ValueFromPipelineByPropertyName, ParameterSetName='ByBuffer')]
5656
[Alias('End')]
57-
[Microsoft.PowerShell.EditorServices.BufferPosition]
57+
[Microsoft.PowerShell.EditorServices.Extensions.IFilePosition, Microsoft.PowerShell.EditorServices]
5858
$EndBuffer,
5959

6060
[Parameter(Mandatory,
@@ -66,13 +66,11 @@ function ConvertTo-ScriptExtent {
6666
)
6767
begin {
6868
$fileContext = $psEditor.GetEditorContext().CurrentFile
69-
$emptyExtent = New-Object Microsoft.PowerShell.EditorServices.FullScriptExtent @(
70-
<# filecontext: #> $fileContext,
71-
<# startOffset: #> 0,
72-
<# endOffset: #> 0)
69+
$emptyExtent = [Microsoft.PowerShell.EditorServices.Extensions.FileScriptExtent, Microsoft.PowerShell.EditorServices]::Empty
7370
}
71+
7472
process {
75-
# Already a InternalScriptExtent, FullScriptExtent or is empty.
73+
# Already a InternalScriptExtent, FileScriptExtent or is empty.
7674
$returnAsIs = $Extent -and
7775
(0 -ne $Extent.StartOffset -or
7876
0 -ne $Extent.EndOffset -or
@@ -88,32 +86,36 @@ function ConvertTo-ScriptExtent {
8886
if (-not $EndOffsetNumber) {
8987
$endOffset = $startOffset
9088
}
91-
return New-Object Microsoft.PowerShell.EditorServices.FullScriptExtent @(
89+
90+
return [Microsoft.PowerShell.EditorServices.Extensions.FileScriptExtent, Microsoft.PowerShell.EditorServices]::FromOffsets(
9291
$fileContext,
9392
$startOffset,
9493
$endOffset)
9594
}
96-
if (-not $StartBuffer) {
97-
if (-not $StartColumnNumber) { $StartColumnNumber = 1 }
98-
if (-not $StartLineNumber) { $StartLineNumber = 1 }
99-
$StartBuffer = New-Object Microsoft.PowerShell.EditorServices.BufferPosition @(
100-
$StartLineNumber,
101-
$StartColumnNumber)
102-
103-
if ($EndLineNumber -and $EndColumnNumber) {
104-
$EndBuffer = New-Object Microsoft.PowerShell.EditorServices.BufferPosition @(
105-
$EndLineNumber,
106-
$EndColumnNumber)
95+
96+
if ($StartBuffer) {
97+
if (-not $EndBuffer)
98+
{
99+
$EndBuffer = $StartBuffer
107100
}
101+
102+
return [Microsoft.PowerShell.EditorServices.Extensions.FileScriptExtent, Microsoft.PowerShell.EditorServices]::FromPositions(
103+
$fileContext,
104+
$StartBuffer.Line,
105+
$StartBuffer.Column,
106+
$EndBuffer.Line,
107+
$EndBuffer.Column)
108108
}
109-
if (-not $EndBuffer) { $EndBuffer = $StartBuffer }
110109

111-
$bufferRange = New-Object Microsoft.PowerShell.EditorServices.BufferRange @(
112-
$StartBuffer,
113-
$EndBuffer)
110+
if (-not $StartColumnNumber) { $StartColumnNumber = 1 }
111+
if (-not $StartLineNumber) { $StartLineNumber = 1 }
112+
if (-not $EndLineNumber) { $EndLineNumber = 1 }
113+
if (-not $EndColumnNumber) { $EndColumnNumber = 1 }
114114

115-
return New-Object Microsoft.PowerShell.EditorServices.FullScriptExtent @(
116-
$fileContext,
117-
$bufferRange)
115+
return [Microsoft.PowerShell.EditorServices.Extensions.FileScriptExtent, Microsoft.PowerShell.EditorServices]::FromPositions(
116+
$StartLineNumber,
117+
$StartColumnNumber,
118+
$EndLineNumber,
119+
$EndColumnNumber)
118120
}
119121
}

module/PowerShellEditorServices/Commands/Public/Import-EditorCommand.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function Import-EditorCommand {
77
<#
88
.EXTERNALHELP ..\PowerShellEditorServices.Commands-help.xml
99
#>
10-
[OutputType([Microsoft.PowerShell.EditorServices.Services.PowerShellContext.EditorCommand])]
10+
[OutputType([Microsoft.PowerShell.EditorServices.Extensions.EditorCommand, Microsoft.PowerShell.EditorServices])]
1111
[CmdletBinding(DefaultParameterSetName='ByCommand')]
1212
param(
1313
[Parameter(Position=0,
@@ -72,7 +72,7 @@ function Import-EditorCommand {
7272
$commands = $Command | Get-Command -ErrorAction SilentlyContinue
7373
}
7474
}
75-
$attributeType = [Microsoft.PowerShell.EditorServices.Services.PowerShellContext.EditorCommandAttribute]
75+
$attributeType = [Microsoft.PowerShell.EditorServices.Extensions.EditorCommandAttribute, Microsoft.PowerShell.EditorServices]
7676
foreach ($aCommand in $commands) {
7777
# Get the attribute from our command to get name info.
7878
$details = $aCommand.ScriptBlock.Attributes | Where-Object TypeId -eq $attributeType
@@ -96,7 +96,7 @@ function Import-EditorCommand {
9696
}
9797
# Check for a context parameter.
9898
$contextParameter = $aCommand.Parameters.Values |
99-
Where-Object ParameterType -eq ([Microsoft.PowerShell.EditorServices.Services.PowerShellContext.EditorContext])
99+
Where-Object ParameterType -eq ([Microsoft.PowerShell.EditorServices.Extensions.EditorContext, Microsoft.PowerShell.EditorServices])
100100

101101
# If one is found then add a named argument. Otherwise call the command directly.
102102
if ($contextParameter) {
@@ -106,7 +106,7 @@ function Import-EditorCommand {
106106
$scriptBlock = [scriptblock]::Create($aCommand.Name)
107107
}
108108

109-
$editorCommand = New-Object Microsoft.PowerShell.EditorServices.Services.PowerShellContext.EditorCommand @(
109+
$editorCommand = [Microsoft.PowerShell.EditorServices.Extensions.EditorCommand, Microsoft.PowerShell.EditorServices]::new(
110110
<# commandName: #> $details.Name,
111111
<# displayName: #> $details.DisplayName,
112112
<# suppressOutput: #> $details.SuppressOutput,

module/PowerShellEditorServices/Commands/Public/Set-ScriptExtent.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ function Set-ScriptExtent {
2727
)
2828
begin {
2929
$fileContext = $psEditor.GetEditorContext().CurrentFile
30-
$extentList = New-Object System.Collections.Generic.List[Microsoft.PowerShell.EditorServices.FullScriptExtent]
30+
$extentList = [System.Collections.Generic.List[Microsoft.PowerShell.EditorServices.Extensions.FileScriptExtent, Microsoft.PowerShell.EditorServices]]::new()
3131
}
3232
process {
33-
if ($Extent -isnot [Microsoft.PowerShell.EditorServices.FullScriptExtent]) {
34-
$Extent = New-Object Microsoft.PowerShell.EditorServices.FullScriptExtent @(
33+
if ($Extent -isnot [Microsoft.PowerShell.EditorServices.Extensions.FileScriptExtent, Microsoft.PowerShell.EditorServices]) {
34+
$Extent = [Microsoft.PowerShell.EditorServices.Extensions.FileScriptExtent, Microsoft.PowerShell.EditorServices]::FromOffsets(
3535
$fileContext,
3636
$Extent.StartOffset,
3737
$Extent.EndOffset)
@@ -69,7 +69,7 @@ function Set-ScriptExtent {
6969
$differenceOffset = $aText.Length - $aExtent.Text.Length
7070
$scriptText = $fileContext.GetText()
7171

72-
$fileContext.InsertText($aText, $aExtent.BufferRange)
72+
$fileContext.InsertText($aText, $aExtent)
7373

7474
$newText = $scriptText.Remove($aExtent.StartOffset, $aExtent.Text.Length).Insert($aExtent.StartOffset, $aText)
7575

src/PowerShellEditorServices/Extensions/EditorFileRanges.cs

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,146 @@
77
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
88
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
99
using System;
10+
using System.Management.Automation.Language;
1011

1112
namespace Microsoft.PowerShell.EditorServices.Extensions
1213
{
14+
public class FileScriptPosition : IScriptPosition, IFilePosition
15+
{
16+
public static FileScriptPosition Empty { get; } = new FileScriptPosition(null, 0, 0, 0);
17+
18+
public static FileScriptPosition FromPosition(FileContext file, int lineNumber, int columnNumber)
19+
{
20+
int offset = 0;
21+
int currLine = 1;
22+
string fileText = file.Ast.Extent.Text;
23+
while (offset < fileText.Length && currLine < lineNumber)
24+
{
25+
offset = fileText.IndexOf('\n', offset);
26+
currLine++;
27+
}
28+
29+
offset += columnNumber - 1;
30+
31+
return new FileScriptPosition(file, lineNumber, columnNumber, offset);
32+
}
33+
34+
public static FileScriptPosition FromOffset(FileContext file, int offset)
35+
{
36+
37+
int line = 1;
38+
string fileText = file.Ast.Extent.Text;
39+
40+
if (offset >= fileText.Length)
41+
{
42+
throw new ArgumentException(nameof(offset), "Offset greater than file length");
43+
}
44+
45+
int lastLineOffset = -1;
46+
for (int i = 0; i < offset; i++)
47+
{
48+
if (fileText[i] == '\n')
49+
{
50+
lastLineOffset = i;
51+
line++;
52+
}
53+
}
54+
55+
int column = offset - lastLineOffset;
56+
57+
return new FileScriptPosition(file, line, column, offset);
58+
}
59+
60+
private readonly FileContext _file;
61+
62+
internal FileScriptPosition(FileContext file, int lineNumber, int columnNumber, int offset)
63+
{
64+
_file = file;
65+
Line = file.GetTextLines()[lineNumber - 1];
66+
ColumnNumber = columnNumber;
67+
LineNumber = lineNumber;
68+
Offset = offset;
69+
}
70+
71+
public int ColumnNumber { get; }
72+
73+
public string File { get; }
74+
75+
public string Line { get; }
76+
77+
public int LineNumber { get; }
78+
79+
public int Offset { get; }
80+
81+
int IFilePosition.Column => ColumnNumber;
82+
83+
int IFilePosition.Line => LineNumber;
84+
85+
public string GetFullScript() => _file.GetText();
86+
}
87+
88+
public class FileScriptExtent : IScriptExtent, IFileRange
89+
{
90+
public static bool IsEmpty(FileScriptExtent extent)
91+
{
92+
return extent == Empty
93+
|| (extent.StartOffset == 0 && extent.EndOffset == 0);
94+
}
95+
96+
public static FileScriptExtent Empty { get; } = new FileScriptExtent(null, FileScriptPosition.Empty, FileScriptPosition.Empty);
97+
98+
public static FileScriptExtent FromOffsets(FileContext file, int startOffset, int endOffset)
99+
{
100+
return new FileScriptExtent(
101+
file,
102+
FileScriptPosition.FromOffset(file, startOffset),
103+
FileScriptPosition.FromOffset(file, endOffset));
104+
}
105+
106+
public static FileScriptExtent FromPositions(FileContext file, int startLine, int startColumn, int endLine, int endColumn)
107+
{
108+
return new FileScriptExtent(
109+
file,
110+
FileScriptPosition.FromPosition(file, startLine, startColumn),
111+
FileScriptPosition.FromPosition(file, endLine, endColumn));
112+
}
113+
114+
private readonly FileContext _file;
115+
private readonly FileScriptPosition _start;
116+
private readonly FileScriptPosition _end;
117+
118+
public FileScriptExtent(FileContext file, FileScriptPosition start, FileScriptPosition end)
119+
{
120+
_file = file;
121+
_start = start;
122+
_end = end;
123+
}
124+
125+
public int EndColumnNumber => _end.ColumnNumber;
126+
127+
public int EndLineNumber => _end.LineNumber;
128+
129+
public int EndOffset => _end.Offset;
130+
131+
public IScriptPosition EndScriptPosition => _end;
132+
133+
public string File => _file.Path;
134+
135+
public int StartColumnNumber => _start.ColumnNumber;
136+
137+
public int StartLineNumber => _start.LineNumber;
138+
139+
public int StartOffset => _start.Offset;
140+
141+
public IScriptPosition StartScriptPosition => _start;
142+
143+
public string Text => _file.GetText().Substring(_start.Offset, _end.Offset - _start.Offset);
144+
145+
IFilePosition IFileRange.Start => _start;
146+
147+
IFilePosition IFileRange.End => _end;
148+
}
149+
13150
/// <summary>
14151
/// A 1-based file position, referring to a point in a file.
15152
/// </summary>

0 commit comments

Comments
 (0)