How can a Sublime Text build system access the contents of a buffer, not a file?

做~自己de王妃 提交于 2019-12-21 18:04:12

问题


Let's say I open a new buffer in Sublime Text 3 (ST3); how can I access the contents of that buffer from my build system? i.e. Let's say I have a build system that will run a file thorough node—in that case I can simply use $file, but as far as I can tell, there is no build system variable for the current buffer's contents. Is there any way to pull the contents of the currently selected, unsaved buffer into a build process?


回答1:


To do this you will need to write a custom plugin to perform the build process, then add

"target": "my_plugin_class"

to your .sublime-build file instead of the standard

"cmd": ["external_program", "option1", "option2", "$file"]

In your plugin, you can then reference the current view, or even the current window if you have more than one tab open that you'd like to process. Your plugin could be as simple as writing the current view's contents to a temp file, running it with Python, for example, then deleting the temp file when you're done.

To get an idea of how the standard build process is executed, check out Packages/Default/exec.py. You can browse to this file directly in Sublime Text 2 by selecting Preferences -> Browse Packages.... In ST3, all default packages are wrapped up in .sublime-package zip files, so you'll need to install the excellent PackageResourceViewer plugin. Once installed, open the Command Palette with (Ctrl/)ShiftP or Tools -> Command Palette, type prv to bring up the PackageResourceViewer options, select PackageResourceViewer: Open Resource, then go through the menus to select Default -> exec.py.

Good luck!



来源:https://stackoverflow.com/questions/25408444/how-can-a-sublime-text-build-system-access-the-contents-of-a-buffer-not-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!