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

bug: styler fails #275

Open
1 task done
6801318d8d opened this issue Jan 19, 2024 · 4 comments
Open
1 task done

bug: styler fails #275

6801318d8d opened this issue Jan 19, 2024 · 4 comments
Labels
bug Something isn't working P2 Not a priority. PRs welcome

Comments

@6801318d8d
Copy link

Neovim version (nvim -v)

0.9.5

Operating system/version

Arch Linux

Add the debug logs

  • I have set log_level = vim.log.levels.DEBUG and pasted the log contents below.

Log file

10:57:10[DEBUG] Running formatters on test.R: { "styler" }
10:57:10[INFO] Run styler on test.R
10:57:10[DEBUG] Creating temp file .conform.7244551.test.R
10:57:10[DEBUG] Run command: { "R", "-s", "-e", "r.nvim::format()", "--args", ".conform.7244551.test.R" }
10:57:10[DEBUG] styler exited with code 0
10:57:10[DEBUG] Cleaning up temp file .conform.7244551.test.R

Describe the bug

  1. Formatting R code has no effect

  2. r.nvim is working, and the command line that conform.nvim is trying to run is working as well:

➜ # see minimal example file for test.R
➜ R -s -e "r.nvim::format()" --args ./test.R
df <- function(a, b) {
	print("ciao")
	c <- a + b
	print(paste0(c))
}
  1. Different from "styler" fails to format R/Rmd files #242, in which the error message says that r.nvim is not installed

What is the severity of this bug?

breaking (some functionality is broken)

Steps To Reproduce

See above

Expected Behavior

R code should be formatted, code inside function should be indented

Minimal example file

df <- function(a,b) {
print("ciao")
c = a+b
print(paste0(c))
}

Minimal init.lua

-- conform.nvim
require("conform").setup({
	-- Set the log level. Use `:ConformInfo` to see the location of the log file.
	log_level = vim.log.levels.DEBUG,
	formatters = {
		isort = {
			command = "/usr/bin/isort",
		},
		black = {
			command = "/usr/bin/black",
		},
	},
	formatters_by_ft = {
		python = { "isort", "black" },
		lua = { "stylua" },
		-- styler requires installing the R package `r.nvim` in R
		-- Open R and execute `remotes::install_github("devOpifex/r.nvim")`
		r = { "styler" },
	},
})

vim.api.nvim_create_user_command("ConformFormat", function(args)
	local range = nil
	if args.count ~= -1 then
		local end_line = vim.api.nvim_buf_get_lines(0, args.line2 - 1, args.line2, true)[1]
		range = {
			start = { args.line1, 0 },
			["end"] = { args.line2, end_line:len() },
		}
	end
	require("conform").format({ async = true, lsp_fallback = false, range = range })
end, { range = true })

vim.api.nvim_set_keymap("n", "<Leader>r", ":ConformFormat<CR>", { noremap = true })

Additional context

none

@6801318d8d 6801318d8d added the bug Something isn't working label Jan 19, 2024
@6801318d8d
Copy link
Author

6801318d8d commented Jan 19, 2024

Quick workaround:

Create rprettify:

#!/usr/bin/env sh
cp "$1" "$1".bak
R --quiet --no-echo -e "styler::style_file(\"$1\")" 1>/dev/null 2>&1
cat "$1"

Use this config:

	formatters = {
		rprettify = {
			inherit = false,
			command = "rprettify",
			args = { "$FILENAME" },
		},
	},
	formatters_by_ft = {
		r = { "rprettify" },
	},

@stevearc stevearc added the P2 Not a priority. PRs welcome label Jan 21, 2024
@BlackEdder
Copy link

@6801318d8d solution did not work for me (on save). It seemed to try to format the old save file without your newest changes, instead of saving your changes to the file and then applying formatting. This meant that all changes since last save would disappear.

I got it to work by adding stdin = false to the formatters option.

	formatters = {
		rprettify = {
			inherit = false,
                        stdin = false,
			command = "rprettify",
			args = { "$FILENAME" },
		},
	},
	formatters_by_ft = {
                rmd = { "rprettify" },
		r = { "rprettify" },
	},

The rprettify script can then be simplified to:

R --quiet --no-echo -e "styler::style_file(\"$1\")" 1>/dev/null 2>&1

I guess you should be able to just copy and paste this command into the above formatter, but I did not test that.

I actually prefer this solution over having to rely on another r package, so I will keep it personally.

@JohnCoene
Copy link
Contributor

Do you see any error in the logs? I'm confused as to what is going wrong

@tweakyTweeter
Copy link

I had the same error as OP. The solution provided by @BlackEdder worked for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P2 Not a priority. PRs welcome
Projects
None yet
Development

No branches or pull requests

5 participants