vls: autocomplete for function signatures
Some checks failed
Tools CI / tools-macos (clang) (push) Waiting to run
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
native backend CI / native-backend-ubuntu (push) Has been cancelled
native backend CI / native-backend-windows (push) Has been cancelled
wasm backend CI / wasm-backend (ubuntu-22.04) (push) Has been cancelled
wasm backend CI / wasm-backend (windows-2022) (push) Has been cancelled

This commit is contained in:
Alexander Medvednikov 2025-08-15 19:00:51 +03:00
parent 63997d6e43
commit d51277e2fc
4 changed files with 55 additions and 17 deletions

View file

@ -21,6 +21,17 @@ fn abs(a int) int {
pub fn (mut c Checker) run_ac(ast_file &ast.File) { pub fn (mut c Checker) run_ac(ast_file &ast.File) {
} }
pub fn (mut c Checker) autocomplete_for_fn_call_expr() {
// println(c.pref.linfo.expr)
fn_name := c.pref.linfo.expr.replace('()', '').trim_space()
f := c.table.find_fn(fn_name) or {
println('failed to find fn "${fn_name}"')
return
}
res := c.build_fn_summary(f)
println(res)
}
fn (mut c Checker) ident_autocomplete(node ast.Ident) { fn (mut c Checker) ident_autocomplete(node ast.Ident) {
// Mini LS hack (v -line-info "a.v:16") // Mini LS hack (v -line-info "a.v:16")
if c.pref.is_verbose { if c.pref.is_verbose {
@ -45,6 +56,7 @@ fn (mut c Checker) ident_autocomplete(node ast.Ident) {
} }
// Module autocomplete // Module autocomplete
// `os. ...` // `os. ...`
// println(node)
if node.name == '' && node.mod != 'builtin' { if node.name == '' && node.mod != 'builtin' {
c.module_autocomplete(node) c.module_autocomplete(node)
return return
@ -165,3 +177,14 @@ fn build_method_summary(method ast.Fn) string {
} }
return s + ')' return s + ')'
} }
fn (c &Checker) build_fn_summary(method ast.Fn) string {
mut s := method.name + '('
for i, param in method.params {
s += param.name + ' ' + c.table.type_to_str(param.typ)
if i < method.params.len - 1 {
s += ', '
}
}
return s + ')'
}

View file

@ -433,6 +433,10 @@ pub fn (mut c Checker) check_files(ast_files []&ast.File) {
// println('setting is_running=true, pref.path=${c.pref.linfo.path} curdir' + os.getwd()) // println('setting is_running=true, pref.path=${c.pref.linfo.path} curdir' + os.getwd())
c.pref.linfo.is_running = true c.pref.linfo.is_running = true
// println('linfo path=${c.pref.linfo.path}') // println('linfo path=${c.pref.linfo.path}')
if c.pref.linfo.expr.contains('()') {
c.autocomplete_for_fn_call_expr()
exit(0)
}
for i, file in ast_files { for i, file in ast_files {
// println(file.path) // println(file.path)
if file.path == c.pref.linfo.path { if file.path == c.pref.linfo.path {

View file

@ -2,6 +2,16 @@
// Use of this source code is governed by an MIT license that can be found in the LICENSE file. // Use of this source code is governed by an MIT license that can be found in the LICENSE file.
module pref module pref
pub struct LineInfo {
pub mut:
line_nr int // a quick single file run when called with v -line-info (contains line nr to inspect)
path string // same, but stores the path being parsed
expr string // "os.foo()" V code (expression) which needs autocomplete, right only function calls
col int
is_running bool // so that line info is fetched only on the second checker run
vars_printed map[string]bool // to avoid dups
}
fn (mut p Preferences) parse_line_info(line string) { fn (mut p Preferences) parse_line_info(line string) {
// println("parse_line_info '${line}'") // println("parse_line_info '${line}'")
format_err := 'wrong format, use `-line-info "file.v:24:7"' format_err := 'wrong format, use `-line-info "file.v:24:7"'
@ -12,16 +22,27 @@ fn (mut p Preferences) parse_line_info(line string) {
} }
file_name := vals[0] file_name := vals[0]
line_nr := vals[1].int() - 1 line_nr := vals[1].int() - 1
col := vals[2].int() - 1
// expr := vals[2]
if !file_name.ends_with('.v') || line_nr == -1 { if !file_name.ends_with('.v') || line_nr == -1 {
eprintln(format_err) eprintln(format_err)
return return
} }
// Third value can be a column or expression for autocomplete like `os.create()`
third := vals[2]
if third[0].is_digit() {
col := vals[2].int() - 1
p.linfo = LineInfo{ p.linfo = LineInfo{
line_nr: line_nr line_nr: line_nr
path: file_name path: file_name
// expr: expr
col: col col: col
} }
} else {
expr := vals[2]
p.linfo = LineInfo{
line_nr: line_nr
path: file_name
expr: expr
}
}
} }

View file

@ -262,16 +262,6 @@ pub mut:
json_errors bool // -json-errors, for VLS and other tools json_errors bool // -json-errors, for VLS and other tools
} }
pub struct LineInfo {
pub mut:
line_nr int // a quick single file run when called with v -line-info (contains line nr to inspect)
path string // same, but stores the path being parsed
// expr string // "foo" or "foo.bar" V code (expression) which needs autocomplete
col int
is_running bool // so that line info is fetched only on the second checker run
vars_printed map[string]bool // to avoid dups
}
pub fn parse_args(known_external_commands []string, args []string) (&Preferences, string) { pub fn parse_args(known_external_commands []string, args []string) (&Preferences, string) {
return parse_args_and_show_errors(known_external_commands, args, false) return parse_args_and_show_errors(known_external_commands, args, false)
} }