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

Using code actions for formatting prevents format steps from being grouped correctly in history #24588

Open
probably-neb opened this issue Feb 10, 2025 · 0 comments
Assignees

Comments

@probably-neb
Copy link
Contributor

Summary

When multiple formatters are specified for the same file, the normal and desired behavior is to group all format operations as a single transaction so that a single undo operation will undo the effects of all formatters. However, LSP code actions are executed differently and prevent the transaction from being grouped properly leading to multiple history entries

Steps to trigger the problem:

  1. Add the following to language settings and install ruff + python extensions
        "Python": {
            "formatter": [
                {
                    "language_server": {
                        "name": "ruff"
                    }
                },
                {
                    "code_actions": {
                        "source.organizeImports.ruff": true,
                        "source.fixAll.ruff": true
                    }
                }        
            ],
            "format_on_save": "on",
            "language_servers": [
                "pyright",
                "ruff"
            ]
        }
  1. Create a python project
mkdir python-project && cd python-project
uv init

replace the contents of hello.py with the following

import numpy as np

import os

print (1, 2,)

def main():
    print(os.path.dirname(__file__))
    print(np.arange(10))


if __name__ == "__main__":
    main()
  1. Save the file, then hit undo

Actual Behavior:

Only the import sorting is undone, resulting in the following

import numpy as np

import os

print(
    1,
    2,
)

Expected Behavior:

Both the import sorting and the formatting should be undone (the following is after hitting undo twice instead of once)

import numpy as np

import os

print(1, 2, )

Relevant Code

crates/project/src/lsp_store.rs > execute_formatters

crates/project/src/lsp_store.rs > execute_code_actions_on_servers

crates/project/src/lsp_store.rs > deserialize_workspace_edits

Plan

  • break up execute_code_actions_on_servers and deserialize_workspace_edits
    • one that just generates the list of edits
    • another that executes the edits
  • call the edit generation fn from execute_formatters during gather step and call execution fn during exec step, passing param to prevent finalize_ last_transaction from being called
  • refactor execute_code_actions_on_servers and deserialize_workspace_edits to call into the new functions as appropriate based on their usage elsewhere

Zed Version and System Specs

Zed: v0.174.0 (Zed Nightly 6f7f0f3)
OS: Linux X11 endeavouros unknown
Memory: 31.3 GiB
Architecture: x86_64
GPU: NVIDIA GeForce RTX 3070 || NVIDIA || 565.77

@probably-neb probably-neb self-assigned this Feb 10, 2025
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

No branches or pull requests

1 participant