diff --git a/JavaScript/Default.sublime-keymap b/JavaScript/Default.sublime-keymap index eac30e9ea7..bb4d409f29 100644 --- a/JavaScript/Default.sublime-keymap +++ b/JavaScript/Default.sublime-keymap @@ -51,4 +51,24 @@ { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true } ] }, + + // Add indented line in square brackets + { "keys": ["enter"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Add Line in Braces.sublime-macro"}, "context": + [ + { "key": "setting.auto_indent" }, + { "key": "selection_empty", "match_all": true }, + { "key": "selector", "operand": "source.js, source.jsx, source.ts, source.tsx" }, + { "key": "preceding_text", "operator": "regex_contains", "operand": "\\[$", "match_all": true }, + { "key": "following_text", "operator": "regex_contains", "operand": "^\\]", "match_all": true } + ] + }, + { "keys": ["keypad_enter"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Add Line in Braces.sublime-macro"}, "context": + [ + { "key": "setting.auto_indent" }, + { "key": "selection_empty", "match_all": true }, + { "key": "selector", "operand": "source.js, source.jsx, source.ts, source.tsx" }, + { "key": "preceding_text", "operator": "regex_contains", "operand": "\\[$", "match_all": true }, + { "key": "following_text", "operator": "regex_contains", "operand": "^\\]", "match_all": true } + ] + }, ] diff --git a/JavaScript/Indentation Rules - Template Strings.tmPreferences b/JavaScript/Indentation Rules - Template Strings.tmPreferences new file mode 100644 index 0000000000..7b80e2402e --- /dev/null +++ b/JavaScript/Indentation Rules - Template Strings.tmPreferences @@ -0,0 +1,29 @@ + + + + scope + string.quoted.other.js + settings + + + + + + increaseIndentPattern + ^[^`]*`\s*$ + + unIndentedLinePattern + ^[^`]*$ + + + diff --git a/JavaScript/Indentation Rules - Values.tmPreferences b/JavaScript/Indentation Rules - Values.tmPreferences new file mode 100644 index 0000000000..02e6057a98 --- /dev/null +++ b/JavaScript/Indentation Rules - Values.tmPreferences @@ -0,0 +1,72 @@ + + + + scope + + source.js meta.mapping, source.js meta.sequence, + source.jsx meta.mapping, source.jsx meta.sequence, + source.ts meta.mapping, source.ts meta.sequence, + source.tsx meta.mapping, source.tsx meta.sequence + + settings + + + decreaseIndentPattern + (?x) + # When an object is closed, but not opened + ( + ^ + ( + # Consume strings + "(?:[^"\\]|\\.)*" + | + # Consume all chars that don't start a string, comment or + # open an object on this line + [^"/{\n] + )* + \}.*$ + ) + | + # When an array is closed by itself on a line (interacts with indentSquareBrackets) + ( + ^(.*\*/)?\s*\].*$ + ) + + increaseIndentPattern + (?x) + # When an object is opened, but not closed + ( + ^.*\{ + ( + # Consume strings + "(?:[^"\\]|\\.)*" + | + # Consume all chars that don't start a string, comment or + # end the object that was opened on this line + [^"/}] + )* + # Stop matching at the end of the line, or once we hit a comment + ($|/[/*]) + ) + | + # When an array is opened, but not closed + ( + ^.*\[ + ( + # Consume strings + "(?:[^"\\]|\\.)*" + | + # Consume all chars that don't start a string, comment or + # end the array that was opened on this line + [^"/\]] + )* + # Stop matching at the end of the line, or once we hit a comment + ($|/[/*]) + ) + + + bracketIndentNextLinePattern + + + + diff --git a/JavaScript/Indentation Rules.tmPreferences b/JavaScript/Indentation Rules.tmPreferences index 90c4f24bde..b3e2755cb2 100644 --- a/JavaScript/Indentation Rules.tmPreferences +++ b/JavaScript/Indentation Rules.tmPreferences @@ -1,23 +1,41 @@ + scope - source.js, source.ts, source.jsx, source.tsx + + source.js - source.js meta.string, + source.js meta.function - source.js meta.string, + source.jsx - source.jsx meta.string, + source.jsx meta.function - source.jsx meta.string, + source.ts - source.ts meta.string, + source.ts meta.function - source.ts meta.string, + source.tsx - source.tsx meta.string, + source.tsx meta.function - source.tsx meta.string + settings decreaseIndentPattern @@ -30,11 +48,11 @@ # but exclude lines such as `extern "C" {` .* \{ (?: \s* /\*.*\*/ )* \s* (?: //.* )? $ # indent after opening tagged template: e.g.: "css`" - | .* \w+ \s* ` + # see: Indentation Rules - Template Strings.tmPreferences # indent after `case ... :` - | case\b + | case\b.*: # indent after `default:` - | default\b + | default\s*: ) ]]> @@ -72,8 +90,6 @@ ) ]]> - indentSquareBrackets - diff --git a/JavaScript/tests/syntax_test_js_indent_common.js b/JavaScript/tests/syntax_test_js_indent_common.js index 132c8fcf0d..e81f8b2ad2 100644 --- a/JavaScript/tests/syntax_test_js_indent_common.js +++ b/JavaScript/tests/syntax_test_js_indent_common.js @@ -1,5 +1,88 @@ // SYNTAX TEST reindent-unchanged "Packages/JavaScript/JavaScript.sublime-syntax" +/* + * Export definitions + */ + +export default { + default: 'value', + case() { + const map1 = { + default: 'value' + } + const list1 = [ + default + ] + + if (foo == true) + return 1 + else if (bar == true) + return 2 + else + return 3 + + switch (map1) { + case null: + return 0 + default: + const map2 = { + default: 'value' + } + const list2 = [ + default, + () => { + switch (map2) { + default: + return 0 + case null: + return 1 + } + } + ] + } + } +} + +/* + * mapping definitions + */ + +const maps = { + case: "case", + default: "default", + switch: "switch", + if: "if", + elif: "elif", + else: "else", + function: "function", + object: { + "key": "value", + }, + list1: [ + "switch", + "case" + ], + list3: ["value1", "value2"] +}; + +/* + * list definitions + */ + +const list = [ + case, + default, + switch, + if, + elif, + else, + [ + case, + default + ], + ["value1", "value2"] +] + /** * This is my first JavaScript program. * This will print 'Hello World' as the output @@ -548,16 +631,12 @@ function testIfElseIndentationWithBracesAndComment(v) { function testSwitchCaseIndentation(v) { switch (s) { - case case: - case break case: break case "(": break case ")": break; case ":": break; case ";": break; - case - break; case: break; case ":" @@ -982,6 +1061,8 @@ function testWhileIndentationWithBracesAndComments(v) { * CSS Templates */ +var style = css`tr{color:red}`; + var style = css` tr, p { background: red solid; @@ -992,6 +1073,8 @@ var style = css` * HTML Templates */ +var html = html`

${content}

`; + var html = html`