├── .gitignore ├── setup.cfg ├── i_opener.sublime-commands ├── messages.json ├── messages └── 2.31.0.txt ├── tox.ini ├── i_opener.sublime-settings ├── RELEASE_NOTES.md ├── Default (Linux).sublime-keymap ├── Default (OSX).sublime-keymap ├── Default (Windows).sublime-keymap ├── README.md ├── paths.py ├── Main.sublime-menu ├── matching.py ├── i_opener.py └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [flake8] 2 | max-line-length = 120 3 | ignore = 4 | E221, 5 | W504 6 | -------------------------------------------------------------------------------- /i_opener.sublime-commands: -------------------------------------------------------------------------------- 1 | [ 2 | { "caption": "iOpener: Find File", "command": "i_opener" } 3 | ] 4 | -------------------------------------------------------------------------------- /messages.json: -------------------------------------------------------------------------------- 1 | { 2 | "install": "message/RELEASE_NOTES.md", 3 | "3.0.0": "messages/2.31.0.txt" 4 | } 5 | -------------------------------------------------------------------------------- /messages/2.31.0.txt: -------------------------------------------------------------------------------- 1 | iOpener is back! 2 | 3 | - Sublime text 4 Support 4 | - Fix KeyError when adding folder to empty project 5 | - Fix tab autocomplete to work more like bash 6 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | skipsdist = true 3 | envlist = py27,py39 4 | [testenv] 5 | deps = nose 6 | commands = nosetests --ignore-files i_opener.py --all-modules --traverse-namespace --with-doctest 7 | -------------------------------------------------------------------------------- /i_opener.sublime-settings: -------------------------------------------------------------------------------- 1 | { 2 | // Whether or not to consider the project's directory when choosing a path at which to start 3 | "use_project_dir": false, 4 | 5 | // Whether to open folders in a new window or add them to the project in the current window 6 | "open_folders_in_new_window": true, 7 | 8 | // Whether or not to ignore case when completing. 9 | "case_sensitive": false, 10 | 11 | // Number of entries to store in history. 12 | "history_entries": 30 13 | } 14 | -------------------------------------------------------------------------------- /RELEASE_NOTES.md: -------------------------------------------------------------------------------- 1 | iOpener - Release Notes 2 | ======================= 3 | 4 | Note 5 | ---- 6 | - This plugin takes control of your default fileopen shortcut. To access the 7 | old dialog, use `shift+super+o` or `shift+ctrl+o`, depending on your OS. 8 | 9 | - The open panel will not show unless a window is open. You will need to use 10 | `cntl+n` or `super+n` to open a new window first. 11 | 12 | - You can access history by pressing `up` or `down`. 13 | 14 | - You can auto-complete using `tab`. 15 | 16 | Bugs 17 | ---- 18 | - Bugs can be reported on GitHub, https://github.com/rosshemsley/iOpener. 19 | 20 | -------------------------------------------------------------------------------- /Default (Linux).sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | // Steal open command and use it for iOpener. 3 | { "keys": ["ctrl+o"], "command": "i_opener" }, 4 | 5 | // Allow over-riding of iOpener, and show normal system dialog. 6 | { "keys": ["ctrl+shift+o"], "command": "prompt_open_file" }, 7 | 8 | 9 | //////////////////////////////////////////////////////////////////////////// 10 | // Do not edit // 11 | //////////////////////////////////////////////////////////////////////////// 12 | { 13 | "keys": ["tab"], 14 | "command": "i_opener_complete", 15 | "context": [{"key": "i_opener"}] 16 | }, 17 | { 18 | "keys": ["up"], 19 | "command": "i_opener_cycle_history", 20 | "context": [{"key": "i_opener"}], 21 | "args": {"direction": "up"} 22 | }, 23 | { 24 | "keys": ["down"], 25 | "command": "i_opener_cycle_history", 26 | "context": [{"key": "i_opener"}], 27 | "args": {"direction": "down"} 28 | } 29 | //////////////////////////////////////////////////////////////////////////// 30 | ] 31 | -------------------------------------------------------------------------------- /Default (OSX).sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | // Steal open command and use it for iOpener. 3 | { "keys": ["super+o"], "command": "i_opener" }, 4 | 5 | // Allow over-riding of iOpener, and show normal system dialog. 6 | { "keys": ["super+shift+o"], "command": "prompt_open_file" }, 7 | 8 | 9 | //////////////////////////////////////////////////////////////////////////// 10 | // Do not edit // 11 | //////////////////////////////////////////////////////////////////////////// 12 | { 13 | "keys": ["tab"], 14 | "command": "i_opener_complete", 15 | "context": [{"key": "i_opener"}] 16 | }, 17 | { 18 | "keys": ["up"], 19 | "command": "i_opener_cycle_history", 20 | "context": [{"key": "i_opener"}], 21 | "args": {"direction": "up"} 22 | }, 23 | { 24 | "keys": ["down"], 25 | "command": "i_opener_cycle_history", 26 | "context": [{"key": "i_opener"}], 27 | "args": {"direction": "down"} 28 | } 29 | //////////////////////////////////////////////////////////////////////////// 30 | ] 31 | -------------------------------------------------------------------------------- /Default (Windows).sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | // Steal open command and use it for iOpener. 3 | { "keys": ["ctrl+o"], "command": "i_opener" }, 4 | 5 | // Allow over-riding of iOpener, and show normal system dialog. 6 | { "keys": ["ctrl+shift+o"], "command": "prompt_open_file" }, 7 | 8 | 9 | //////////////////////////////////////////////////////////////////////////// 10 | // Do not edit // 11 | //////////////////////////////////////////////////////////////////////////// 12 | { 13 | "keys": ["tab"], 14 | "command": "i_opener_complete", 15 | "context": [{"key": "i_opener"}] 16 | }, 17 | { 18 | "keys": ["up"], 19 | "command": "i_opener_cycle_history", 20 | "context": [{"key": "i_opener"}], 21 | "args": {"direction": "up"} 22 | }, 23 | { 24 | "keys": ["down"], 25 | "command": "i_opener_cycle_history", 26 | "context": [{"key": "i_opener"}], 27 | "args": {"direction": "down"} 28 | } 29 | //////////////////////////////////////////////////////////////////////////// 30 | ] 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | iOpener 2 | ======= 3 | 4 | A Sublime Text 2/3 package to make finding and opening files less painful. 5 | Keep your hands on the keyboard with smart auto completion, history, and directory listings. 6 | 7 | Usage 8 | ----- 9 | Use `cmd + o` or `cntl + o` as usual (or choose `Find File` in the command pallete.) 10 | 11 | - Pressing tab will cause smart completion to occur (hint: `f1` can complete to `filename1`!) 12 | 13 | - Double-pressing tab will give you the directory listing 14 | 15 | - Up/down allow you to navigate previously entered paths 16 | 17 | - Opening a path that doesn't exist will create a new file or directory 18 | 19 | ![demo](https://raw.github.com/rosshemsley/iOpener/screenshots/demo.gif) 20 | 21 | NB 22 | -- 23 | - This plugin will take over your default 'open file' shortcut. To 24 | use your system default, use `cmd + shift + o` or `ctrl + shift + o` 25 | 26 | - The open panel will not show unless a window is open. You will need to use 27 | `cntl + n` or `cmd + n` to open a new window first 28 | 29 | 30 | Installing 31 | ---------- 32 | Installation is easiest using Package Control. In the Command Pallette, select 33 | "Install Package" and then select "iOpener". 34 | 35 | 36 | Bugs 37 | ---- 38 | Bugs can be reported on GitHub, https://github.com/rosshemsley/iOpener. 39 | 40 | 41 | Development 42 | ----------- 43 | 44 | Run unit test with 45 | 46 | ```bash 47 | pip install tox 48 | tox 49 | ```` 50 | -------------------------------------------------------------------------------- /paths.py: -------------------------------------------------------------------------------- 1 | from os.path import isdir, expanduser, split, relpath, join, commonprefix 2 | from os import listdir, sep 3 | 4 | 5 | HOME_DIRECTORY = '~' 6 | 7 | 8 | def directory_listing_with_slahes(path): 9 | """ 10 | Return directory listing with directories having trailing slashes. 11 | """ 12 | output = [] 13 | 14 | for filename in listdir(path): 15 | if isdir(join(path, filename)): 16 | output.append(filename + sep) 17 | else: 18 | output.append(filename) 19 | 20 | return output 21 | 22 | 23 | def get_current_directory(view_filename, folders, use_project_dir): 24 | """ 25 | Try to give a sensible estimate for 'current directory'. 26 | If there is a single folder open, we return that. 27 | Else, if there is an active file, return its path. 28 | If all else fails, return the home directory. 29 | """ 30 | if folders and use_project_dir: 31 | directory = folders[0] 32 | elif view_filename is not None: 33 | directory, _ = split(view_filename) 34 | else: 35 | directory = HOME_DIRECTORY 36 | 37 | if directory != sep: 38 | directory += sep 39 | 40 | return get_path_relative_to_home(directory) 41 | 42 | 43 | def get_path_relative_to_home(path): 44 | home = expanduser(HOME_DIRECTORY) 45 | 46 | if len(commonprefix([home, path])) > 1: 47 | relative_path = relpath(path, home) 48 | if len(relative_path) > 1: 49 | return join(HOME_DIRECTORY, relpath(path, home)) + sep 50 | else: 51 | return HOME_DIRECTORY + sep 52 | else: 53 | return path 54 | -------------------------------------------------------------------------------- /Main.sublime-menu: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": "file", 4 | "children": 5 | [ 6 | { 7 | "caption": "Open Path...", 8 | "command": "i_opener", 9 | "mnemonic": "O" 10 | } 11 | ] 12 | }, 13 | { 14 | "id": "preferences", 15 | "children": 16 | [ 17 | { 18 | "caption": "Package Settings", 19 | "id": "package-settings", 20 | "mnemonic": "P", 21 | "children": 22 | [ 23 | { 24 | "caption": "iOpener", 25 | "children": 26 | [ 27 | { 28 | "caption": "Settings – Default", 29 | "command": "open_file", "args": 30 | { 31 | "file": "${packages}/iOpener/i_opener.sublime-settings" 32 | } 33 | }, 34 | { 35 | "caption": "Settings – User", 36 | "command": "open_file", "args": 37 | { 38 | "file": "${packages}/User/i_opener.sublime-settings" 39 | } 40 | }, 41 | { "caption": "-" }, 42 | // Default Key mappings 43 | { 44 | "command": "open_file", 45 | "id": "bindings", 46 | "args": { 47 | "file": "${packages}/iOpener/Default (Windows).sublime-keymap", 48 | "platform": "Windows" 49 | }, 50 | "caption": "Key Bindings – Default" 51 | }, 52 | { 53 | "command": "open_file", 54 | "args": { 55 | "file": "${packages}/iOpener/Default (OSX).sublime-keymap", 56 | "platform": "OSX" 57 | }, 58 | "caption": "Key Bindings – Default" 59 | }, 60 | { 61 | "command": "open_file", 62 | "args": { 63 | "file": "${packages}/iOpener/Default (Linux).sublime-keymap", 64 | "platform": "Linux" 65 | }, 66 | "caption": "Key Bindings – Default" 67 | }, 68 | 69 | // User Key mappings 70 | { 71 | "command": "open_file", 72 | "args": { 73 | "file": "${packages}/User/Default (Windows).sublime-keymap", 74 | "platform": "Windows" 75 | }, 76 | "caption": "Key Bindings – User" 77 | }, 78 | { 79 | "command": "open_file", 80 | "args": { 81 | "file": "${packages}/User/Default (OSX).sublime-keymap", 82 | "platform": "OSX" 83 | }, 84 | "caption": "Key Bindings – User" 85 | }, 86 | { 87 | "command": "open_file", 88 | "args": { 89 | "file": "${packages}/User/Default (Linux).sublime-keymap", 90 | "platform": "Linux" 91 | }, 92 | "caption": "Key Bindings – User" 93 | } 94 | ] 95 | } 96 | ] 97 | } 98 | ] 99 | } 100 | ] 101 | -------------------------------------------------------------------------------- /matching.py: -------------------------------------------------------------------------------- 1 | from unittest import TestCase 2 | from os.path import commonprefix 3 | 4 | 5 | class COMPLETION_TYPE: 6 | CompleteButNotUnique = 0 7 | Complete = 1 8 | NoMatch = 2 9 | 10 | 11 | def complete_path(filename, directory_listing, case_sensitive=False): 12 | lcs_completion = get_lcs_completion_or_none(filename, directory_listing) 13 | 14 | if lcs_completion is not None: 15 | return lcs_completion, COMPLETION_TYPE.Complete 16 | 17 | matches = get_matches(filename, directory_listing, case_sensitive) 18 | 19 | if len(matches) > 1: 20 | new_filename = longest_completion(filename, matches) 21 | return new_filename, COMPLETION_TYPE.CompleteButNotUnique 22 | 23 | elif len(matches) == 1: 24 | return matches[0], COMPLETION_TYPE.Complete 25 | else: 26 | return filename, COMPLETION_TYPE.NoMatch 27 | 28 | 29 | def get_matches(filename, directory_listing, case_sensitive): 30 | if case_sensitive: 31 | return [f for f in directory_listing if f.startswith(filename)] 32 | else: 33 | return [f for f in directory_listing if f.lower().startswith(filename.lower())] 34 | 35 | 36 | def longest_completion(filename, matches): 37 | return filename + commonprefix(matches)[len(filename):] 38 | 39 | 40 | def get_lcs_completion_or_none(filename, directory_listing): 41 | """ 42 | If there is a unique way to complete the path such that the LCS is the same 43 | as the query string, return that (similar to the way Fish shell works) 44 | """ 45 | completion = None 46 | 47 | for candidate in directory_listing: 48 | if lcs(filename, candidate) == filename: 49 | if completion is not None: 50 | return None 51 | else: 52 | completion = candidate 53 | 54 | return completion 55 | 56 | 57 | def lcs(A, B): 58 | """ 59 | Taken and adapted from 60 | http://rosettacode.org/wiki/Longest_common_subsequence#Dynamic_Programming_7 61 | """ 62 | lengths = [[0 for j in range(len(B) + 1)] for i in range(len(A) + 1)] 63 | 64 | for i, x in enumerate(A): 65 | for j, y in enumerate(B): 66 | if x == y: 67 | lengths[i + 1][j + 1] = lengths[i][j] + 1 68 | else: 69 | lengths[i + 1][j + 1] = max(lengths[i + 1][j], lengths[i][j + 1]) 70 | 71 | result = "" 72 | x, y = len(A), len(B) 73 | while x != 0 and y != 0: 74 | if lengths[x][y] == lengths[x - 1][y]: 75 | x -= 1 76 | elif lengths[x][y] == lengths[x][y - 1]: 77 | y -= 1 78 | else: 79 | result = A[x - 1] + result 80 | x -= 1 81 | y -= 1 82 | return result 83 | 84 | 85 | ## 86 | # Unit tests 87 | ## 88 | 89 | 90 | class TestLCSCompletion(TestCase): 91 | def test1(self): 92 | directory_listing = [ 93 | 'filename1', 94 | 'filename2', 95 | 'test', 96 | ] 97 | 98 | output1 = get_lcs_completion_or_none('file1', directory_listing) 99 | self.assertEqual('filename1', output1) 100 | 101 | output2 = get_lcs_completion_or_none('file2', directory_listing) 102 | self.assertEqual('filename2', output2) 103 | 104 | output3 = get_lcs_completion_or_none('tst', directory_listing) 105 | self.assertEqual('test', output3) 106 | 107 | output4 = get_lcs_completion_or_none('tes', directory_listing) 108 | self.assertEqual('test', output4) 109 | 110 | output5 = get_lcs_completion_or_none('django', directory_listing) 111 | self.assertIsNone(output5) 112 | 113 | 114 | class TestLCS(TestCase): 115 | def test1(self): 116 | self.assertEqual('hel', lcs('hello', 'heal')) 117 | self.assertEqual('oe', lcs('hope', 'oe')) 118 | self.assertEqual('', lcs('this', 'xyz')) 119 | self.assertEqual('bcd', lcs('abcdefg', 'bcd')) 120 | self.assertEqual('test', lcs('test', 'test')) 121 | self.assertEqual('rd', lcs('read', 'rd')) 122 | self.assertEqual('round', lcs('round', 'arounded')) 123 | 124 | 125 | class TestCompletion(TestCase): 126 | def test1(self): 127 | filename = 'test' 128 | matches = [ 129 | 'testable', 130 | 'testa', 131 | 'testand', 132 | 'testand' 133 | ] 134 | output = longest_completion(filename, matches) 135 | expected = 'testa' 136 | 137 | self.assertEqual(expected, output) 138 | 139 | def test2(self): 140 | filename = 'test' 141 | matches = [ 142 | 'testable', 143 | 'testa', 144 | 'testand', 145 | 'Testand' 146 | ] 147 | output = longest_completion(filename, matches) 148 | expected = 'test' 149 | 150 | self.assertEqual(expected, output) 151 | 152 | def test3(self): 153 | filename = 'test' 154 | matches = [ 155 | 'tester', 156 | 'testable', 157 | 'testa', 158 | 'testand', 159 | 'Testand' 160 | ] 161 | output = longest_completion(filename, matches) 162 | expected = 'test' 163 | 164 | self.assertEqual(expected, output) 165 | 166 | 167 | class TestMatches(TestCase): 168 | def test1(self): 169 | directory_listing = [ 170 | 'filename', 171 | 'filen', 172 | 'fi', 173 | 'Filename', 174 | ] 175 | 176 | output = get_matches('file', directory_listing, case_sensitive=True) 177 | expected = [ 178 | 'filename', 179 | 'filen', 180 | ] 181 | 182 | self.assertListEqual(expected, output) 183 | 184 | def test2(self): 185 | directory_listing = [ 186 | 'filename', 187 | 'filen', 188 | 'fi', 189 | 'Filename', 190 | ] 191 | 192 | output = get_matches('file', directory_listing, case_sensitive=False) 193 | expected = [ 194 | 'filename', 195 | 'filen', 196 | 'Filename', 197 | ] 198 | 199 | self.assertListEqual(expected, output) 200 | 201 | def test3(self): 202 | directory_listing = ['test'] 203 | output = get_matches('', directory_listing, case_sensitive=True) 204 | self.assertListEqual(['test'], output) 205 | -------------------------------------------------------------------------------- /i_opener.py: -------------------------------------------------------------------------------- 1 | """ 2 | A package to make opening files in Sublime Text a little bit less painful. 3 | Licensed under GPL V2. 4 | 5 | Written by Ross Hemsley and other collaborators 2013. 6 | """ 7 | 8 | import sublime 9 | import sublime_plugin 10 | from os.path import isdir, isfile, expanduser, split, join 11 | from os import listdir, sep, makedirs 12 | 13 | from .matching import complete_path, COMPLETION_TYPE, get_matches 14 | from .paths import get_current_directory, directory_listing_with_slahes 15 | 16 | # Locations of settings files. 17 | HISTORY_FILE = 'i_opener_history.sublime-settings' 18 | SETTINGS_FILE = 'i_opener.sublime-settings' 19 | 20 | STATUS_MESSAGES = { 21 | COMPLETION_TYPE.CompleteButNotUnique: 'Complete, but not unique', 22 | COMPLETION_TYPE.NoMatch: 'No match', 23 | COMPLETION_TYPE.Complete: None, 24 | } 25 | 26 | 27 | def load_settings(): 28 | # We set these globals. 29 | global USE_PROJECT_DIR 30 | global OPEN_FOLDERS_IN_NEW_WINDOW 31 | global HISTORY_ENTRIES 32 | global CASE_SENSITIVE 33 | 34 | settings = sublime.load_settings(SETTINGS_FILE) 35 | 36 | USE_PROJECT_DIR = settings.get('use_project_dir') 37 | OPEN_FOLDERS_IN_NEW_WINDOW = settings.get('open_folders_in_new_window') 38 | CASE_SENSITIVE = settings.get('case_sensitive') 39 | HISTORY_ENTRIES = settings.get('history_entries') 40 | 41 | 42 | def is_sublime_text_2(): 43 | return 2000 <= int(sublime.version()) <= 2999 44 | 45 | 46 | def is_sublime_text_3(): 47 | return 3000 <= int(sublime.version()) <= 3999 48 | 49 | 50 | def is_sublime_text_4(): 51 | return 4000 <= int(sublime.version()) <= 4999 52 | 53 | 54 | def get_completion(path): 55 | """ 56 | Function to find and return longest possible completion for a path p from a 57 | list of candidates l. Returns new_path, status, completed. 58 | Find filename and directory. 59 | """ 60 | directory, filename = split(path) 61 | 62 | if not isdir(expanduser(directory)): 63 | return path, COMPLETION_TYPE.NoMatch 64 | 65 | directory_listing = listdir(expanduser(directory)) 66 | new_filename, completion_type = complete_path(filename, directory_listing, CASE_SENSITIVE) 67 | 68 | if (new_filename != '' and 69 | isdir(expanduser(join(directory, new_filename))) and 70 | completion_type != COMPLETION_TYPE.CompleteButNotUnique): 71 | new_filename += sep 72 | 73 | return join(directory, new_filename), completion_type 74 | 75 | 76 | class iOpenerPathInput(): 77 | """ 78 | This class encapsulates the behaviors relating to the file open panel 79 | used by the package. We create an instance when the panel is open, and destroy 80 | it when the panel is closed. 81 | """ 82 | def __init__(self): 83 | # If the user presses tab, and nothing happens, remember it. 84 | # If they press tab again, we show them a list of files. 85 | self.last_completion_failed = False 86 | self.path_cache = None 87 | active_window = sublime.active_window() 88 | 89 | view_filename = active_window.active_view().file_name() 90 | folders = active_window.folders() 91 | 92 | path = get_current_directory(view_filename, folders, USE_PROJECT_DIR) 93 | 94 | # We only reload the history each time the input window is opened. 95 | self.history_cache = self.get_history()[0] 96 | 97 | # Store default at end of history cache and 'select' it. 98 | self.history_cache.append(path) 99 | self.history_index = len(self.history_cache) - 1 100 | 101 | self.view = active_window.show_input_panel( 102 | "Find file: ", 103 | path, 104 | self.open_file, 105 | self.update, 106 | self.cancel 107 | ) 108 | 109 | def update(self, text): 110 | """ 111 | If the user updates the input, reset the 'failed completion' flag. 112 | """ 113 | self.last_completion_failed = False 114 | 115 | def goto_prev_history(self): 116 | """ 117 | Temporarily store any changes in cache, as bash does. 118 | """ 119 | self.history_cache[self.history_index] = self.get_text() 120 | self.history_index -= 1 121 | if self.history_index < 0: 122 | sublime.status_message("Reached start of history") 123 | self.history_index = 0 124 | self.set_text(self.history_cache[self.history_index]) 125 | 126 | def goto_next_history(self): 127 | # Temporarily store any changes in cache, as bash does. 128 | self.history_cache[self.history_index] = self.get_text() 129 | self.history_index += 1 130 | if self.history_index == len(self.history_cache): 131 | sublime.status_message("Reached end of history") 132 | self.history_index = len(self.history_cache) - 1 133 | self.set_text(self.history_cache[self.history_index]) 134 | 135 | def add_to_history(self, path): 136 | file_history, history_settings = self.get_history() 137 | 138 | # Trim the history to the correct length and add latest entry. 139 | if HISTORY_ENTRIES > 1: 140 | file_history = file_history[-HISTORY_ENTRIES + 1:] 141 | file_history.append(path) 142 | elif HISTORY_ENTRIES == 1: 143 | file_history = [path] 144 | elif HISTORY_ENTRIES == 0: 145 | file_history = [] 146 | 147 | # Save updated history. 148 | history_settings.set("file_history", file_history) 149 | sublime.save_settings(HISTORY_FILE) 150 | 151 | def get_history(self): 152 | history_settings = sublime.load_settings(HISTORY_FILE) 153 | file_history = history_settings.get("file_history") 154 | # Trim history. 155 | if not file_history: 156 | file_history = [] 157 | else: 158 | file_history = file_history[-HISTORY_ENTRIES:] 159 | return file_history, history_settings 160 | 161 | def cancel(self): 162 | """ 163 | Method called when we exit from the input panel without opening a file. 164 | Cancel implies removing panel object. 165 | """ 166 | iOpenerCommand.input_panel = None 167 | 168 | def get_text(self): 169 | """ 170 | Get current text being displayed by input panel. 171 | """ 172 | return self.view.substr(sublime.Region(0, self.view.size())) 173 | 174 | def open_file(self, path): 175 | """ 176 | Open the given path. Can be a directory OR a file. 177 | """ 178 | path = expanduser(path) 179 | 180 | # Ignore empty paths. 181 | if not path: 182 | sublime.status_message("Warning: Ignoring empty path.") 183 | return 184 | 185 | self.add_to_history(path) 186 | 187 | directory = "" 188 | filename = "" 189 | 190 | # If the user enters a path without a filename. 191 | if path[-1] == sep: 192 | directory = path 193 | else: 194 | directory, filename = split(path) 195 | 196 | # Path doesn't exist, ask the user if they want to create it. 197 | if not isdir(directory): 198 | create = sublime.ok_cancel_dialog( 199 | "The path you entered does not exist, create it?", 200 | "Yes" 201 | ) 202 | if not create: 203 | return 204 | else: 205 | try: 206 | makedirs(directory) 207 | except OSError as e: 208 | sublime.error_message("Failed to create path with error: " + str(e)) 209 | return 210 | 211 | if isdir(path): 212 | if is_sublime_text_2(): 213 | # Project folders can not be added using the ST2 API. 214 | sublime.status_message("Warning: Opening folders requires ST v3.") 215 | elif OPEN_FOLDERS_IN_NEW_WINDOW: 216 | sublime.run_command("new_window") 217 | project_data = dict(folders=[dict(follow_symlinks=True, path=path)]) 218 | sublime.active_window().set_project_data(project_data) 219 | else: 220 | project_data = sublime.active_window().project_data() or {} 221 | project_folders = project_data.get('folders') or [] 222 | 223 | folder = dict(path=path, follow_symlinks=True, folder_exclude_patterns=['.*']) 224 | if all(folder['path'] != path for folder in project_folders): 225 | project_data.setdefault('folders', []).append(folder) 226 | sublime.active_window().set_project_data(project_data) 227 | else: 228 | # If file doesn't exist, add a message in the status bar. 229 | if not isfile(path): 230 | sublime.status_message("Created new buffer '{}'".format(filename)) 231 | sublime.active_window().open_file(path) 232 | iOpenerCommand.input_panel = None 233 | 234 | def set_text(self, s): 235 | """ 236 | Set the text in the file open input panel. 237 | """ 238 | self.view.run_command("i_opener_update", {"append": False, "text": s}) 239 | 240 | def show_completions(self): 241 | """ 242 | Show a quick panel containing the possible completions. 243 | """ 244 | active_window = sublime.active_window() 245 | directory, filename = split(self.get_text()) 246 | 247 | directory_listing = directory_listing_with_slahes(expanduser(directory)) 248 | self.path_cache = get_matches(filename, directory_listing, CASE_SENSITIVE) 249 | 250 | if len(self.path_cache) == 0: 251 | show_completion_message(COMPLETION_TYPE.NoMatch) 252 | else: 253 | active_window.show_quick_panel(self.path_cache, self.on_done) 254 | 255 | def on_done(self, i): 256 | if self.path_cache is None: 257 | return 258 | 259 | elif i != -1: 260 | directory, _ = split(self.get_text()) 261 | new_path = join(directory, self.path_cache[i]) 262 | self.path_cache = None 263 | 264 | if isdir(expanduser(new_path)): 265 | self.set_text(new_path) 266 | sublime.active_window().focus_view(self.view) 267 | else: 268 | self.open_file(new_path) 269 | sublime.active_window().run_command("hide_panel", {"cancel": True}) 270 | else: 271 | sublime.active_window().focus_view(self.view) 272 | 273 | def append_text(self, s): 274 | self.view.run_command("i_opener_update", {"append": True, "text": s}) 275 | 276 | 277 | def show_completion_message(completion_type): 278 | status = STATUS_MESSAGES.get(completion_type) 279 | 280 | if status is not None: 281 | sublime.status_message(status) 282 | 283 | 284 | ## 285 | # Commands and listeners. 286 | ## 287 | 288 | 289 | class iOpenerEventListener(sublime_plugin.EventListener): 290 | """ 291 | Event listener to allow querying of context. All our events (for now) only 292 | need to know if the plugin is active. If so, Sublime Text calls the correct 293 | commands. 294 | """ 295 | def on_query_context(self, view, key, operator, operand, match_all): 296 | return ( 297 | key == 'i_opener' and 298 | iOpenerCommand.input_panel is not None and 299 | iOpenerCommand.input_panel.view.id() == view.id() 300 | ) 301 | 302 | 303 | class iOpenerUpdateCommand(sublime_plugin.TextCommand): 304 | """ 305 | The edit command used for editing the text in the input panel. 306 | """ 307 | def run(self, edit, append, text): 308 | if append: 309 | self.view.insert(edit, self.view.size(), text) 310 | else: 311 | self.view.replace(edit, sublime.Region(0, self.view.size()), text) 312 | 313 | 314 | class iOpenerCompleteCommand(sublime_plugin.WindowCommand): 315 | """ 316 | The command called by tapping tab in the open panel. 317 | """ 318 | def run(self): 319 | input_panel = iOpenerCommand.input_panel 320 | 321 | if input_panel.last_completion_failed: 322 | input_panel.last_completion_failed = False 323 | input_panel.show_completions() 324 | else: 325 | completion, completion_type = get_completion(input_panel.get_text()) 326 | show_completion_message(completion_type) 327 | input_panel.set_text(completion) 328 | 329 | completion_failed = completion_type != COMPLETION_TYPE.Complete 330 | input_panel.last_completion_failed = completion_failed 331 | 332 | 333 | class iOpenerCycleHistoryCommand(sublime_plugin.WindowCommand): 334 | """ 335 | Receive requests to cycle history. 336 | """ 337 | def run(self, direction): 338 | if direction == "up": 339 | iOpenerCommand.input_panel.goto_prev_history() 340 | elif direction == "down": 341 | iOpenerCommand.input_panel.goto_next_history() 342 | 343 | 344 | class iOpenerCommand(sublime_plugin.WindowCommand): 345 | """ 346 | This is the command caled by the UI. 347 | input_panel contains an instance of the class iOpenerPathInput when the input is 348 | active, otherwise it contains None. 349 | """ 350 | input_panel = None 351 | 352 | def run(self): 353 | if not (is_sublime_text_2() or is_sublime_text_3() or is_sublime_text_4()): 354 | print("iOpener plugin is only for Sublime Text v2, v3 and v4.") 355 | else: 356 | load_settings() 357 | iOpenerCommand.input_panel = iOpenerPathInput() 358 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | --------------------------------------------------------------------------------