Sea_Inevitable_5522 · 2022-06-24 23:18
I frequently work with JSON files that are made up of two kinds of data:
* mostly irrelevant data I never want to see or change
* some important properties, some of which are, written in a programming language.
I would like to be able to edit these files in such a way that:
* the irrelevant data is hidden
* the code is presented in a nicely formatted way with syntax highlighting
What is the best approach for this? For example:
* Is there an existing library or mode that does something like this?
* Should this feature be implemented as a minor mode? If so, which mode should it extend?
* Should this feature be implemented as a major mode?
For example, the JSON file:
[
{
... hundreds of lines of mostly irrelevant properties ...
"name": "foo",
"code": "def function():\n. print(\"hello, world from foo!\") ... many more lines...",
... hundreds of lines of mostly irrelevant properties ...
},
{
... hundreds of lines of mostly irrelevant properties ...
"name": "bar",
"code": "def function():\n. print(\"hello, world from bar!\") ... many more lines...",
... hundreds of lines of mostly irrelevant properties ...
}
]
What I'd like to see in my buffer:
// foo
def function():
print("hello, world from foo!")
... many more lines ...
// bar
def function():
print("hello, world from bar!")
... many more lines ...
Saving the buffer would update the underlying JSON file.
Thanks!
ieure · 2022-06-25 01:33
That file format looks like an absolute nightmare.
If it's just you working on these files, my suggestion would be to use Org Babel to pull the different pieces out into logical chunks, then tangle them to create the output file.
If you have to collaborate with others, I don't know if you have any good options. Multiple major modes (MMM) possibly?
The problem you have is that the Python (looks like it, anyway) source representation isn't the same as the JSON representation. Quotes are escaped, for example. So you can't edit the JSON directly, but have to edit the true code, then change it when it goes into the JSON.
Nice_Elk_55 · 2022-06-25 04:51
I would make a function or keyboard macro to copy a given line to a separate buffer and unescape it so you can view it and edit normally. Then have another function/macro to escape it and copy it back. It'll probably be a lot less work than writing a major mode to edit JSON and Python at once.
Also can you change the format? Ideally you'd save the code in regular files (even source controlled) and have a script generate the JSON. Putting code in JSON is pretty weird in any case. Only reason I can imagine is if you're running eval() on code passed in at runtime, which is also a security and debugging nightmare.
7890yuiop · 2022-06-25 04:59
In principle it's all doable -- you would narrow to the JSON string of interest, decode the JSON to populate a new buffer with the decoded programming code; set the major mode for that language; edit it; re-encode that buffer as a JSON string when you're done editing; and finally replace the original (narrowed) text.
In practice I expect you'll have to write that yourself, because I can't imagine very many people are writing code this way (or at least I hope they're not).
ekd123 · 2022-06-25 07:18
Alternatively, write a program to convert this json file to and from an editable source file.
Writing an Emacs mode is doable but this syntax is too specific, so it won’t be very reusable in the future. Therefore, its probably not worth the effort.