sublime-text-i18n-rails icon indicating copy to clipboard operation
sublime-text-i18n-rails copied to clipboard

Yaml formatting

Open nicosantangelo opened this issue 11 years ago • 13 comments

Right now, when a key is added or edited, the yaml file gets sorted in alphabetical order and quotes are deleted. For example:

From this:

en:
  test:
    new: "Bye"
    index:
      few: "Hello"

to this:

en:
  test:
    index:
      few: Hello
    new: Bye

nicosantangelo avatar Dec 26 '13 13:12 nicosantangelo

Any news on this? Would be really nice to keep order and format of yaml file.

atipugin avatar Sep 14 '14 18:09 atipugin

Sadly, no, I use the package and can't add values on old projects for the same reason :(.

I have asked colleagues for help but I didn't have much luck yet.

nicosantangelo avatar Sep 14 '14 19:09 nicosantangelo

Is it possible to add setting for quotes and pass it to yaml.dump (according this comment on SO)?

atipugin avatar Sep 14 '14 20:09 atipugin

Doh, it won't help as PyYAML will wrap keys with quotes too :(

atipugin avatar Sep 14 '14 20:09 atipugin

The problem with adding that is that the keys also get wrapped in double quotes, but adding it as a setting could be nice.

Edit: Exactly your comment :P

nicosantangelo avatar Sep 14 '14 20:09 nicosantangelo

That's a problem :( Maybe we can add a custom dumper class? I don't have enough skills in Python :(

atipugin avatar Sep 15 '14 06:09 atipugin

I think it's the best (or only) solution. I gave it a try and didn't got very far, but this package was my first (complex) Python code, I should revisit it.

I'll let you know how it goes, but if you someone who wants to contribute, awesome!

nicosantangelo avatar Sep 15 '14 12:09 nicosantangelo

Any thought about just shelling out to ruby to reformat the document?

coreymartella avatar Oct 02 '15 14:10 coreymartella

@coreymartella I've actually never thought of that. The problem with using Ruby is that people doesn't need to have a Ruby version installed on their systems to run Sublime. Also running a ruby file from the package (Python) is a bit cumbersome.

That said, I could give it a try, did you have a specific idea on how to implement it?

nicosantangelo avatar Oct 04 '15 23:10 nicosantangelo

Actually with the addition of #14 I'm no longer worried about this.

coreymartella avatar Oct 05 '15 14:10 coreymartella

@NicoSantangelo I know this issue has been quiet for a while, but I believe it is related to an issue I just encountered. Since many words appear at many places, I like to create a dictionary at the top of the yaml file and use anchors and references to use them throughout the file. After spending a while building out the dictionary and replacing entries with references, I used the Add Key function to add a new entry, only to find out that all of the anchors had been removed, and the references replaced with the anchored values.

Is this the default behavior of PyYaml? Do you know of anyway of not having the achors/references interpolated?

hoenth avatar Mar 16 '18 11:03 hoenth

I think it's the default behavior of the lib yes. Searching around the web I found:

The anchor information in PyYAML is discarded according to the YAML 1.1 specification that PyYAML follows anchor names are a serialization detail and are discarded once composing is completed as far as I know this didn't change on YAML 1.2 :/

that said, PyYaml I might be wrong.

If you want to change the way PyYaml is used by the project you can take a look at the Yaml.py class, and more specifically to the dump here.

nicosantangelo avatar Apr 14 '18 13:04 nicosantangelo