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

[JavaScript] Code and Data specific indentation rules #4015

Merged
merged 14 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
20 changes: 20 additions & 0 deletions JavaScript/Default.sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,24 @@
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
]
},

// Add indented line in square brackets
FichteFoll marked this conversation as resolved.
Show resolved Hide resolved
{ "keys": ["enter"], "command": "insert_snippet", "args": {"contents": "\n\t$0\n"}, "context":
[
{ "key": "setting.auto_indent" },
{ "key": "selector", "operand": "source.js, source.jsx, source.ts, source.tsx" },
{ "key": "selection_empty", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\[$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\]", "match_all": true }
]
},
{ "keys": ["keypad_enter"], "command": "insert_snippet", "args": {"contents": "\n\t$0\n"}, "context":
[
{ "key": "setting.auto_indent" },
{ "key": "selector", "operand": "source.js, source.jsx, source.ts, source.tsx" },
{ "key": "selection_empty", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\[$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\]", "match_all": true }
]
},
]
77 changes: 77 additions & 0 deletions JavaScript/Indentation Rules - Values.tmPreferences
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>scope</key>
<string>
(source.js meta.mapping, source.js meta.sequence) - source.js meta.function,
source.js meta.function meta.mapping,
source.js meta.function meta.sequence,
(source.jsx meta.mapping, source.jsx meta.sequence) - source.jsx meta.function,
source.jsx meta.function meta.mapping,
source.jsx meta.function meta.sequence,
(source.ts meta.mapping, source.ts meta.sequence) - source.ts meta.function,
source.ts meta.function meta.mapping,
source.ts meta.function meta.sequence,
(source.tsx meta.mapping, source.tsx meta.sequence) - source.tsx meta.function,
source.tsx meta.function meta.mapping,
source.tsx meta.function meta.sequence
</string>
<key>settings</key>
<dict>
<!-- NOTE: Keep in sync with JSON! -->
<key>decreaseIndentPattern</key>
<string>(?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*\].*$
)
</string>
<key>increaseIndentPattern</key>
<string>(?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
($|/[/*])
)
</string>
</dict>
</dict>
</plist>
25 changes: 17 additions & 8 deletions JavaScript/Indentation Rules.tmPreferences
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@
<plist version="1.0">
<dict>
<key>scope</key>
<string>source.js, source.ts, source.jsx, source.tsx</string>
<string>
source.js - source.js meta.mapping - source.js meta.sequence,
source.js meta.function,
source.jsx - source.jsx meta.mapping - source.jsx meta.sequence,
source.jsx meta.function,
source.ts - source.ts meta.mapping - source.ts meta.sequence,
source.ts meta.function,
source.tsx - source.tsx meta.mapping - source.tsx meta.sequence
source.tsx meta.function
</string>
<key>settings</key>
<dict>
<key>decreaseIndentPattern</key>
<string><![CDATA[(?x)
# line beginning with whitespace or block comments
# line beginning with whitespace
^ (.*\*/)? \s*
(?:
# dedent closing braces
\}
# dedent closing brackets
| \]
# detent `case ... :`
| case\b
| case\b.*:
# detent `default:`
| default\b
| default\s*:
)
]]></string>

Expand All @@ -28,9 +39,9 @@
# but exclude lines such as `extern "C" {`
.* \{ (?: \s* /\*.*\*/ )* \s* (?: //.* )? $
# indent after `case ... :`
| case\b
| case\b.*:
# indent after `default:`
| default\b
| default\s*:
)
]]></string>

Expand Down Expand Up @@ -68,8 +79,6 @@
)
]]></string>

<key>indentSquareBrackets</key>
<true/>
</dict>
</dict>
</plist>
87 changes: 83 additions & 4 deletions JavaScript/tests/syntax_test_js_indent_common.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 ":"
Expand Down
42 changes: 42 additions & 0 deletions JavaScript/tests/syntax_test_js_indent_embed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SYNTAX TEST reindent-unchanged "Packages/Markdown/Markdown.sublime-syntax"

```js
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
}
}
]
}
}
}
```
43 changes: 43 additions & 0 deletions JavaScript/tests/syntax_test_js_indent_embed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SYNTAX TEST reindent-unchanged "Packages/PHP/PHP.sublime-syntax"

<?php
$var = <<<JAVASCRIPT
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
}
}
]
}
}
}
JAVASCRIPT
Loading