### YAS Style Argument Alignment Example Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/yas_style.md Demonstrates how arguments are aligned to just after the start of the opener ([, {, (, etc.) in YASStyle. ```julia function_call(arg1, arg2) ``` -------------------------------- ### Install pre-commit and Add Hook Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Install the pre-commit tool and then add the JuliaFormatter hook to your repository. ```bash # Install pre-commit and the hook pip install pre-commit pre-commit install ``` -------------------------------- ### Install JuliaFormatter CLI Tool Source: https://github.com/domluna/juliaformatter.jl/blob/master/README.md Install the `jlfmt` command-line executable using the Julia package manager. ```julia pkg> app add JuliaFormatter ``` -------------------------------- ### Install JuliaFormatter Package Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Installs the JuliaFormatter package using Pkg. This is a prerequisite for using the formatting tools. ```julia using Pkg Pkg.add("JuliaFormatter") ``` -------------------------------- ### Install JuliaFormatter.jl Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md Use the Julia package manager to add JuliaFormatter.jl to your environment. ```julia ] add JuliaFormatter ``` -------------------------------- ### Basic Configuration Example Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/config.md Specify indentation and margin in a TOML configuration file. Files in the same directory or subdirectories will use these settings. ```toml indent = 2 margin = 100 ``` -------------------------------- ### Style Guides Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Information on the built-in style guides provided by JuliaFormatter. ```APIDOC ## Style Guides JuliaFormatter provides five built-in style types that encode opinionated defaults. Each style can be passed as the second argument to any formatting function or declared in `.JuliaFormatter.toml`. ### Usage ```julia using JuliaFormatter src = """ function longname(argument1, argument2, argument3, argument4, argument5) argument1 + argument2 end """ # DefaultStyle — 4-space indent, 92-char margin, trailing commas on nesting println(format_text(src, DefaultStyle())) # YASStyle — aligns args to opening paren, closer sticks to last arg println(format_text(src, YASStyle())) # BlueStyle — follows https://github.com/invenia/BlueStyle println(format_text(src, BlueStyle())) # SciMLStyle — SciML ecosystem conventions; supports yas_style_nesting option println(format_text(src, SciMLStyle(); yas_style_nesting = true)) ``` ``` -------------------------------- ### Install jlfmt CLI Tool Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Install the standalone jlfmt CLI tool, which wraps the Julia API and is compatible with Runic.jl conventions. ```bash # Install the app once julia -e 'using Pkg; Pkg.add("JuliaFormatter"); Pkg.app("add", "JuliaFormatter")' ``` -------------------------------- ### Install JuliaFormatter Source: https://github.com/domluna/juliaformatter.jl/blob/master/README.md Use the Julia package manager to add JuliaFormatter to your environment. ```julia pkg> add JuliaFormatter ``` -------------------------------- ### Invoke JuliaFormatter without Installation Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/cli.md Run the formatter directly using the `julia -m` command without prior installation. ```bash julia -m JuliaFormatter [] ... ``` -------------------------------- ### Install JuliaFormatter Pkg App Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/integrations.md Before using the pre-commit hook, you need to install the `jlfmt` Pkg app. This command adds JuliaFormatter as a package application. ```julia pkg> app add JuliaFormatter ``` -------------------------------- ### Install Onda.jl for Development Source: https://github.com/domluna/juliaformatter.jl/blob/master/test/files/Onda.jl/README.md Use this command to install Onda.jl in development mode, placing it in the default Julia package development directory. ```julia julia -e 'using Pkg; Pkg.develop(PackageSpec(url="https://github.com/beacon-biosignals/Onda.jl"))' ``` -------------------------------- ### Join Lines Based on Source Example (False) Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md When false, lines are formatted to fit within the maximum margin, potentially joining arguments onto a single line. ```julia function foo(arg1, arg2, arg3 ) body end ``` ```julia function foo(arg1, arg2, arg3) body end ``` -------------------------------- ### YAS Style Nesting Example (True) Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md With `yas_style_nesting = true`, the SciMLStyle adopts YASStyle nesting rules for function arguments. ```julia # With `yas_style_nesting = true` function my_large_function(argument1, argument2, argument3, argument4, argument5, x, y, z) foo(x) + goo(y) end ``` -------------------------------- ### Example of Identical FST from Different Source Code Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/how_it_works.md Syntactically identical Julia code, regardless of initial whitespace, produces the same FST. This demonstrates the formatter's ability to normalize code structure. ```julia # p1.jl a = foo(a, b, c,d) ``` ```julia # p2.jl a = foo(a, b, c,d) ``` ```julia # fst output a = foo(a, b, c, d) ``` -------------------------------- ### Join Lines Based on Source Example (True) Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md When true, lines are joined as they appear in the original source file, preserving line breaks even if arguments could fit on the same line. ```julia function foo(arg1, arg2, arg3, ) end ``` -------------------------------- ### For-In Replacement Example Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md Replaces the default 'in' keyword with '∈' or '=' when `always_for_in` is true. ```julia for a = 1:10 end # formatted with always_for_in = true, for_in_replacement = "∈" for a ∈ 1:10 end ``` -------------------------------- ### Variable Call Indent Example Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md Allows calls without aligning to the opening parenthesis. The default is an empty list. ```julia # Allowed with and without `Dict in variable_call_indent` Dict{Int, Int}(1 => 2, 3 => 4) # Allowed when `Dict in variable_call_indent`, but # will be changed to the first example when `Dict ∉ variable_call_indent`. Dict{Int, Int}( 1 => 2, 3 => 4) ``` -------------------------------- ### Surround Type Parameters Example Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md Surrounds type parameters with curly brackets if they are not already present when set to true. ```julia function func(...) where TPARAM end -> function func(...) where {TPARAM} end ``` -------------------------------- ### Indent Submodule Example Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md When `indent_submodule` is true, nested modules within the same file are indented. ```julia module A a = 1 module B b = 2 module C c = 3 end end d = 4 end ``` ```julia module A a = 1 module B b = 2 module C c = 3 end end d = 4 end ``` -------------------------------- ### YAS Style Nesting Example (False) Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md With `yas_style_nesting = false`, the SciMLStyle uses default nesting rules for function arguments. ```julia # With `yas_style_nesting = false` function my_large_function(argument1, argument2, argument3, argument4, argument5, x, y, z) foo(x) + goo(y) end ``` -------------------------------- ### Trailing Comma Example Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md Demonstrates how trailing commas are added after the final argument during nesting when the closing punctuation is on the next line. ```julia funccall(arg1, arg2, arg3) ``` ```julia funccall( arg1, arg2, arg3, # trailing comma added after `arg3` (final argument) !!! ) ``` -------------------------------- ### Separate Keyword Arguments with Semicolon Example Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md When true, keyword arguments in a function call are separated by a semicolon instead of a comma. ```julia f(a, b=1) -> f(a; b=1) ``` -------------------------------- ### YAS Style Nesting Behavior Example Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/yas_style.md Illustrates that nesting (line breaks) in YASStyle only occurs when the margin of the next argument exceeds the maximum limit. ```julia function_call(arg1, arg2, arg3) ``` -------------------------------- ### Example of Custom Assignment Alignment Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/custom_alignment.md When `align_assignment` is enabled, the formatter aligns assignment operators if there's more than one whitespace before them. This alignment applies to all expressions in the same code block. ```julia const variable1 = 1 const var2 = 2 const var3 = 3 const var4 = 4 const var5 = 5 ``` ```julia const variable1 = 1 const var2 = 2 const var3 = 3 const var4 = 4 const var5 = 5 ``` ```julia const variable1 = 1 const variable2 = 2 const var3 = 3 const var4 = 4 const var5 = 5 ``` -------------------------------- ### Recursively Format Files and Directories Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Use `format` as the main entry point for formatting entire projects. It accepts a path string, a collection of paths, or a `Module`, walking directories recursively and respecting configuration files. ```julia using JuliaFormatter # Format everything under the current directory format(".") ``` ```julia # Format a specific directory format("src/") ``` ```julia # Format multiple paths in one call format(["src/", "test/", "docs/"]) ``` ```julia # Format with options (overridden by any .JuliaFormatter.toml found on the path) format("src/"; indent = 2, margin = 100, remove_extra_newlines = true) ``` ```julia # Format a loaded module (resolves to pkgdir(MyPkg)) using MyPkg format(MyPkg) ``` ```julia # Use SciMLStyle across the whole project using JuliaFormatter: SciMLStyle format(".", SciMLStyle()) ``` ```julia # Returns true when every file was already formatted all_clean = format("src/") @assert all_clean == true # passes in CI when the repo is clean ``` -------------------------------- ### Display Help Information Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/cli.md Show all available options and usage instructions for the `jlfmt` command. ```julia using JuliaFormatter # hide JuliaFormatter.main(["--help"]); # hide ``` -------------------------------- ### Preview Formatted Output Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/cli.md Use `jlfmt` to see how a file would be formatted without making changes. ```bash # Preview formatted output jlfmt src/file.jl ``` -------------------------------- ### Prioritize Configuration File Settings Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/cli.md Make settings from the configuration file take precedence over command-line options. This is useful for language server integration. ```bash jlfmt --prioritize-config-file --indent=2 src/file.jl ``` -------------------------------- ### format Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Recursively formats files and directories. This is the main entry point for formatting entire projects. ```APIDOC ## `format` — Recursively format files and directories The main entry point for formatting entire projects. Accepts a path string, a collection of paths, or even a `Module`. Walks directories recursively, respects `.JuliaFormatter.toml` config files found along the way, and formats every `.jl` / `.md` / `.jmd` / `.qmd` file it encounters. ### Usage ```julia using JuliaFormatter # Format everything under the current directory format(".") # Format a specific directory format("src/") # Format multiple paths in one call format(["src/", "test/", "docs/"]) # Format with options (overridden by any .JuliaFormatter.toml found on the path) format("src/"; indent = 2, margin = 100, remove_extra_newlines = true) # Format a loaded module (resolves to pkgdir(MyPkg)) using MyPkg format(MyPkg) # Use SciMLStyle across the whole project using JuliaFormatter: SciMLStyle format(".", SciMLStyle()) # Returns true when every file was already formatted all_clean = format("src/") ``` ``` -------------------------------- ### Format Project Automatically with Config File Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Once a .JuliaFormatter.toml file is present, the format function automatically picks up the configuration. ```julia using JuliaFormatter format(".") # picks up config automatically ``` -------------------------------- ### Formatting Option: import_to_using Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md When true, rewrites 'import' expressions to 'using' expressions, with exceptions for 'as' clauses and '@everywhere' contexts. ```julia import A import A, B, C ``` ```julia using A: A using A: A using B: B using C: C ``` ```julia import Base.Threads as th ``` ```julia @everywhere import A, B ``` -------------------------------- ### Use JuliaFormatter CLI Tool Source: https://github.com/domluna/juliaformatter.jl/blob/master/README.md Utilize the `jlfmt` command-line tool for various formatting tasks, including in-place modification, checking, and diffing. ```bash # Format a file and write to stdout jlfmt src/file.jl # Format a file in place jlfmt --inplace src/file.jl # Check if all files in a directory are already formatted with verbose mode jlfmt --check -v src/ # Format all files in a directory with multiple threads jlfmt --threads=6 -- --inplace -v src/ # Show diff without modifying files jlfmt --diff src/file.jl ``` -------------------------------- ### Format Stdin with Specified Configuration Directory Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/cli.md Format code piped to stdin, using a configuration file found by searching from a specified directory upwards. ```bash # Format stdin using config from ./src directory echo 'f(x,y)=x+y' | jlfmt --config-dir=./src ``` ```bash # Useful in editor integrations to respect project config cat file.jl | jlfmt --config-dir=$(dirname file.jl) ``` -------------------------------- ### Show Diff Without Modifying Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/cli.md Display the differences between the current code and the formatted version without altering the original file. ```bash # Show diff without modifying jlfmt --diff src/file.jl ``` -------------------------------- ### Check Files for Formatting Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/cli.md Verify if files in a directory are already formatted using verbose mode. ```bash # Check if files are already formatted with verbose mode jlfmt --check -v src/ ``` -------------------------------- ### Configure JuliaFormatter with .JuliaFormatter.toml Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Persist formatting options by placing a .JuliaFormatter.toml file in your project directory. The deepest config file found upwards from a file's location is used. ```toml # .JuliaFormatter.toml style = "blue" # "default" | "yas" | "blue" | "sciml" | "minimal" indent = 4 margin = 92 always_for_in = true for_in_replacement = "∈" # "in" | "=" | "∈" remove_extra_newlines = true import_to_using = false always_use_return = false format_docstrings = true short_to_long_function_def = true trailing_comma = true normalize_line_endings = "unix" # "unix" | "windows" | "auto" # Alignment options align_assignment = true align_struct_field = true align_pair_arrow = true align_matrix = false # Ignore specific files / directories (glob patterns supported) ignore = ["generated/", "vendor/", "*.jmd"] ``` -------------------------------- ### Run pre-commit Hooks Manually Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Execute all configured pre-commit hooks, including JuliaFormatter, against all files in the repository. ```bash # Run against all files manually pre-commit run --all-files ``` -------------------------------- ### Override Configuration with Command-Line Options Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/cli.md Use command-line arguments to override settings specified in a `.JuliaFormatter.toml` configuration file. ```bash # Use indent=2 even if config file specifies indent=4 jlfmt --indent=2 src/file.jl ``` -------------------------------- ### Format Files In-Place with Multiple Threads Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/cli.md Format files directly in the file system using multiple threads for faster processing. ```bash # Format files in-place with multiple threads jlfmt --threads=6 -- --inplace -v src/ ``` -------------------------------- ### Format Julia Code with MinimalStyle Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Use MinimalStyle for formatting to apply minimal transformations, staying close to the original source code. ```julia println(format_text(src, MinimalStyle())) ``` -------------------------------- ### Unnest Short Function Definition Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/style.md Demonstrates the unnesting process for short function definitions, potentially joining lines if margin allows. ```julia # short function def function foo(arg1, arg2, arg3) = body -> function foo(arg1, arg2, arg3) = body -> function foo( arg1, arg2, arg3, ) = body # If the margin allows it, `body` will be joined back # with the previous line. function foo( arg1, arg2, arg3, ) = body ``` -------------------------------- ### Add JuliaFormatter.jl to pre-commit Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/integrations.md Add this configuration to your `.pre-commit-config.yaml` file to enable JuliaFormatter.jl integration. Ensure you specify the correct release tag. ```yaml repos: # ... other repos you may have - repo: "https://github.com/domluna/JuliaFormatter.jl" rev: "v1.0.18" # or whatever the desired release is hooks: - id: "julia-formatter" # ... other repos you may have ``` -------------------------------- ### Format Julia Source String with Custom Style Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Demonstrates formatting a Julia source string using a specific named style like `YASStyle`. ```julia using JuliaFormatter src = """ function longname(argument1, argument2, argument3, argument4, argument5) argument1 + argument2 end """ # DefaultStyle — 4-space indent, 92-char margin, trailing commas on nesting println(format_text(src, DefaultStyle())) # YASStyle — aligns args to opening paren, closer sticks to last arg println(format_text(src, YASStyle())) # function longname(argument1, argument2, argument3, # argument4, argument5) # argument1 + argument2 # end # BlueStyle — follows https://github.com/invenia/BlueStyle println(format_text(src, BlueStyle())) # SciMLStyle — SciML ecosystem conventions; supports yas_style_nesting option println(format_text(src, SciMLStyle(); yas_style_nesting = true)) ``` -------------------------------- ### jlfmt: Pass Formatting Options via CLI Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Specify formatting options like indent and margin directly on the command line when using jlfmt. ```bash # Pass formatting options directly on the CLI jlfmt --indent=2 --margin=100 --inplace src/file.jl ``` -------------------------------- ### Blue Style TOML Configuration Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/blue_style.md Configure the Blue Style formatter using a TOML file. Set the style to 'blue' or override specific settings. ```toml style = "blue" ``` ```toml style = "blue" remove_extra_newlines = false ``` -------------------------------- ### Ignoring Specific Files and Directories Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/config.md Define patterns for files and directories to be ignored by the formatter. These entries will be reported as already formatted. ```toml ignore = ["file.jl", "directory", "file_*.jl"] ``` -------------------------------- ### jlfmt: Format Directory with Multiple Threads Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Format an entire directory using multiple worker threads by specifying the --threads option. ```bash # Format a directory using 6 worker threads jlfmt --threads=6 -- --inplace src/ ``` -------------------------------- ### jlfmt: Format from stdin with Config Directory Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Format code piped to jlfmt from standard input, specifying a configuration directory to resolve .JuliaFormatter.toml. ```bash # Format from stdin, looking for .JuliaFormatter.toml in ./src echo 'f(x,y)=x+y' | jlfmt --config-dir=./src ``` -------------------------------- ### jlfmt: Prioritize Config File Over CLI Flags Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Use the --prioritize-config-file flag to ensure that settings in the configuration file are respected over command-line flags. ```bash # Let the config file win instead of CLI flags jlfmt --prioritize-config-file --indent=2 src/file.jl ``` -------------------------------- ### FST Transformation: Try-Catch-Finally Block Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/how_it_works.md Illustrates how a complex block like try-catch-finally is represented in the FST, showing the transformation from a compact source to a more structured, multi-line FST output. ```julia # original source try a1;a2 catch e b1;b2 finally c1;c2 end -> # printed FST try a1 a2 catch e b1 b2 finally c1 c2 end ``` -------------------------------- ### jlfmt: Format File In-Place Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Use the --inplace flag with jlfmt to format the specified file directly. ```bash # Format file in place jlfmt --inplace src/file.jl ``` -------------------------------- ### Apply `import_to_using` Transformation Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Converts `import` statements to `using` statements when `import_to_using` is true. ```julia format_text("import A\nimport B, C\n"; import_to_using = true) ``` -------------------------------- ### Non-Default Style Configuration Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/config.md Set a non-default formatting style like 'yas' in the configuration file. Available styles include 'default', 'yas', 'blue', 'sciml', and 'minimal'. ```toml style = "yas" ``` -------------------------------- ### Toggle Formatting On and Off Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/skipping_formatting.md Use `#! format: off` to disable formatting from that point onwards and `#! format: on` to re-enable it. Ensure these directives are on their own lines with exact whitespace. ```julia #! format: off # Turns off formatting from this point onwards ... #! format: on # Turns formatting back on from this point onwards ``` -------------------------------- ### jlfmt: Show Diff Without Modifying Files Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Use the --diff flag to preview the formatting changes without altering the original files. ```bash # Show a diff without modifying files jlfmt --diff src/file.jl ``` -------------------------------- ### Samples Operations Source: https://github.com/domluna/juliaformatter.jl/blob/master/test/files/Onda.jl/docs/src/index.md Functions for encoding, decoding, and accessing sample data within a Samples object. ```APIDOC ## `Samples` ### Description Functions for interacting with `Samples` objects, including encoding, decoding, and accessing channel information. ### Methods - `Samples` - `channel` - `channel_count` - `sample_count` - `encode` - `encode!` - `decode` - `decode!` ``` -------------------------------- ### FST Nesting: Breaking Long Function Calls Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/how_it_works.md Demonstrates how the FST is nested to adhere to margin specifications, specifically showing a long function call being broken into multiple lines for readability. ```julia begin foo = funccall(argument1, argument2, ..., argument120) # way over margin limit !!! end ``` ```julia begin foo = funccall( argument1, argument2, ..., argument120 ) # way over margin limit !!! end ``` -------------------------------- ### Exception to Join Lines Based on Source Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md This formatting applies even when `join_lines_based_on_source` is true for if-elseif-else structures. ```julia if a body1 elseif b body2 else body3 end ``` ```julia if a body1 elseif b body2 else body3 end ``` -------------------------------- ### Format Julia Code Source: https://github.com/domluna/juliaformatter.jl/blob/master/README.md Quickly format Julia code recursively in a directory, a single file, or a string. ```julia julia> using JuliaFormatter # Recursively formats all Julia files in the current directory julia> format(".") # Formats an individual file julia> format_file("foo.jl") # Formats a string (contents of a Julia file) julia> format_text(str) ``` -------------------------------- ### Formatting Option: long_to_short_function_def Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md Transforms long function definitions to short ones if the short form does not exceed the maximum margin. ```julia function f(arg2, arg2) body end ``` ```julia f(arg1, arg2) = body ``` -------------------------------- ### Apply `always_for_in` and `for_in_replacement` Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Transforms `for i = 1:10` to `for i ∈ 1:10` when `always_for_in` is true and `for_in_replacement` is set to `"∈"`. ```julia using JuliaFormatter format_text("for i = 1:10\nend\n"; always_for_in = true, for_in_replacement = "∈") ``` -------------------------------- ### Apply SciML Style Directly in Julia Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/sciml_style.md Apply the SciMLStyle directly to a file using the `format` function. You can also pass keyword arguments to override specific settings. ```julia format("file.jl", SciMLStyle()) ``` ```julia format("file.jl", SciMLStyle(), remove_extra_newlines=false) ``` -------------------------------- ### Configure JuliaFormatter with SciML Style Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/sciml_style.md Use the `.JuliaFormatter.toml` file to set the default style to 'sciml'. You can also override specific settings like `remove_extra_newlines`. ```toml style = "sciml" ``` ```toml style = "sciml" remove_extra_newlines = false ``` -------------------------------- ### Format a Julia File on Disk Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Use `format_file` to format a Julia, Markdown, or Quarto file. Returns `true` if the file was already correctly formatted, `false` if it was changed. By default, the file is overwritten in place. ```julia using JuliaFormatter # Format in place (default) already_ok = format_file("src/mymodule.jl") println(already_ok) # false if the file was modified ``` ```julia # Dry-run: check without overwriting already_ok = format_file("src/mymodule.jl"; overwrite = false) ``` ```julia # Verbose mode — prints each file name as it is processed format_file("src/mymodule.jl"; verbose = true) ``` ```julia # Also format Markdown files (Julia code blocks inside ```.julia``` fences) format_file("docs/guide.md"; format_markdown = true) ``` ```julia # Use BlueStyle using JuliaFormatter: BlueStyle format_file("src/mymodule.jl", BlueStyle()) ``` -------------------------------- ### Formatting Option: always_use_return Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md When true, prepends 'return' to the last expression in function definitions, macro definitions, and do blocks where applicable. ```julia function foo() expr1 expr2 end ``` ```julia function foo() expr1 return expr2 end ``` -------------------------------- ### format_file Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Formats a specified file on disk. Returns true if the file was already formatted, false otherwise. By default, the file is overwritten in place. ```APIDOC ## `format_file` — Format a file on disk Formats a `.jl`, `.md`, `.jmd`, or `.qmd` file. Returns `true` if the file was already correctly formatted, `false` if it was changed. By default the file is overwritten in place. ### Usage ```julia using JuliaFormatter # Format in place (default) already_ok = format_file("src/mymodule.jl") # Dry-run: check without overwriting already_ok = format_file("src/mymodule.jl"; overwrite = false) # Verbose mode — prints each file name as it is processed format_file("src/mymodule.jl"; verbose = true) # Also format Markdown files (Julia code blocks inside ```.julia``` fences) format_file("docs/guide.md"; format_markdown = true) # Use BlueStyle using JuliaFormatter: BlueStyle format_file("src/mymodule.jl", BlueStyle()) ``` ``` -------------------------------- ### Formatting Option: short_to_long_function_def Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md Transforms short function definitions to long ones if they exceed the maximum margin or if force_long_function_def is true. ```julia f(arg1, arg2) = body ``` ```julia function f(arg2, arg2) body end ``` -------------------------------- ### Format Simple Function Definition Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/style.md Short function definitions without arguments are formatted into a single line. ```julia function foo end -> function foo end ``` -------------------------------- ### Control Formatting with Special Comments in Julia Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md Illustrates how to use `#! format: off` and `#! format: on` comments to selectively disable and re-enable code formatting for specific sections. This is useful for preserving the original formatting of critical code blocks. ```julia # this should be formatted a = f(aaa, bbb, ccc) # this should not be formatted #! format: off a = f(aaa, bbb,ccc) c = 102000 d = @foo 10 20 e = "what the foocho" #! format: on # this should be formatted a = f(aaa, bbb, ccc) # ok ``` -------------------------------- ### Format Indexing Expression Spacing Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/style.md Indexing expressions with binary operations inside are formatted with minimal whitespace. ```julia list[a + b] -> list[a+b] ``` -------------------------------- ### Add decimal point and zero for Float32 literals Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/transforms.md For `Float32` literals without a decimal point, `.0` is added, converting `1f0` to `1.0f0`. ```julia a = 1f0 -> a = 1.0f0 ``` -------------------------------- ### Convert Short Circuit to If Expression Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md Converts short-circuit expressions (|| and &&) into equivalent if statements. ```julia function foo(a, b) a || return "bar" "hello" b && return "ooo" end BECOMES function foo(a, b) if !(a) return "bar" end "hello" if b return "ooo" else false end end ``` -------------------------------- ### Format Binary Operation Spacing Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/style.md Binary operations are placed on a single line and separated by whitespace. ```julia a+b -> a + b ``` -------------------------------- ### Apply `short_to_long_function_def` Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Transforms short function definitions (`f(a, b) = a + b`) to long form (`function f(a, b) a + b end`) when the definition exceeds the specified `margin`. ```julia format_text("f(a, b) = a + b\n"; short_to_long_function_def = true, margin = 5) ``` -------------------------------- ### Configure JuliaFormatter as a pre-commit Hook Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Integrate JuliaFormatter into your Git workflow by adding it as a pre-commit hook to automatically enforce code formatting on commits. ```yaml # .pre-commit-config.yaml repos: - repo: "https://github.com/domluna/JuliaFormatter.jl" rev: "v1.0.18" # replace with the desired release tag hooks: - id: "julia-formatter" ``` -------------------------------- ### Apply `surround_whereop_typeparameters` Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Adds curly braces around type parameters in `where` clauses, transforming `where T` to `where {T}` when `surround_whereop_typeparameters` is true. ```julia format_text("foo(x::T) where T = x\n"; surround_whereop_typeparameters = true) ``` -------------------------------- ### Formatting Option: whitespace_in_kwargs Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md When true, surrounds '=' in keyword arguments with whitespace. An exception is made if the LHS ends with '!', to preserve semantics. ```julia f(; a=4) ``` ```julia f(; a = 4) ``` -------------------------------- ### Format Julia Source String with Default Style Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Use `format_text` to format a Julia source code string. All formatting options can be passed as keyword arguments. The function validates that the formatted output is still parseable before returning it. ```julia using JuliaFormatter # Basic formatting with default style src = """ function foo(a,b,c) a+b end """ println(format_text(src)) # Output: # function foo(a, b, c) # a + b # end ``` ```julia # Custom indent and margin println(format_text(src; indent = 2, margin = 60)) ``` ```julia # Enable several transforms code = """ import A, B x |> f struct T field end """ println(format_text(code; import_to_using = true, # rewrite import A -> using A: A pipe_to_function_call = true, # rewrite x |> f -> f(x) always_use_return = false, annotate_untyped_fields_with_any = true, # field -> field::Any )) # Output: # using A: A # using B: B # f(x) # struct T # field::Any # end ``` ```julia # Use a named style using JuliaFormatter: YASStyle println(format_text(src, YASStyle())) ``` -------------------------------- ### jlfmt: Print Formatted Output to stdout Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Use jlfmt to print formatted code to standard output without modifying the original file. ```bash # Print formatted output to stdout (no file modification) jlfmt src/file.jl ``` -------------------------------- ### Nest Binary Operation Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/style.md Binary operations are nested back-to-front when exceeding line limits. ```julia arg1 + arg2 -> arg1 + arg2 ``` -------------------------------- ### Format Struct Definition with Fields Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/style.md Struct definitions with fields are formatted with consistent indentation. ```julia struct Foo{A, B} a::A b::B end -> struct Foo{A,B} a::A b::B end ``` -------------------------------- ### Apply `long_to_short_function_def` Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Converts long function definitions to short form (`f(a) = a`) when `long_to_short_function_def` is true. ```julia format_text("function f(a)\n a\nend\n"; long_to_short_function_def = true) ``` -------------------------------- ### Apply `pipe_to_function_call` Transformation Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Converts pipe expressions (`x |> f`) to function calls (`f(x)`) when `pipe_to_function_call` is true. ```julia format_text("x |> f\n"; pipe_to_function_call = true) ``` -------------------------------- ### Format Type Definition with Multi-line Parameters Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/style.md Type definitions with parameters spread across multiple lines have whitespace removed after commas. ```julia Foo{ a,b ,c } -> Foo{a,b,c} ``` -------------------------------- ### Formatting Option: annotate_untyped_fields_with_any Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/index.md When true, annotates fields in type definitions with '::Any' if no type annotation is provided. ```julia struct A arg1 end ``` ```julia struct A arg1::Any end ``` -------------------------------- ### Format Function Call with Multi-line Arguments Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/style.md Function calls with arguments spread across multiple lines are condensed into a single line. ```julia f( a,b ,c ) -> f(a, b, c) ``` -------------------------------- ### Format `for in` vs `for =` loops Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/transforms.md Controls the conversion between `for in` and `for =` loops based on the RHS. Set `always_for_in` to `true` to always convert `=` to `in`, or `nothing` to leave it to the user. ```julia format_text(..., always_for_in = true) ``` -------------------------------- ### jlfmt: Check with Verbose Output Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Use --check --verbose flags to see which files are being checked during the formatting verification process. ```bash jlfmt --check --verbose src/ # prints each file being checked ``` -------------------------------- ### format_text Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Formats a Julia source code string and returns the formatted string. Supports various keyword arguments for customization. ```APIDOC ## `format_text` — Format a Julia source string Accepts a Julia source code string and returns a new formatted string. All formatting options can be passed as keyword arguments. The function validates that the formatted output is still parseable before returning it. ### Usage ```julia using JuliaFormatter # Basic formatting with default style src = """ function foo(a,b,c) a+b end """ println(format_text(src)) # Custom indent and margin println(format_text(src; indent = 2, margin = 60)) # Enable several transforms code = """ import A, B x |> f struct T field end """ println(format_text(code; import_to_using = true, pipe_to_function_call = true, always_use_return = false, annotate_untyped_fields_with_any = true, )) # Use a named style using JuliaFormatter: YASStyle println(format_text(src, YASStyle())) ``` ``` -------------------------------- ### jlfmt: Check if Files are Formatted Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Use the --check flag to verify if files are already formatted. The command exits with status 1 if any file is not formatted. ```bash # Check whether files are already formatted; exit 1 if not jlfmt --check src/ ``` -------------------------------- ### Onda Metadata Objects Source: https://github.com/domluna/juliaformatter.jl/blob/master/test/files/Onda.jl/docs/src/index.md Functions for creating and manipulating metadata objects like Signal, Annotation, and Recording. ```APIDOC ## Onda Metadata Objects ### Description Functions for creating and manipulating Onda metadata objects. ### Methods - `Signal` - `signal_from_template` - `span` - `sizeof_samples` - `Annotation` - `Recording` - `set_span!` - `annotate!` ``` -------------------------------- ### Directly Apply YASStyle in Julia Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/yas_style.md Apply the YASStyle directly to a file using the format function. You can also override specific settings like 'remove_extra_newlines' during direct application. ```julia format("file.jl", YASStyle()) ``` ```julia format("file.jl", YASStyle(), remove_extra_newlines=false) ``` -------------------------------- ### Dataset Operations Source: https://github.com/domluna/juliaformatter.jl/blob/master/test/files/Onda.jl/docs/src/index.md Functions for managing and interacting with Dataset objects, including loading, storing, and deleting data. ```APIDOC ## `Dataset` ### Description Functions related to the `Dataset` type. ### Methods - `Dataset` - `samples_path` - `create_recording!` - `load` - `store!` - `delete!` - `save_recordings_file` ``` -------------------------------- ### Apply `conditional_to_if` Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Converts ternary conditional expressions (`x = a > 0 ? foo : bar`) to `if-else` blocks when the expression exceeds the `margin` and `conditional_to_if` is true. ```julia format_text("x = a > 0 ? foo : bar\n"; conditional_to_if = true, margin = 5) ``` -------------------------------- ### Format Abstract Type Declaration Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/style.md Abstract type declarations without arguments are formatted into a single line. ```julia abstract type AbstractFoo end -> abstract type AbstractFoo end ``` -------------------------------- ### Align Assignments with JuliaFormatter Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Use `align_assignment = true` to align '=' across consecutive assignment statements, improving readability. ```julia using JuliaFormatter # align_assignment: aligns = across a block of consecutive assignments src = """ const UTF8PROC_STABLE = (1 << 1) const UTF8PROC_COMPAT = (1 << 2) const UTF8PROC_COMPOSE = (1 << 3) const short = 4 """ println(format_text(src; align_assignment = true)) ``` -------------------------------- ### Direct Blue Style Usage in Julia Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/blue_style.md Apply the Blue Style formatter directly within Julia code. You can use the default BlueStyle or override specific settings. ```julia format("file.jl", BlueStyle()) ``` ```julia format("file.jl", BlueStyle(), remove_extra_newlines=false) ``` -------------------------------- ### Serialization Source: https://github.com/domluna/juliaformatter.jl/blob/master/test/files/Onda.jl/docs/src/index.md Functions for serializing and deserializing Onda data, including support for different LPCM formats. ```APIDOC ## Serialization ### Description Functions related to the serialization and deserialization of Onda data. ### Methods - `AbstractLPCMSerializer` - `Onda.serializer_constructor_for_file_extension` - `serializer` - `deserialize_lpcm` - `serialize_lpcm` - `LPCM` - `LPCMZst` ``` -------------------------------- ### Skip Formatting with Inline Comments Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Exclude specific sections of a file from formatting using `#! format: off` and `#! format: on` directives. Whitespace within these sections is preserved. ```julia # Normally formatted a = f(aaa, bbb, ccc) #! format: off # Everything below is left completely untouched a = f(aaa, bbb,ccc) c = 102000 d = @foo 10 20 #! format: on # Formatting resumes here b = g(ddd, eee) ``` -------------------------------- ### AbstractTimeSpan Operations Source: https://github.com/domluna/juliaformatter.jl/blob/master/test/files/Onda.jl/docs/src/index.md Functions for working with time spans, including checking for containment, overlap, and calculating duration. ```APIDOC ## `AbstractTimeSpan` ### Description Functions for manipulating and querying time spans. ### Methods - `AbstractTimeSpan` - `TimeSpan` - `contains` - `overlaps` - `shortest_timespan_containing` - `duration` - `time_from_index` - `index_from_time` ``` -------------------------------- ### Apply `disallow_single_arg_nesting` Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Removes unnecessary nesting for single-argument function calls when `disallow_single_arg_nesting` is true. ```julia format_text("function_call(\"arg\")\n"; disallow_single_arg_nesting = true) ``` -------------------------------- ### Aligning Assignments and Short Function Definitions Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/custom_alignment.md The `align_assignment` option aligns tokens around `=` or `=>` operators, useful for variable assignments and short definition functions. ```julia const UTF8PROC_STABLE = (1 << 1) const UTF8PROC_COMPAT = (1 << 2) const UTF8PROC_COMPOSE = (1 << 3) const UTF8PROC_DECOMPOSE = (1 << 4) const UTF8PROC_IGNORE = (1 << 5) const UTF8PROC_REJECTNA = (1 << 6) const UTF8PROC_NLF2LS = (1 << 7) const UTF8PROC_NLF2PS = (1 << 8) const UTF8PROC_NLF2LF = (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS) const UTF8PROC_STRIPCC = (1 << 9) const UTF8PROC_CASEFOLD = (1 << 10) const UTF8PROC_CHARBOUND = (1 << 11) const UTF8PROC_LUMP = (1 << 12) const UTF8PROC_STRIP = (1 << 13) vcat(X::T...) where {T} = T[X[i] for i = 1:length(X)] vcat(X::T...) where {T<:Number} = T[X[i] for i = 1:length(X)] hcat(X::T...) where {T} = T[X[j] for i = 1:1, j = 1:length(X)] hcat(X::T...) where {T<:Number} = T[X[j] for i = 1:1, j = 1:length(X)] a = 1 bc = 2 long_variable = 1 other_var = 2 ``` -------------------------------- ### Add leading zero to float literals Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/transforms.md Prepends a leading zero to float literals that are missing one, standardizing the format to `0.1` instead of `.1`. ```julia a = .1 -> a = 0.1 ``` -------------------------------- ### Format Comments Alignment Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/style.md Comments are aligned to surrounding code blocks, maintaining their relative positions. ```julia # comment if a # comment elseif b # comment elseif c # comment else # comment end # comment -> # comment if a # comment elseif b # comment elseif c # comment else # comment end # comment ``` -------------------------------- ### Apply `separate_kwargs_with_semicolon` Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Separates keyword arguments from positional arguments with a semicolon when `separate_kwargs_with_semicolon` is true. ```julia format_text("f(a, b=1)\n"; separate_kwargs_with_semicolon = true) ``` -------------------------------- ### Align Struct Fields with JuliaFormatter Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Enable `align_struct_field = true` to align ':' and '=' within struct field definitions. ```julia # align_struct_field: aligns :: and = in struct field definitions src2 = """ Base.@kwdef struct Opts indent::Int = 4 margin::Int = 92 verbose::Bool = false end """ println(format_text(src2; align_struct_field = true)) ``` -------------------------------- ### Apply `trailing_zero` Source: https://context7.com/domluna/juliaformatter.jl/llms.txt Adds a trailing zero (`.0`) to bare float literals, e.g., `1.` becomes `1.0` and `.5` becomes `0.5`. ```julia format_text("a = 1.\nb = .5\n") ``` -------------------------------- ### Nest Type Parameters with Where Clause Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/style.md Type parameters in a `where` clause are nested when the function definition exceeds the margin. ```julia function f(arg1::A, key1 = val1; key2 = val2) where {A,B,C} body end -> function f( arg1::A, key1 = val1; key2 = val2, ) where {A,B,C} body end -> function f( arg1::A, key1 = val1; key2 = val2, ) where { A, B, C, } body end ``` -------------------------------- ### jlfmt: CLI Flags Override Config File Source: https://context7.com/domluna/juliaformatter.jl/llms.txt By default, command-line flags passed to jlfmt take precedence over settings in the configuration file. ```bash # Override config-file settings from the CLI (default: CLI wins) jlfmt --indent=2 src/file.jl ``` -------------------------------- ### Disable Formatting for an Entire File Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/skipping_formatting.md Place `#! format: off` at the top of a file to prevent any formatting within that file. The formatter expects the directive to be on its own line with exact whitespace. ```julia #! format: off # # It doesn't actually matter if it's on # the first line of the line but anything # onwards will NOT be formatted. module Foo ... end ``` -------------------------------- ### Configure YAS Style in TOML Source: https://github.com/domluna/juliaformatter.jl/blob/master/docs/src/yas_style.md Set the formatting style to 'yas' in the .JuliaFormatter.toml configuration file. This can be used to override specific settings like 'remove_extra_newlines'. ```toml style = "yas" ``` ```toml style = "yas" remove_extra_newlines = false ```