1
0
mirror of https://github.com/golang/go synced 2024-11-18 14:54:40 -07:00

internal/lsp: handle language ID in didOpen calls

This change merely modifies session.DidOpen to accept the document's
language ID. It does not actually add any handling of the language ID.

Change-Id: I2582ae307d1ca062f37b4683907cdbcfdfc61809
Reviewed-on: https://go-review.googlesource.com/c/tools/+/184160
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Rebecca Stambler 2019-06-27 14:59:09 -04:00
parent 8308f91286
commit 128c804424
5 changed files with 41 additions and 3 deletions

View File

@ -183,7 +183,8 @@ func (s *session) Logger() xlog.Logger {
return s.log
}
func (s *session) DidOpen(ctx context.Context, uri span.URI, text []byte) {
// TODO: Propagate the language ID through to the view.
func (s *session) DidOpen(ctx context.Context, uri span.URI, _ source.FileKind, text []byte) {
// Mark the file as open.
s.openFiles.Store(uri, true)

View File

@ -335,12 +335,14 @@ func (c *cmdClient) getFile(ctx context.Context, uri span.URI) *cmdFile {
func (c *connection) AddFile(ctx context.Context, uri span.URI) *cmdFile {
c.Client.filesMu.Lock()
defer c.Client.filesMu.Unlock()
file := c.Client.getFile(ctx, uri)
if !file.added {
file.added = true
p := &protocol.DidOpenTextDocumentParams{}
p.TextDocument.URI = string(uri)
p.TextDocument.Text = string(file.mapper.Content)
p.TextDocument.LanguageID = source.DetectLanguage("", file.uri.Filename()).String()
if err := c.Server.DidOpen(ctx, p); err != nil {
file.err = fmt.Errorf("%v: %v", uri, err)
}

View File

@ -9,9 +9,41 @@ import (
"go/ast"
"go/token"
"go/types"
"path/filepath"
"strings"
)
func DetectLanguage(langID, filename string) FileKind {
switch langID {
case "go":
return Go
case "go.mod":
return Mod
case "go.sum":
return Sum
}
// Fallback to detecting the language based on the file extension.
switch filepath.Ext(filename) {
case ".mod":
return Mod
case ".sum":
return Sum
default: // fallback to Go
return Go
}
}
func (k FileKind) String() string {
switch k {
case Mod:
return "go.mod"
case Sum:
return "go.sum"
default:
return "go"
}
}
// indexExprAtPos returns the index of the expression containing pos.
func indexExprAtPos(pos token.Pos, args []ast.Expr) int {
for i, expr := range args {

View File

@ -154,7 +154,7 @@ type Session interface {
FileSystem
// DidOpen is invoked each time a file is opened in the editor.
DidOpen(ctx context.Context, uri span.URI, text []byte)
DidOpen(ctx context.Context, uri span.URI, kind FileKind, text []byte)
// DidSave is invoked each time an open file is saved in the editor.
DidSave(uri span.URI)

View File

@ -20,8 +20,11 @@ func (s *Server) didOpen(ctx context.Context, params *protocol.DidOpenTextDocume
uri := span.NewURI(params.TextDocument.URI)
text := []byte(params.TextDocument.Text)
// Confirm that the file's language ID is related to Go.
fileKind := source.DetectLanguage(params.TextDocument.LanguageID, uri.Filename())
// Open the file.
s.session.DidOpen(ctx, uri, text)
s.session.DidOpen(ctx, uri, fileKind, text)
// Run diagnostics on the newly-changed file.
view := s.session.ViewOf(uri)