zed-industries / zed

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
https://zed.dev
Other
45.7k stars 2.51k forks source link

clang-format on C code remove existent code #8410

Open Warkanlock opened 6 months ago

Warkanlock commented 6 months ago

Check for existing issues

Describe the bug / provide steps to reproduce it

After setting a language configuration for C code in languages my code, after being saved, is replaced with the latest N-1 step previously done on the editor.

For example:

  1. I added a summary comment before a struct definition.
  2. ctrl + s
  3. I added a struct, together with a summary comment.
  4. ctrl + s
  5. my code disappears, and it keeps showing the previous step (0)

This is the configuration used (utilizing clang-format as default):

[{
    "C": {
      "formatter": {
        "external": {
          "command": "clang-format",
          "arguments": ["{buffer_path}"]
        }
      },
      "format_on_save": "on"
}] 

Turning format_on_save to off solves the issue, but I need to manually save > format workflow.

Environment

Zed: v0.123.6 (Zed) OS: macOS 14.3.1 Memory: 16 GiB Architecture: aarch64

If applicable, add mockups / screenshots to help explain present your vision of the feature

With format_on_save: on

https://github.com/zed-industries/zed/assets/145113/0d044fd9-33ab-4019-8d24-57d2c3e683de

With format_on_save: off

https://github.com/zed-industries/zed/assets/145113/0c5509e3-6f41-490f-b98d-6c81b30e8608

notpeter commented 1 month ago

@Warkanlock I can't reproduce this. I did observe some similar behavior around the time of this report where format_on_save was losing my file content, but haven't seen it in a few months.

Can you see if you are still experiencing thing scary behavior?

Warkanlock commented 1 month ago

@Warkanlock I can't reproduce this. I did observe some similar behavior around the time of this report where format_on_save was losing my file content, but haven't seen it in a few months.

Can you see if you are still experiencing thing scary behavior?

Might it be solved on the latest versions of Zed? I've not used Zed anymore for C programming, given this issue, so I cannot confirm nor deny that this keeps happening

0xVitea commented 1 month ago

@notpeter I think this problem still persists. This is my config:

{
  "telemetry": {
    "diagnostics": true,
    "metrics": false
  },
  "theme": "Kanagawa Wave",
  "vim_mode": true,
  "ui_font_size": 16,
  "font_family": "Fira Mono",
  "buffer_font_family": "Fira Mono",
  "buffer_font_size": 16,
  "autosave": "on_focus_change",
  "relative_line_numbers": true,
  "project_panel": {
    "dock": "right",
    "git_status": true
  },
  "terminal": {
    "font_family": "Fira Mono",
    "blinking": "on"
  },
  "preferred_line_length": 180,
  "languages": {
    "C++": {
      "tab_size": 4,
      "formatter": {
        "external": {
          "command": "clang-format",
          "arguments": ["{buffer_path}"]
        }
      },
      "format_on_save": "on"
    }
  }
}

MacBook M1 - macOS Sonoma 14.5 - Zed 0.143.7

Whenever I delete any line and press escape to :w, the code just reappears.

https://github.com/user-attachments/assets/637b7aba-39eb-40d1-ac5a-129bd2fb385e

Warkanlock commented 1 month ago

I went back to my configuration file after reading this thread since I opened a long time ago, and something that fixed the issue for me was to disable the format_on_save

    ...
    "C": {
      "show_copilot_suggestions": true,
      "formatter": {
        "external": {
          "command": "clang-format",
          "arguments": ["{buffer_path}"]
        }
      },
      "format_on_save": "off"
    }
    ...

Using this configuration chunk now essentially I can use Zed as part of the workflow; however, I need to run the format command every time I wanna use clang-format (which is not a big deal, honestly)

0xVitea commented 1 month ago

Thanks for quick response.

Disabling format_on_save worked for me. I've also added a quick keymap for this one.

I agree it's not a priority, rather a good feature to have. It would be great if you could add a message or a quick note about clang-format in documentation.

Thank you once again for what you're doing. Switched fully to zed and enjoy it on a daily basis !

konstantintutsch commented 1 month ago

This issue might not be related to only clang-format. It also occurs when using GNU Indent as the external formatter for C.

Configuration

Here is a snippet of my Zed configuration.

"languages": {
    "C": {
        "formatter": {
            "external": {
                "command": "indent",
                "arguments": [ "{buffer_path}", "-linux", "-nut", "-i4", "-o", "/dev/stdout" ]
            }
        }
    }
}

A short summary of the arguments:

Environment

Zed: 0.143.6 (via DNF, Terra) OS: Fedora 40 with Linux 6.9.8 Memory: 32GB Architecture: x86_64 (AMD)

konstantintutsch commented 1 month ago

I've read the Zed documentation again. Using {buffer_path} is not intended, instead stdin should be used.

Bildschirmfoto vom 2024-07-18 10-11-33

"languages": {
    "C": {
        "formatter": {
            "external": {
                "command": "indent",
                "arguments": [ "-", "-linux", "-nut", "-i4", "-o", "/dev/stdout" ]
            }
        }
    }
}

Replacing {buffer_path} with - fixes the issue with format_on_save.

konstantintutsch commented 1 month ago

~Using /dev/stdin instead of - doesn't work. (#14716)~ It does.

siboehm commented 3 weeks ago

This works for clang-format:

  "languages": {
    "C": {
      "format_on_save": "on",
      "formatter": {
        "external": {
          "command": "bash",
          "arguments": ["-c", "clang-format"]
        }
      }
    }
  },