@@ -12,7 +12,6 @@ import (
12
12
"go/token"
13
13
"go/types"
14
14
"log"
15
- "path/filepath"
16
15
"sort"
17
16
"strings"
18
17
"time"
@@ -94,7 +93,7 @@ func (s *Server) computeSemanticTokens(ctx context.Context, td protocol.TextDocu
94
93
if err != nil {
95
94
return nil , err
96
95
}
97
- // ignore pgf.ParseErr. Do what we can.
96
+ // don't return errors on pgf.ParseErr. Do what we can.
98
97
if rng == nil && len (pgf .Src ) > maxFullFileSize {
99
98
err := fmt .Errorf ("semantic tokens: file %s too large for full (%d>%d)" ,
100
99
fh .URI ().Filename (), len (pgf .Src ), maxFullFileSize )
@@ -123,7 +122,6 @@ func (s *Server) computeSemanticTokens(ctx context.Context, td protocol.TextDocu
123
122
124
123
func (e * encoded ) semantics () {
125
124
f := e .pgf .File
126
- // may not be in range, but harmless
127
125
e .token (f .Package , len ("package" ), tokKeyword , nil )
128
126
e .token (f .Name .NamePos , len (f .Name .Name ), tokNamespace , nil )
129
127
inspect := func (n ast.Node ) bool {
@@ -168,11 +166,8 @@ const (
168
166
)
169
167
170
168
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" )
176
171
}
177
172
if start >= e .end || start + token .Pos (leng ) <= e .start {
178
173
return
@@ -191,7 +186,10 @@ func (e *encoded) token(start token.Pos, leng int, typ tokenType, mods []string)
191
186
return
192
187
}
193
188
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 )
195
193
return
196
194
}
197
195
// token is all on one line
@@ -238,22 +236,12 @@ func (e *encoded) strStack() string {
238
236
if len (e .stack ) > 0 {
239
237
loc := e .stack [len (e .stack )- 1 ].Pos ()
240
238
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 ))
243
240
}
244
241
msg = append (msg , "]" )
245
242
return strings .Join (msg , " " )
246
243
}
247
244
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
-
257
245
func (e * encoded ) inspector (n ast.Node ) bool {
258
246
pop := func () {
259
247
e .stack = e .stack [:len (e .stack )- 1 ]
@@ -421,26 +409,6 @@ func (e *encoded) ident(x *ast.Ident) {
421
409
use := e .ti .Uses [x ]
422
410
switch y := use .(type ) {
423
411
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" )
444
412
e .token (x .NamePos , len (x .Name ), tokVariable , []string {"definition" })
445
413
case * types.Builtin :
446
414
e .token (x .NamePos , len (x .Name ), tokFunction , []string {"defaultLibrary" })
@@ -674,21 +642,16 @@ func (e *encoded) importSpec(d *ast.ImportSpec) {
674
642
}
675
643
// and fall through for _
676
644
}
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 == "" {
680
646
return
681
647
}
682
- nm := val [1 : len (val )- 1 ] // remove surrounding "s
648
+ nm := d . Path . Value [1 : len (d . Path . Value )- 1 ] // trailing "
683
649
v := strings .LastIndex (nm , "/" )
684
650
if v != - 1 {
685
- // in import "lib/math", 'math' is the package name
686
651
nm = nm [v + 1 :]
687
652
}
688
653
start := d .Path .End () - token .Pos (1 + len (nm ))
689
654
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.)
692
655
}
693
656
694
657
// log unexpected state
0 commit comments