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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- `--output-format=json` now outputs all (error) messages in JSON format ([#453](https://github.com/JohnnyMorganz/StyLua/issues/453))
- Added WASM build support. Stylua is available on npm for consumption in Node.js or a browser (using a bundler) - https://www.npmjs.com/package/@johnnymorganz/stylua
- Ignore comments will now be respected before fields inside tables ([#448](https://github.com/JohnnyMorganz/StyLua/issues/448))

### Fixed
- [**Luau**] Fixed spacing lost before a comment within a type generic ([#446](https://github.com/JohnnyMorganz/StyLua/issues/446))
Expand Down
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ AST, flagging any syntax errors or possible code semantics changes. This flag ma
where not every file can be examined for spurious formatting.

### Ignoring parts of a file
If there is a specific statement within your file which you wish to skip formatting on, you can precede it with `-- stylua: ignore`,
and it will be skipped over during formatting. This may be useful when there is a specific formatting style you wish to preserve for
a statement. For example:

To skip formatting a particular part of a file, you can add `-- stylua: ignore` before it.
This may be useful if there is a particular style you want to preseve for readability, e.g.:

```lua
-- stylua: ignore
local matrix = {
Expand All @@ -119,7 +120,9 @@ local matrix = {
{ 0, 0, 0 },
}
```
You can also disable formatting over a block of code by using `-- stylua: ignore start` / `-- stylua: ignore end` respectively.

Formatting can also be skipped over a block of code using `-- stylua: ignore start` and `-- stylua: ignore end`:

```lua
local foo = true
-- stylua: ignore start
Expand All @@ -128,8 +131,8 @@ local baz = 0
-- stylua: ignore end
local foobar = false
```
Note: this comment must be preceding a statement (same as `-- stylua: ignore`), and cannot cross block scope boundaries
(i.e. if formatting is disabled, and we exit a block, formatting is automatically re-enabled).

Note that ignoring cannot cross scope boundaries - once a block is exited, formatting will be re-enabled.

### Formatting Ranges
If you only want to format a specific range within a file, you can pass the `--range-start <num>` and/or `--range-end <num>` arguments,
Expand Down
33 changes: 11 additions & 22 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,33 +106,22 @@ impl Context {
}

if let Some(range) = self.range {
let mut in_range = true;

if let Some(start_bound) = range.start {
if let Some(node_start) = node.start_position() {
if node_start.bytes() < start_bound {
in_range = false;
}
match (range.start, node.start_position()) {
(Some(start_bound), Some(node_start)) if node_start.bytes() < start_bound => {
return FormatNode::NotInRange
}
}
_ => (),
};

if let Some(end_bound) = range.end {
if let Some(node_end) = node.end_position() {
if node_end.bytes() > end_bound {
in_range = false;
}
match (range.end, node.end_position()) {
(Some(end_bound), Some(node_end)) if node_end.bytes() > end_bound => {
return FormatNode::NotInRange
}
_ => (),
}

if in_range {
FormatNode::Normal
} else {
FormatNode::NotInRange
}
} else {
// No range provided, therefore always in formatting range
FormatNode::Normal
}

FormatNode::Normal
}

pub fn should_omit_string_parens(&self) -> bool {
Expand Down
8 changes: 7 additions & 1 deletion src/formatters/table.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
context::{create_indent_trivia, create_newline_trivia, Context},
context::{create_indent_trivia, create_newline_trivia, Context, FormatNode},
fmt_symbol,
formatters::{
expression::{format_expression, hang_expression, is_brackets_string},
Expand Down Expand Up @@ -110,6 +110,12 @@ fn format_field(
table_type: TableType,
shape: Shape,
) -> (Field, Vec<Token>) {
match dbg!(ctx.should_format_node(field)) {
FormatNode::Skip => return (field.to_owned(), Vec::new()),
FormatNode::NotInRange => unreachable!("called format_field on a field not in range"),
_ => (),
}

let leading_trivia = match table_type {
TableType::MultiLine => FormatTriviaType::Append(vec![create_indent_trivia(ctx, shape)]),
_ => FormatTriviaType::NoChange,
Expand Down
20 changes: 20 additions & 0 deletions tests/inputs-ignore/ignore-table-field.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local foo = {
-- stylua: ignore
x = 2,
y = 3,
z = " he " ,
}

local bar = {
x = 2,
-- stylua: ignore
y = 3,
z = " he " ,
}

local baz = {
x = 2,
y = 3,
-- stylua: ignore
z = " he " ,
}
5 changes: 5 additions & 0 deletions tests/inputs-ignore/multiline-block-ignore-1.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local foo = bar
-- stylua: ignore start
local bar = baz
-- stylua: ignore end
local bar = baz
6 changes: 6 additions & 0 deletions tests/inputs-ignore/multiline-block-ignore-2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local foo = bar
-- stylua: ignore start
local bar = baz
local bar = baz
-- stylua: ignore end
local bar = baz
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--stylua: ignore start
local a = 1
--stylua: ignore end

--stylua: ignore start
local b = 2
--stylua: ignore end

--stylua: ignore start
local c = 3
--stylua: ignore end

-- Some very large comment

--stylua: ignore start
local d = 4
--stylua: ignore end
5 changes: 5 additions & 0 deletions tests/inputs-ignore/multiline-block-ignore-no-ending.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local foo = bar
-- stylua: ignore start
local bar = baz
local bar = baz
local bar = baz
5 changes: 5 additions & 0 deletions tests/inputs-ignore/multiline-block-ignore-no-starting.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local foo = bar
local bar = baz
local bar = baz
-- stylua: ignore end
local bar = baz
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local foo = bar
do
-- stylua: ignore start
local bar = baz
local bar = baz
end
local bar = baz
8 changes: 8 additions & 0 deletions tests/inputs-ignore/multiline-block-ignore-scope.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local foo = bar
do
-- stylua: ignore start
local bar = baz
-- stylua: ignore end
local bar = baz
end
local bar = baz
3 changes: 3 additions & 0 deletions tests/inputs-ignore/singleline-ignore-1.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
local foo = bar
-- stylua: ignore
local bar = baz
4 changes: 4 additions & 0 deletions tests/inputs-ignore/singleline-ignore-2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
local foo = bar
-- stylua: ignore
local bar = baz
local bar = baz
2 changes: 2 additions & 0 deletions tests/inputs-ignore/singleline-ignore-last-stmt.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- stylua: ignore
return "hi"
5 changes: 5 additions & 0 deletions tests/inputs-ignore/singleline-ignore-stmt-block.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local x = 1
-- stylua: ignore
function foo ()
return x + 1
end
27 changes: 27 additions & 0 deletions tests/snapshots/tests__ignores@ignore-table-field.lua.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: tests/tests.rs
assertion_line: 56
expression: format(&contents)

---
local foo = {
-- stylua: ignore
x = 2,
y = 3,
z = " he ",
}

local bar = {
x = 2,
-- stylua: ignore
y = 3,
z = " he ",
}

local baz = {
x = 2,
y = 3,
-- stylua: ignore
z = " he " ,
}

12 changes: 12 additions & 0 deletions tests/snapshots/tests__ignores@multiline-block-ignore-1.lua.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: tests/tests.rs
assertion_line: 56
expression: format(&contents)

---
local foo = bar
-- stylua: ignore start
local bar = baz
-- stylua: ignore end
local bar = baz

13 changes: 13 additions & 0 deletions tests/snapshots/tests__ignores@multiline-block-ignore-2.lua.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: tests/tests.rs
assertion_line: 56
expression: format(&contents)

---
local foo = bar
-- stylua: ignore start
local bar = baz
local bar = baz
-- stylua: ignore end
local bar = baz

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
source: tests/tests.rs
assertion_line: 56
expression: format(&contents)

---
--stylua: ignore start
local a = 1
--stylua: ignore end

--stylua: ignore start
local b = 2
--stylua: ignore end

--stylua: ignore start
local c = 3
--stylua: ignore end

-- Some very large comment

--stylua: ignore start
local d = 4
--stylua: ignore end

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: tests/tests.rs
assertion_line: 56
expression: format(&contents)

---
local foo = bar
-- stylua: ignore start
local bar = baz
local bar = baz
local bar = baz

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: tests/tests.rs
assertion_line: 56
expression: format(&contents)

---
local foo = bar
local bar = baz
local bar = baz
-- stylua: ignore end
local bar = baz

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: tests/tests.rs
assertion_line: 56
expression: format(&contents)

---
local foo = bar
do
-- stylua: ignore start
local bar = baz
local bar = baz
end
local bar = baz

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: tests/tests.rs
assertion_line: 56
expression: format(&contents)

---
local foo = bar
do
-- stylua: ignore start
local bar = baz
-- stylua: ignore end
local bar = baz
end
local bar = baz

10 changes: 10 additions & 0 deletions tests/snapshots/tests__ignores@singleline-ignore-1.lua.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: tests/tests.rs
assertion_line: 56
expression: format(&contents)

---
local foo = bar
-- stylua: ignore
local bar = baz

11 changes: 11 additions & 0 deletions tests/snapshots/tests__ignores@singleline-ignore-2.lua.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: tests/tests.rs
assertion_line: 56
expression: format(&contents)

---
local foo = bar
-- stylua: ignore
local bar = baz
local bar = baz

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: tests/tests.rs
assertion_line: 56
expression: format(&contents)

---
-- stylua: ignore
return "hi"

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: tests/tests.rs
assertion_line: 56
expression: format(&contents)

---
local x = 1
-- stylua: ignore
function foo ()
return x + 1
end

Loading