zyedidia / micro

A modern and intuitive terminal-based text editor
https://micro-editor.github.io
MIT License
24.66k stars 1.16k forks source link

Micro can get confused by a shebang #1592

Open akowiz opened 4 years ago

akowiz commented 4 years ago

On a fresh install of micro 2.0.2 (with only the default plugin installed), open a python script (Using a proper file extension such as .py, .py2 or *.py3) and with the following shebang:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

Half of the time, the buffer is properly initialized (filetype set properly, options available through Buf.Settings[name]).

And half of the time, the buffer is not properly initialized. The most obvious way to see it is that the filetype is set to '' (empty) in the status bar. If you have a plugin that query the buffer setting:

panel.Buf.Settings[option]

it will return the global setting instead of the filetype option. it seems none of the options for the filetype in settings.json are loaded. So it makes it challenging to write plugin that is language specific (for example a plugin that should only run when a specific language is in the buffer.

Removing the shebang in the script, makes micro behave properly 100% of the time (I am guessing when there are no shebang, micro relies on the the file extension).

I experienced the problem on:

Commit hash: c51f849 OS: Ubuntu 19.10 Terminal: Gnome Terminal

Commit hash: c51f849 OS: Manjaro with Gnome 3.34.4 Terminal: Gnome Terminal

Commit hash: c51f849 OS: Android/Termux App Terminal: Termux

akowiz commented 4 years ago

Hi,

I notice you closed the issue, but I think it is more complex than just the yaml file.

As I mentioned, it is intermittent: by that I mean opening the same file (this is important, I am not changing the file or its name) again and again, half the time the file type is recognized and half of the time it is not. If it was only a yaml issue, it would be 100% of the time it works or 100% it doesn't for the same file, but this is not the case here.

It is clearly related to the code that analyze the shebang (because when the shebang is not present, the filetype is properly detected thanks to the file extension).

To confirm, I downloaded the latest nightly build of micro:

Version: 2.0.3-dev.11 Commit hash: 18d5405 Compiled on March 24, 2020

And added the updated yaml files into .config/micro/syntax.

The issue still happens.

Here is my dummy python test file that I use to test my plugin. It is just random classes and functions, it is not mean to be run ;-)

python_file_shebang.txt

akowiz commented 4 years ago

Hi,

I downloaded the latest version of micro:

Version: 2.0.3-dev.21 Commit hash: 1cf9537 Compiled on March 27, 2020

And the issue has been solved it seems (I can't reproduce the problem anymore).

I am a bit baffled because I did test the previous version (version 2.0.3-dev.11). Your fix was not integrated yet, but since I copied the yaml files, I expected I would get the same result as your fix (the diff only showed the yaml files being modified).

In any case, I think you can close this issue.

Thanks a lot!

zyedidia commented 4 years ago

When the yaml files are copied you have to re-make the runtime with make runtime, so maybe you forgot to do that?

akowiz commented 4 years ago

Well, I didn't rebuild micro, I simply downloaded the nightly build and added the yaml files into the micro/syntax folder.