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

Multi-line string literal indentation #64

Open
shichuzhu opened this issue Dec 30, 2021 · 2 comments
Open

Multi-line string literal indentation #64

shichuzhu opened this issue Dec 30, 2021 · 2 comments

Comments

@shichuzhu
Copy link

I'm trying to generate some code using dst package.
Part of the code involves multi-line string literal.
I'd like to keep the code prettier by propagating the indentation level into the string literal

... // node :=
&dst.CallExpr{
	Fun: &dst.SelectorExpr{
		X:   &dst.Ident{Name: "mypackage"},
		Sel: &dst.Ident{Name: "MyFunc"},
	},
	Args: []dst.Expr{
		&dst.BasicLit{
			Kind:  token.STRING,
			Value: fmt.Sprintf("`\n%v\n`", "multi\nline\nliteral"),
		},
	},
},

The generated code will look like

func GeneratedFunc() {
	node := mypackage.MyFunc(`
multi
line
literal
`) // <-- This non-indent looks rather bothering
}

Is it possible that I can make the string literal indent aligned with the caller, or somehow retrieve the 'indentation level' via the dst package, so that I can manually Tweak the literal? e.g.

func GeneratedFunc() {
	node := mypackage.MyFunc(`
		multi
		line
		literal
		`)
}
@dave
Copy link
Owner

dave commented Dec 30, 2021

Aah unfortunately dst knows nothing of the indentation level. When we render the output we just convert the dst nodes to ast nodes and feed the output through the standard Go printer. This automatically adds the indents.

@shichuzhu
Copy link
Author

Thanks for the info!
In that case, I can think of a workaround specifically for this case

  1. Print the generated code and store as a string s
  2. Re-parse string s using the ast with the FileSet info.
  3. Find that node I want to add indent, and retrieve it's token.Pos -> token.Position
  4. Hopefully the token.Position column number will provide hint about the indentation of that line.

I definitely don't expect this to be a general solution to integrate into the library, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants