adrianlee44 / atom-aligner

Easily align multi-line with support for different operators and custom configurations
https://atom.io/packages/aligner
MIT License
78 stars 3 forks source link

aligner for salt states #71

Closed dgmorrisjr closed 7 years ago

dgmorrisjr commented 7 years ago

Hi @adrianlee44 - I've been using your atom-aligner for python and it works great. But I find myself wanting to also use this same functionality in the Salt States that i develop. (SaltStack files ending in .sls extension). These are essentially files that contain python, jinja, and yaml content.

I was looking through the source for the atom-aligner-python. I'm wondering if it's just a matter of copying this project and then changing the selector list in the provider.js file?

I've never developed anything for atom... so not even sure how to determine what the selector options for a salt (sls) file would be. I started reading the Atom development documentation... haven't found it quite yet... but wanted to see what your thoughts on this are.

dgmorrisjr commented 7 years ago

I found this in the language-salt package settings source.salt

adrianlee44 commented 7 years ago

Hi @dgmorrisjr, You can copy the same format in the python and port it over for Salt States. The selector would change to .source.salt.

What are you trying to align? In order for aligner to work, you would need to figure out the scope of the character you are trying to align. You can do cmd-alt-p or search for "Log cursor scope" in the command palette.

dgmorrisjr commented 7 years ago

Thanks for the help,

Here is an example of content of a salt state... this file could be <filename>.sls:

{% set service_name = 'sftp' %}
{% set domain = pillar['var']['domain'] %}
{% set encryption_key = pillar['myapp']['encryption_key'] %}

# should be a grain
{% set interface_api_key = salt['grains.get']('interface_api_keys')['sftp'] %}

include:
  - software.interface.configure

install_iface_sftp_app_config_file:
  file.managed:
    - name: /opt/myapp/sftp-daemon.properties
    - source: salt://myapp/conf/sftp-daemon-.properties.jinja
    - user: root
    - group: myapp
    - mode: 660
    - makedirs: True
    - template: jinja
    - defaults:
        domain: {{ domain }}
        service_name: {{ service_name }}
        encryption_key: {{ encryption_key }}
        api_key: {{ interface_api_key }}

When the cursor is over the Jinja Set Statements, the scope that's revealed when pressing alt+cmd+p is source.salt, meta.scope.jinja.tag, and variable.other.jinja.

When the cursor is over any of the Yaml Key Names, the scope that's revealed is: source.salt, and entity.name.tag.yaml. Then when the cursor is over the Yaml Values, the scope that's revealed is: source.salt, string.unquoted.yaml.

If my cursor is in the Jinja set statements or i've highlighted a block of jinja statements, and then press the ctrl+cmd+/ to invoke the aligner, the jinja is properly aligned into this:

{% set service_name   = 'sftp' %}
{% set domain         = pillar['var']['domain'] %}
{% set encryption_key = pillar['myapp']['encryption_key'] %}

So that works as i would expect it to.

However, when I have the cursor positioned in the yaml statements, and press ctrl+cmd+/ to invoke aligner, nothing happens.

I currently have the selector statement as:

  selector: [
    '.source.python',
    '.source.embedded.python',
    '.source.salt',
  ],

But i've tried individually adding the following lines to the selector statement: '.yaml', 'entity.name.tag.yaml', 'string.unquoted.yaml',

between each time, i would shutdown atom and restart it to ensure it was reloading the modules correctly.

What i was hoping to get with the Yaml block, is the following output:

install_iface_sftp_app_config_file:
  file.managed:
    - name:             /opt/myapp/sftp-daemon.properties
    - source:           salt://myapp/conf/sftp-daemon-.properties.jinja
    - user:             root
    - group:            myapp
    - mode:             660
    - makedirs:         True
    - template:         jinja
    - defaults:
        domain:         {{ domain }}
        service_name:   {{ service_name }}
        encryption_key: {{ encryption_key }}
        api_key:        {{ interface_api_key }}
dgmorrisjr commented 7 years ago

one of the other things that might be helpful... I also have the language-salt package installed. within the settings for that package, they have this setting:

image

I'm wondering if the Unscoped Value: which includes a : has an impact?

adrianlee44 commented 7 years ago

Looking at the information you have given me and installing language-salt. : is definitely tokenized but not the = in the set statement.

The following code should work:

module.exports = {
  selector: [
    '.source.salt'
  ],
  id: 'aligner-python',
  config: {
    ':-alignment': {
      title: 'Padding for :',
      description: 'Pad left or right of the character',
      type: 'string',
      "enum": ['left', 'right'],
      "default": 'right'
    },
    ':-leftSpace': {
      title: 'Left space for :',
      description: 'Add 1 whitespace to the left',
      type: 'boolean',
      "default": false
    },
    ':-rightSpace': {
      title: 'Right space for :',
      description: 'Add 1 whitespace to the right',
      type: 'boolean',
      "default": true
    }
  },
  privateConfig: {
    ':-scope': 'key-value',
  }
};

In order to get = to work, the grammar file would need to be updated to tokenize that character.

dgmorrisjr commented 7 years ago

I believe it was the privateConfig from your example above that made it work. After updating and restarting Atom, it works. It's working for both the = assignment in the jinja and the : key-value pairs in the yaml. But I have both .source.salt and .source.python in the selector list.

Would you like setup a project for aligner-salt? i'd be happy to provide what I have to serve as the starting point for that.

adrianlee44 commented 7 years ago

@dgmorrisjr, I have published a https://atom.io/packages/aligner-salt for Salt support. The package only targets .source.salt. Please use aligner-python when targeting .source.python.

Going to close this now. Let me know if you are having issue with aligner-salt

dgmorrisjr commented 7 years ago

Thanks So much!