Skip to content

Commit 91d7ad3

Browse files
committed
feat: initial commit
0 parents  commit 91d7ad3

10 files changed

Lines changed: 579 additions & 0 deletions

File tree

.github/workflows/build.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: build
2+
3+
on:
4+
push:
5+
tags:
6+
- "v*.*.*"
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
matrix:
13+
include:
14+
- { goos: "linux", goarch: "amd64" }
15+
- { goos: "linux", goarch: "arm64" }
16+
- { goos: "darwin", goarch: "amd64" }
17+
- { goos: "darwin", goarch: "arm64" }
18+
- { goos: "windows", goarch: "amd64" }
19+
- { goos: "windows", goarch: "arm64" }
20+
fail-fast: true
21+
name: Go ${{ matrix.goos }} ${{ matrix.goarch }} build
22+
steps:
23+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
24+
25+
- uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
26+
with:
27+
go-version-file: "go.mod"
28+
29+
- name: Build
30+
env:
31+
GOOS: ${{ matrix.goos }}
32+
GOARCH: ${{ matrix.goarch }}
33+
CGO_ENABLED: "0"
34+
run: |
35+
BINARY_NAME="golens"
36+
[ "$GOOS" = "windows" ] && BINARY_NAME="${BINARY_NAME}.exe"
37+
go build -ldflags="-s -w" -o ${BINARY_NAME} .
38+
39+
- name: Packaging
40+
run: |
41+
ARTIFACT="golens-${{ matrix.goos }}-${{ matrix.goarch }}.tar.gz"
42+
tar -czf $ARTIFACT golens*
43+
echo "ARTIFACT=$ARTIFACT" >> $GITHUB_ENV
44+
45+
- name: Release
46+
uses: softprops/action-gh-release@b4309332981a82ec1c5618f44dd2e27cc8bfbfda # v3.0.0
47+
with:
48+
files: ${{ env.ARTIFACT }}

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.DS_Store
2+
.idea/
3+
.vscode/
4+
*.swp
5+
6+
*.exe
7+
*.test
8+
*.out
9+
10+
.env

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# GoLens
2+
3+
**GoLens** is a language server that provides [CodeLens-style](https://code.visualstudio.com/blogs/2017/02/12/code-lens-roundup) implementation and references usage counts for [Go](https://go.dev/), similar to what you get in Rust, TypeScript, and other language.
4+
5+
This project exists as a workaround while waiting for native CodeLens support in `gopls` ([golang/go#56695](https://github.com/golang/go/issues/56695)).
6+
7+
> Built entirely with Go standrad libraries
8+
9+
## Features
10+
- Show implementation counts for interface
11+
- Show reference usage counts for interface
12+
13+
## Editor Integration
14+
15+
Currently available extensions:
16+
- Zed - [zed-golens](https://github.com/riflowth/zed-golens)

go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/vectier/golens
2+
3+
go 1.26
4+
5+
require golang.org/x/tools v0.45.0
6+
7+
require (
8+
golang.org/x/mod v0.36.0 // indirect
9+
golang.org/x/sync v0.20.0 // indirect
10+
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
2+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3+
golang.org/x/mod v0.36.0 h1:JJjpVx6myfUsUdAzZuOSTTmRE0PfZeNWzzvKrP7amb4=
4+
golang.org/x/mod v0.36.0/go.mod h1:moc6ELqsWcOw5Ef3xVprK5ul/MvtVvkIXLziUOICjUQ=
5+
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
6+
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
7+
golang.org/x/tools v0.45.0 h1:18qN3FAooORvApf5XjCXgsuayZOEtXf6JK18I3+ONa8=
8+
golang.org/x/tools v0.45.0/go.mod h1:LuUGqqaXcXMEFEruIVJVm5mgDD8vww/z/SR1gQ4uE/0=

internal/lsp/codec.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package lsp
2+
3+
import (
4+
"bufio"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
"strconv"
9+
"strings"
10+
)
11+
12+
// Parse parses the content of a JSON-RPC message from r following the LSP specification
13+
//
14+
// See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#baseProtocol
15+
func Parse(r *bufio.Reader) ([]byte, error) {
16+
var contentLength int
17+
for {
18+
line, err := r.ReadString('\n')
19+
if err != nil {
20+
return nil, err
21+
}
22+
if !strings.HasSuffix(line, "\r\n") {
23+
return nil, fmt.Errorf(`line ending must be \r\n`)
24+
}
25+
line = strings.TrimSuffix(line, "\r\n")
26+
if line == "" {
27+
break
28+
}
29+
if value, ok := strings.CutPrefix(line, "Content-Length: "); ok {
30+
contentLength, err = strconv.Atoi(strings.TrimSpace(value))
31+
if err != nil {
32+
return nil, fmt.Errorf("invalid Content-Length value: %w", err)
33+
}
34+
}
35+
}
36+
37+
if contentLength == 0 {
38+
return nil, fmt.Errorf("no Content-Length header found")
39+
}
40+
41+
buf := make([]byte, contentLength)
42+
_, err := io.ReadFull(r, buf)
43+
return buf, err
44+
}
45+
46+
// Respond writes JSON data to w following the LSP specification
47+
//
48+
// See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#baseProtocol
49+
func Respond(w io.Writer, v any) error {
50+
data, err := json.Marshal(v)
51+
if err != nil {
52+
return err
53+
}
54+
if _, err := fmt.Fprintf(w, "Content-Length: %d\r\n\r\n", len(data)); err != nil {
55+
return err
56+
}
57+
if _, err := w.Write(data); err != nil {
58+
return err
59+
}
60+
return nil
61+
}

0 commit comments

Comments
 (0)