skip to Main Content

I am currently working on a project in Visual Studio Code that has poorly formatted code. Due to project constraints, I am not allowed to reformat the existing code and commit those changes. This makes the code difficult to read and maintain. I’m looking for a workaround that allows me to view and work with the code in a more readable format in my local environment without affecting the original formatting in the repository.

Specifically, my questions are:

  1. Is there a way in VSCode to temporarily apply code formatting that only affects my local view, without changing the actual file content that gets committed?
  2. Are there any extensions or settings in VSCode that can help with this situation?
  3. If VSCode cannot do this, is there any other tool or IDE that offers such a feature?

2

Answers


  1. You are looking for formatters. I personally recommend you autopep8 or Black Formatter.

    Once the extensions are installed, you only have to righ-click the on any part of the .py file and click on Format Document.

    enter image description here

    The document will be formatted. Once you finish to read it, you can undo the formatting with ctrl+z.

    Login or Signup to reply.
  2. There are format all files extensions in VS Code. What those do is essentially open all files, hit the formatting shortcut and save. You can adjust for what file endings/paths this should be applied.

    With that, I would do the following:

    1. Create a new branch on top of the current one.
    2. Configure your formatters as you like
    3. Apply the formatting to all files
    4. Commit those changes to a separate branch for your viewing.

    Any actual changes you do still need to happen on the original branch and you could reformat it when required.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search