scztt / LanguageServer.quark

16 stars 10 forks source link

[c++] Add support for code auto-formatting via LSP #10

Open scztt opened 1 year ago

scztt commented 1 year ago

See https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_formatting

jamshark70 commented 10 months ago

A year and a half later -- nice to see some work on this.

The current state appears to be that CodeFormatter has an unmet dependency: start calls Pipe.argvReadWrite, but this method has not been provided by any of the quarks that LanguageServer pulled in.

This error is invisible because it's wrapped in a try block without an error handler. If I change the startup code as follows:

    start {
        inPipe !? _.close;
        outPipe !? _.close;

        try {

            #inPipe, outPipe = Pipe.argvReadWrite([
                formatCmd,
                "-i",
                tabSize.asString,
                insertSpaces.if("", "-t"),
                "-w",
            ]);

            1000.do {
                if (inPipe.isOpen) { ^true };
                0.01.wait;
            };
        } { |err|
            if(err.notNil) {
                err.reportError;
                "^^ Error initializing CodeFormatter".postln;
            };
        };

        ^false;
    }

Then I do see:

... call stack...

^^ ERROR: Message 'argvReadWrite' not understood.
RECEIVER: Pipe

^^ Error initializing CodeFormatter

So for troubleshooting purposes, it probably should fail un-silently.

Also in order to get this far, I had to change TextDocumentFormattingProvider *initClass to wrap the initialization in a StartUp block. If the class is initialized immediately, then the user has no opportunity to configure formatterEnabled in startup.scd. The user should not be required to edit class definitions by hand for configuration.

I can go ahead and put in a PR for the StartUp thing, although it won't be useful to anyone else until the Pipe method is made publicly available.