Skip to content
This repository was archived by the owner on Apr 6, 2018. It is now read-only.

Commit 6327adf

Browse files
gf implemented
1 parent 225cc74 commit 6327adf

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

keymaps/vim-mode.cson

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@
131131
'n': 'vim-mode:repeat-search'
132132
'N': 'vim-mode:repeat-search-backwards'
133133

134+
'g f': 'vim-mode:open-file-under-cursor'
135+
134136
'%': 'vim-mode:bracket-matching-motion'
135137

136138
'1': 'vim-mode:repeat-prefix'

lib/motions/index.coffee

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Motions = require './general-motions'
22
{Search, SearchCurrentWord, BracketMatchingMotion, RepeatSearch} = require './search-motion'
33
MoveToMark = require './move-to-mark-motion'
44
{Find, Till} = require './find-motion'
5+
OpenFileUnderCursor = require './open-file-under-cursor'
56

67
Motions.Search = Search
78
Motions.SearchCurrentWord = SearchCurrentWord
@@ -10,5 +11,6 @@ Motions.RepeatSearch = RepeatSearch
1011
Motions.MoveToMark = MoveToMark
1112
Motions.Find = Find
1213
Motions.Till = Till
14+
Motions.OpenFileUnderCursor = OpenFileUnderCursor
1315

1416
module.exports = Motions
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{Motion} = require './general-motions'
2+
path = require 'path'
3+
{Directory} = require 'atom'
4+
shell = require 'shell'
5+
{exec} = require 'child_process'
6+
7+
class OpenFileUnderCursor extends Motion
8+
execute: (count) ->
9+
###
10+
/usr/bin/test
11+
../../.travis.yml
12+
./search-motion
13+
./index
14+
http://github.com
15+
https://thing.com/item%20test
16+
//github.com
17+
###
18+
wordRegex = /[a-z0-9\.\-_\/\\%:]+/i
19+
for cursor in @editor.getCursors()
20+
range = cursor.getCurrentWordBufferRange wordRegex: wordRegex
21+
selectedPath = @editor.getTextInRange range
22+
23+
# exit early for url match
24+
if /^https?:/i.test(selectedPath)
25+
return @openUrl selectedPath
26+
if /^\/\//i.test(selectedPath)
27+
return @openUrl "http:#{selectedPath}"
28+
29+
if selectedPath[0] isnt '/' and selectedPath[0] isnt '\\'
30+
selectedPath = path.join path.dirname(@editor.buffer.file.path), selectedPath
31+
32+
pathDir = path.dirname selectedPath
33+
dir = new Directory pathDir
34+
ext = path.extname @editor.buffer.file.path
35+
dir.getEntries (err, entries) =>
36+
# find any paths that contain the selected path
37+
matches = entries.filter (entry) -> entry.path.indexOf(selectedPath) > -1
38+
return @loadFile matches[0].path if matches.length is 1
39+
40+
# if there are other possible matches, try to match on extension
41+
extMatches = matches.filter (entry) -> path.extname(entry.path) is ext
42+
return @loadFile extMatches[0].path if extMatches.length is 1
43+
44+
loadFile: (path) -> atom.workspace.open path
45+
46+
openUrl: (url) ->
47+
# pulled from https://github.com/magbicaleman/open-in-browser/blob/master/lib/open-in-browser.coffee
48+
process_architecture = process.platform
49+
switch process_architecture
50+
when 'darwin' then exec "open '#{url}'"
51+
when 'linux' then exec "xdg-open '#{url}'"
52+
when 'win32' then shell.openExternal url
53+
54+
module.exports = OpenFileUnderCursor

lib/vim-state.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ class VimState
173173
'search-current-word': (e) => new Motions.SearchCurrentWord(@editor, this)
174174
'bracket-matching-motion': (e) => new Motions.BracketMatchingMotion(@editor, this)
175175
'reverse-search-current-word': (e) => (new Motions.SearchCurrentWord(@editor, this)).reversed()
176+
'open-file-under-cursor': (e) => new Motions.OpenFileUnderCursor(@editor, this)
176177

177178
# Private: Register multiple command handlers via an {Object} that maps
178179
# command names to command handler functions.

0 commit comments

Comments
 (0)