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:
parent
8308f91286
commit
128c804424
|
@ -183,7 +183,8 @@ func (s *session) Logger() xlog.Logger {
|
||||||
return s.log
|
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.
|
// Mark the file as open.
|
||||||
s.openFiles.Store(uri, true)
|
s.openFiles.Store(uri, true)
|
||||||
|
|
||||||
|
|
|
@ -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 {
|
func (c *connection) AddFile(ctx context.Context, uri span.URI) *cmdFile {
|
||||||
c.Client.filesMu.Lock()
|
c.Client.filesMu.Lock()
|
||||||
defer c.Client.filesMu.Unlock()
|
defer c.Client.filesMu.Unlock()
|
||||||
|
|
||||||
file := c.Client.getFile(ctx, uri)
|
file := c.Client.getFile(ctx, uri)
|
||||||
if !file.added {
|
if !file.added {
|
||||||
file.added = true
|
file.added = true
|
||||||
p := &protocol.DidOpenTextDocumentParams{}
|
p := &protocol.DidOpenTextDocumentParams{}
|
||||||
p.TextDocument.URI = string(uri)
|
p.TextDocument.URI = string(uri)
|
||||||
p.TextDocument.Text = string(file.mapper.Content)
|
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 {
|
if err := c.Server.DidOpen(ctx, p); err != nil {
|
||||||
file.err = fmt.Errorf("%v: %v", uri, err)
|
file.err = fmt.Errorf("%v: %v", uri, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,9 +9,41 @@ import (
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/token"
|
"go/token"
|
||||||
"go/types"
|
"go/types"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"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.
|
// indexExprAtPos returns the index of the expression containing pos.
|
||||||
func indexExprAtPos(pos token.Pos, args []ast.Expr) int {
|
func indexExprAtPos(pos token.Pos, args []ast.Expr) int {
|
||||||
for i, expr := range args {
|
for i, expr := range args {
|
||||||
|
|
|
@ -154,7 +154,7 @@ type Session interface {
|
||||||
FileSystem
|
FileSystem
|
||||||
|
|
||||||
// DidOpen is invoked each time a file is opened in the editor.
|
// 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 is invoked each time an open file is saved in the editor.
|
||||||
DidSave(uri span.URI)
|
DidSave(uri span.URI)
|
||||||
|
|
|
@ -20,8 +20,11 @@ func (s *Server) didOpen(ctx context.Context, params *protocol.DidOpenTextDocume
|
||||||
uri := span.NewURI(params.TextDocument.URI)
|
uri := span.NewURI(params.TextDocument.URI)
|
||||||
text := []byte(params.TextDocument.Text)
|
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.
|
// Open the file.
|
||||||
s.session.DidOpen(ctx, uri, text)
|
s.session.DidOpen(ctx, uri, fileKind, text)
|
||||||
|
|
||||||
// Run diagnostics on the newly-changed file.
|
// Run diagnostics on the newly-changed file.
|
||||||
view := s.session.ViewOf(uri)
|
view := s.session.ViewOf(uri)
|
||||||
|
|
Loading…
Reference in New Issue