Skip to content

Commit

Permalink
Add LanguageID configuration
Browse files Browse the repository at this point in the history
Update #37
  • Loading branch information
fhs committed Oct 21, 2020
1 parent e951b7e commit 7c528a8
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 24 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,19 @@ CodeActionsOnPut = ["source.organizeImports"]
hoverKind = "FullDocumentation"

[[FilenameHandlers]]
Pattern = '([/\\]go\.mod)|([/\\]go\.sum)|(\.go)$'
ServerKey = "gopls"
Pattern = "[/\\\\]go\\.mod$"
LanguageID = "go.mod"
ServerKey = "gopls"

[[FilenameHandlers]]
Pattern = "[/\\\\]go\\.sum$"
LanguageID = "go.sum"
ServerKey = "gopls"

[[FilenameHandlers]]
Pattern = "\\.go$"
LanguageID = "go"
ServerKey = "gopls"
```

## Hints & Tips
Expand Down
4 changes: 3 additions & 1 deletion internal/lsp/acmelsp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (h *clientHandler) ApplyEdit(ctx context.Context, params *protocol.ApplyWor
// ClientConfig contains LSP client configuration values.
type ClientConfig struct {
*config.Server
*config.FilenameHandler
RootDirectory string // used to compute RootURI in initialization
HideDiag bool // don't write diagnostics to DiagWriter
RPCTrace bool // print LSP rpc trace to stderr
Expand All @@ -103,10 +104,11 @@ type ClientConfig struct {
type Client struct {
protocol.Server
initializeResult *protocol.InitializeResult
cfg *ClientConfig
}

func NewClient(conn net.Conn, cfg *ClientConfig) (*Client, error) {
c := &Client{}
c := &Client{cfg: cfg}
if err := c.init(conn, cfg); err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/lsp/acmelsp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func testGoModule(t *testing.T, server string, src string, f func(t *testing.T,
defer srv.Close()

ctx := context.Background()
err = lsp.DidOpen(ctx, srv.Client, gofile, []byte(src))
err = lsp.DidOpen(ctx, srv.Client, gofile, "go", []byte(src))
if err != nil {
t.Fatalf("DidOpen failed: %v", err)
}
Expand Down Expand Up @@ -306,7 +306,7 @@ func main() {
defer srv.Close()

ctx := context.Background()
err = lsp.DidOpen(ctx, srv.Client, gofile, []byte(src))
err = lsp.DidOpen(ctx, srv.Client, gofile, "go", []byte(src))
if err != nil {
t.Fatalf("DidOpen failed: %v", err)
}
Expand Down Expand Up @@ -375,7 +375,7 @@ func testPython(t *testing.T, src string, f func(t *testing.T, c *Client, uri pr
defer srv.Close()

ctx := context.Background()
err = lsp.DidOpen(ctx, srv.Client, pyfile, []byte(src))
err = lsp.DidOpen(ctx, srv.Client, pyfile, "python", []byte(src))
if err != nil {
t.Fatalf("DidOpen failed: %v", err)
}
Expand Down
9 changes: 5 additions & 4 deletions internal/lsp/acmelsp/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ type File struct {
// LSP servers keyed by a user provided name.
Servers map[string]*Server

// LanguageHandlers is a mapping from LSP language ID to server key.
// This might be nice to support later, since this will avoid having to use regexp.
//LanguageHandlers map[string]string

// Servers determined by regular expression match on filename,
// as supplied by -server and -dial flags.
FilenameHandlers []FilenameHandler
Expand Down Expand Up @@ -107,6 +103,11 @@ type FilenameHandler struct {
// Pattern is a regular expression that matches filename.
Pattern string

// Language identifier (e.g. "go" or "python")
// See list of languages here:
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocumentItem
LanguageID string

// ServerKey is the key in Config.File.Servers.
ServerKey string
}
Expand Down
25 changes: 14 additions & 11 deletions internal/lsp/acmelsp/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func dialServer(cs *config.Server, cfg *ClientConfig) (*Server, error) {
// ServerInfo holds information about a LSP server and optionally a connection to it.
type ServerInfo struct {
*config.Server
*config.FilenameHandler

Re *regexp.Regexp // filename regular expression
Logger *log.Logger // Logger for config.Server.LogFile
Expand Down Expand Up @@ -169,7 +170,7 @@ func NewServerSet(cfg *config.Config, diagWriter DiagnosticsWriter) (*ServerSet,
}

var data []*ServerInfo
for _, h := range cfg.FilenameHandlers {
for i, h := range cfg.FilenameHandlers {
cs, ok := cfg.Servers[h.ServerKey]
if !ok {
return nil, fmt.Errorf("server not found for key %q", h.ServerKey)
Expand All @@ -190,9 +191,10 @@ func NewServerSet(cfg *config.Config, diagWriter DiagnosticsWriter) (*ServerSet,
logger = log.New(f, "", log.LstdFlags)
}
data = append(data, &ServerInfo{
Server: cs,
Re: re,
Logger: logger,
Server: cs,
FilenameHandler: &cfg.FilenameHandlers[i],
Re: re,
Logger: logger,
})
}
return &ServerSet{
Expand All @@ -214,13 +216,14 @@ func (ss *ServerSet) MatchFile(filename string) *ServerInfo {

func (ss *ServerSet) ClientConfig(info *ServerInfo) *ClientConfig {
return &ClientConfig{
Server: info.Server,
RootDirectory: ss.cfg.RootDirectory,
HideDiag: ss.cfg.HideDiagnostics,
RPCTrace: ss.cfg.RPCTrace,
DiagWriter: ss.diagWriter,
Workspaces: ss.Workspaces(),
Logger: info.Logger,
Server: info.Server,
FilenameHandler: info.FilenameHandler,
RootDirectory: ss.cfg.RootDirectory,
HideDiag: ss.cfg.HideDiagnostics,
RPCTrace: ss.cfg.RPCTrace,
DiagWriter: ss.diagWriter,
Workspaces: ss.Workspaces(),
Logger: info.Logger,
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/acmelsp/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (fm *FileManager) didOpen(winid int, name string) error {
if err != nil {
return err
}
return lsp.DidOpen(context.Background(), c, name, b)
return lsp.DidOpen(context.Background(), c, name, c.cfg.FilenameHandler.LanguageID, b)
})
}

Expand Down
7 changes: 5 additions & 2 deletions internal/lsp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,14 @@ func LocationLink(l *protocol.Location) string {
l.Range.End.Line+1, l.Range.End.Character+1)
}

func DidOpen(ctx context.Context, server protocol.Server, filename string, body []byte) error {
func DidOpen(ctx context.Context, server protocol.Server, filename string, lang string, body []byte) error {
if lang == "" {
lang = DetectLanguage(filename)
}
return server.DidOpen(ctx, &protocol.DidOpenTextDocumentParams{
TextDocument: protocol.TextDocumentItem{
URI: text.ToURI(filename),
LanguageID: DetectLanguage(filename),
LanguageID: lang,
Version: 0,
Text: string(body),
},
Expand Down

0 comments on commit 7c528a8

Please sign in to comment.