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

clang-format on C code remove existent code #8410

Open
1 task done
Warkanlock opened this issue Feb 25, 2024 · 9 comments
Open
1 task done

clang-format on C code remove existent code #8410

Warkanlock opened this issue Feb 25, 2024 · 9 comments
Labels
c C programming language support defect [core label] language An umbrella label for all programming languages syntax behaviors vim

Comments

@Warkanlock
Copy link

Warkanlock commented Feb 25, 2024

Check for existing issues

  • Completed

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

format_on_save_on.mov

With format_on_save: off

format_on_save_off.mov
@Warkanlock Warkanlock added admin read Pending admin review defect [core label] triage Maintainer needs to classify the issue labels Feb 25, 2024
@JosephTLyons JosephTLyons added language An umbrella label for all programming languages syntax behaviors c C programming language support and removed triage Maintainer needs to classify the issue admin read Pending admin review labels Feb 27, 2024
@notpeter
Copy link
Contributor

notpeter commented Jul 9, 2024

@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
Copy link
Author

@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
Copy link

0xVitea commented Jul 15, 2024

@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.

Screen.Recording.2024-07-15.at.12.00.32.mov

@notpeter notpeter reopened this Jul 15, 2024
@Warkanlock
Copy link
Author

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
Copy link

0xVitea commented Jul 16, 2024

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 !

@notpeter notpeter added the vim label Jul 16, 2024
@konstantintutsch
Copy link

konstantintutsch commented Jul 17, 2024

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:

  • -linux: same styling as code in the Linux kernel
  • -nut and -i4: indentation with 4 spaces
  • -o /dev/stdout: output to stdout

Environment

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

@konstantintutsch
Copy link

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
Copy link

konstantintutsch commented Jul 18, 2024

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

@siboehm
Copy link

siboehm commented Jul 31, 2024

This works for clang-format:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c C programming language support defect [core label] language An umbrella label for all programming languages syntax behaviors vim
Projects
None yet
Development

No branches or pull requests

6 participants