Skip to content

feat: Format the code suggestion by running the VSCode command

Olena Horal-Koretska requested to merge POC-format-suggestion into main

Description

This MR is a POC for the Approach #3 described in gitlab-org/editor-extensions/gitlab-lsp#63 (comment 1654714253)

I've repurposed it for the implementation MR.

To format the accepted suggestion in the VSCode, when the suggestion is accepted, we "select" it programatically and format by running the VSCode's format command on the Range of the suggestion text

This works together with a small change on the LS side.

Note 1: We should keep an eye on the user feedback and see whether it causes any issues.

Note 2: The ideal solution with this approach would be to intercept the response from the LS and format it before the ghost text is displayed. Kind of creating a virtual document and formatting it. Unfortunately to do that with vscode api we actually need to open the document in the editor which we do not want to do

Pros

  • Low effort level for the future support
  • Works for all the languages (as long as formatting plugin is available)
  • Provides familiar experience for the user

Cons

  • This solution is VSCode only
  • The formatting result will largely depend on the user setting for the specific language formatter. E.g. on the gif below, we used VSCode's default js/ts formatter. When I set the formatter to some "Prettier" plugin, the formatting does not work for the case below as code is incomplete and Prettier can't work with "invalid/incomplete" code as my other POC mentions.

Screenshots:

Note: In my observation the suggestion is rarely returned badly formatted. So to prove the point I've hardcoded the unformatted suggestion on the LS side.

Notice how the ghost text is unformatted and after suggestion is accepted, it is prettified. The case in the example if pretty drastic case of "bad formatting" and hopefully we do not have such cases)

2023-12-01_10.43.04

Edited by Olena Horal-Koretska

Merge request reports