Skip to content

Commit

Permalink
Better differentiation between func body and struct literal.
Browse files Browse the repository at this point in the history
  • Loading branch information
nsf committed Oct 7, 2017
1 parent c7fddb3 commit 1a78dd6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
Empty file added _testing/test.0061/cursor.61
Empty file.
4 changes: 4 additions & 0 deletions _testing/test.0061/out.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Found 3 candidates:
func Foo() *http.Request
func main()
package http
10 changes: 10 additions & 0 deletions _testing/test.0061/test.go.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import "net/http"

func Foo() *http.Request {

}

func main() {
}
44 changes: 28 additions & 16 deletions cursorcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,27 @@ func (this *token_iterator) skip_to_left_curly() bool {
return this.skip_to_left(token.LBRACE, token.RBRACE)
}

func (ti *token_iterator) extract_type_alike() string {
if ti.token().tok != token.IDENT { // not Foo, return nothing
return ""
}
b := ti.token().literal()
if !ti.go_back() { // just Foo
return b
}
if ti.token().tok != token.PERIOD { // not .Foo, return Foo
return b
}
if !ti.go_back() { // just .Foo, return Foo (best choice recovery)
return b
}
if ti.token().tok != token.IDENT { // not lib.Foo, return Foo
return b
}
ti.go_back()
return ti.token().literal() + "." + b // lib.Foo
}

// Extract the type expression right before the enclosing curly bracket block.
// Examples (# - the cursor):
// &lib.Struct{Whatever: 1, Hel#} // returns "lib.Struct"
Expand All @@ -130,30 +151,21 @@ func (ti *token_iterator) extract_struct_type() string {
if !ti.go_back() {
return ""
}
if ti.token().tok == token.LBRACE {
if ti.token().tok == token.LBRACE { // Foo{#{}}
if !ti.go_back() {
return ""
}
} else if ti.token().tok == token.COMMA {
} else if ti.token().tok == token.COMMA { // Foo{abc,#{}}
return ti.extract_struct_type()
}
if ti.token().tok != token.IDENT {
typ := ti.extract_type_alike()
if typ == "" {
return ""
}
b := ti.token().literal()
if !ti.go_back() {
return b
}
if ti.token().tok != token.PERIOD {
return b
}
if !ti.go_back() {
return b
}
if ti.token().tok != token.IDENT {
return b
if ti.token().tok == token.RPAREN || ti.token().tok == token.MUL {
return ""
}
return ti.token().literal() + "." + b
return typ
}

// Starting from the token under the cursor move back and extract something
Expand Down

0 comments on commit 1a78dd6

Please sign in to comment.