[Solved] Make file extension name invisible in Tree-View


#1

Greetings.

I understand that this might be quite an unusual question, but I would like to hide the file extentions from the tree-view. I have logos for each file type and I would like the extensions to not appear at the end of each file name.

Is this possible? If so, which are the steps I should do in order to make the extensions invisible?

Thank you for your time.


#2

With JavaScript, anything is possible. The tree-view package does not expose any functionality to modify its contents or watch it for changes, so we’ll have to do some DOM hacking. Drop this into your init.coffee file and reload Atom.

# Function to hide the extensions of files in the tree view.
hideTreeViewExtensions = () =>
  treeViewItems = document.querySelectorAll('[is=tree-view-file] span.name')

  for item in treeViewItems
    if item.getAttribute('data-name').lastIndexOf('.') != 0
      item.innerHTML = item.getAttribute('data-name').slice(0, item.getAttribute('data-name').lastIndexOf('.'))

# Run the preceding function on startup.
hideTreeViewExtensions()

# Register a command to run the preceding function manually if necessary.
atom.commands.add 'atom-workspace', 'custom:hide-tree-view-extensions', ->
  hideTreeViewExtensions()

# Bind an event handler to watch the DOM for changes
treeViewChanges = new MutationObserver ->
  hideTreeViewExtensions()
treeViewChanges.observe(document.querySelector('.tree-view'), {childList: true, subtree: true})

This little bit of code forcibly removes all text following and including the final . from files that don’t have their final . as the first character of their title (like .gitignore). However, since the tree view constantly generates new child elements whenever you expand a folder or add a new one, we have to watch it for changes. The command is just there as a fallback in case the other two methods don’t catch it and you want to hide extensions manually (there should never be a time when the observer doesn’t work, but if you find one, let me know). It doesn’t need a keybinding because you can just invoke it from the Command Palette if you ever need to.

It also doesn’t work 100% on specific things that you might see like .tar.gz, rar.0, download.zip.part, or the occasional backup strategy of renaming files like notes.txt.old (which is not good anyway because it breaks on Windows). I don’t consider any of these cases to be prevalent enough to worry about.


#3

Thank you. That has worked.