-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
220 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.8 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"bun_path": "~/.bun/bin", | ||
"bun_binary_files": [ | ||
{ | ||
"pattern": "bun.lockb", | ||
"syntax": "scope:text.yarnlock" | ||
}, | ||
{ | ||
"pattern": "node_modules.bun", | ||
"syntax": "scope:source.js", | ||
"pretty": true | ||
} | ||
], | ||
"prettify_options": { | ||
"indentWidth": 2, | ||
"lineWidth": 120 | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
[ | ||
{ | ||
"caption": "Preferences", | ||
"mnemonic": "n", | ||
"id": "preferences", | ||
"children": [ | ||
{ | ||
"caption": "Package Settings", | ||
"mnemonic": "P", | ||
"id": "package-settings", | ||
"children": [ | ||
{ | ||
"caption": "Bun", | ||
"children": [ | ||
{ | ||
"caption": "Settings", | ||
"command": "edit_settings", | ||
"args": { | ||
"base_file": "${packages}/sublime-bun/Bun.sublime-settings", | ||
"default": "// Settings in here override those in \"${packages}/sublime-bun/Bun.sublime-settings\"\n\n{\n\t$0\n}\n" | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,18 @@ | ||
# sublime-bun | ||
Bun binary files viewer and other Bun-related stuff | ||
|
||
## Installation | ||
|
||
Plugin is not publushed yet on [Package Control](https://packagecontrol.io/), to install it follow these steps: | ||
|
||
- run `Package Control: Add Repository` command, add `https://github.com/alexkuz/sublime-bun` there; | ||
- find and select `sublime-bun` package in `Install Package` menu. | ||
|
||
## Settings | ||
|
||
- `bun_path` - path to Bun executable (required for `bun.lockb` files processing), `~/.bun/bin` by default; | ||
- `bun_binary_files` - a list of syntax definitions for binary files output; | ||
- `pattern` - pattern to match file name; | ||
- `syntax` - ST syntax definition that should be applied for that file; | ||
- `pretty` - indicates if prettifier should be applied (works for JS/TS files); | ||
- `prettify_options` - pretifier options (see [Configuration struct](https://github.com/dprint/dprint-plugin-typescript/blob/64064984dc24339249c6425a1401f93d94887967/src/configuration/types.rs#L258) for full list) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import sys | ||
import os | ||
import threading | ||
import fnmatch | ||
import json | ||
|
||
import sublime | ||
import sublime_plugin | ||
from subprocess import PIPE, Popen | ||
from .dprint import dprint_python_bridge | ||
|
||
PLUGIN_NAME = "Bun" | ||
|
||
def check_syntax(syntax = ""): | ||
if syntax.startswith("scope:") and sublime.find_syntax_by_scope(syntax.split(":")[1]): | ||
return True | ||
if sublime.find_syntax_by_name(syntax): | ||
return True | ||
if sublime.syntax_from_path(syntax): | ||
return True | ||
return False | ||
|
||
def get_setting(view, key, default_value=None): | ||
settings_filename = '{0}.sublime-settings'.format(PLUGIN_NAME) | ||
|
||
settings = view.settings().get(PLUGIN_NAME) | ||
if settings is None or settings.get(key) is None: | ||
settings = sublime.load_settings(settings_filename) | ||
|
||
value = settings.get(key, default_value) | ||
|
||
return value | ||
|
||
def expand_var(window, var_to_expand): | ||
if var_to_expand: | ||
expanded = os.path.expanduser(var_to_expand) | ||
expanded = os.path.expandvars(expanded) | ||
if window: | ||
window_variables = window.extract_variables() | ||
expanded = sublime.expand_variables(expanded, window_variables) | ||
return expanded | ||
|
||
return var_to_expand | ||
|
||
|
||
def is_supported_file(view, filename): | ||
bun_binary_files = get_setting(view, "bun_binary_files", []) | ||
|
||
return next(True for file in bun_binary_files if fnmatch.fnmatch(filename, file["pattern"])) | ||
|
||
|
||
def update_hint(view, text): | ||
view.erase_phantoms("bun-prettify") | ||
if text: | ||
template = '<body style="background-color: #a8201a; padding: 3px 10px;">{0}</body>' | ||
view.add_phantom("bun-prettify", sublime.Region(0), template.format(text), sublime.LAYOUT_INLINE) | ||
|
||
|
||
class BunPreviewBinaryCommand(sublime_plugin.TextCommand): | ||
def run(self, edit, prettified_text): | ||
try: | ||
if not self.is_supported_file(): | ||
return | ||
if prettified_text: | ||
self.preview_prettified(edit, prettified_text) | ||
else: | ||
self.preview_binary(edit) | ||
|
||
except Exception as err: | ||
self.view.replace(edit, sublime.Region(0), "\n") | ||
print("ERROR: {0}".format(err)) | ||
update_hint(self.view, str(err)) | ||
self.view.set_scratch(True) | ||
|
||
def is_supported_file(self): | ||
filepath = self.view.file_name() | ||
if not filepath: | ||
return False | ||
|
||
[dirname, filename] = os.path.split(filepath) | ||
|
||
if not is_supported_file(self.view, filename): | ||
return False | ||
|
||
return True | ||
|
||
def preview_prettified(self, edit, prettified_text): | ||
region = sublime.Region(0, self.view.size()) | ||
self.view.set_read_only(False) | ||
update_hint(self.view, None) | ||
self.view.replace(edit, region, prettified_text) | ||
self.view.set_read_only(True) | ||
self.view.set_scratch(True) | ||
|
||
def preview_binary(self, edit): | ||
filepath = self.view.file_name() | ||
[dirname, filename] = os.path.split(filepath) | ||
|
||
region = sublime.Region(0, self.view.size()) | ||
|
||
command = ["./%s" % filename] | ||
|
||
bun_path = expand_var(self.view.window(), get_setting(self.view, "bun_path")) | ||
|
||
env = os.environ.copy() | ||
if bun_path + ":" not in env["PATH"]: | ||
env["PATH"] = bun_path + ":" + env["PATH"] | ||
|
||
with Popen(command, cwd=dirname, stdout=PIPE, stderr=PIPE, shell=False, env=env) as process: | ||
res = process.communicate() | ||
if process.returncode != 0: | ||
raise RuntimeError("{0}\n(Return code: {1})".format(res[1].decode("utf-8"), process.returncode)) | ||
output = res[0].decode("utf-8") | ||
|
||
bun_binary_files = get_setting(self.view, "bun_binary_files", []) | ||
config = next(file for file in bun_binary_files if fnmatch.fnmatch(filename, file["pattern"])) | ||
|
||
syntax = config.get("syntax", "scope:text.plain") | ||
if not check_syntax(syntax): | ||
syntax = "scope:text.plain" | ||
prettify = config.get("pretty", False) | ||
prettify_options = get_setting(self.view, "prettify_options", {}) | ||
win_id = self.view.window().id() | ||
|
||
self.view.set_read_only(False) | ||
replace_text = "\n\n{0}".format(output) if prettify else output | ||
self.view.replace(edit, region, replace_text) | ||
self.view.assign_syntax(syntax) | ||
|
||
if prettify: | ||
update_hint(self.view, "Prettifying...") | ||
sublime.set_timeout_async(lambda: self.prettify(output, filepath, win_id, prettify_options), 0) | ||
|
||
self.view.set_read_only(True) | ||
self.view.set_scratch(True) | ||
|
||
def prettify(self, output, filepath, win_id, prettify_options): | ||
try: | ||
options = json.dumps(prettify_options) | ||
prettified = dprint_python_bridge.format_text(filepath, output, options) | ||
except Exception as err: | ||
update_hint(self.view, str(err)) | ||
|
||
for window in sublime.windows(): | ||
if window.id() == win_id: | ||
window.active_view().run_command("bun_preview_binary", {'prettified_text': prettified}) | ||
|
||
|
||
class BunLockbViewEventListener(sublime_plugin.ViewEventListener): | ||
def on_load(self): | ||
[dirname, filename] = os.path.split(self.view.file_name()) | ||
|
||
if not is_supported_file(self.view, filename): | ||
return | ||
|
||
self.view.run_command("bun_preview_binary", {'prettified_text': None}) |
Binary file not shown.