Skip to content

Add New-EditorFile #622

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 4 commits into from
Feb 20, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ FunctionsToExport = @('Register-EditorCommand',
'Out-CurrentFile',
'Join-ScriptExtent',
'Test-ScriptExtent',
'Open-EditorFile')
'Open-EditorFile',
'New-EditorFile')

# 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
106 changes: 102 additions & 4 deletions module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,111 @@ function Unregister-EditorCommand {
}
}

<#
.SYNOPSIS
Creates new files and opens them in your editor window
.DESCRIPTION
Creates new files and opens them in your editor window
.EXAMPLE
PS > New-EditorFile './foo.ps1'
Creates and opens a new foo.ps1 in your editor
.EXAMPLE
PS > Get-Process | New-EditorFile proc.txt
Creates and opens a new foo.ps1 in your editor with the contents of the call to Get-Process
.EXAMPLE
PS > Get-Process | New-EditorFile proc.txt -Force
Creates and opens a new foo.ps1 in your editor with the contents of the call to Get-Process. Overwrites the file if it already exists
.INPUTS
Path
an array of files you want to open in your editor
Value
The content you want in the new files
Force
Overwrites a file if it exists
#>
function New-EditorFile {
[CmdletBinding()]
param(
[Parameter()]
[String[]]
[ValidateNotNullOrEmpty()]
$Path,

[Parameter(ValueFromPipeline=$true)]
$Value,

[Parameter()]
[switch]
$Force
)

begin {
$valueList = @()
}

process {
$valueList += $Value
}

end {
if ($Path) {
foreach ($fileName in $Path)
{
if (-not (Test-Path $fileName) -or $Force) {
New-Item -Path $fileName -ItemType File | Out-Null

if ($Path.Count -gt 1) {
$preview = $false
} else {
$preview = $true
}

$psEditor.Workspace.OpenFile($fileName, $preview)
$psEditor.GetEditorContext().CurrentFile.InsertText(($valueList | Out-String))
} else {
$PSCmdlet.WriteError( (
New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList @(
[System.IO.IOException]"The file '$fileName' already exists.",
'NewEditorFileIOError',
[System.Management.Automation.ErrorCategory]::WriteError,
$fileName) ) )
}
}
} else {
$psEditor.Workspace.NewFile()
$psEditor.GetEditorContext().CurrentFile.InsertText(($valueList | Out-String))
}
}
}

function Open-EditorFile {
param([Parameter(Mandatory=$true)]$FilePaths)
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
$Path
Copy link
Contributor

Choose a reason for hiding this comment

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

As long as this parameter supports this scenario Get-ChildItem *.ps1 -r -file | Open-EditorFile, I'm good.

Copy link
Member Author

Choose a reason for hiding this comment

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

So @rkeithhill ... as it turns out, this will proceed to open the file temporarily (with the italicized file name)... so what ends up happening is, it opens the file temporarily and then the next file opens and replaces the file temporarily.

I'm not sure how to durably open files - if that's possible. I'll have to dig around.

@daviwil any thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, yes. That's why I double-click the editor window tab. That converts it from a temp (resuable) window to a permanent one. Not sure how you do that programmatically though.

Copy link
Member Author

Choose a reason for hiding this comment

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

Exactly. Maybe if @SeeminglyScience or @daviwil know? Otherwise, can I convince you to call this out of scope 😄

Copy link
Contributor

@rkeithhill rkeithhill Feb 13, 2018

Choose a reason for hiding this comment

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

You can use the vscode.window.showTextDocument() overload that takes a TextDocumentShowOptions object. One of the fields of that object is preview?. That should allow you to create each window in a "non-preview" window.

https://code.visualstudio.com/docs/extensionAPI/vscode-api#_a-nametextdocumentshowoptionsaspan-classcodeitem-id239textdocumentshowoptionsspan

Copy link
Contributor

Choose a reason for hiding this comment

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

That would be my preference. Later we could update the openFile() method to take an optional "preview" Boolean parameter.

Copy link
Member Author

Choose a reason for hiding this comment

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

Later we could update the openFile() method to take an optional "preview" Boolean parameter.

What if I already have this working? Is it worth adding it now or saving it for when someone actually asks for that?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll just push and you can judge whether we should keep it in or not

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like to have that option, so yeah, if it's working - go for it.

Copy link
Member Author

Choose a reason for hiding this comment

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

@rkeithhill pushed that. Here's the vscode-powershell PR as well:
PowerShell/vscode-powershell#1197

)

Get-ChildItem $FilePaths -File | ForEach-Object {
$psEditor.Workspace.OpenFile($_.FullName)
begin {
$Paths = @()
}

process {
$Paths += $Path
}

end {
if ($Paths.Count -gt 1) {
$preview = $false
} else {
$preview = $true
}

Get-ChildItem $Paths -File | ForEach-Object {
$psEditor.Workspace.OpenFile($_.FullName, $preview)
}
}
}
Set-Alias psedit Open-EditorFile -Scope Global

Export-ModuleMember -Function Open-EditorFile
Export-ModuleMember -Function Open-EditorFile,New-EditorFile
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,15 @@ public static readonly
public class OpenFileRequest
{
public static readonly
RequestType<string, EditorCommandResponse, object, object> Type =
RequestType<string, EditorCommandResponse, object, object>.Create("editor/openFile");
RequestType<OpenFileDetails, EditorCommandResponse, object, object> Type =
RequestType<OpenFileDetails, EditorCommandResponse, object, object>.Create("editor/openFile");
}

public class OpenFileDetails
{
public string FilePath { get; set; }

public bool Preview { get; set; }
}

public class CloseFileRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace Microsoft.PowerShell.EditorServices.Protocol.Server
{
internal class LanguageServerEditorOperations : IEditorOperations
{
private const bool DefaultPreviewSetting = true;

private EditorSession editorSession;
private IMessageSender messageSender;

Expand Down Expand Up @@ -115,7 +117,24 @@ public Task OpenFile(string filePath)
return
this.messageSender.SendRequest(
OpenFileRequest.Type,
filePath,
new OpenFileDetails
{
FilePath = filePath,
Preview = DefaultPreviewSetting
},
true);
}

public Task OpenFile(string filePath, bool preview)
{
return
this.messageSender.SendRequest(
OpenFileRequest.Type,
new OpenFileDetails
{
FilePath = filePath,
Preview = preview
},
true);
}

Expand Down
12 changes: 12 additions & 0 deletions src/PowerShellEditorServices/Extensions/EditorWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ public void OpenFile(string filePath)
this.editorOperations.OpenFile(filePath).Wait();
}

/// <summary>
/// Opens a file in the workspace. If the file is already open
/// its buffer will be made active.
/// You can specify whether the file opens as a preview or as a durable editor.
/// </summary>
/// <param name="filePath">The path to the file to be opened.</param>
/// <param name="preview">Determines wether the file is opened as a preview or as a durable editor.</param>
public void OpenFile(string filePath, bool preview)
{
this.editorOperations.OpenFile(filePath, preview).Wait();
}

#endregion
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/PowerShellEditorServices/Extensions/IEditorOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ public interface IEditorOperations
/// <returns>A Task that can be tracked for completion.</returns>
Task OpenFile(string filePath);

/// <summary>
/// Causes a file to be opened in the editor. If the file is
/// already open, the editor must switch to the file.
/// You can specify whether the file opens as a preview or as a durable editor.
/// </summary>
/// <param name="filePath">The path of the file to be opened.</param>
/// <param name="preview">Determines wether the file is opened as a preview or as a durable editor.</param>
/// <returns>A Task that can be tracked for completion.</returns>
Task OpenFile(string filePath, bool preview);

/// <summary>
/// Causes a file to be closed in the editor.
/// </summary>
Expand Down
Loading