Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'show build results only on a failed build' feature #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ Set to `1` to trigger a build on save. By default, this is set to `1`. I.e., Sub
* **filename_filter**
SublimeOnSaveBuild matches the name of the file being saved against this regular expression to determine if a build should be triggered. By default, the setting has a value of `"\\.(css|js|sass|less|scss)$"`.

* **show_build_window_on_failure_only**
Set to `1` if you want the build window to display only upon a failed build. The default setting is `1`. This setting can be configured on a project level.

Usage
-----
1. Make sure you have a build operation set up in your Sublime Text 2 project and you are able to build using `Control+B` (Linux/Windows) or `Command+B` (OS X).
Expand Down
26 changes: 25 additions & 1 deletion SublimeOnSaveBuild.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sublime
import sublime_plugin
import re

import functools

class SublimeOnSaveBuild(sublime_plugin.EventListener):
def on_post_save(self, view):
Expand All @@ -15,10 +15,34 @@ def on_post_save(self, view):
# Load filename filter. Again, a project level setting takes precedence.
filename_filter = view.settings().get('filename_filter', global_settings.get('filename_filter', '*'))

# Check if we should automatically hide the build window
auto_hide_build_window = view.settings().get('auto_hide_build_window', global_settings.get('auto_hide_build_window', True))

if not should_build:
return

if not re.search(filename_filter, view.file_name()):
return

# show the 'exec' view before building, so we can read from it afterwards
self.output_view = view.window().get_output_panel("exec")

view.window().run_command('build')

if auto_hide_build_window:
self.num_polls = 0
# start polling for results every 100s
self.poll_for_results(view)

def poll_for_results(self, view):
build_finished = self.output_view.find('Finished', 0) != None

if build_finished:
errors = self.output_view.find('Error', 0)
if errors == None:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at Packages/Default/exec.py, a more robust way to do the error check would be:

errs = self.output_view.find_all_results()
if len(errs) == 0:

view.window().run_command("hide_panel", {"panel": "output.exec"})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I believe the panel argument to hide_panel is a no-op, unlike to its sibling show_panel. I just tried it out in ST2 and ST3 with the 'find' panel open; the above hide_panel call would still close the find panel even though the output.exec panel was specified. Seems https://sublimetext.userecho.com/topics/1930-add-panel-param-to-hide_panel-command/ is still open. @wbond, correct?

else:
if self.num_polls < 300:
sublime.set_timeout(functools.partial(self.poll_for_results, view), 200)

self.num_polls += 1