@@ -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
124123func (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
170168func (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-
257245func (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
0 commit comments