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

feat: tlin support for onSave requests #10

Merged
merged 8 commits into from
Oct 5, 2024
3 changes: 3 additions & 0 deletions internal/lsp/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log/slog"
"path/filepath"

"github.com/gnolang/gnopls/internal/tools"
"go.lsp.dev/jsonrpc2"
"go.lsp.dev/protocol"
)
Expand Down Expand Up @@ -77,5 +78,7 @@ func (s *server) DidSave(ctx context.Context, reply jsonrpc2.Replier, req jsonrp
slog.Info("save " + string(uri.Filename()))
s.UpdateCache(filepath.Dir(string(params.TextDocument.URI.Filename())))
notification := s.publishDiagnostics(ctx, s.conn, file)

tools.Lint(ctx, s.conn, uri)
return reply(ctx, notification, nil)
}
92 changes: 92 additions & 0 deletions internal/tools/lint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package tools

import (
"context"
"encoding/json"
"io"
"os"
"os/exec"

"go.lsp.dev/jsonrpc2"
"go.lsp.dev/protocol"
)

type Token struct {
Offset uint32 `json:"offset"`
Line uint32 `json:"line"`
Column uint32 `json:"column"`
}

type Issue struct {
Rule string `json:"rule"`
Category string `json:"category"`
Message string `json:"message"`
Suggestion string `json:"suggestion"`
Note string `json:"note"`
Start Token `json:"start"`
End Token `json:"end"`
Confidence int `json:"confidence"`
}

type FileIssues map[string][]Issue

func Lint(ctx context.Context, conn jsonrpc2.Conn, uri protocol.DocumentURI) error {
tempFile, err := os.CreateTemp("", "temp-*.json")
if err != nil {
return err
}
defer os.Remove(tempFile.Name())
defer tempFile.Close()

path := uri.Filename()
cmd := exec.Command("tlin", "-json-output", tempFile.Name(), path)
0xtekgrinder marked this conversation as resolved.
Show resolved Hide resolved

if err := cmd.Run(); err != nil {
return err
}

// read the temp file
content, err := io.ReadAll(tempFile)
if err != nil {
return err
}
if len(content) == 0 {
return nil
}

var data FileIssues
err = json.Unmarshal(content, &data)
if err != nil {
return err
}

// send the diagnostics
for _, issues := range data {
diagnostics := make([]protocol.Diagnostic, len(issues))
for i, issue := range issues {
diagnostics[i] = protocol.Diagnostic{
Range: protocol.Range{
Start: protocol.Position{
Line: issue.Start.Line,
Character: issue.Start.Column,
},
End: protocol.Position{
Line: issue.End.Line,
Character: issue.End.Column,
},
},
Severity: protocol.DiagnosticSeverityError,
Code: issue.Rule,
Message: issue.Message,
Source: "gnopls",
}
}
notification := protocol.PublishDiagnosticsParams{
URI: uri,
Diagnostics: diagnostics,
}
if err := conn.Notify(ctx, protocol.MethodTextDocumentPublishDiagnostics, notification); err != nil {
return err
}
}
}