janet-lang / vscode-janet

Janet language support for Visual Studio Code
MIT License
49 stars 14 forks source link

Enhancement: Make extension work independently on jpm / spork being installed #13

Open fuxoft opened 1 year ago

fuxoft commented 1 year ago

Janet is a small language that can be conveniently installed and used just by copying a single binary, with no other dependencies or libraries.

However, the formatting in the VS Code extension only works if spork is installed. To install spork, jpm has to be installed.

It's trivial to modify the extension so that it runs independently on jpm or spork, requiring just the janet binary. I think this would make it easier to use the extension for people who are e.g. just trying Janet out and do not (yet) even know what jpm or spork is.

sogaiu commented 1 year ago

I started thinking about how this might be done.

Two possibilities (may be there are others) about how the extension might access bits necessary for spork/fmt functionality to work are:

  1. Include a bundled spork/fmt.janet file
  2. Include the content of the file as a string

The latter might look rather messy if the formatting is accomplished via the current means of sending text to a janet repl.

Perhaps the approach of a bundled file would be preferrable.

It's been a while since I've done VSCode extension tinkering, but it looks like it is (or at least was) possible to get at a file which the extension bundles. I think the following is an example of a function that can get the absolute path to a relevant file:

    /**
     * Returns the absolute path to a file located in our misc folder.
     *
     * @param file The base file name.
     * @param context The context of this extension to get its path regardless where it is installed.
     * @param webview When given format the path for use in this webview.
     *
     * @returns The computed path.
     */
    public static getMiscPath(file: string, context: ExtensionContext, webview?: Webview): string {
        if (webview) {
            const uri = Uri.file(context.asAbsolutePath(path.join("misc", file)));

            return webview.asWebviewUri(uri).toString();
        }

        return context.asAbsolutePath(path.join("misc", file));
    }

via: https://github.com/mike-lischke/vscode-antlr4/blob/c25c25c1bd56a92ffafc24c26caee053db077941/src/frontend/FrontendUtils.ts#L22-L39

So with the absolute path in hand presumably we can modify this bit:

sendSource(terminal, "(import spork/fmt) (fmt/format-file \""+

to import our bundled file.

If that kind of approach works, may be it will also be easier (as compared with storing the functionality in a string) to update the included spork/fmt.janet file from time to time. One example of why one might want to update the content is that sometimes things are added to janet that one might want to highlight differently.

sogaiu commented 1 year ago

Ok, here is a draft.

Have only tried on a Linux box.

There may be better ways, but at least it seems doable.

The main changes code-wise are here. I tried to arrange for things in a way such that nothing got introduced in one's repl environment that would interfere with other things going on (not sure if I was successful).