I will save this but will probably change this
This commit is contained in:
173
modules/lsp/lsp.go
Normal file
173
modules/lsp/lsp.go
Normal file
@@ -0,0 +1,173 @@
|
||||
package lsp
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"fes/modules/config"
|
||||
)
|
||||
|
||||
var destRoot string = filepath.Join(func () string {
|
||||
res, err := os.UserHomeDir()
|
||||
if err != nil { panic(err) }
|
||||
return res
|
||||
}(), ".local", "LuaAddons", "fes")
|
||||
|
||||
func usage(status int) {
|
||||
fmt.Fprintln(flag.CommandLine.Output(), "Lsp Usage:")
|
||||
fmt.Fprintln(flag.CommandLine.Output(), " help Print this help and exit")
|
||||
fmt.Fprintln(flag.CommandLine.Output(), " install Install the Lsp")
|
||||
fmt.Fprintln(flag.CommandLine.Output(), " uninstall Uninstall the Lsp")
|
||||
fmt.Fprintln(flag.CommandLine.Output(), " doctor Verify Lsp installation")
|
||||
os.Exit(status)
|
||||
}
|
||||
|
||||
func writeFile(fs embed.FS, src, dst string) error {
|
||||
in, err := fs.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer in.Close()
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
out, err := os.Create(dst)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
_, err = io.Copy(out, in)
|
||||
return err
|
||||
}
|
||||
|
||||
func install() error {
|
||||
if err := os.RemoveAll(destRoot); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var found bool
|
||||
|
||||
err := fsWalkDir(config.LspStubs, "lsp", func(path string) error {
|
||||
if path == "lsp" {
|
||||
return nil
|
||||
}
|
||||
|
||||
rel := strings.TrimPrefix(path, "lsp/")
|
||||
dst := filepath.Join("library", destRoot, rel)
|
||||
|
||||
info, err := fsStat(config.LspStubs, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
return os.MkdirAll(dst, 0755)
|
||||
}
|
||||
|
||||
found = true
|
||||
return writeFile(config.LspStubs, path, dst)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !found {
|
||||
return errors.New("no lsp stubs found in embedded fs")
|
||||
}
|
||||
|
||||
err = os.WriteFile(filepath.Join("dstRoot", "config.json"), []byte(""), 0755)
|
||||
|
||||
fmt.Println("Lua LSP installed to:", destRoot)
|
||||
return nil
|
||||
}
|
||||
|
||||
func uninstall() error {
|
||||
if _, err := os.Stat(destRoot); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
fmt.Println("Lua LSP not installed")
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.RemoveAll(destRoot); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("Lua LSP uninstalled from:", destRoot)
|
||||
return nil
|
||||
}
|
||||
|
||||
func doctor() error {
|
||||
info, err := os.Stat(destRoot)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return errors.New("lsp not installed")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if !info.IsDir() {
|
||||
return errors.New("lsp install path is not a directory")
|
||||
}
|
||||
|
||||
var luaFiles int
|
||||
|
||||
err = filepath.Walk(destRoot, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() && strings.HasSuffix(info.Name(), ".lua") {
|
||||
luaFiles++
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if luaFiles == 0 {
|
||||
return errors.New("no lua stubs found in lsp directory")
|
||||
}
|
||||
|
||||
fmt.Println("LSP stubs: OK")
|
||||
fmt.Println("Location:", destRoot)
|
||||
fmt.Println()
|
||||
fmt.Println("LuaLS configuration required:")
|
||||
fmt.Println()
|
||||
fmt.Println(` "Lua.workspace.library": {`)
|
||||
fmt.Printf(` "%s": true`, destRoot)
|
||||
fmt.Println()
|
||||
fmt.Println(` }`)
|
||||
fmt.Println()
|
||||
fmt.Println("Restart your editor after installing or updating the LSP stubs")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Do(arg string) error {
|
||||
switch arg {
|
||||
case "help":
|
||||
usage(0)
|
||||
case "install":
|
||||
return install()
|
||||
case "uninstall":
|
||||
return uninstall()
|
||||
case "doctor":
|
||||
return doctor()
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "Unknown command: %s\n", arg)
|
||||
usage(1)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
36
modules/lsp/util.go
Normal file
36
modules/lsp/util.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package lsp
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func fsWalkDir(fs embed.FS, root string, fn func(string) error) error {
|
||||
entries, err := fs.ReadDir(root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, e := range entries {
|
||||
p := filepath.Join(root, e.Name())
|
||||
if err := fn(p); err != nil {
|
||||
return err
|
||||
}
|
||||
if e.IsDir() {
|
||||
if err := fsWalkDir(fs, p, fn); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func fsStat(fs embed.FS, path string) (os.FileInfo, error) {
|
||||
f, err := fs.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
return f.Stat()
|
||||
}
|
||||
Reference in New Issue
Block a user