This repository has been archived by the owner on Jan 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(formatting): use gno fmt Replace the usage of gofumpt/gofmt by `gno fmt` which has the benefit of updating gno imports in addition to the formatting. Added 2 txtar files to assert the behavior. * fix: `Format` returns only the stdout of gno fmt
- Loading branch information
Showing
7 changed files
with
170 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,20 @@ | ||
package tools | ||
|
||
import ( | ||
"errors" | ||
"go/format" | ||
|
||
gofumpt "mvdan.cc/gofumpt/format" | ||
) | ||
|
||
type FormattingOption int | ||
|
||
const ( | ||
Gofmt FormattingOption = iota | ||
Gofumpt | ||
"bytes" | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
func Format(data string, opt FormattingOption) ([]byte, error) { | ||
switch opt { | ||
case Gofmt: | ||
return RunGofmt(data) | ||
case Gofumpt: | ||
return RunGofumpt(data) | ||
default: | ||
return nil, errors.New("gnopls: invalid formatting option") | ||
func Format(file string) ([]byte, error) { | ||
cmd := exec.Command("gno", "fmt", file) | ||
var stdin, stderr bytes.Buffer | ||
cmd.Stdout = &stdin | ||
cmd.Stderr = &stderr | ||
err := cmd.Run() | ||
if err != nil { | ||
return nil, fmt.Errorf("running '%s': %w: %s", strings.Join(cmd.Args, " "), err, stderr.String()) | ||
} | ||
} | ||
|
||
func RunGofmt(data string) ([]byte, error) { | ||
return format.Source([]byte(data)) | ||
} | ||
|
||
func RunGofumpt(data string) ([]byte, error) { | ||
return gofumpt.Source([]byte(data), gofumpt.Options{}) | ||
return stdin.Bytes(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Init phase | ||
lsp initialize input/initialize.json | ||
lsp initialized input/initialized.json | ||
lsp workspace/didChangeConfiguration input/didChangeConfiguration.json | ||
lsp textDocument/didOpen input/didOpen_x.json | ||
|
||
lsp textDocument/formatting input/formatting.json | ||
cmp output/formatting.json expected/formatting.json | ||
-- x.gno -- | ||
package foo | ||
func Hello(){ | ||
_=42 | ||
} | ||
-- input/initialize.json -- | ||
{ | ||
"rootUri": "file://$WORK" | ||
} | ||
-- input/initialized.json -- | ||
{} | ||
-- input/didChangeConfiguration.json -- | ||
{ | ||
"settings": { | ||
"gno": "$GOBIN/gno", | ||
"gopls": "$GOBIN/gopls", | ||
"root": "$GNOPATH", | ||
"precompileOnSave": true, | ||
"buildOnSave": true | ||
} | ||
} | ||
-- input/didOpen_x.json -- | ||
{ | ||
"textDocument": { | ||
"uri":"file://$WORK/x.gno", | ||
"text":"${FILE_x.gno}" | ||
} | ||
} | ||
-- input/formatting.json -- | ||
{ | ||
"textDocument": { | ||
"uri":"file://$WORK/x.gno" | ||
} | ||
} | ||
-- expected/formatting.json -- | ||
[ | ||
{ | ||
"newText": "package foo\n\nfunc Hello() {\n\t_ = 42\n}\n", | ||
"range": { | ||
"end": { | ||
"character": 2147483647, | ||
"line": 2147483647 | ||
}, | ||
"start": { | ||
"character": 0, | ||
"line": 0 | ||
} | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Init phase | ||
lsp initialize input/initialize.json | ||
lsp initialized input/initialized.json | ||
lsp workspace/didChangeConfiguration input/didChangeConfiguration.json | ||
lsp textDocument/didOpen input/didOpen_x.json | ||
|
||
lsp textDocument/formatting input/formatting.json | ||
cmp output/formatting.json expected/formatting.json | ||
-- x.gno -- | ||
package foo | ||
func Hello() { | ||
ufmt.Sprintf() | ||
} | ||
-- input/initialize.json -- | ||
{ | ||
"rootUri": "file://$WORK" | ||
} | ||
-- input/initialized.json -- | ||
{} | ||
-- input/didChangeConfiguration.json -- | ||
{ | ||
"settings": { | ||
"gno": "$GOBIN/gno", | ||
"gopls": "$GOBIN/gopls", | ||
"root": "$GNOPATH", | ||
"precompileOnSave": true, | ||
"buildOnSave": true | ||
} | ||
} | ||
-- input/didOpen_x.json -- | ||
{ | ||
"textDocument": { | ||
"uri":"file://$WORK/x.gno", | ||
"text":"${FILE_x.gno}" | ||
} | ||
} | ||
-- input/formatting.json -- | ||
{ | ||
"textDocument": { | ||
"uri":"file://$WORK/x.gno" | ||
} | ||
} | ||
-- expected/formatting.json -- | ||
[ | ||
{ | ||
"newText": "package foo\n\nimport \"gno.land/p/demo/ufmt\"\n\nfunc Hello() {\n\tufmt.Sprintf()\n}\n", | ||
"range": { | ||
"end": { | ||
"character": 2147483647, | ||
"line": 2147483647 | ||
}, | ||
"start": { | ||
"character": 0, | ||
"line": 0 | ||
} | ||
} | ||
} | ||
] |