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

Fixes & Improvements #403

Merged
merged 20 commits into from
Oct 17, 2019
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
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
language: python

dist: trusty

python:
- 3.3

Expand Down
8 changes: 8 additions & 0 deletions CodeFormatter.sublime-settings
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
"codeformatter_debug": false,

"codeformatter_go_options":
{
"syntaxes": "go",
"format_on_save": false // Format on save. Either a boolean (true/false) or a string regexp tested on filename. Example : "^((?!.min.|vendor).)*$"

// Optionless support
},

"codeformatter_php_options":
{
"syntaxes": "php", // Syntax names which must process PHP formatter
Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ CodeFormatter has support for the following languages:
* HTML - By [Custom fork of BeautifulSoup](https://github.com/akalongman/python-beautifulsoup)
* CSS,LESS,SASS - By JSBeautifier
* Python - By PythonTidy (only ST2)
* Go - By [gofmt](https://golang.org/cmd/gofmt/)
* Visual Basic/VBScript
* Coldfusion/Railo/Lucee

Expand Down Expand Up @@ -139,7 +140,11 @@ Language specific options:
"wrap_line_length": 0, // Wrap lines at next opportunity after N characters
"break_chained_methods": false, // Break chained method calls across subsequent lines
"end_with_newline": false, // Add new line at end of file
"comma_first": false // Add comma first
"comma_first": false, // Add comma first
"space_after_anon_function": false, // Add a space before an anonymous function's parens, ie. function ()
"space_after_named_function": false, // Add a space before a named function's parens, i.e. function example ()
"unindent_chained_methods": false, // Don't indent chained method calls
"operator_position": "before-newline" // Set operator position (before-newline|after-newline|preserve-newline) [before-newline]
}
```

Expand Down Expand Up @@ -267,6 +272,19 @@ Language specific options:
"java_style_list_dedent": false
}
```

### Go
Go - used [gofmt](https://golang.org/src/cmd/gofmt/gofmt.go)

Currently no options are supported:
```js
"codeformatter_go_options":
{
"syntaxes": "go",
"format_on_save": false // Format on save. Either a boolean (true/false) or a string regexp tested on filename. Example : "^((?!.min.|vendor).)*$"
}
```

### Visual Basic/VBScript
Visual Basic/VBScript - used custom approach using the HTML beautifier as a guide

Expand Down
3 changes: 3 additions & 0 deletions codeformatter/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from .pyformatter import PyFormatter
from .vbscriptformatter import VbscriptFormatter
from .coldfusionformatter import ColdfusionFormatter
from .goformatter import GoFormatter

except (ValueError):
# Python 2
Expand All @@ -35,6 +36,7 @@
from pyformatter import PyFormatter
from vbscriptformatter import VbscriptFormatter
from coldfusionformatter import ColdfusionFormatter
from goformatter import GoFormatter


class Formatter:
Expand Down Expand Up @@ -64,6 +66,7 @@ def __init__(self, view, syntax=None):
('codeformatter_vbscript_options', VbscriptFormatter),
('codeformatter_scss_options', ScssFormatter),
('codeformatter_coldfusion_options', ColdfusionFormatter),
('codeformatter_go_options', GoFormatter),
]

for name, _class in map_settings_formatter:
Expand Down
40 changes: 40 additions & 0 deletions codeformatter/goformatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import re

import sublime
import subprocess


class GoFormatter:

def __init__(self, formatter):
self.formatter = formatter
self.opts = formatter.settings.get('codeformatter_go_options')

def format(self, text):

stderr = ''
stdout = ''

try:
p = subprocess.Popen(
['gofmt'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
stdout, stderr = p.communicate(text)
except Exception as e:
stderr = str(e)

if (not stderr and not stdout):
stderr = 'Formatting error!'

return stdout, stderr

def format_on_save_enabled(self, _):
format_on_save = False
if ('format_on_save' in self.opts and self.opts['format_on_save']):
format_on_save = self.opts['format_on_save']
if (isinstance(format_on_save, str)):
format_on_save = re.search(format_on_save, file_name) is not None
return format_on_save
140 changes: 69 additions & 71 deletions codeformatter/htmlformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import sublime

directory = os.path.dirname(os.path.realpath(__file__))
libs_path = os.path.join(directory, 'lib')
libs_path = os.path.join(libs_path, 'htmlbeautifier')
libs_path = os.path.join(directory, 'lib', 'htmlbeautifier')

if libs_path not in sys.path:
sys.path.append(libs_path)
Expand All @@ -27,91 +26,90 @@ class HtmlFormatter:

def __init__(self, formatter):
self.formatter = formatter
self.opts = formatter.settings.get('codeformatter_html_options')
self.options = htmlbeautifier.default_options()

def format(self, text):
text = text.decode('utf-8')
stderr = ''
stdout = ''
# fill custom options
custom_options = formatter.settings.get('codeformatter_html_options')
self.fill_custom_options(custom_options)

formatter = ''
def fill_custom_options(self, options):

if 'formatter_version' in self.opts:
formatter = self.opts['formatter_version']
if use_bs4 is False and self.opts['formatter_version'] == 'bs4':
formatter = 'regexp'
sublime.error_message(
u'CodeFormatter\n\nUnable to load BeautifulSoup HTML '
u'formatter. The old RegExp-based formatter was '
u'automatically used for you instead.'
)
if not options:
return

if formatter == 'bs4' and use_bs4:
p_indent_size = 4
if 'indent_size' in self.opts:
p_indent_size = self.opts['indent_size']

try:
soup = BeautifulSoup(text, 'html.parser')
stdout = soup.prettify(
formatter=None, indent_size=p_indent_size)
except Exception as e:
stderr = str(e)
else:
options = htmlbeautifier.default_options()

if 'indent_size' in self.opts:
options.indent_size = self.opts['indent_size']

if 'indent_char' in self.opts:
options.indent_char = str(self.opts['indent_char'])

if 'minimum_attribute_count' in self.opts:
options.minimum_attribute_count = (
self.opts['minimum_attribute_count']
)
custom_options = [
'formatter_version',
'indent_size',
'indent_char',
'minimum_attribute_count',
'first_attribute_on_new_line',
'indent_with_tabs',
'expand_tags',
'reduce_empty_tags',
'reduce_whole_word_tags',
'exception_on_tag_mismatch',
'custom_singletons',
'format_on_save'
]

if 'first_attribute_on_new_line' in self.opts:
options.first_attribute_on_new_line = (
self.opts['first_attribute_on_new_line']
)
casters = {'indent_char': str}

if 'indent_with_tabs' in self.opts:
options.indent_with_tabs = self.opts['indent_with_tabs']
for key in custom_options:

if 'expand_tags' in self.opts:
options.expand_tags = self.opts['expand_tags']
value = options.get(key)
if value is None:
continue

if 'reduce_empty_tags' in self.opts:
options.reduce_empty_tags = self.opts['reduce_empty_tags']
cast = casters.get(key)
if cast:
value = cast(value)

if 'reduce_whole_word_tags' in self.opts:
options.reduce_whole_word_tags = (
self.opts['reduce_whole_word_tags']
)
setattr(self.options, key, value)

if 'exception_on_tag_mismatch' in self.opts:
options.exception_on_tag_mismatch = (
self.opts['exception_on_tag_mismatch']
)
def format_with_bs4(self, text):
stdout, stderr = '', ''

p_indent_size = getattr(self.options, 'indent_size', 4)

if 'custom_singletons' in self.opts:
options.custom_singletons = self.opts['custom_singletons']
try:
soup = BeautifulSoup(text, 'html.parser')
stdout = soup.prettify(formatter=None, indent_size=p_indent_size)
except Exception as e:
stderr = str(e)

try:
stdout = htmlbeautifier.beautify(text, options)
except Exception as e:
stderr = str(e)
return stdout, stderr

if (not stderr and not stdout):
stderr = 'Formatting error!'
def format_with_beautifier(self, text):
stdout, stderr = '', ''

try:
stdout = htmlbeautifier.beautify(text, self.options)
except Exception as e:
stderr = str(e)

if (not stderr and not stdout):
stderr = 'Formatting error!'

return stdout, stderr

def format(self, text):
text = text.decode('utf-8')

formatter = getattr(self.options, 'formatter_version')
if formatter == 'bs4':
if not use_bs4:
sublime.error_message(
u'CodeFormatter\n\nUnable to load BeautifulSoup HTML '
u'formatter. The old RegExp-based formatter was '
u'automatically used for you instead.'
)
else:
return self.format_with_bs4(text)

return self.format_with_beautifier(text)

def format_on_save_enabled(self, file_name):
format_on_save = False
if ('format_on_save' in self.opts and self.opts['format_on_save']):
format_on_save = self.opts['format_on_save']
if (isinstance(format_on_save, str)):
format_on_save = getattr(self.options, 'format_on_save', False)
if isinstance(format_on_save, str):
format_on_save = re.search(format_on_save, file_name) is not None
return format_on_save
8 changes: 8 additions & 0 deletions codeformatter/jsformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ def format(self, text):
else:
options.space_after_anon_function = False

if (
'space_after_named_function' in self.opts and
self.opts['space_after_named_function']
):
options.space_after_named_function = True
else:
options.space_after_named_function = False

if (
'unindent_chained_methods' in self.opts and
self.opts['unindent_chained_methods']
Expand Down
2 changes: 1 addition & 1 deletion codeformatter/lib/cssbeautifier/css/beautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
import copy
from .options import BeautifierOptions
from jsbeautifier.core.options import mergeOpts
from jsbeautifier.core.options import _mergeOpts as mergeOpts
from jsbeautifier.core.output import Output
from jsbeautifier.__version__ import __version__

Expand Down
Loading