Entry point from autocompletion, the function looks at text before the cursor and figures out the declaration the cursor is on. This declaration is used in filtering the resulting set of autocompletion suggestions.
(file []byte, cursor int)
| 301 | // and figures out the declaration the cursor is on. This declaration is |
| 302 | // used in filtering the resulting set of autocompletion suggestions. |
| 303 | func (c *auto_complete_context) deduce_cursor_context(file []byte, cursor int) (cursor_context, bool) { |
| 304 | if cursor <= 0 { |
| 305 | return cursor_context{}, true |
| 306 | } |
| 307 | |
| 308 | iter := new_token_iterator(file, cursor) |
| 309 | if len(iter.tokens) == 0 { |
| 310 | return cursor_context{}, false |
| 311 | } |
| 312 | |
| 313 | // figure out what is just before the cursor |
| 314 | switch tok := iter.token(); tok.tok { |
| 315 | case token.STRING: |
| 316 | // make sure cursor is inside the string |
| 317 | s := tok.literal() |
| 318 | if len(s) > 1 && s[len(s)-1] == '"' && tok.off+len(s) <= cursor { |
| 319 | return cursor_context{}, true |
| 320 | } |
| 321 | // now figure out if inside an import declaration |
| 322 | var ptok = token.STRING |
| 323 | for iter.go_back() { |
| 324 | itok := iter.token().tok |
| 325 | switch itok { |
| 326 | case token.STRING: |
| 327 | switch ptok { |
| 328 | case token.SEMICOLON, token.IDENT, token.PERIOD: |
| 329 | default: |
| 330 | return cursor_context{}, true |
| 331 | } |
| 332 | case token.LPAREN, token.SEMICOLON: |
| 333 | switch ptok { |
| 334 | case token.STRING, token.IDENT, token.PERIOD: |
| 335 | default: |
| 336 | return cursor_context{}, true |
| 337 | } |
| 338 | case token.IDENT, token.PERIOD: |
| 339 | switch ptok { |
| 340 | case token.STRING: |
| 341 | default: |
| 342 | return cursor_context{}, true |
| 343 | } |
| 344 | case token.IMPORT: |
| 345 | switch ptok { |
| 346 | case token.STRING, token.IDENT, token.PERIOD, token.LPAREN: |
| 347 | path_len := cursor - tok.off |
| 348 | path := s[1:path_len] |
| 349 | return cursor_context{decl_import: true, partial: path}, true |
| 350 | default: |
| 351 | return cursor_context{}, true |
| 352 | } |
| 353 | default: |
| 354 | return cursor_context{}, true |
| 355 | } |
| 356 | ptok = itok |
| 357 | } |
| 358 | case token.PERIOD: |
| 359 | // we're '<whatever>.' |
| 360 | // figure out decl, Partial is "" |
no test coverage detected