Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Oct 8, 2024
1 parent 815d213 commit 8de2f46
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 30 deletions.
37 changes: 12 additions & 25 deletions formatter/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package formatter
import (
"fmt"
"strings"
"unicode"

"github.com/fatih/color"
"github.com/gnolang/tlin/internal"
Expand Down Expand Up @@ -149,7 +150,7 @@ func (b *IssueFormatterBuilder) AddCodeSnippet() *IssueFormatterBuilder {
continue
}

line := expandTabs(b.snippet.Lines[i-1])
line := b.snippet.Lines[i-1]
line = strings.TrimPrefix(line, commonIndent)
lineNum := fmt.Sprintf("%*d", maxLineNumWidth, i)

Expand Down Expand Up @@ -248,21 +249,6 @@ func calculateMaxLineNumWidth(endLine int) int {
return len(fmt.Sprintf("%d", endLine))
}

// expandTabs replaces tab characters('\t') with spaces.
// Assuming a table width of 8.
func expandTabs(line string) string {
var expanded strings.Builder
for i, ch := range line {
if ch == '\t' {
spaceCount := tabWidth - (i % tabWidth)
expanded.WriteString(strings.Repeat(" ", spaceCount))
} else {
expanded.WriteRune(ch)
}
}
return expanded.String()
}

// calculateVisualColumn calculates the visual column position
// in a string. taking into account tab characters.
func calculateVisualColumn(line string, column int) int {
Expand Down Expand Up @@ -290,39 +276,40 @@ func findCommonIndent(lines []string) string {
}

// find first non-empty line's indent
var firstIndent string
var firstIndent []rune
for _, line := range lines {
trimmed := strings.TrimSpace(line)
// trimmed := strings.TrimSpace(line)
trimmed := strings.TrimLeftFunc(line, unicode.IsSpace)
if trimmed != "" {
firstIndent = line[:len(line)-len(trimmed)]
firstIndent = []rune(line[:len(line)-len(trimmed)])
break
}
}

if firstIndent == "" {
if len(firstIndent) == 0 {
return ""
}

// search common indent for all non-empty lines
for _, line := range lines {
trimmed := strings.TrimSpace(line)
trimmed := strings.TrimLeftFunc(line, unicode.IsSpace)
if trimmed == "" {
continue
}

currentIndent := line[:len(line)-len(trimmed)]
currentIndent := []rune(line[:len(line)-len(trimmed)])
firstIndent = commonPrefix(firstIndent, currentIndent)

if firstIndent == "" {
if len(firstIndent) == 0 {
break
}
}

return firstIndent
return string(firstIndent)
}

// commonPrefix finds the common prefix of two strings.
func commonPrefix(a, b string) string {
func commonPrefix(a, b []rune) []rune {
minLen := len(a)
if len(b) < minLen {
minLen = len(b)
Expand Down
8 changes: 4 additions & 4 deletions formatter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestFormatIssuesWithArrows_UnnecessaryElse(t *testing.T) {
"package main",
"",
"func unnecessaryElse() bool {",
" if condition {",
" if condition {",
" return true",
" } else {",
" return false",
Expand Down Expand Up @@ -280,9 +280,9 @@ func TestFindCommonIndent(t *testing.T) {
{
name: "tab indent",
lines: []string{
"\tif foo {",
"\t\tprintln()",
"\t}",
" if foo {",
" println()",
" }",
},
expected: "\t",
},
Expand Down
2 changes: 1 addition & 1 deletion testdata/deprecate/orig_caller.gno
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func main() {
origCaller := std.GetOrigCaller()
origCaller := std.GetOrigCaller()
println(origCaller.String())

prev := std.PrevRealm()
Expand Down

0 comments on commit 8de2f46

Please sign in to comment.