Skip to content

Commit

Permalink
gopls.proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Oct 10, 2023
1 parent e096cc6 commit f1ff0fc
Show file tree
Hide file tree
Showing 11 changed files with 580 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Add no patterns to .gitignore except for files generated by the build.
last-change

go.work*
log.txt
2 changes: 2 additions & 0 deletions gopls/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ require (
require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/google/safehtml v0.1.0 // indirect
github.com/goplus/gox v1.11.37 // indirect
github.com/goplus/mod v0.11.2 // indirect
github.com/qiniu/x v1.11.9 // indirect
golang.org/x/exp/typeparams v0.0.0-20221212164502-fae10dda9338 // indirect

Expand Down
2 changes: 2 additions & 0 deletions gopls/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ github.com/goplus/gop v1.1.7 h1:6PemF6HYQZ2xjqIA06tQXk9LSmWz5p8+wo4UpQKTkes=
github.com/goplus/gop v1.1.7/go.mod h1:+kYk2ZSZ+rPD8AVoWTz3r5L/dkbtmUNvJOP0h1ET8KI=
github.com/goplus/gox v1.11.21/go.mod h1:wRCRSNukie4cDqADF4w0Btc2Gk6V3p3V6hI5+rsVqa8=
github.com/goplus/gox v1.11.32/go.mod h1:hdKq5ghywtKWnBJNQNVBkPITmWCqCFRwwd2LTYTfg2U=
github.com/goplus/gox v1.11.37 h1:8oAXjokhvttO/4NZgfAJ02GE9OivbX2VSsEJWu3fGng=
github.com/goplus/gox v1.11.37/go.mod h1:NkgUJWIjKxrhUwM4bgyUt3ZE0WlTunqfksMzrbnh7V8=
github.com/goplus/libc v0.3.13/go.mod h1:xqG4/g3ilKBE/UDn5vkaE7RRQPQPyspj7ecuMuvlQJ8=
github.com/goplus/mod v0.9.12/go.mod h1:YoPIowz71rnLLROA4YG0AC8bzDtPRyMaQwgTRLr8ri4=
github.com/goplus/mod v0.11.2 h1:kgIdro905lTZgqGecTbZ416O8ayN+ca2DR5OB4ayeWI=
github.com/goplus/mod v0.11.2/go.mod h1:d40I3nOr2qkGfLUjwc/BqLSq3oUUlQy5+/SpdiBKgY4=
github.com/jba/printsrc v0.2.2 h1:9OHK51UT+/iMAEBlQIIXW04qvKyF3/vvLuwW/hL8tDU=
github.com/jba/printsrc v0.2.2/go.mod h1:1xULjw59sL0dPdWpDoVU06TIEO/Wnfv6AHRpiElTwYM=
Expand Down
13 changes: 13 additions & 0 deletions gopls/goxls/cmd/gopls.proxy/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2022 The GoPlus Authors (goplus.org). All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
"golang.org/x/tools/gopls/goxls/proxy"
)

func main() {
proxy.Main()
}
11 changes: 11 additions & 0 deletions gopls/goxls/cmd/goxls/goxls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2022 The GoPlus Authors (goplus.org). All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import "golang.org/x/tools/gopls/goxls"

func main() {
goxls.Main()
}
3 changes: 1 addition & 2 deletions gopls/goxls/goxls.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2022 The GoPlus Authors (goplus.org). All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package goxls

import (
Expand All @@ -9,12 +10,10 @@ import (

"golang.org/x/tools/gopls/internal/goxls/cmd"
"golang.org/x/tools/gopls/internal/hooks"
"golang.org/x/tools/gopls/internal/telemetry"
"golang.org/x/tools/internal/tool"
)

func Main() {
telemetry.Start()
ctx := context.Background()
tool.Main(ctx, cmd.New("goxls", "", nil, hooks.Options), os.Args[1:])
}
75 changes: 75 additions & 0 deletions gopls/goxls/proxy/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2022 The GoPlus Authors (goplus.org). All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package proxy

import (
"io"
"log"
"os"
"os/exec"
"strconv"
"time"
)

const (
gopls = "gopls.origin"
)

func Main() {
home, err := os.UserHomeDir()
check(err)

goplsDir := home + "/.gopls"
rotateDir := goplsDir + "/" + strconv.FormatInt(time.Now().UnixMicro(), 36)
err = os.MkdirAll(rotateDir, 0755)
check(err)

createFile := func(name string) (f *os.File, err error) {
normal, rotate := goplsDir+name, rotateDir+name
os.Rename(normal, rotate)
return os.Create(normal)
}

logf, err := createFile("/gopls.log")
check(err)
defer logf.Close()

stdinf, err := createFile("/gopls.in")
check(err)
defer stdinf.Close()

stdoutf, err := createFile("/gopls.out")
check(err)
defer stdoutf.Close()

stderrf, err := createFile("/gopls.err")
check(err)
defer stderrf.Close()

log.SetOutput(logf)
log.Println("[INFO] app start:", os.Args)

pr, pw, err := os.Pipe()
check(err)
go func() {
w := io.MultiWriter(pw, stdinf)
for {
io.Copy(w, os.Stdin)
}
}()

cmd := exec.Command(gopls, os.Args[1:]...)
cmd.Stdin = pr
cmd.Stdout = io.MultiWriter(os.Stdout, stdoutf)
cmd.Stderr = io.MultiWriter(os.Stderr, stderrf)
err = cmd.Run()
check(err)
}

func check(err error) {
if err != nil {
log.Panicln(err)
}
}
63 changes: 63 additions & 0 deletions gopls/internal/goxls/packages/gengo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2023 The GoPlus Authors (goplus.org). All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package packages

import (
"bytes"
"log"
"os"
"os/exec"
"strings"

"github.com/goplus/gop"
"github.com/goplus/gop/x/gopprojs"
)

var (
gopInstalled bool
)

const (
debugGop = false
)

func init() {
go initGop()
}

func initGop() {
var b bytes.Buffer
cmd := exec.Command("gop", "env", "GOPROOT")
cmd.Stdout = &b
err := cmd.Run()
if gopRoot := b.String(); err == nil && gopRoot != "" {
os.Setenv("GOPROOT", strings.TrimRight(gopRoot, "\n\r"))
gopInstalled = true
} else if debugGop {
log.Panicln("FATAL: gop not installed")
}
}

func GenGo(pattern ...string) (err error) {
if !gopInstalled {
return nil
}
projs, err := gopprojs.ParseAll(pattern...)
if err != nil {
return
}
for _, proj := range projs {
switch v := proj.(type) {
case *gopprojs.DirProj:
_, _, err = gop.GenGo(v.Dir, nil, true)
case *gopprojs.PkgPathProj:
if v.Path == "builtin" {
continue
}
_, _, err = gop.GenGoPkgPath("", v.Path, nil, true)
}
}
return
}
1 change: 1 addition & 0 deletions gopls/internal/goxls/packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ type Module = packages.Module
// proceeding with further analysis. The PrintErrors function is
// provided for convenient display of all errors.
func Load(cfg *Config, patterns ...string) ([]*Package, error) {
GenGo(patterns...)
pkgs, err := packages.Load(cfg, patterns...)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit f1ff0fc

Please sign in to comment.