Hey all,

I created a ticket some months ago, where I wanted to have the ability of 
changing a MIMEType of a current opened file temporarily and not make it happen 
for the whole file extension: 
https://issues.apache.org/jira/browse/NETBEANS-5087. As you can follow the 
discussion, this is not possible but a beloved feature in VS Code and Sublime 
for example.

Also we have this plugin: 
https://github.com/junichi11/netbeans-noext-mime-resolver which is pretty 
awesome and works for files w/o having a file extension.
How does it work? It parses the first line, which is called the shebang line 
and adds the MIMEType to the file and reloads it.

This is common behaviour in linux systems. But this is not working for files 
with an extension which is not needed until this case.

So I had a look into the code and I know there are file attributes like 
mimeType and we can get and set it but Setting will have no effect for files 
with a file extenson. We can get the MIMEType from a FileObject:   
https://github.com/apache/netbeans/blob/master/platform/openide.filesystems/src/org/openide/filesystems/FileObject.java#L640
 but there is just a getMimeType method, no set method.

There is a setMimeType function but in class FileUtils: 
https://github.com/apache/netbeans/blob/master/platform/openide.filesystems/src/org/openide/filesystems/FileUtil.java#L1352
 and as you can see, it will map the MIMEType that I want, to a specific 
extension which will overrite all files with that extension which is not what I 
want. This is used for the options to map a MIMEType to a file Extension but 
permanently.

So the problem is, when I change the MIMEType of a current opened file like 
foo.txt which is text/plain to text/x-json as you can see in the ticket for the 
use case, I can do that on the JEditorKit but I need to reload the file and 
after that it is the same mimeType as before, due to the file extension. For 
inMemory files in a inMemory file system, it is possible to create a file with 
a different mimetype and change it as often as I want until I save the file to 
the operating filesystem with a specific extension. The case with changing the 
mimetype just to get other syntax highlighting is not only one case. Sometimes 
you copy code, open your IDE, open a new file which shouldn’t be on the 
filesystem for now, paste the code in it, choose the correct mimetype and 
format the code if it is not formatted. This could be done with inmemory 
filesystem but as I said, after I save the file, it is not possible anymore to 
change it.

I created a plugin called NbShowMimeType which had another name called 
NbMimeTypeSwitcher: https://ibb.co/YRn6bZQ. It will print the mimetype of the 
file in the status bar next to the encoding DropDown. As you can see, it is 
just a label and I wanted to create a selectbox with all supported mimetypes of 
NetBeans (already done), choose the mimetype and change it for the file 
temporarily. But this is not possible. It is really just temporarily. When I 
close the file and reopen it, it is the same file as before due to the file 
extension. So I dig into the code and see no chance to do that at the moment, 
right?

What needs to be done to do this? Setting the mimeType file attribute and when 
it is set, not using the file extension? Resetting the mimeType file attribute 
after closing? How should I start. I thought it is simple to Change the 
behaviour of setMimeType to have the same method with just one Parameter.

I really hope for help. Thank you all.


Cheers

Chris

Reply via email to