-->

How can a Sublime Text build system access the con

2020-07-18 02:39发布

问题:

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!