Skip to content

Add position function set to Commands #496

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

Merged
merged 9 commits into from
Jun 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ PowerShellEditorServices.sln.ide/edb.chk
PowerShellEditorServices.sln.ide/edbres00001.jrs
PowerShellEditorServices.sln.ide/storage.ide
*.jrs

# Don't include PlatyPS generated MAML
module/PowerShellEditorServices/Commands/en-US/*-help.xml
3 changes: 3 additions & 0 deletions PowerShellEditorServices.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ task Clean {
Remove-Item .\module\PowerShellEditorServices\bin -Recurse -Force -ErrorAction Ignore
Get-ChildItem -Recurse src\*.nupkg | Remove-Item -Force -ErrorAction Ignore
Get-ChildItem .\module\PowerShellEditorServices\*.zip | Remove-Item -Force -ErrorAction Ignore
Get-ChildItem .\module\PowerShellEditorServices\Commands\en-US\*-help.xml | Remove-Item -Force -ErrorAction Ignore
}

task GetProductVersion -Before PackageNuGet, PackageModule, UploadArtifacts {
Expand Down Expand Up @@ -196,6 +197,8 @@ task LayoutModule -After Build {
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\net451\Newtonsoft.Json.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Desktop\
}
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\netstandard1.6\* -Filter Microsoft.PowerShell.EditorServices*.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Core\

New-ExternalHelp -Path $PSScriptRoot\module\docs -OutputPath $PSScriptRoot\module\PowerShellEditorServices\Commands\en-US -Force
}

task PackageNuGet {
Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ install:
Import-PackageProvider NuGet -Force | Out-Null
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted | Out-Null
Install-Module InvokeBuild -RequiredVersion 3.2.1 -Scope CurrentUser -Force | Out-Null
Install-Module platyPS -RequiredVersion 0.7.6 -Scope CurrentUser -Force | Out-Null

build_script:
- ps: Invoke-Build -Configuration Release
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ RootModule = 'PowerShellEditorServices.Commands.psm1'
ModuleVersion = '1.0.0'

# ID used to uniquely identify this module
GUID = '9ca15887-53a2-479a-9cda-48d26bcb6c47'
GUID = '6064d846-0fa0-4b6d-afc1-11e5bed3c4a9'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I realized I screwed that up after I merged the PR ;)


# Author of this module
Author = 'Microsoft'
Expand Down Expand Up @@ -57,7 +57,7 @@ Description = 'Provides internal commands for PowerShell Editor Services that on
# ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()
TypesToProcess = @('PowerShellEditorServices.Commands.types.ps1xml')

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()
Expand All @@ -66,7 +66,17 @@ Description = 'Provides internal commands for PowerShell Editor Services that on
# NestedModules = @()

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @('Register-EditorCommand', 'Unregister-EditorCommand')
FunctionsToExport = @('Register-EditorCommand',
'Unregister-EditorCommand',
'Set-ScriptExtent',
'Find-Ast',
'Import-EditorCommand',
'ConvertFrom-ScriptExtent',
'ConvertTo-ScriptExtent',
'Get-Token',
'Join-ScriptExtent',
'Test-ScriptExtent',
'psedit')

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Import-LocalizedData -BindingVariable Strings -FileName Strings

# TODO: Use a better script loading process here
. $PSScriptRoot\Public\CmdletInterface.ps1
Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 | ForEach-Object {
. $PSItem.FullName
}

Export-ModuleMember -Function *-*
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8" ?>
<Types>
<Type>
<Name>Microsoft.PowerShell.EditorServices.FullScriptExtent</Name>
<Members>
<MemberSet>
<Name>PSStandardMembers</Name>
<Members>
<PropertySet>
<Name>DefaultDisplayPropertySet</Name>
<ReferencedProperties>
<Name>File</Name>
<Name>StartScriptPosition</Name>
<Name>EndScriptPosition</Name>
<Name>StartLineNumber</Name>
<Name>StartColumnNumber</Name>
<Name>EndLineNumber</Name>
<Name>EndColumnNumber</Name>
<Name>StartOffset</Name>
<Name>EndOffset</Name>
<Name>Text</Name>
</ReferencedProperties>
</PropertySet>
</Members>
</MemberSet>
</Members>
</Type>
</Types>
Original file line number Diff line number Diff line change
@@ -1,50 +1,5 @@
<#
.SYNOPSIS
Registers a command which can be executed in the host editor.

.DESCRIPTION
Registers a command which can be executed in the host editor. This
command will be shown to the user either in a menu or command palette.
Upon invoking this command, either a function/cmdlet or ScriptBlock will
be executed depending on whether the -Function or -ScriptBlock parameter
was used when the command was registered.

This command can be run multiple times for the same command so that its
details can be updated. However, re-registration of commands should only
be used for development purposes, not for dynamic behavior.

.PARAMETER Name
Specifies a unique name which can be used to identify this command.
This name is not displayed to the user.

.PARAMETER DisplayName
Specifies a display name which is displayed to the user.

.PARAMETER Function
Specifies a function or cmdlet name which will be executed when the user
invokes this command. This function may take a parameter called $context
which will be populated with an EditorContext object containing information
about the host editor's state at the time the command was executed.

.PARAMETER ScriptBlock
Specifies a ScriptBlock which will be executed when the user invokes this
command. This ScriptBlock may take a parameter called $context
which will be populated with an EditorContext object containing information
about the host editor's state at the time the command was executed.

.PARAMETER SuppressOutput
If provided, causes the output of the editor command to be suppressed when
it is run. Errors that occur while running this command will still be
written to the host.

.EXAMPLE
PS> Register-EditorCommand -Name "MyModule.MyFunctionCommand" -DisplayName "My function command" -Function Invoke-MyCommand -SuppressOutput

.EXAMPLE
PS> Register-EditorCommand -Name "MyModule.MyScriptBlockCommand" -DisplayName "My ScriptBlock command" -ScriptBlock { Write-Output "Hello from my command!" }

.LINK
Unregister-EditorCommand
.EXTERNALHELP ..\PowerShellEditorServices.Commands-help.xml
#>
function Register-EditorCommand {
[CmdletBinding()]
Expand Down Expand Up @@ -100,21 +55,7 @@ function Register-EditorCommand {
}

<#
.SYNOPSIS
Unregisters a command which has already been registered in the host editor.

.DESCRIPTION
Unregisters a command which has already been registered in the host editor.
An error will be thrown if the specified Name is unknown.

.PARAMETER Name
Specifies a unique name which identifies a command which has already been registered.

.EXAMPLE
PS> Unregister-EditorCommand -Name "MyModule.MyFunctionCommand"

.LINK
Register-EditorCommand
.EXTERNALHELP ..\PowerShellEditorServices.Commands-help.xml
#>
function Unregister-EditorCommand {
[CmdletBinding()]
Expand All @@ -138,3 +79,4 @@ function psedit {
$psEditor.Workspace.OpenFile($_.FullName)
}
}
Export-ModuleMember -Function psedit
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
function ConvertFrom-ScriptExtent {
<#
.EXTERNALHELP ..\PowerShellEditorServices.Commands-help.xml
#>
[CmdletBinding()]
[OutputType([Microsoft.PowerShell.EditorServices.BufferRange], ParameterSetName='BufferRange')]
[OutputType([Microsoft.PowerShell.EditorServices.BufferPosition], ParameterSetName='BufferPosition')]
param(
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.Language.IScriptExtent[]]
$Extent,

[Parameter(ParameterSetName='BufferRange')]
[switch]
$BufferRange,

[Parameter(ParameterSetName='BufferPosition')]
[switch]
$BufferPosition,

[Parameter(ParameterSetName='BufferPosition')]
[switch]
$Start,

[Parameter(ParameterSetName='BufferPosition')]
[switch]
$End
)
process {
foreach ($aExtent in $Extent) {
switch ($PSCmdlet.ParameterSetName) {
BufferRange {
# yield
New-Object Microsoft.PowerShell.EditorServices.BufferRange @(
$aExtent.StartLineNumber,
$aExtent.StartColumnNumber,
$aExtent.EndLineNumber,
$aExtent.EndColumnNumber)
}
BufferPosition {
if ($End) {
$line = $aExtent.EndLineNumber
$column = $aExtent.EndLineNumber
} else {
$line = $aExtent.StartLineNumber
$column = $aExtent.StartLineNumber
}
# yield
New-Object Microsoft.PowerShell.EditorServices.BufferPosition @(
$line,
$column)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
function ConvertTo-ScriptExtent {
<#
.EXTERNALHELP ..\PowerShellEditorServices.Commands-help.xml
#>
[CmdletBinding()]
[OutputType([System.Management.Automation.Language.IScriptExtent])]
param(
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName='ByOffset')]
[Alias('StartOffset', 'Offset')]
[int]
$StartOffsetNumber,

[Parameter(ValueFromPipelineByPropertyName, ParameterSetName='ByOffset')]
[Alias('EndOffset')]
[int]
$EndOffsetNumber,

[Parameter(ValueFromPipelineByPropertyName, ParameterSetName='ByPosition')]
[Alias('StartLine', 'Line')]
[int]
$StartLineNumber,

[Parameter(ValueFromPipelineByPropertyName, ParameterSetName='ByPosition')]
[Alias('StartColumn', 'Column')]
[int]
$StartColumnNumber,

[Parameter(ValueFromPipelineByPropertyName, ParameterSetName='ByPosition')]
[Alias('EndLine')]
[int]
$EndLineNumber,

[Parameter(ValueFromPipelineByPropertyName, ParameterSetName='ByPosition')]
[Alias('EndColumn')]
[int]
$EndColumnNumber,

[Parameter(ValueFromPipelineByPropertyName, ParameterSetName='ByPosition')]
[Parameter(ValueFromPipelineByPropertyName, ParameterSetName='ByOffset')]
[Parameter(ValueFromPipelineByPropertyName, ParameterSetName='ByBuffer')]
[Alias('File', 'FileName')]
[string]
$FilePath = $psEditor.GetEditorContext().CurrentFile.Path,

[Parameter(ValueFromPipelineByPropertyName, ParameterSetName='ByBuffer')]
[Alias('Start')]
[Microsoft.PowerShell.EditorServices.BufferPosition]
$StartBuffer,

[Parameter(ValueFromPipelineByPropertyName, ParameterSetName='ByBuffer')]
[Alias('End')]
[Microsoft.PowerShell.EditorServices.BufferPosition]
$EndBuffer,

[Parameter(Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName,
ParameterSetName='ByExtent')]
[System.Management.Automation.Language.IScriptExtent]
$Extent
)
begin {
$fileContext = $psEditor.GetEditorContext().CurrentFile
$emptyExtent = New-Object Microsoft.PowerShell.EditorServices.FullScriptExtent @(
<# filecontext: #> $fileContext,
<# startOffset: #> 0,
<# endOffset: #> 0)
}
process {
# Already a InternalScriptExtent, FullScriptExtent or is empty.
$returnAsIs = $Extent -and
(0 -ne $Extent.StartOffset -or
0 -ne $Extent.EndOffset -or
$Extent -eq $emptyExtent)

if ($returnAsIs) { return $Extent }

if ($StartOffsetNumber) {
$startOffset = $StartOffsetNumber
$endOffset = $EndOffsetNumber

# Allow creating a single position extent with just the offset parameter.
if (-not $EndOffsetNumber) {
$endOffset = $startOffset
}
return New-Object Microsoft.PowerShell.EditorServices.FullScriptExtent @(
$fileContext,
$startOffset,
$endOffset)
}
if (-not $StartBuffer) {
if (-not $StartColumnNumber) { $StartColumnNumber = 1 }
if (-not $StartLineNumber) { $StartLineNumber = 1 }
$StartBuffer = New-Object Microsoft.PowerShell.EditorServices.BufferPosition @(
$StartColumnNumber,
$StartLineNumber)

if ($EndLineNumber -and $EndColumnNumber) {
$EndBuffer = New-Object Microsoft.PowerShell.EditorServices.BufferPosition @(
$EndLineNumber,
$EndColumnNumber)
}
}
if (-not $EndBuffer) { $EndBuffer = $StartBuffer }

$bufferRange = New-Object Microsoft.PowerShell.EditorServices.BufferRange @(
$StartBuffer,
$EndBuffer)

return New-Object Microsoft.PowerShell.EditorServices.FullScriptExtent @(
$fileContext,
$bufferRange)
}
}
Loading