Skip to content

Commit cb1acef

Browse files
committed
Revert "internal/lsp/semantic.go: repress useless messages and tighten logic"
This reverts commit 5b540d3. Reason for revert: left in logging statements Change-Id: I9ef5cd79e9ae8c94098fceca3a356fa3377c16e7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/333711 Trust: Peter Weinberger <[email protected]> Reviewed-by: Robert Findley <[email protected]> Run-TryBot: Peter Weinberger <[email protected]>
1 parent 5b540d3 commit cb1acef

File tree

3 files changed

+11
-54
lines changed

3 files changed

+11
-54
lines changed

internal/lsp/semantic.go

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"go/token"
1313
"go/types"
1414
"log"
15-
"path/filepath"
1615
"sort"
1716
"strings"
1817
"time"
@@ -94,7 +93,7 @@ func (s *Server) computeSemanticTokens(ctx context.Context, td protocol.TextDocu
9493
if err != nil {
9594
return nil, err
9695
}
97-
// ignore pgf.ParseErr. Do what we can.
96+
// don't return errors on pgf.ParseErr. Do what we can.
9897
if rng == nil && len(pgf.Src) > maxFullFileSize {
9998
err := fmt.Errorf("semantic tokens: file %s too large for full (%d>%d)",
10099
fh.URI().Filename(), len(pgf.Src), maxFullFileSize)
@@ -123,7 +122,6 @@ func (s *Server) computeSemanticTokens(ctx context.Context, td protocol.TextDocu
123122

124123
func (e *encoded) semantics() {
125124
f := e.pgf.File
126-
// may not be in range, but harmless
127125
e.token(f.Package, len("package"), tokKeyword, nil)
128126
e.token(f.Name.NamePos, len(f.Name.Name), tokNamespace, nil)
129127
inspect := func(n ast.Node) bool {
@@ -168,11 +166,8 @@ const (
168166
)
169167

170168
func (e *encoded) token(start token.Pos, leng int, typ tokenType, mods []string) {
171-
172-
if start == token.NoPos {
173-
// This is not worth reporting
174-
//e.unexpected("token at token.NoPos")
175-
return
169+
if start == 0 {
170+
e.unexpected("token at token.NoPos")
176171
}
177172
if start >= e.end || start+token.Pos(leng) <= e.start {
178173
return
@@ -191,7 +186,10 @@ func (e *encoded) token(start token.Pos, leng int, typ tokenType, mods []string)
191186
return
192187
}
193188
if lspRange.End.Line != lspRange.Start.Line {
194-
// this happens if users are typing at the end of the file, but report nothing
189+
// abrupt end of file, without \n. TODO(pjw): fix?
190+
pos := e.fset.PositionFor(start, false)
191+
msg := fmt.Sprintf("token at %s:%d.%d overflows", pos.Filename, pos.Line, pos.Column)
192+
event.Log(e.ctx, msg)
195193
return
196194
}
197195
// token is all on one line
@@ -238,22 +236,12 @@ func (e *encoded) strStack() string {
238236
if len(e.stack) > 0 {
239237
loc := e.stack[len(e.stack)-1].Pos()
240238
add := e.pgf.Tok.PositionFor(loc, false)
241-
nm := filepath.Base(add.Filename)
242-
msg = append(msg, fmt.Sprintf("(%s:%d,col:%d)", nm, add.Line, add.Column))
239+
msg = append(msg, fmt.Sprintf("(line:%d,col:%d)", add.Line, add.Column))
243240
}
244241
msg = append(msg, "]")
245242
return strings.Join(msg, " ")
246243
}
247244

248-
func (e *encoded) srcLine(x ast.Node) string {
249-
file := e.pgf.Tok
250-
line := file.Line(x.Pos())
251-
start := file.Offset(file.LineStart(line))
252-
end := file.Offset(file.LineStart(line + 1)) // and hope it's not the last line of the file
253-
ans := e.pgf.Src[start : end-1]
254-
return string(ans)
255-
}
256-
257245
func (e *encoded) inspector(n ast.Node) bool {
258246
pop := func() {
259247
e.stack = e.stack[:len(e.stack)-1]
@@ -421,26 +409,6 @@ func (e *encoded) ident(x *ast.Ident) {
421409
use := e.ti.Uses[x]
422410
switch y := use.(type) {
423411
case nil:
424-
// In this position we think the identifier is either a function or a variable
425-
// and it is possible that it is being defined. The decision has to be made based
426-
// on where we are in the parse tree (and all that's known is the parse stack).
427-
// The present logic is inadequate, and will be fixed in the next CL:
428-
// ExprStmt CallExpr Ident: var [x in a(x)]
429-
// ExprStmt CallExpr Ident: function [f()]
430-
// CallExpr TypeAssertExpr Ident: type [so not variable nor function]
431-
log.SetFlags(log.Lshortfile)
432-
log.Printf("%s %s%q", x.Name, e.strStack(), e.srcLine(x))
433-
if len(e.stack) >= 3 {
434-
n := len(e.stack) - 1
435-
if _, ok := e.stack[n-1].(*ast.SelectorExpr); ok {
436-
if _, ok = e.stack[n-2].(*ast.CallExpr); ok {
437-
log.Print("function")
438-
e.token(x.NamePos, len(x.Name), tokFunction, nil)
439-
break
440-
}
441-
}
442-
}
443-
log.Print("var def")
444412
e.token(x.NamePos, len(x.Name), tokVariable, []string{"definition"})
445413
case *types.Builtin:
446414
e.token(x.NamePos, len(x.Name), tokFunction, []string{"defaultLibrary"})
@@ -674,21 +642,16 @@ func (e *encoded) importSpec(d *ast.ImportSpec) {
674642
}
675643
// and fall through for _
676644
}
677-
val := d.Path.Value
678-
if len(val) < 2 || val[0] != '"' || val[len(val)-1] != '"' {
679-
// avoid panics on imports without a properly quoted string
645+
if d.Path.Value == "" {
680646
return
681647
}
682-
nm := val[1 : len(val)-1] // remove surrounding "s
648+
nm := d.Path.Value[1 : len(d.Path.Value)-1] // trailing "
683649
v := strings.LastIndex(nm, "/")
684650
if v != -1 {
685-
// in import "lib/math", 'math' is the package name
686651
nm = nm[v+1:]
687652
}
688653
start := d.Path.End() - token.Pos(1+len(nm))
689654
e.token(start, len(nm), tokNamespace, nil)
690-
// There may be more cases, as import strings are implementation defined.
691-
// (E.g., module a.b.c (without a /), the 'a' should be tokNamespace, if we cared.)
692655
}
693656

694657
// log unexpected state

internal/lsp/testdata/semantic/a.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ func (a *A) f() bool {
5555
w := b[4:]
5656
j := len(x)
5757
j--
58-
q := []interface{}{j, 23i, &y}
59-
g(q...)
6058
return true
6159
}
6260

@@ -76,6 +74,5 @@ Never:
7674
if !ok {
7775
switch x := vv[0].(type) {
7876
}
79-
goto Never
8077
}
8178
}

internal/lsp/testdata/semantic/a.go.golden

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,13 @@
5656
/*⇒1,variable,[definition]*/w /*⇒2,operator,[]*/:= /*⇒1,variable,[]*/b[/*⇒1,number,[]*/4:]
5757
/*⇒1,variable,[definition]*/j /*⇒2,operator,[]*/:= /*⇒3,function,[defaultLibrary]*/len(/*⇒1,variable,[]*/x)
5858
/*⇒1,variable,[]*/j/*⇒2,operator,[]*/--
59-
/*⇒1,variable,[definition]*/q /*⇒2,operator,[]*/:= []/*⇒9,keyword,[]*/interface{}{/*⇒1,variable,[]*/j, /*⇒3,number,[]*/23i, /*⇒1,operator,[]*/&/*⇒1,variable,[]*/y}
60-
/*⇒1,function,[]*/g(/*⇒1,variable,[]*/q/*⇒3,operator,[]*/...)
6159
/*⇒6,keyword,[]*/return /*⇒4,variable,[readonly]*/true
6260
}
6361

6462
/*⇒4,keyword,[]*/func /*⇒1,function,[definition]*/g(/*⇒2,parameter,[definition]*/vv /*⇒3,operator,[]*/.../*⇒9,keyword,[]*/interface{}) {
6563
/*⇒2,variable,[definition]*/ff /*⇒2,operator,[]*/:= /*⇒4,keyword,[]*/func() {}
6664
/*⇒5,keyword,[]*/defer /*⇒2,variable,[]*/ff()
67-
/*⇒2,keyword,[]*/go /*⇒3,namespace,[]*/utf./*⇒9,function,[]*/RuneCount(/*⇒2,string,[]*/"")
65+
/*⇒2,keyword,[]*/go /*⇒3,namespace,[]*/utf./*⇒9,variable,[definition]*/RuneCount(/*⇒2,string,[]*/"")
6866
/*⇒2,keyword,[]*/go /*⇒4,namespace,[]*/utf8./*⇒9,function,[]*/RuneCount(/*⇒2,variable,[]*/vv.(/*⇒6,variable,[definition]*/string))
6967
/*⇒2,keyword,[]*/if /*⇒4,variable,[readonly]*/true {
7068
} /*⇒4,keyword,[]*/else {
@@ -77,7 +75,6 @@
7775
/*⇒2,keyword,[]*/if /*⇒1,operator,[]*/!/*⇒2,variable,[]*/ok {
7876
/*⇒6,keyword,[]*/switch /*⇒1,variable,[definition]*/x /*⇒2,operator,[]*/:= /*⇒2,variable,[]*/vv[/*⇒1,number,[]*/0].(/*⇒4,keyword,[]*/type) {
7977
}
80-
/*⇒4,keyword,[]*/goto Never
8178
}
8279
}
8380

0 commit comments

Comments
 (0)