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

[DO NOT MERGE] Prototype for SPI integration #809

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

daveyc123
Copy link

This change is a prototype for SPI integration. It provides two new commands that leverage the new add package workflows within swiftpm

  • Add package
  • Browse Package Index...

Add package is fairly straight forward. It prompts the user for both the package URL and the version, and adds the package to their Package.swift (by calling swift package add...). An alternative here would be to have the LSP do the equivalent refactoring operation.

Browse Package Index opens a webview to the SPI (note in this patch it's using localhost) in an iframe. The intent is that the SPI can detect that it is running through the vscode webview and provide alternative UI for adding a package. This is accomplished through:

  • A window event listener between the frame and the webview in which messages that can be sent [1]
  • Access to the vscode api from the webview that post a message to run the command. This invokes the add package command detailed above

[1] - SPI usage

  • SPI would need to detect that it is running in the vscode context and present alternative (or augmented UI). In the example given it could use window.name to check and see if it is equal to "vscode_iframe"
  • When the user takes the action to add their package using this new UI, SPI needs to send a message to its parent. window.parent.postMessage( { action: 'addPackage', url: url, version: version }, '*')

This change is a prototype for SPI integration. It provides two new
commands that leverage the new add package workflows within swiftpm
  * Add package
  * Browse Package Index...

Add package is fairly straight forward. It prompts the user for
both the package URL and the version, and adds the package to their
Package.swift (by calling swift package add...). An alternative here
would be to have the LSP do the equivalent refactoring operation.

Browse Package Index opens a webview to the SPI (note in this patch
it's using localhost) in an iframe. The intent is that the SPI can
detect that it is running through the vscode webview and provide
alternative UI for adding a package. This is accomplished through:
  * A window event listener between the frame and the webview in which
    messages that can be sent [1]
  * Access to the vscode api from the webview that post a message
    to run the command. This invokes the add package command detailed
    above

[1] - SPI usage
- SPI would need to detect that it is running in the vscode context
  and present alternative (or augmented UI). In the example given
  it could use window.name to check and see if it is equal to
  "vscode_iframe"
- When the user takes the action to add their package using this new
  UI, SPI needs to send a message to its parent.
    window.parent.postMessage(
        {
            action: 'addPackage',
            url: url,
            version: version
        }, '*')
@daveyc123 daveyc123 linked an issue May 17, 2024 that may be closed by this pull request
@daveyc123
Copy link
Author

@finestructure @daveverwer

@plemarquand plemarquand marked this pull request as draft May 17, 2024 20:26
src/commands.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@adam-fowler adam-fowler left a comment

Choose a reason for hiding this comment

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

Thought I'd try and get this running but nothing shows up 😞 when I run browsePackageIndex

The add package related commands (add package, browse swift package
index) would previously show an error if the user ran the command
but had a swift toolchain that was older than 6.0. This adds a context
key so that the command doesn't appear in this case.
@daveyc123
Copy link
Author

Thought I'd try and get this running but nothing shows up 😞 when I run browsePackageIndex

Unfortunately it won't be functional until SPI has added support

@adam-fowler
Copy link
Contributor

But @daveyc123 it didn't even show the SPI website.

@daveyc123
Copy link
Author

But @daveyc123 it didn't even show the SPI website.

In the patch it's pointing to localhost so that it can be tested against a locally running SPI with the appropriate changes.

return;
}

const panel = vscode.window.createWebviewPanel(
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we break out some of the web view stuff, into a general function that can be used by other systems. Currently when you right click on a package dependency and select View Repository it opens a web page in your default browser. Would be cool if we could do that inside VSCode instead in a similar manner to how this works.

// The context key `swift.addPackageRefactoringAvailable` only works if the extension has been
// activated. As such, we also have to allow this command to run when no workspace is
// active. Show an error to the user if the command is unavailable.
if (!ctx.toolchain.swiftVersion.isGreaterThanOrEqual(new Version(6, 0, 0))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the point of having this command available when you have no package loaded?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Integration with Swift Package Index
2 participants