Skip to content

Commit baaaa78

Browse files
committed
Add option to use sublime's indentation settings instead of the ones defined in .jsbeautifyrc
Signed-off-by: Victor Porof <victor.porof@gmail.com>
1 parent 5cfc03e commit baaaa78

7 files changed

Lines changed: 82 additions & 19 deletions

File tree

HTMLPrettify.sublime-settings

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
"format_selection_only": true,
1616

1717
// Use current syntax to determine file type, instead of the extension.
18-
"use_syntax": true,
18+
"use_editor_syntax": true,
19+
20+
// Use current identation settings to overwrite the ones from `.jsbeautifyrc`.
21+
"use_editor_indentation": false,
1922

2023
// Log the settings passed to the prettifier from `.jsbeautifyrc`.
2124
"print_diagnostics": true

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ To beautify your code when saving the document, set the `format_on_save` setting
7575
To stop beautifying only the selected text, set the `format_selection_only` setting to `false` in `HTMLPrettify.sublime-settings`.
7676

7777
## Using editor syntax for determining file type
78-
To stop beautifying only the selected text, set the `use_syntax` setting to `false` in `HTMLPrettify.sublime-settings`.
78+
To stop beautifying only the selected text, set the `use_editor_syntax` setting to `false` in `HTMLPrettify.sublime-settings`.
79+
80+
## Using editor indentation settings to determine formatting style
81+
To stop using the formatting style regarding indentation size and whether or not to use tabs or spaces (defined in the `.jsbeautifyrc` config file), and use sublime's settings instead, then set the `use_editor_indentation` setting to `true` in `HTMLPrettify.sublime-settings`.
7982

8083
## Ignoring certain files
8184
To add ignore rules use `disallowed_file_patterns` in the `.jsbeautifyrc` file. If the file(including path) matches any of the regexp patterns defined in `disallowed_file_patterns` it will not be beautified:

src/js/main.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import * as beautify from 'js-beautify';
99

1010
import * as stdio from './utils/stdioUtils';
1111
import { parseDefaultJsbeautifyConfig, extendJsbeautifyConfigFromFolders } from './utils/configUtils';
12+
import { finalizeJsbeautifyConfig } from './utils/configUtils';
1213
import { getPotentialConfigDirs } from './utils/pathUtils';
1314
import { isCSS, isHTML, isJS, isAllowedFilePath } from './utils/fileUtils';
14-
import { EDITOR_FILE_SYNTAX, EDITOR_TEXT_FILE_PATH, ORIGINAL_FILE_PATH } from './utils/constants';
15+
import { EDITOR_FILE_SYNTAX, EDITOR_INDENT_SIZE, EDITOR_INDENT_WITH_TABS } from './utils/constants';
16+
import { EDITOR_TEXT_FILE_PATH, ORIGINAL_FILE_PATH } from './utils/constants';
1517

1618
process.on('uncaughtException', (err) => {
1719
stdio.err('Uncaught exception', err);
@@ -27,36 +29,36 @@ async function main() {
2729
const baseConfig = await parseDefaultJsbeautifyConfig();
2830
const pathsToLook = getPotentialConfigDirs(path.dirname(ORIGINAL_FILE_PATH));
2931
const extendedConfig = await extendJsbeautifyConfigFromFolders(pathsToLook, baseConfig);
30-
31-
extendedConfig.html.js = extendedConfig.js;
32-
extendedConfig.html.css = extendedConfig.css;
32+
const finalConfig = finalizeJsbeautifyConfig(extendedConfig, EDITOR_INDENT_SIZE, EDITOR_INDENT_WITH_TABS);
3333

3434
// Dump some diagnostics messages, parsed out by the plugin.
3535
stdio.info(`Using editor file syntax: ${EDITOR_FILE_SYNTAX}`);
36+
stdio.info(`Using editor indent size: ${EDITOR_INDENT_SIZE}`);
37+
stdio.info(`Using editor indent with tabs: ${EDITOR_INDENT_WITH_TABS}`);
3638
stdio.info(`Using editor text path: ${EDITOR_TEXT_FILE_PATH}`);
3739
stdio.info(`Using original file path: ${ORIGINAL_FILE_PATH}`);
3840
stdio.info(`Using paths for .jsbeautifyrc: ${JSON.stringify(pathsToLook)}`);
39-
stdio.info(`Using prettify options: ${JSON.stringify(extendedConfig)}`);
41+
stdio.info(`Using prettify options: ${JSON.stringify(finalConfig)}`);
4042

4143
const fileContents = await fs.readFile(EDITOR_TEXT_FILE_PATH, { encoding: 'utf8' });
4244

43-
if (isCSS(EDITOR_FILE_SYNTAX, ORIGINAL_FILE_PATH, extendedConfig)) {
45+
if (isCSS(EDITOR_FILE_SYNTAX, ORIGINAL_FILE_PATH, finalConfig)) {
4446
stdio.info('Attempting to prettify what seems to be a CSS file.');
4547
stdio.endDiagnostics();
4648
stdio.beginPrettifiedCode();
47-
stdio.out(beautify.css(fileContents, extendedConfig.css));
49+
stdio.out(beautify.css(fileContents, finalConfig.css));
4850
stdio.endPrettifiedCode();
49-
} else if (isHTML(EDITOR_FILE_SYNTAX, ORIGINAL_FILE_PATH, fileContents, extendedConfig)) {
51+
} else if (isHTML(EDITOR_FILE_SYNTAX, ORIGINAL_FILE_PATH, fileContents, finalConfig)) {
5052
stdio.info('Attempting to prettify what seems to be a HTML file.');
5153
stdio.endDiagnostics();
5254
stdio.beginPrettifiedCode();
53-
stdio.out(beautify.html(fileContents, extendedConfig.html));
55+
stdio.out(beautify.html(fileContents, finalConfig.html));
5456
stdio.endPrettifiedCode();
55-
} else if (isJS(EDITOR_FILE_SYNTAX, ORIGINAL_FILE_PATH, fileContents, extendedConfig)) {
57+
} else if (isJS(EDITOR_FILE_SYNTAX, ORIGINAL_FILE_PATH, fileContents, finalConfig)) {
5658
stdio.info('Attempting to prettify what seems to be a JS file.');
5759
stdio.endDiagnostics();
5860
stdio.beginPrettifiedCode();
59-
stdio.out(beautify.js(fileContents, extendedConfig.js));
61+
stdio.out(beautify.js(fileContents, finalConfig.js));
6062
stdio.endPrettifiedCode();
6163
} else {
6264
stdio.info('Unsupported file type');

src/js/utils/configUtils.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,39 @@ export const extendJsbeautifyConfigFromFolders = async (folderPaths, oldJsbeauti
4747

4848
return clone(oldJsbeautifyConfig);
4949
};
50+
51+
// Clones and extends a given .jsbeautifyrc with some additonal meta-options
52+
// following some specific rules.
53+
export const finalizeJsbeautifyConfig = (
54+
jsbeautifyConfig, editorIndentSize, editorIndentWithTabs,
55+
) => {
56+
const newJsbeautifyConfig = clone(jsbeautifyConfig);
57+
58+
if (editorIndentSize !== '?') {
59+
newJsbeautifyConfig.html.indent_size = +editorIndentSize;
60+
newJsbeautifyConfig.css.indent_size = +editorIndentSize;
61+
newJsbeautifyConfig.js.indent_size = +editorIndentSize;
62+
}
63+
if (editorIndentWithTabs !== '?') {
64+
if (editorIndentWithTabs === 'True') {
65+
newJsbeautifyConfig.html.indent_with_tabs = true;
66+
newJsbeautifyConfig.html.indent_char = '\t';
67+
newJsbeautifyConfig.css.indent_with_tabs = true;
68+
newJsbeautifyConfig.css.indent_char = '\t';
69+
newJsbeautifyConfig.js.indent_with_tabs = true;
70+
newJsbeautifyConfig.js.indent_char = '\t';
71+
} else {
72+
newJsbeautifyConfig.html.indent_with_tabs = false;
73+
newJsbeautifyConfig.html.indent_char = ' ';
74+
newJsbeautifyConfig.css.indent_with_tabs = false;
75+
newJsbeautifyConfig.css.indent_char = ' ';
76+
newJsbeautifyConfig.js.indent_with_tabs = false;
77+
newJsbeautifyConfig.js.indent_char = ' ';
78+
}
79+
}
80+
81+
newJsbeautifyConfig.html.js = newJsbeautifyConfig.js;
82+
newJsbeautifyConfig.html.css = newJsbeautifyConfig.css;
83+
84+
return newJsbeautifyConfig;
85+
};

src/js/utils/constants.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export const PRETTIFIED_CODE_MARKER_END = '### HTMLPrettify prettified code end
99

1010
// The source file to be prettified, original source's path and some options.
1111
export const EDITOR_FILE_SYNTAX = process.argv[2] || '';
12-
export const EDITOR_TEXT_FILE_PATH = process.argv[3] || '';
13-
export const ORIGINAL_FILE_PATH = process.argv[4] || '';
14-
export const CONFIG_EXTRA_LOOKUP_PATHS = [process.argv[5] || '', process.argv[6] || ''];
12+
export const EDITOR_INDENT_SIZE = process.argv[3] || '';
13+
export const EDITOR_INDENT_WITH_TABS = process.argv[4] || '';
14+
export const EDITOR_TEXT_FILE_PATH = process.argv[5] || '';
15+
export const ORIGINAL_FILE_PATH = process.argv[6] || '';
16+
export const CONFIG_EXTRA_LOOKUP_PATHS = [process.argv[7] || '', process.argv[8] || ''];

src/py/main.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import os
77

88
from .utils.window_utils import get_pref
9-
from .utils.editor_utils import get_syntax, get_editor_selections_copy, get_editor_folded_contents
9+
from .utils.editor_utils import get_syntax, get_indent_size, get_indent_with_tabs
10+
from .utils.editor_utils import get_editor_selections_copy, get_editor_folded_contents
1011
from .utils.editor_utils import get_entire_buffer_text, get_first_selected_text
1112
from .utils.editor_utils import has_selection
1213
from .utils.editor_utils import force_set_viewport_position
@@ -17,7 +18,12 @@
1718

1819

1920
def main(view, edit):
20-
editor_file_syntax = get_syntax(view) if get_pref("use_syntax") else "?"
21+
editor_file_syntax = get_syntax(view) if get_pref(
22+
"use_editor_syntax") else "?"
23+
editor_indent_size = get_indent_size(view) if get_pref(
24+
"use_editor_indentation") else "?"
25+
editor_indent_with_tabs = get_indent_with_tabs(view) if get_pref(
26+
"use_editor_indentation") else "?"
2127
original_file_path = view.file_name() or "?"
2228

2329
previous_viewport_position = view.viewport_position()
@@ -37,7 +43,8 @@ def main(view, edit):
3743
editor_text_file_path = save_text_to_temp_file(text_to_prettify)
3844

3945
prettified_text = prettify_verbose(view.window(), [
40-
editor_file_syntax, editor_text_file_path, original_file_path
46+
editor_file_syntax, editor_indent_size, editor_indent_with_tabs,
47+
editor_text_file_path, original_file_path
4148
])
4249

4350
os.remove(editor_text_file_path)

src/py/utils/editor_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ def get_syntax(view):
1111
return view.settings().get("syntax")
1212

1313

14+
def get_indent_size(view):
15+
"""Gets the given view's indentation size"""
16+
return str(view.settings().get("tab_size"))
17+
18+
19+
def get_indent_with_tabs(view):
20+
"""Gets the given view's indentation character"""
21+
return str(view.settings().get("use_tab_stops"))
22+
23+
1424
def get_editor_selections_copy(view):
1525
"""Gets a copy of the given view's selections"""
1626
return list(view.sel())

0 commit comments

Comments
 (0)