Skip to content

Commit

Permalink
actually use zig.executable config var
Browse files Browse the repository at this point in the history
  • Loading branch information
emekoi committed Jul 24, 2021
1 parent eed4d9a commit 815cffa
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 29 deletions.
48 changes: 23 additions & 25 deletions Build Systems/Zig.sublime-build
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
{
"cmd": ["zig", "build"],
"target": "zig_build",
// "cancel": { "kill": true },
"selector": "source.zig",
"working_dir": "$folder",
"file_regex": "^(\\S.*):(\\d*):(\\d*): (?:[^:]*): (.*)$",
"line_regex": "^(?:\\S.*):(\\d*):(\\d*): (?:[^:]*): (.*)$",

"keyfiles": ["build.zig"],
"variants": [
{
"cmd": ["zig", "run", "$file"],
"selector": "source.zig",
"name": "Run File"
"name": "Run File",
"run": true,
},
{
"cmd": ["zig", "test", "$file"],
"selector": "source.zig",
"name": "Test File"
"name": "Run Project",
"run": true,
"project": true,

},
{
"cmd": ["zig", "fmt", "$file"],
"selector": "source.zig",
"name": "Format File",
"quiet": true,
"name": "Test File",
"test":true
},
{
"cmd": ["zig", "build", "test"],
"selector": "source.zig",
"working_dir": "$folder",
"name": "Test Project"
"name": "Test Project",
"test": true,
"project": true,
},
{
"cmd": ["zig", "build"],
"selector": "source.zig",
"working_dir": "$folder",
"name": "Build Project"
"name": "Format File",
"format": true,
"quiet": true,
},
{
"cmd": ["zig", "fmt", "$folder"],
"selector": "source.zig",
"name": "Format Project",
"format": true,
"project": true,
"quiet": true
},
{
"name": "Build Project",
"project": true,
},
]
}
2 changes: 1 addition & 1 deletion Settings/Zig.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"zig.fmt.mode": "file",

// automatically hide the build/fmt output panel
"zig.quiet": true,
"zig.quiet": false,
}
136 changes: 133 additions & 3 deletions Zig.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import sublime
import sublime_plugin

settings = sublime.load_settings('Zig.sublime-settings')
import subprocess
import threading
import os

settings = sublime.load_settings('Zig.sublime-settings')
preferences_settings = sublime.load_settings("Preferences.sublime-settings")

def get_setting(view, opt, default):
return view.settings().get(opt, settings.get(opt, default))


class Zig(sublime_plugin.EventListener):
def on_post_save_async(self, view):
sel = view.sel()[0]
Expand All @@ -24,4 +27,131 @@ def on_post_save_async(self, view):
if (should_build):
view.window().run_command('build')
if (is_quiet):
view.window().run_command("hide_panel")
view.window().run_command("hide_panel")

class ZigBuildCommand(sublime_plugin.WindowCommand):
encoding = 'utf-8'
killed = False
proc = None
panel = None
panel_lock = threading.Lock()

def is_enabled(self, run=False, format=False, test=False, project=False, quiet=False, kill=False):
# The Cancel build option should only be available
# when the process is still running
if kill:
return self.proc is not None and self.proc.poll() is None
return True

def run(self, run=False, format=False, test=False, project=False, quiet=False, kill=False):
if kill:
if self.proc:
self.killed = True
self.proc.terminate()
return

vars = self.window.extract_variables()
working_dir = vars.get('file_path', vars['folder'])

view = self.window.active_view()
is_quiet = get_setting(view, 'zig.quiet', quiet)

# A lock is used to ensure only one thread is
# touching the output panel at a time
with self.panel_lock:
# Creating the panel implicitly clears any previous contents
self.panel = self.window.create_output_panel('exec')

# Enable result navigation. The result_file_regex does
# the primary matching, but result_line_regex is used
# when build output includes some entries that only
# contain line/column info beneath a previous line
# listing the file info. The result_base_dir sets the
# path to resolve relative file names against.
settings = self.panel.settings()
settings.set(
'result_file_regex',
r'^(?:.\/)(\S.*):(\d*):(\d*): (?:[^:]*): (.*)$'
)
settings.set(
'result_line_regex',
r'^(?:\S.*):(\d*):(\d*): (?:[^:]*): (.*)$'
)
settings.set('result_base_dir', working_dir)
self.window.create_output_panel('exec')

if not is_quiet:
self.window.run_command('show_panel', {'panel': 'output.exec'})

if self.proc is not None:
self.proc.terminate()
self.proc = None

args = [get_setting(view, 'zig.executable', 'zig')]

if project:
if format:
args.append('fmt')
args.append(vars['folder'])
else:
args.append('build')
if run: args.append('run')
elif test: args.append('test')
else:
if format: args.append('fmt')
elif test: args.append('test')
else: args.append('run')
args.append(vars['file_name'])

self.proc = subprocess.Popen(
args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=working_dir
)
self.killed = False

threading.Thread(
target=self.read_handle,
args=(self.proc.stdout,)
).start()

def read_handle(self, handle):
chunk_size = 2 ** 13
out = b''
while True:
try:
data = os.read(handle.fileno(), chunk_size)
# If exactly the requested number of bytes was
# read, there may be more data, and the current
# data may contain part of a multibyte char
out += data
if len(data) == chunk_size:
continue
if data == b'' and out == b'':
raise IOError('EOF')
# We pass out to a function to ensure the
# timeout gets the value of out right now,
# rather than a future (mutated) version
self.queue_write(out.decode(self.encoding))
if data == b'':
raise IOError('EOF')
out = b''
except (UnicodeDecodeError) as e:
msg = 'Error decoding output using %s - %s'
self.queue_write(msg % (self.encoding, str(e)))
break
except (IOError):
if self.killed:
msg = 'Cancelled'
else:
msg = 'Finished'
self.queue_write('\n[%s]' % msg)
break

def queue_write(self, text):
sublime.set_timeout(lambda: self.do_write(text), 1)

def do_write(self, text):
with self.panel_lock:
self.panel.run_command('append', {'characters': text})

0 comments on commit 815cffa

Please sign in to comment.