micro-editor / micro-editor/micro

[bug] reformatting a Python source code that is in dos fileformat gets damaged after save

Open
#3,045 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
29.6k
Forks
1.4k
Avg merge
2d 18h
Merged PRs (30d)
10

Description

I have a Python source code that has dos fileformat. I have a plugin that automatically reformats the code using the black formatter. However, after saving the file, the source code gets damaged. I think the problem is caused by the dos fileformat.

The problematic file is here: https://www.dropbox.com/scl/fi/rsk4u0gj2k33y4vabexn8/dos.py?rlkey=kb4ppmuiufxi865kzmkyvuyre&dl=0

Here is the reformatter plugin:

VERSION = "0.1.4"

local config = import("micro/config")

function init()
    config.MakeCommand("fmt", onSave, config.NoComplete)
end

function onSave(bp)
    local cmd = ""

    local ft = bp.Buf:FileType()
    if ft == "python" then
        cmd = string.format("black --line-length 100 '%s'", bp.Buf.Path)
        reformat(bp, cmd)
    end
end

function reformat(bp, cmd)
    bp:Save()
    local handle = io.popen(cmd)
    local result = handle:read("*a")
    handle:close()
    bp.Buf:ReOpen()
end

If you open this particular file in micro and save it, some lines get damaged. Example:

This line:

    z: str = 'z' if word[0].upper() in MAGANHANGZOK else ''

becomes this:

    z: str = 'z' if wo"d"0].upper() in MAGANHANGZOK else ''

If I run black in the command-line on the file (outside of micro), the file is not damaged.

OS: Linux
micro Version: 2.0.13, Commit hash: 68d88b57

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The payload names no repository files or tests. Reproduce with micro 2.0.13, the linked dos.py file, and the provided save/reformat plugin, then trace the save and ReOpen behavior for DOS-format files. Done means saving through this workflow no longer damages source text, while the command-line Black result remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, lua, python
Domain
cli, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.