Skip to content

Commit

Permalink
feat: handle new gno precompile output
Browse files Browse the repository at this point in the history
Fix #18
  • Loading branch information
tbruyelle committed Feb 23, 2024
1 parent c81d963 commit 8bfa5ae
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
61 changes: 37 additions & 24 deletions internal/gno/gno.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import (
"go.lsp.dev/uri"
)

const (
genGoExt = ".gen.go"
genGoLineShift = 4
)

var ErrNoGno = errors.New("no gno binary found")

// BinManager is a wrapper for the gno binary and related tooling.
Expand Down Expand Up @@ -167,17 +172,15 @@ func (s Span) Gno2GenGo() Span {
panic(fmt.Sprintf("span %v is not a .gno referrence", s))
}
// Remove .gen.go extention, we want to target the gno file
s.URI = uri.New(string(s.URI) + ".gen.go")
// Shift lines & columns
s.Start.Line += 5
s.Start.Column++
s.End.Line += 5
s.End.Column++
s.URI = uri.New(string(s.URI) + genGoExt)
// Shift lines
s.Start.Line += genGoLineShift
s.End.Line += genGoLineShift
return s
}

func (s Span) IsGenGo() bool {
return strings.Contains(string(s.URI), ".gen.go")
return strings.Contains(string(s.URI), genGoExt)
}

// GenGo2Gno shifts the .gen.go Span s into a .gno Span.
Expand All @@ -186,31 +189,47 @@ func (s Span) GenGo2Gno() Span {
panic(fmt.Sprintf("span %v is not a .gen.go referrence", s))
}
// Remove .gen.go extention, we want to target the gno file
s.URI = uri.New(strings.ReplaceAll(string(s.URI), ".gen.go", ""))
// Shift lines & columns
s.Start.Line -= 5
s.Start.Column--
s.End.Line -= 5
s.End.Column--
s.URI = uri.New(strings.ReplaceAll(string(s.URI), genGoExt, ""))
// Shift lines
s.Start.Line -= genGoLineShift
s.End.Line -= genGoLineShift
return s
}

// ToLocation converts s to a protocol.Location.
// NOTE: In LSP, a position inside a document is expressed as a zero-based line
// and character offset, thus we need to decrement by one the span Start and
// End position.
func (s Span) ToLocation() protocol.Location {
return protocol.Location{
URI: s.URI,
Range: protocol.Range{
Start: protocol.Position{
Line: s.Start.Line,
Character: s.Start.Column,
Line: s.Start.Line - 1,
Character: s.Start.Column - 1,
},
End: protocol.Position{
Line: s.End.Line,
Character: s.End.Column,
Line: s.End.Line - 1,
Character: s.End.Column - 1,
},
},
}
}

// SpanFromLSPLocation converts a protocol.Location to a Span.
// NOTE: In LSP, a position inside a document is expressed as a zero-based line
// and character offset, thus we need to decrement by one the span Start and
// End position.
func SpanFromLSPLocation(uri uri.URI, line, col uint32) Span {
return Span{
URI: uri,
Start: Location{
Line: line + 1,
Column: col + 1,
},
}
}

// Definition returns the definition of the symbol at the given position
// using the `gopls` tool.
//
Expand All @@ -220,13 +239,7 @@ func (s Span) ToLocation() protocol.Location {
// * move gnols stuff in an other packahe
func (m *BinManager) Definition(ctx context.Context, uri uri.URI, line, col uint32) (GoplsDefinition, error) {
// Build a reference to the .gen.gno file position
target := Span{
URI: uri,
Start: Location{
Line: line,
Column: col,
},
}.Gno2GenGo().Position()
target := SpanFromLSPLocation(uri, line, col).Gno2GenGo().Position()
slog.Info("fetching definition", "uri", uri, "line", line, "col", col, "target", target)

// Prepare call to gopls
Expand Down
15 changes: 2 additions & 13 deletions internal/gno/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,9 @@ var errorRe = regexp.MustCompile(`(?m)^([^#]+?):(\d+):(\d+):(.+)$`)
// parseErrors parses the output of the `gno precompile -gobuild` command for
// errors.
//
// They look something like this:
//
// The format is:
// ```
// command-line-arguments
// # command-line-arguments
// <file>:20:9: undefined: strin
//
// <pkg_path>: build pkg: std go compiler: exit status 1
//
// 1 go build errors
// <file.gno>:<line>:<col>: <error>
// ```
func (m *BinManager) parseErrors(output, cmd string) ([]BuildError, error) {
errors := []BuildError{}
Expand Down Expand Up @@ -59,10 +52,6 @@ func (m *BinManager) parseErrors(output, cmd string) ([]BuildError, error) {
Column: uint32(column),
},
}
if span.IsGenGo() {
// Shift from .gen.go to .gno
span = span.GenGo2Gno()
}
errors = append(errors, BuildError{
Span: span,
Msg: msg,
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ func (h *handler) getDiagnostics(doc *store.Document) ([]protocol.Diagnostic, er
})
}

slog.Info("diagnostics", "parsed", diagnostics, "count", len(diagnostics))
slog.Info("diagnostics", "count", len(diagnostics), "parsed", diagnostics)
return diagnostics, nil
}

0 comments on commit 8bfa5ae

Please sign in to comment.