@@ -18,30 +18,39 @@ fn abs(a int) int {
1818 return a
1919}
2020
21+ pub fn (mut c Checker) run_ac (ast_file & ast.File) {
22+ }
23+
2124fn (mut c Checker) ident_autocomplete (node ast.Ident) {
2225 // Mini LS hack (v -line-info "a.v:16")
2326 if c.pref.is_verbose {
2427 println (
2528 'checker.ident_autocomplete() info.line_nr=${c .pref .linfo .line_nr } node.line_nr=${node .pos .line_nr } ' +
2629 ' node.col=${node .pos .col } pwd="${os .getwd ()}" file="${c .file .path }", ' +
2730 // ' pref.linfo.path="${c.pref.linfo.path}" node.name="${node.name}" expr="${c.pref.linfo.expr}"')
28- ' pref.linfo.path="${c .pref .linfo .path }" node.name="${node .name }" col="${c .pref .linfo .col }"' )
31+ ' pref.linfo.path="${c .pref .linfo .path }" node.name="${node .name }" node.mod="${ node . mod }" col="${c .pref .linfo .col }"' )
2932 }
3033 // Make sure this ident is on the same line as requeste, in the same file, and has the same name
3134 same_line := node.pos.line_nr in [c.pref.linfo.line_nr - 1 , c.pref.linfo.line_nr + 1 , c.pref.linfo.line_nr]
3235 if ! same_line {
3336 return
3437 }
35- // same_name := c.pref.linfo.expr == node.name
3638 same_col := abs (c.pref.linfo.col - node.pos.col) < 3
37- // if !same_name {
3839 if ! same_col {
3940 return
4041 }
4142 abs_path := os.join_path (os.getwd (), c.file.path)
4243 if c.pref.linfo.path ! in [c.file.path, abs_path] {
4344 return
4445 }
46+ // Module autocomplete
47+ // `os. ...`
48+ if node.name == '' && node.mod != 'builtin' {
49+ c.module_autocomplete (node)
50+ return
51+ } else if node.name == '' && node.mod == 'builtin' {
52+ return
53+ }
4554 mut sb := strings.new_builder (10 )
4655 if node.kind == .unresolved {
4756 // println(node)
@@ -67,6 +76,7 @@ fn (mut c Checker) ident_autocomplete(node ast.Ident) {
6776 */
6877
6978 mut fields := []ACFieldMethod{cap: 10 }
79+ mut methods := []ACFieldMethod{cap: 10 }
7080 if sym.kind == .struct {
7181 // Add fields, but only if it's a struct.
7282 struct_info := sym.info as ast.Struct
@@ -85,11 +95,13 @@ fn (mut c Checker) ident_autocomplete(node ast.Ident) {
8595 fields << ACFieldMethod{'cap' , 'int' }
8696 }
8797 // array_info := sym.info as ast.Array
98+ } else if sym.kind == .string {
99+ fields << ACFieldMethod{'len' , 'int' }
88100 }
89101 // Aliases and other types can have methods, add them
90102 for method in sym.methods {
91103 method_ret_type := c.table.sym (method.return_type)
92- fields << ACFieldMethod{build_method_summary (method), method_ret_type.name}
104+ methods << ACFieldMethod{build_method_summary (method), method_ret_type.name}
93105 }
94106 fields.sort (a.name < b.name)
95107 for i, field in fields {
@@ -99,6 +111,14 @@ fn (mut c Checker) ident_autocomplete(node ast.Ident) {
99111 sb.writeln (', ' )
100112 }
101113 }
114+ sb.writeln ('\n\t ], "methods":[' )
115+
116+ for i, method in methods {
117+ sb.write_string ('\t\t "${method .name }:${method .typ }"' )
118+ if i < methods.len - 1 {
119+ sb.writeln (', ' )
120+ }
121+ }
102122 sb.writeln ('\n\t ]\n }' )
103123 res := sb.str ().trim_space ()
104124 if res != '' {
@@ -108,12 +128,39 @@ fn (mut c Checker) ident_autocomplete(node ast.Ident) {
108128 }
109129}
110130
131+ fn (mut c Checker) module_autocomplete (node ast.Ident) {
132+ mut sb := strings.new_builder (10 )
133+ // println(c.table.fns)
134+ sb.writeln ('{"methods":[' )
135+ prefix := node.mod + '.'
136+ mut empty := true
137+ for _, f in c.table.fns {
138+ mut name := f.name
139+ if name.starts_with (prefix) {
140+ empty = false
141+ if name.contains ('__static__' ) {
142+ name = name.replace ('__static__' , '.' )
143+ }
144+ name = name.after ('.' ) // The user already typed `mod.`, so suggest the name without module
145+ sb.writeln ('"${name }:int" ,' )
146+ }
147+ }
148+ if ! empty {
149+ sb.go_back (2 ) // remove final ,
150+ }
151+ sb.writeln (']}' )
152+ println (sb.str ().trim_space ())
153+ }
154+
111155fn build_method_summary (method ast.Fn) string {
112156 mut s := method.name + '('
113157 for i, param in method.params {
158+ if i == 0 {
159+ continue
160+ }
114161 s + = param.name
115162 if i < method.params.len - 1 {
116- s + = ','
163+ s + = ', '
117164 }
118165 }
119166 return s + ')'
0 commit comments