Created
July 26, 2024 17:00
-
-
Save acestudiooleg/8ad334bee21e14bfe6902dacd28dc2c5 to your computer and use it in GitHub Desktop.
terminator open in vscode file
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
| import inspect | |
| import os | |
| import re | |
| import shlex | |
| import subprocess | |
| from terminatorlib import plugin, config | |
| AVAILABLE = ['OpenFileInVSCode'] | |
| class OpenFileInVSCode(plugin.URLHandler): | |
| capabilities = ['url_handler'] | |
| handler_name = 'OpenFileInVSCode' | |
| nameopen = "Open the file" | |
| namecopy = "Copy file path" | |
| match = '[^ \t\n\r\f:]+(:[0-9]+)?' | |
| def open_url(self): | |
| """ Return True if we should open the file. """ | |
| # HACK: Because the plugin doesn't tell us we should open or copy | |
| # the command, we need to climb the stack to see how we got here. | |
| return inspect.stack()[3][3] == 'OpenFileInVSCode' | |
| def callback(self, url): | |
| m = re.match(r'([^ \t\n\r\f:]+)(?::([0-9]+))?', url) | |
| if m and m.group(2): | |
| url = m.group(1) | |
| # # HACK (from https://github.com/mchelem/terminator-editor-plugin) | |
| # # Because the current working directory is not available to | |
| # # plugins, we need to use the inspect module to climb up the stack to | |
| # # the Terminal object and call get_cwd() from there. | |
| for frameinfo in inspect.stack(): | |
| frameobj = frameinfo[0].f_locals.get('self') | |
| if frameobj and frameobj.__class__.__name__ == 'Terminal': | |
| cwd = frameobj.get_cwd() | |
| path = os.path.join(cwd, url) | |
| command = 'code --goto {filepath}:{line}'.format( | |
| line=m.group(2) or 1, | |
| filepath=path | |
| ) | |
| subprocess.call(shlex.split(command)) | |
| return '--version' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment