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

Add document tracker to set activeFileEntrypoint context #1883

Merged
merged 19 commits into from
Jul 3, 2024

Conversation

dotNomad
Copy link
Collaborator

@dotNomad dotNomad commented Jun 26, 2024

This PR adds a new context to the extension that tracks if the active document is an entrypoint with a ContentType that is known (aka not Unknown)

If the active document is an entrypoint - found via /api/inspect - we show a button which currently is non-functional, but will lead a user through deploying.

Note that licenses.md didn't need to change since vscode-uri is already included from dependencies.

Intent

Start of #1848

Type of Change

    • Bug Fix
    • New Feature
    • Breaking Change
    • Documentation
    • Refactor
    • Tooling

Approach

The approach here was to create a tracker that created all of the listeners needed itself, tracked the documents it needed to, and updated the context.

This way the extension could create a DocumentTracker and add it to its diposables with no other interaction between the two.

The DocumentTracker handles which documents need to be tracked via VSCode listeners, and TrackedEntrypointDocuments handle updating themselves if they are or are becoming the active document.

Everything else was broken off as a utility function.

Directions for Reviewers

Test the entrypoint context in a few cases:

  • open a non-entrypoint file
  • open an entrypoint file
  • open a file outside of the workspace entirely (I do this by running code some-file.txt from the Desktop for example)

Then do the same from a folder at a higher level. The dir and entrypoint query params should take care of the nested folder part for us.

@dotNomad dotNomad changed the title Dotnomad/entrypoint tracker Add document tracker to set active file entrypoint context Jun 26, 2024
@dotNomad dotNomad changed the title Add document tracker to set active file entrypoint context Add document tracker to set activeFileEntrypoint context Jun 26, 2024
@dotNomad dotNomad force-pushed the dotnomad/entrypoint-tracker branch from bf49247 to c03b1b5 Compare July 2, 2024 21:45
@dotNomad dotNomad force-pushed the dotnomad/entrypoint-tracker branch 2 times, most recently from d475928 to a826fdd Compare July 3, 2024 02:04
@dotNomad dotNomad force-pushed the dotnomad/entrypoint-tracker branch from a826fdd to df6aca0 Compare July 3, 2024 02:34
@@ -685,6 +698,7 @@
"eventsource": "^2.0.2",
"get-port": "5.1.1",
"mutexify": "^1.4.0",
"retry": "^0.13.1"
"retry": "^0.13.1",
"vscode-uri": "^3.0.8"
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added vscode-uri as a direct dependency to use the basename and dirname functions: https://github.com/microsoft/vscode-uri/blob/main/src/utils.ts

{
"command": "posit.publisher.deployWithEntrypoint",
"title": "Deploy with this Entrypoint",
"icon": "$(cloud-upload)",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Using a cloud-upload icon to avoid needing to deal with a PNG, or getting an SVG into a font for release.

Copy link
Collaborator

Choose a reason for hiding this comment

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

As discussed, do we just want to use the PNG that we have for the old publisher icon, so we can replace both at once to the new one?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tested this out and it does work, and doesn't look too bad even though it is being scaled down from 450x450 to 16x16

CleanShot 2024-07-03 at 14 25 44@2x

However since it is a png it doesn't get colored using the active theme. So even if I changed it to be white/gray/etc it would look incorrect depending on the theme used.

Personally I'd prefer to avoid the non-native look until we get that fixed.

If others disagree, it can be easily changed while I'm out with the below example:

      {
        "command": "posit.publisher.deployWithEntrypoint",
        "title": "Deploy with this Entrypoint",
        "icon": "assets/img/color/posit-publisher.png",
        "category": "Posit Publisher"
      }

new DocumentTracker(),
commands.registerCommand(Commands.DeployWithEntrypoint, () => {
commands.executeCommand(Commands.HomeView.Focus);
console.log("'Deploy with this Entrypoint' button hit!");
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Currently the button only console logs.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Easily built upon! Great!

dir: workspace.asRelativePath(dirname),
entrypoint: uriUtils.basename(document.uri),
});
return hasKnownContentType(response.data);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm only marking a document as an entrypoint if any of the inspections have a ContentType different than Unknown

Comment on lines +260 to +262
if (dirname.fsPath === workspaceFolder.uri.fsPath) {
return ".";
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

workspace.asRelativePath(dirname) returns a path relative to the workspace folder, but if the path IS the workspace folder then it will return an absolute path.

That doesn't work for us, so here I check to see if the file system path is the same and return a ".".

* @returns If the text document is an entrypoint
*/
async function isDocumentEntrypoint(document: TextDocument): Promise<boolean> {
const dir = relativeDir(document.uri);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was quite tricky digging into what VSCode could do for us and what else is out there. I broke it out into its own utils function so we can get a dir path for any API requests.

* @param options Options for the activation
* @param options.forceUpdate Whether to force the entrypoint to update
*/
async activate(options?: { forceUpdate?: boolean }) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The API calls only happen when we create a TrackedEntrypointDocument or when it is activated and needs an updated.

This occurs when the document is saved, or the document is re-opened.

TrackedEntrypointDocuments don't update if the file is open in VSCode, another file becomes active, then the original file is active again. That is because nothing has changed since the first API call.

Comment on lines +186 to +188
onTextDocumentClosed(document: TextDocument) {
this.removeDocument(document);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Documents are removed when closed since another editor could potentially change the document. This way when a document is re-opened we recheck just in case.

@dotNomad dotNomad marked this pull request as ready for review July 3, 2024 02:49
async function isDocumentEntrypoint(document: TextDocument): Promise<boolean> {
const dir = relativeDir(document.uri);
// If the file is outside the workspace, it cannot be an entrypoint
if (dir === undefined) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If we don't want anything but . level entrypoints for now we can easily add that check here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

True, but I don't think we need to per our implementation plan.

Copy link
Collaborator

@sagerb sagerb left a comment

Choose a reason for hiding this comment

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

This looks really good and I think I have a handle on it.

If you could update the command to accept the document URI parameter that will be passed in, per my comment, and then output it in the console log, I think you'll have this PR at a good state. I'll approve it so you aren't blocked by me. Thanks!

{
"command": "posit.publisher.deployWithEntrypoint",
"title": "Deploy with this Entrypoint",
"icon": "$(cloud-upload)",
Copy link
Collaborator

Choose a reason for hiding this comment

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

As discussed, do we just want to use the PNG that we have for the old publisher icon, so we can replace both at once to the new one?

extensions/vscode/src/extension.ts Outdated Show resolved Hide resolved
async function isDocumentEntrypoint(document: TextDocument): Promise<boolean> {
const dir = relativeDir(document.uri);
// If the file is outside the workspace, it cannot be an entrypoint
if (dir === undefined) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

True, but I don't think we need to per our implementation plan.

new DocumentTracker(),
commands.registerCommand(Commands.DeployWithEntrypoint, () => {
commands.executeCommand(Commands.HomeView.Focus);
console.log("'Deploy with this Entrypoint' button hit!");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Easily built upon! Great!

@dotNomad dotNomad merged commit c2e605c into main Jul 3, 2024
12 checks passed
@dotNomad dotNomad deleted the dotnomad/entrypoint-tracker branch July 3, 2024 21:28
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.

None yet

2 participants