Last active
October 22, 2015 12:11
-
-
Save dtinth/513df09879a8c1a3b075 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
atom.commands.add '.fuzzy-finder', 'custom:fuzzy-finder-import', (e) -> | |
# utilities | |
{ relative, basename, dirname } = require 'path' | |
getSelectedFilePathFromFuzzyFinderAndCloseIt = -> | |
# weird hack to acquire space-pen instance — suggestions welcome | |
{ $ } = window.require 'space-pen' | |
# obtain the FuzzyFinderView instance | |
view = $(e.target).closest('.fuzzy-finder').view() | |
# obtain the selected file path, and close the fuzzy finder | |
{ filePath } = view.getSelectedItem() | |
view.cancel() | |
filePath | |
generateImportCode = (sourcePath, destinationPath) -> | |
# handle special case for index file | |
if basename(destinationPath).match(/^index\./) | |
destinationPath = dirname(destinationPath) | |
# figure out the relative path to that file | |
relativePath = relative(dirname(sourcePath), destinationPath) | |
relativePath = relativePath.replace(/^([^\.])/, './$1') | |
relativePath = relativePath.replace(/\.jsx?$/, '') | |
# figure out the variable name | |
identifierName = basename(destinationPath).replace(/\..+/, '') | |
# return the generated code | |
"import #{identifierName} from '#{relativePath}'\n" | |
do -> | |
destinationPath = getSelectedFilePathFromFuzzyFinderAndCloseIt() | |
editor = atom.workspace.getActiveTextEditor() | |
sourcePath = editor.getPath() | |
code = generateImportCode(sourcePath, destinationPath) | |
editor.insertText code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment