Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed Jul 21, 2019
0 parents commit 9f9fac2
Show file tree
Hide file tree
Showing 13 changed files with 1,566 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

.DS_Store
/.vscode
/.idea
/lsif-go
/data.lsif
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 ᴊ. ᴄʜᴇɴ

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Language Server Indexing Format Implementation for Go

🚨 This implementation is still in very early stage and follows the latest LSIF specification closely.

## Language Server Index Format

The purpose of the Language Server Index Format (LSIF) is to define a standard format for language servers or other programming tools to dump their knowledge about a workspace. This dump can later be used to answer language server [LSP](https://microsoft.github.io/language-server-protocol/) requests for the same workspace without running the language server itself. Since much of the information would be invalidated by a change to the workspace, the dumped information typically excludes requests used when mutating a document. So, for example, the result of a code complete request is typically not part of such a dump.

A first draft specification can be found [here](https://github.com/Microsoft/language-server-protocol/blob/master/indexFormat/specification.md).

## Quickstart

1. Download and build this program via `go get github.com/sourcegraph/lsif-go`.
2. The binary `lsif-go` should be installed into your `$GOPATH/bin` directory.
3. Make sure you have added `$GOPATH/bin` to your `$PATH` envrionment variable.
4. Go to a root directory of a Go project, then execute `lsif-go export`:

```
➜ lsif-go export
Package: protocol
File: /Users/unknwon/Work/Sourcegraph/lsif-go/protocol/protocol.go
Package: export
File: /Users/unknwon/Work/Sourcegraph/lsif-go/export/exporter.go
File: /Users/unknwon/Work/Sourcegraph/lsif-go/export/helper.go
File: /Users/unknwon/Work/Sourcegraph/lsif-go/export/types.go
Package: main
File: /Users/unknwon/Work/Sourcegraph/lsif-go/cmd.go
File: /Users/unknwon/Work/Sourcegraph/lsif-go/export.go
File: /Users/unknwon/Work/Sourcegraph/lsif-go/main.go
File: /Users/unknwon/Work/Sourcegraph/lsif-go/version.go
Processed in 950.942253ms
```

By default, the exporter dumps LSIF data to the file `data.lsif` in the working directory.

Use `lsif-go -h` for more information

## Testing Commands

- Validate: `lsif-util validate data.lsif`
- Visualize: `lsif-util visualize data.lsif --distance 2 | dot -Tpng -o image.png`
110 changes: 110 additions & 0 deletions cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// This file is a modified version of sourcegraph/src-cli/cmd/src/cmd.go.

package main

import (
"flag"
"fmt"
"log"
"os"
)

// command is a subcommand handler and its flag set.
type command struct {
// flagSet is the flag set for the command.
flagSet *flag.FlagSet

// handler is the function that is invoked to handle this command.
handler func(args []string) error

// flagSet.Usage function to invoke on e.g. -h flag. If nil, a default one
// one is used.
usageFunc func()
}

// matches tells if the given name matches this command.
func (c *command) matches(name string) bool {
if name == c.flagSet.Name() {
return true
}
return false
}

// commander represents a top-level command with subcommands.
type commander []*command

// run runs the command.
func (c commander) run(flagSet *flag.FlagSet, cmdName, usageText string, args []string) {
// Parse flags
flagSet.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), usageText)
}
if !flagSet.Parsed() {
flagSet.Parse(args)
}

// Print usage if the command is "help"
if flagSet.Arg(0) == "help" || flagSet.NArg() == 0 {
flagSet.Usage()
os.Exit(0)
}

// Configure default usage funcs for commands
for _, cmd := range c {
cmd := cmd
if cmd.usageFunc != nil {
cmd.flagSet.Usage = cmd.usageFunc
continue
}
cmd.flagSet.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of '%s %s':\n", cmdName, cmd.flagSet.Name())
cmd.flagSet.PrintDefaults()
}
}

// Find the subcommand to execute
name := flagSet.Arg(0)
for _, cmd := range c {
if !cmd.matches(name) {
continue
}

// Parse subcommand flags
args := flagSet.Args()[1:]
if err := cmd.flagSet.Parse(args); err != nil {
panic(fmt.Sprintf("all registered commands should use flag.ExitOnError: error: %s", err))
}

// Execute the subcommand.
if err := cmd.handler(flagSet.Args()[1:]); err != nil {
if _, ok := err.(*usageError); ok {
log.Println(err)
cmd.flagSet.Usage()
os.Exit(2)
}
if e, ok := err.(*exitCodeError); ok {
if e.error != nil {
log.Println(e.error)
}
os.Exit(e.exitCode)
}
log.Fatal(err)
}
os.Exit(0)
}
log.Printf("%s: unknown subcommand %q", cmdName, name)
log.Fatalf("Run '%s help' for usage.", cmdName)
}

// usageError is an error type that subcommands can return in order to signal
// that a usage error has occurred.
type usageError struct {
error
}

// exitCodeError is an error type that subcommands can return in order to
// specify the exact exit code.
type exitCodeError struct {
error
exitCode int
}
65 changes: 65 additions & 0 deletions export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"flag"
"fmt"
"log"
"os"
"time"

"github.com/sourcegraph/lsif-go/export"
"github.com/sourcegraph/lsif-go/protocol"
)

func init() {
usage := `
Examples:
Generate an LSIF dump for a workspace:
$ lsif-go export -workspace=myrepo -output=myrepo.lsif
`

flagSet := flag.NewFlagSet("export", flag.ExitOnError)
usageFunc := func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'lsif-go %s':\n", flagSet.Name())
flagSet.PrintDefaults()
fmt.Println(usage)
}
var (
workspaceFlag = flagSet.String("workspace", "", `The path to the workspace. (required)`)
outputFlag = flagSet.String("output", "data.lsif", `The output location of the dump.`)
)

handler := func(args []string) error {
flagSet.Parse(args)

start := time.Now()

out, err := os.Create(*outputFlag)
if err != nil {
return fmt.Errorf("create dump file: %v", err)
}
defer out.Close()

err = export.Export(*workspaceFlag, out, protocol.ToolInfo{
Name: "lsif-go",
Version: version,
Args: args,
})
if err != nil {
return fmt.Errorf("export: %v", err)
}

log.Println("Processed in", time.Since(start))
return nil
}

// Register the command
commands = append(commands, &command{
flagSet: flagSet,
handler: handler,
usageFunc: usageFunc,
})
}
Loading

0 comments on commit 9f9fac2

Please sign in to comment.