Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Provide a simpler way to act on files by extension #2593

Closed

Description

@thedaniel

A very common question in IRC is "How can I make files with some extension set a certain behavior / use a certain grammar". As far as I know the state of the art is to put something like this in user.coffee:

atom.workspaceView.eachEditorView (editorView) ->
  editor = editorView.getEditor()
  if path.extname(editor.getPath()) is '.md'
    editor.setSoftWrap(true)

Perhaps we could provide some syntactic sugar nicer than iterating over every EditorView, like say an object of path matchers and functions that get the current editor added to their context and run when initializing a new editor?

atom.fileActions = #terribly named
  '\.md$': -> currentEditor.setSoftWrap(true)

Along the same lines it might be worth encouraging configurable file types in packages like e.g. atom/language-c

Activity

  1. lee-dohm commented on Jun 13, 2014

    @lee-dohm
    Contributor

    This is what I have in my init.coffee for this kind of thing:

    # Settings to assign based on grammar name.
    grammarSettings =
      'GitHub Markdown':
        showInvisibles: false
        softWrap: true
        tabLength: 4
      Java:
        tabLength: 4
      Python:
        tabLength: 4
    
    # Apply the settings to the editor session.
    #
    # editorView - EditorView to which to apply the settings.
    # settings   - The settings to apply.
    applySettingsForGrammar = (editorView, settings) ->
      editor = editorView.getEditor()
      for key, value of settings
        switch key
          when 'showInvisibles' then editorView.setShowInvisibles(value)
          when 'softWrap' then editor.setSoftWrap(value)
          when 'tabLength' then editor.setTabLength(value)
    
    # Apply grammar-specific settings.
    #
    # editorView - EditorView to which to apply the grammar-specific settings.
    applySettings = (editorView) ->
      settings = grammarSettings[editorView.getEditor().getGrammar().name]
      return unless settings?
    
      applySettingsForGrammar(editorView, settings)
    
    # Executes for each and every EditorView, past and future.
    atom.workspaceView.eachEditorView (editorView) ->
      applySettings(editorView)
    
      editorView.getEditor().on 'grammar-changed', ->
        applySettings(editorView)

    Some things to note:

    1. I key off of the grammar name because that way if the grammar is updated with new extensions, I get that update for free. It also makes the settings structure prettier.
    2. I also set things on grammar-changed because sometimes I open a new file and it isn't recognized as X grammar and I have to set it after open
    3. It should execute pretty fast because if there aren't any settings for that grammar, then it's a no-op, rather than matching the path to an ever increasing list of regular expressions

    The one thing that I want to do is offer a way in the user's configuration of saying that the glob *.foo matches the Foo grammar. That way if I have a new type of Ruby file that uses an rbw extension, I can add it without having to patch the grammar.

  2. benogle commented on Dec 5, 2014

    @benogle
    Contributor

    This was fixed in #3718

  3. lock commented on Jan 24, 2019

    @lock

    This issue has been automatically locked since there has not been any recent activity after it was closed. If you can still reproduce this issue in Safe Mode then please open a new issue and fill out the entire issue template to ensure that we have enough information to address your issue. Thanks!

  4. locked as resolved and limited conversation to collaborators on Jan 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions