Skip to content
Merged
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [v3.2.0] – 2023-05-02

- Commands: improved/generalized syntax detection for enabling/disabling commands.
- Commands: fix: output both stdout/stderr when running `mix format`/`mix test`.
- Commands: auto-scroll `mix format` output when it's compiling.
- SQL: recognize `FILTER` in `array_agg(x) FILTER (...)`.

## [v3.1.5] – 2023-04-30

- Elixir: recognize `name` in `defmodule name do end`.
Expand Down
35 changes: 26 additions & 9 deletions commands/mix_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import subprocess
import shlex
from .utils import *
from time import time as now
from datetime import datetime

__author__ = 'Aziz Köksal'
__email__ = 'aziz.koeksal@gmail.com'
Expand Down Expand Up @@ -80,32 +82,47 @@ def call_mix_format(window, **kwargs):
)
return

proc = subprocess.Popen(cmd, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proc = subprocess.Popen(cmd, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

panel_name = 'mix_format'
panel_params = {'panel': 'output.%s' % panel_name}
window.run_command('erase_view', panel_params)
output_view = None

past_timestamp = now()
panel_update_interval = 2

while proc.poll() is None:
stderr_line = proc.stderr.readline().decode(encoding='UTF-8')
line = proc.stdout.readline().decode(encoding='UTF-8')

if stderr_line:
if line:
if not output_view:
first_lines = '$ cd %s && %s\n\n' % (shlex.quote(cwd), ' '.join(map(shlex.quote, cmd)))
output_view = window.create_output_panel(panel_name)
output_view.settings().set('result_file_regex', r'/([^/]+):(\d+):(\d+)')
output_view.set_read_only(False)
output_view.run_command('append', {'characters': first_lines})
output_view = create_mix_format_panel(window, panel_name, cmd, cwd)
window.run_command('show_panel', panel_params)

output_view.run_command('append', {'characters': stderr_line})
output_view.run_command('append', {'characters': line})

if now() - past_timestamp > panel_update_interval:
output_view.show(output_view.size())
past_timestamp = now()

if output_view:
output_view.set_read_only(True)
else:
if window.active_panel() == panel_params['panel']:
window.run_command('hide_panel', panel_params)
window.destroy_output_panel(panel_name)

msg = 'Formatted %s %s!' % (file_path and 'file' or 'directory', repr(file_path or cwd))
print_status_msg(msg)

def create_mix_format_panel(window, panel_name, cmd_args, cwd):
first_lines = '$ cd %s && %s' % (shlex.quote(cwd), ' '.join(map(shlex.quote, cmd_args)))
first_lines += '\n# Timestamp: %s\n\n' % datetime.now().replace(microsecond=0)

output_view = window.create_output_panel(panel_name)
output_view.settings().set('result_file_regex', r'/([^/]+):(\d+):(\d+)')
output_view.set_read_only(False)
output_view.run_command('append', {'characters': first_lines})

return output_view
2 changes: 1 addition & 1 deletion commands/mix_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ def write_output(txt):
)
return

proc = subprocess.Popen(cmd_args, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proc = subprocess.Popen(cmd_args, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

if output_view:
output_view.settings().set('view_id', output_view.id())
Expand Down
11 changes: 5 additions & 6 deletions commands/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ def expand_scope_right(view, begin_point, scope):
)
return sublime.Region(begin_point, end_point)

def is_one_of_syntaxes(view, syntaxes):
view_syntax_path = view.settings().get('syntax')
return any(basename + '.sublime-syntax' in view_syntax_path for basename in syntaxes)
def has_one_of_scope_suffixes(view, scope_suffixes):
view_scope_suffixes = view.scope_name(0).split(' ')[0].split('.')[1:]
return any(suffix in view_scope_suffixes for suffix in scope_suffixes)

def is_elixir_syntax(view):
return is_one_of_syntaxes(view, ['Elixir'])
return has_one_of_scope_suffixes(view, ['elixir'])

def is_formattable_syntax(view):
syntaxes = ['Elixir', 'EEx', 'Elixir (EEx)', 'HTML (EEx)', 'HTML (HEEx)', 'HTML (Surface)']
return is_one_of_syntaxes(view, syntaxes)
return has_one_of_scope_suffixes(view, ['elixir', 'eex', 'heex', 'surface'])

def reverse_find_root_folder(bottom_path):
bottom_path = Path(bottom_path)
Expand Down
2 changes: 1 addition & 1 deletion syntaxes/SQL (Elixir).sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ contexts:
all|any|array|analy[sz]e|a?symmetric|authorization|at(?=\s+time\s+zone\b)|binary|both|by
| (?<=\bat\s)time(?=\s+zone\b) | (?<=\btime\s)zone | (?<=\bdo\s)nothing | (?<=\bon\s)conflict | (?<=\bwith\s)ordinality
| cast|cross|column|concurrently|collat(?:e|ion)|create|distinct|(?<!\bas\s)do
| except|excluded(?=\s*\.)|fetch|for(?:eign)?|freeze|full|group|intersect|into|inner|isnull
| except|excluded(?=\s*\.)|fetch|filter(?=\s+\()|for(?:eign)?|freeze|full|group|intersect|into|inner|isnull
| i[sn]|ilike|lateral|leading|left|natural|not(?:null)?|nullif|on(?:ly)?|offset|order|outer
| over(?:laps)?|primary|recursive|returning|right|similar|some|table(?:sample)?|to|trailing
| unnest|unique|user|variadic|verbose|window
Expand Down
11 changes: 11 additions & 0 deletions tests/syntax_test_sql.ex.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
-- ^ punctuation.accessor.dot
-- ^^^^^ constant.other.table-name

SELECT array_agg(vote) FILTER (WHERE vote IS NOT NULL) FROM posts
-- ^^^^^ variable.other
-- ^^^^ keyword.other.DML
-- ^^^^ constant.language.null
-- ^^^ keyword.other
-- ^^ keyword.other
-- ^^^^ variable.other
-- ^^^^^ keyword.other.DML
-- ^^^^^^ keyword.other
-- ^^^^ variable.other
-- ^^^^^^^^^ variable.function

-- Numbers

Expand Down