Skip to content

Commit 025e0f3

Browse files
committed
Merge pull request #87 from trotzig/cli-resolve
Cli resolve
2 parents ffded13 + 9fedf84 commit 025e0f3

File tree

10 files changed

+62
-21
lines changed

10 files changed

+62
-21
lines changed

plugin/import-js-sublime/Default.sublime-commands renamed to Default.sublime-commands

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@
77
"caption": "ImportJS: import word under cursor",
88
"command": "import_js",
99
"args": {"word": true}
10+
},
11+
{
12+
"caption": "ImportJS: goto module",
13+
"command": "import_js",
14+
"args": {"word": true, "goto": true}
1015
}
1116
]

plugin/import-js-sublime/Main.sublime-menu renamed to Main.sublime-menu

File renamed without changes.

README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
# Introduction
22

33
`import-js` is a tool to automatically import dependencies in your JavaScript
4-
project. Use it in Vim or Emacs by placing your cursor on a variable and hit
5-
`<leader>j` (Vim), or `(M-x) import-js-import` (Emacs).
4+
project. Use it in Vim, Emacs, or Sublime by placing your cursor on a variable
5+
and hit `<leader>j` (Vim), or `(M-x) import-js-import` (Emacs), or select
6+
"ImportJS: import word under cursor" from the Command Palette (Sublime).
67

78
![Demo of import-js in action](https://raw.github.com/trotzig/import-js/master/import-js-demo.gif)
89

910
## Editor support
1011

1112
import-js comes with plugins for the following editors:
1213

13-
- [Emacs (experimental)](EMACS.md) (Thanks to @kevin.kehl!)
14+
- [Emacs (experimental)](EMACS.md) (Thanks to
15+
[@kevin.kehl](https://github.com/kevin.kehl)!)
1416
- [Vim](VIM.md)
17+
- [Sublime](Sublime.md) (Thanks to
18+
[@janpaul123](https://github.com/janpaul123))
1519
- [(your editor here?)](CONTRIBUTING.md)
1620

1721
Detailed instructions on how to install import-js can be found in the editor
@@ -47,8 +51,9 @@ document.createElement(new Button({ text: 'Save' }).toDOMElement());
4751
At this point, `Button` is undefined. We need to import it. If you are used to
4852
doing this manually, this involves figuring out the path to the JavaScript
4953
module that defines `Button`. With import-js, you instead place your cursor on
50-
the word "Button", then hit `<leader>j` (Vim) or `(M-x) import-js-import`
51-
(Emacs). The file buffer will now change to the following:
54+
the word "Button", then hit `<leader>j` (Vim), `(M-x) import-js-import`
55+
(Emacs), or choose "ImportJS: import word under cursor" (Sublime). The file
56+
buffer will now change to the following:
5257

5358
```js
5459
var Button = require('components/button');
@@ -72,8 +77,8 @@ Since import-js is pretty good at finding JS modules, it makes sense that
7277
there's an option to open/go to a file rather than import it. This is similar
7378
to VIM's built in ["Open file under
7479
cursor"](http://vim.wikia.com/wiki/Open_file_under_cursor). Use it by placing
75-
the cursor on a variable and hit `<leader>g` (Vim) or `(M-x) import-js-goto`
76-
(Emacs).
80+
the cursor on a variable and hit `<leader>g` (Vim), `(M-x) import-js-goto`
81+
(Emacs), or choose "ImportJS: goto module" (Sublime).
7782

7883
## Things to note
7984

@@ -82,7 +87,8 @@ the cursor on a variable and hit `<leader>g` (Vim) or `(M-x) import-js-goto`
8287
`var`/`const`/`let`/`import` (configurable through the `declaration_keyword`
8388
option)
8489
- As part of resolving an import, all imports will be sorted
85-
- The Vim plugin is written in Ruby. You need a [Vim with Ruby support](VIM.md).
90+
- The core of the plugin is written in Ruby. If you are using Vim, you need a
91+
[Vim with Ruby support](VIM.md).
8692

8793
## Configuration
8894

SUBLIME.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717

1818
```
1919
{ "keys": ["super+alt+i"], "command": "import_js" },
20-
{ "keys": ["super+alt+j"], "command": "import_js", "args": {"word": true} }
20+
{ "keys": ["super+alt+j"], "command": "import_js", "args": {"word": true} },
21+
{ "keys": ["super+alt+g"], "command": "import_js", "args": {"word": true, "goto": true} },
2122
```

bin/import-js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ require 'json'
66

77
opts = Slop.parse do |o|
88
o.string '-w', '--word', 'a word/variable to import'
9+
o.bool '--goto', 'instead of importing, just print the path to a module'
910
o.array '--selections', 'a list of resolved selections, e.g. Foo:0,Bar:1'
11+
o.string '--filename',
12+
'a path to the file which contents are being passed in as stdin'
1013
o.on '-v', '--version', 'print the current version' do
1114
puts ImportJS::VERSION
1215
exit
@@ -29,14 +32,21 @@ end
2932

3033
editor = ImportJS::CommandLineEditor.new(file_contents, opts)
3134
importer = ImportJS::Importer.new(editor)
32-
if opts[:word]
35+
if opts.goto?
36+
importer.goto
37+
elsif opts[:word]
3338
importer.import
3439
else
3540
importer.import_all
3641
end
3742

38-
# Print resulting file to stdout
39-
puts editor.current_file_content
43+
if opts.goto?
44+
# Print the path to the module to go to
45+
puts editor.goto
46+
else
47+
# Print resulting file to stdout
48+
puts editor.current_file_content
49+
end
4050

4151
# Print messages to stderr
4252
meta = {
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ def run(self, edit, **args):
1616
command.append('--word')
1717
command.append(word)
1818

19+
if(args.get('goto')):
20+
command.append('--goto')
21+
1922
if(args.get('selections')):
2023
command.append('--selections')
2124
command.append(','.join(args.get('selections')))
2225

26+
command.append('--filename')
27+
command.append(self.view.file_name())
28+
2329
print(command)
2430

2531
proc = subprocess.Popen(
@@ -48,12 +54,21 @@ def run(self, edit, **args):
4854
return
4955

5056
stdout = result[0].decode()
51-
self.view.replace(edit, entire_file_region, stdout)
57+
if(args.get('goto')):
58+
if(len(stdout.rstrip()) > 0):
59+
self.view.window().open_file(self.project_path() + '/' + stdout.rstrip())
60+
else:
61+
self.view.replace(edit, entire_file_region, stdout)
5262

5363
def rerun(self, edit, args, selections):
5464
args['selections'] = selections
5565
self.run(edit, **args)
5666

67+
def project_path(self):
68+
for folder in self.view.window().project_data().get('folders'):
69+
if(self.view.file_name().startswith(folder.get('path'))):
70+
return folder.get('path')
71+
5772
def ask_for_selections(self, selections, on_selections_done):
5873
selected = []
5974
selections_iter = iter(selections)

lib/import_js/command_line_editor.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def initialize(lines, opts)
66
@ask_for_selections = []
77
@selections = opts[:selections] unless opts[:selections].empty?
88
@word = opts[:word]
9+
@filename = opts[:filename]
910
end
1011

1112
# @return [String]
@@ -15,13 +16,17 @@ def current_word
1516

1617
# @return [String?]
1718
def path_to_current_file
18-
# Not yet implemented.
19-
nil
19+
@filename
2020
end
2121

2222
# @param file_path [String]
23-
def open_file(_file_path)
24-
fail 'not supported'
23+
def open_file(file_path)
24+
@goto = file_path
25+
end
26+
27+
# @return [String]
28+
def goto
29+
@goto
2530
end
2631

2732
# @param str [String]

lib/import_js/emacs_editor.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ def open_file(file_path)
4747
#
4848
# @return [String?]
4949
def path_to_current_file
50-
# Not yet implemented for Emacs
51-
nil
50+
@path
5251
end
5352

5453
# Display a message to the user.

lib/import_js/importer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def goto
4444
@timing[:end] = Time.now
4545
return if js_modules.empty?
4646
js_module = resolve_one_js_module(js_modules, variable_name)
47-
@editor.open_file(js_module.file_path)
47+
@editor.open_file(js_module.file_path) if js_module
4848
end
4949

5050
def fix_imports

lib/import_js/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Defines the gem version.
22
module ImportJS
3-
VERSION = '0.1.0'
3+
VERSION = '0.1.1'
44
end

0 commit comments

Comments
 (0)