Skip to content
This repository has been archived by the owner on Aug 12, 2023. It is now read-only.

feat(formatting):adding range_formatting for google-java-format builtin #1623

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions doc/BUILTINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3483,15 +3483,19 @@ Reformats Java source code according to Google Java Style.
#### Usage

```lua
local sources = { null_ls.builtins.formatting.google_java_format }
local sources = {
null_ls.builtins.formatting.google_java_format.with({
extra_args = { "-aosp" }, -- Use AOSP style instead of Google Style (4-space indentation).
}),
}
```

#### Defaults

- Filetypes: `{ "java" }`
- Method: `formatting`
- Method: `formatting`, `range_formatting`
- Command: `google-java-format`
- Args: `{ "-" }`
- Args: dynamically resolved (see [source](https://github.com/jose-elias-alvarez/null-ls.nvim/blob/main/lua/null-ls/builtins/formatting/google_java_format.lua))

### [haxe_formatter](https://github.com/HaxeCheckstyle/haxe-formatter)

Expand Down
20 changes: 16 additions & 4 deletions lua/null-ls/builtins/formatting/google_java_format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,32 @@ local h = require("null-ls.helpers")
local methods = require("null-ls.methods")

local FORMATTING = methods.internal.FORMATTING
local RANGE_FORMATTING = methods.internal.RANGE_FORMATTING

return h.make_builtin({
name = "google_java_format",
meta = {
url = "https://github.com/google/google-java-format",
description = "Reformats Java source code according to Google Java Style.",
},
method = FORMATTING,
method = { FORMATTING, RANGE_FORMATTING },
filetypes = { "java" },
generator_opts = {
command = "google-java-format",
args = {
"-",
},
args = function(params)
if params.method == RANGE_FORMATTING and params.range then
return {
"--lines",
params.range.row .. ":" .. params.range.end_row,
"--skip-sorting-imports",
"--skip-removing-unused-imports",
"--skip-javadoc-formatting",
"--skip-reflowing-long-strings",
"-",
}
end
return { "-" }
end,
to_stdin = true,
},
factory = h.formatter_factory,
Expand Down