jeapostrophe / racket-langserver

Other
262 stars 24 forks source link

support formatting options #112

Closed dannypsnl closed 1 year ago

dannypsnl commented 1 year ago

Link: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#formattingOptions

interface FormattingOptions {
    /**
     * Size of a tab in spaces.
     */
    tabSize: uinteger

    /**
     * Prefer spaces over tabs.
     */
    insertSpaces: boolean;

    /**
     * Trim trailing whitespace on a line.
     *
     * @since 3.15.0
     */
    trimTrailingWhitespace?: boolean;

    /**
     * Insert a newline character at the end of the file if one does not exist.
     *
     * @since 3.15.0
     */
    insertFinalNewline?: boolean;

    /**
     * Trim all newlines after the final newline at the end of the file.
     *
     * @since 3.15.0
     */
    trimFinalNewlines?: boolean;

    /**
     * Signature for further properties.
     */
    [key: string]: boolean | integer | string;
}
6cdh commented 1 year ago

I don't know if someone uses tab for indentation. I always use "convert tab to spaces" personally. We can respect these arguments:

jryans commented 1 year ago

I also don't use tabs and always convert to spaces... The Racket style guide also says don't use tabs. (Perhaps someone still uses tabs anyway, but I'm guessing that's quite rare in the Racket ecosystem.)

6cdh commented 1 year ago

I now understand the purpose of tabSize and insertSpaces. The formatting function always uses space indentation. If someone use tab indentation in editor, it will look weird.

Based on the Racket style guide, maybe we can replace all tabs with spaces before formatting?

dannypsnl commented 1 year ago

Yes, that would be great due to the style

6cdh commented 1 year ago

So we ignore the options tabSize and insertSpaces, and always use replace tabs with spaces instead?

dannypsnl commented 1 year ago

Yes, at least we can make a that first, if anyone wants different behavior, then make the implementation be the default setup & provides others

6cdh commented 1 year ago

No, It's not easy to do.

The formatting function works for a range of lines.

For example, it need to format from line start to line end (exclusive).

Then if line start - 1 uses tab indentation, line start will always get wrong indent from compute-racket-amount-to-indent which calls racket-amount-to-indent as it doesn't know the tab size.

We cannot make racket-amount-to-indent to know the really tab size, as it seems hardcoded the tab size to 8.

dannypsnl commented 1 year ago

If we replace \t with 8 spaces of line first?

6cdh commented 1 year ago

Good suggestion! It feels dirty but it works.