2020-02-19 16:12:39 +01:00
|
|
|
module doc
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
import os
|
2020-04-25 20:58:00 +02:00
|
|
|
import strings
|
2020-06-02 12:10:01 +02:00
|
|
|
import time
|
|
|
|
import v.ast
|
2020-09-21 16:40:39 +02:00
|
|
|
import v.checker
|
2020-06-02 12:10:01 +02:00
|
|
|
import v.fmt
|
|
|
|
import v.parser
|
2020-04-25 20:58:00 +02:00
|
|
|
import v.pref
|
2020-06-02 12:10:01 +02:00
|
|
|
import v.scanner
|
2020-04-25 20:58:00 +02:00
|
|
|
import v.table
|
2020-06-02 12:10:01 +02:00
|
|
|
import v.token
|
|
|
|
import v.util
|
2020-02-19 16:12:39 +01:00
|
|
|
|
2020-10-21 10:26:33 +02:00
|
|
|
// intentionally in order as a guide when arranging the docnodes
|
|
|
|
pub enum SymbolKind {
|
|
|
|
none_
|
|
|
|
typedef
|
|
|
|
interface_
|
|
|
|
const_group
|
|
|
|
const_field
|
|
|
|
enum_
|
|
|
|
variable
|
|
|
|
function
|
|
|
|
method
|
|
|
|
struct_
|
|
|
|
}
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
pub struct Doc {
|
2020-10-21 10:26:33 +02:00
|
|
|
prefs &pref.Preferences = new_vdoc_preferences()
|
2020-06-02 12:10:01 +02:00
|
|
|
pub mut:
|
2020-10-21 10:26:33 +02:00
|
|
|
input_path string
|
|
|
|
table &table.Table = &table.Table{}
|
|
|
|
checker checker.Checker = checker.Checker{
|
2020-09-21 16:40:39 +02:00
|
|
|
table: 0
|
|
|
|
cur_fn: 0
|
|
|
|
pref: 0
|
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
fmt fmt.Fmt
|
|
|
|
filename string
|
|
|
|
pos int
|
|
|
|
pub_only bool = true
|
|
|
|
with_comments bool = true
|
|
|
|
with_pos bool
|
|
|
|
with_head bool = true
|
|
|
|
is_vlib bool
|
|
|
|
time_generated time.Time
|
|
|
|
head DocNode
|
|
|
|
contents map[string]DocNode
|
|
|
|
scoped_contents map[string]DocNode
|
2020-02-19 16:12:39 +01:00
|
|
|
}
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
pub struct DocPos {
|
|
|
|
pub:
|
|
|
|
line int
|
2020-06-02 16:19:55 +02:00
|
|
|
col int
|
2020-09-21 16:40:39 +02:00
|
|
|
len int
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-02-26 22:44:29 +01:00
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
pub struct DocNode {
|
|
|
|
pub mut:
|
2020-10-21 10:26:33 +02:00
|
|
|
name string
|
|
|
|
content string
|
|
|
|
comment string
|
|
|
|
pos DocPos = DocPos{-1, -1, 0}
|
|
|
|
file_path string
|
|
|
|
kind SymbolKind
|
|
|
|
deprecated bool
|
|
|
|
parent_name string
|
|
|
|
children []DocNode
|
|
|
|
attrs map[string]string
|
|
|
|
from_scope bool
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
|
2020-07-17 19:13:22 +02:00
|
|
|
pub fn merge_comments(comments []ast.Comment) string {
|
2020-06-08 08:22:10 +02:00
|
|
|
mut res := []string{}
|
2020-07-17 19:13:22 +02:00
|
|
|
for comment in comments {
|
2020-10-15 22:12:59 +02:00
|
|
|
res << comment.text.trim_left('\x01')
|
2020-06-08 08:22:10 +02:00
|
|
|
}
|
|
|
|
return res.join('\n')
|
|
|
|
}
|
|
|
|
|
2020-07-17 19:13:22 +02:00
|
|
|
pub fn get_comment_block_right_before(comments []ast.Comment) string {
|
|
|
|
if comments.len == 0 {
|
2020-06-06 17:47:16 +02:00
|
|
|
return ''
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
mut comment := ''
|
2020-06-06 17:47:16 +02:00
|
|
|
mut last_comment_line_nr := 0
|
2020-07-17 19:13:22 +02:00
|
|
|
for i := comments.len - 1; i >= 0; i-- {
|
|
|
|
cmt := comments[i]
|
2020-06-06 17:47:16 +02:00
|
|
|
if last_comment_line_nr != 0 && cmt.pos.line_nr < last_comment_line_nr - 1 {
|
|
|
|
// skip comments that are not part of a continuous block,
|
|
|
|
// located right above the top level statement.
|
2020-09-09 14:24:42 +02:00
|
|
|
// break
|
2020-06-06 17:47:16 +02:00
|
|
|
}
|
2020-10-15 22:12:59 +02:00
|
|
|
mut cmt_content := cmt.text.trim_left('\x01')
|
2020-06-07 12:27:42 +02:00
|
|
|
if cmt_content.len == cmt.text.len || cmt.is_multi {
|
2020-06-06 17:47:16 +02:00
|
|
|
// ignore /* */ style comments for now
|
|
|
|
continue
|
2020-06-07 12:27:42 +02:00
|
|
|
// if cmt_content.len == 0 {
|
2020-09-09 14:24:42 +02:00
|
|
|
// continue
|
2020-06-07 12:27:42 +02:00
|
|
|
// }
|
|
|
|
// mut new_cmt_content := ''
|
|
|
|
// mut is_codeblock := false
|
|
|
|
// // println(cmt_content)
|
|
|
|
// lines := cmt_content.split_into_lines()
|
|
|
|
// for j, line in lines {
|
2020-09-09 14:24:42 +02:00
|
|
|
// trimmed := line.trim_space().trim_left(cmt_prefix)
|
|
|
|
// if trimmed.starts_with('- ') || (trimmed.len >= 2 && trimmed[0].is_digit() && trimmed[1] == `.`) || is_codeblock {
|
|
|
|
// new_cmt_content += line + '\n'
|
|
|
|
// } else if line.starts_with('```') {
|
|
|
|
// is_codeblock = !is_codeblock
|
|
|
|
// new_cmt_content += line + '\n'
|
|
|
|
// } else {
|
|
|
|
// new_cmt_content += trimmed + '\n'
|
|
|
|
// }
|
2020-06-07 12:27:42 +02:00
|
|
|
// }
|
|
|
|
// return new_cmt_content
|
2020-05-15 23:09:38 +02:00
|
|
|
}
|
2020-07-17 19:13:22 +02:00
|
|
|
// eprintln('cmt: $cmt')
|
|
|
|
cseparator := if cmt_content.starts_with('```') { '\n' } else { ' ' }
|
2020-06-06 17:47:16 +02:00
|
|
|
comment = cmt_content + cseparator + comment
|
|
|
|
last_comment_line_nr = cmt.pos.line_nr
|
2020-02-19 16:12:39 +01:00
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
return comment
|
2020-02-19 16:12:39 +01:00
|
|
|
}
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
fn convert_pos(file_path string, pos token.Position) DocPos {
|
2020-06-02 16:19:55 +02:00
|
|
|
source := util.read_file(file_path) or {
|
|
|
|
''
|
|
|
|
}
|
|
|
|
mut p := util.imax(0, util.imin(source.len - 1, pos.pos))
|
|
|
|
column := util.imax(0, pos.pos - p - 1)
|
|
|
|
return DocPos{
|
|
|
|
line: pos.line_nr + 1
|
|
|
|
col: util.imax(1, column + 1)
|
2020-09-21 16:40:39 +02:00
|
|
|
len: pos.len
|
2020-06-02 16:19:55 +02:00
|
|
|
}
|
2020-02-26 22:44:29 +01:00
|
|
|
}
|
|
|
|
|
2020-06-19 10:36:45 +02:00
|
|
|
pub fn (mut d Doc) get_signature(stmt ast.Stmt, file &ast.File) string {
|
2020-06-02 12:10:01 +02:00
|
|
|
match stmt {
|
|
|
|
ast.Module {
|
2020-06-19 10:36:45 +02:00
|
|
|
return 'module $stmt.name'
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
ast.FnDecl {
|
2020-07-11 15:09:12 +02:00
|
|
|
return stmt.stringify(d.table, d.fmt.cur_mod)
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-06-19 10:36:45 +02:00
|
|
|
d.fmt.out = strings.new_builder(1000)
|
|
|
|
d.fmt.stmt(stmt)
|
|
|
|
return d.fmt.out.str().trim_space()
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 22:44:29 +01:00
|
|
|
}
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
pub fn (d Doc) get_pos(stmt ast.Stmt) token.Position {
|
2020-10-21 10:26:33 +02:00
|
|
|
if stmt is ast.InterfaceDecl {
|
|
|
|
return stmt.pos
|
2020-02-19 16:12:39 +01:00
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
return stmt.position()
|
2020-02-19 16:12:39 +01:00
|
|
|
}
|
|
|
|
|
2020-10-21 10:26:33 +02:00
|
|
|
pub fn get_type_decl_name(decl ast.TypeDecl) string {
|
2020-06-07 12:27:42 +02:00
|
|
|
match decl {
|
2020-10-15 07:08:27 +02:00
|
|
|
ast.SumTypeDecl, ast.FnTypeDecl, ast.AliasTypeDecl { return decl.name }
|
2020-06-07 12:27:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
pub fn (d Doc) get_name(stmt ast.Stmt) string {
|
|
|
|
match stmt {
|
2020-10-15 07:08:27 +02:00
|
|
|
ast.FnDecl, ast.StructDecl, ast.EnumDecl, ast.InterfaceDecl { return stmt.name }
|
2020-10-21 10:26:33 +02:00
|
|
|
ast.TypeDecl { return get_type_decl_name(stmt) }
|
|
|
|
ast.ConstDecl { return '' } // leave it blank
|
|
|
|
else { return '' }
|
2020-02-19 16:12:39 +01:00
|
|
|
}
|
2020-02-26 22:44:29 +01:00
|
|
|
}
|
|
|
|
|
2020-08-28 07:53:12 +02:00
|
|
|
pub fn new_vdoc_preferences() &pref.Preferences {
|
|
|
|
// vdoc should be able to parse as much user code as possible
|
|
|
|
// so its preferences should be permissive:
|
2020-09-09 14:24:42 +02:00
|
|
|
return &pref.Preferences{
|
|
|
|
enable_globals: true
|
|
|
|
}
|
2020-08-28 07:53:12 +02:00
|
|
|
}
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
pub fn new(input_path string) Doc {
|
2020-06-19 10:36:45 +02:00
|
|
|
mut d := Doc{
|
2020-06-02 16:19:55 +02:00
|
|
|
input_path: os.real_path(input_path)
|
|
|
|
table: table.new_table()
|
|
|
|
head: DocNode{}
|
2020-10-21 10:26:33 +02:00
|
|
|
contents: map[string]DocNode{}
|
2020-06-02 12:10:01 +02:00
|
|
|
time_generated: time.now()
|
|
|
|
}
|
2020-06-19 10:36:45 +02:00
|
|
|
d.fmt = fmt.Fmt{
|
|
|
|
indent: 0
|
|
|
|
is_debug: false
|
2020-07-17 19:13:22 +02:00
|
|
|
table: d.table
|
2020-06-19 10:36:45 +02:00
|
|
|
}
|
2020-09-21 16:40:39 +02:00
|
|
|
d.checker = checker.new_checker(d.table, d.prefs)
|
2020-06-19 10:36:45 +02:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut nodes []DocNode) sort_by_name() {
|
|
|
|
nodes.sort_with_compare(compare_nodes_by_name)
|
|
|
|
}
|
|
|
|
|
2020-10-21 10:26:33 +02:00
|
|
|
pub fn (mut nodes []DocNode) sort_by_kind() {
|
|
|
|
nodes.sort_with_compare(compare_nodes_by_kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compare_nodes_by_kind(a &DocNode, b &DocNode) int {
|
|
|
|
ak := int((*a).kind)
|
|
|
|
bk := int((*b).kind)
|
|
|
|
if ak < bk {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if ak > bk {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
2020-06-19 10:36:45 +02:00
|
|
|
}
|
|
|
|
|
2020-10-15 15:17:52 +02:00
|
|
|
fn compare_nodes_by_name(a &DocNode, b &DocNode) int {
|
2020-06-19 10:36:45 +02:00
|
|
|
al := a.name.to_lower()
|
|
|
|
bl := b.name.to_lower()
|
|
|
|
return compare_strings(al, bl)
|
|
|
|
}
|
|
|
|
|
2020-10-21 10:26:33 +02:00
|
|
|
pub fn (cnts map[string]DocNode) arr() []DocNode {
|
|
|
|
mut contents := cnts.keys().map(cnts[it])
|
|
|
|
contents.sort_by_name()
|
|
|
|
contents.sort_by_kind()
|
|
|
|
return contents
|
2020-02-19 16:12:39 +01:00
|
|
|
}
|
2020-03-10 19:49:04 +01:00
|
|
|
|
2020-08-28 07:53:12 +02:00
|
|
|
// get_parent_mod - return the parent mod name, in dot format.
|
|
|
|
// It works by climbing up the folder hierarchy, until a folder,
|
|
|
|
// that either contains main .v files, or a v.mod file is reached.
|
|
|
|
// For example, given something like /languages/v/vlib/x/websocket/tests/autobahn
|
|
|
|
// it returns `x.websocket.tests`, because /languages/v/ has v.mod file in it.
|
|
|
|
// NB: calling this is expensive, so keep the result, instead of recomputing it.
|
2020-10-20 02:52:56 +02:00
|
|
|
fn get_parent_mod(input_dir string) ?string {
|
2020-06-04 23:50:59 +02:00
|
|
|
$if windows {
|
2020-06-06 17:47:16 +02:00
|
|
|
// windows root path is C: or D:
|
2020-10-20 02:52:56 +02:00
|
|
|
if input_dir.len <= 2 {
|
2020-07-17 19:13:22 +02:00
|
|
|
return error('root folder reached')
|
|
|
|
}
|
2020-06-04 23:50:59 +02:00
|
|
|
} $else {
|
2020-10-20 02:52:56 +02:00
|
|
|
if input_dir.len == 0 {
|
2020-07-17 19:13:22 +02:00
|
|
|
return error('root folder reached')
|
|
|
|
}
|
2020-06-04 23:50:59 +02:00
|
|
|
}
|
2020-10-20 02:52:56 +02:00
|
|
|
base_dir := os.dir(input_dir)
|
2020-10-21 10:26:33 +02:00
|
|
|
input_dir_name := os.file_name(base_dir)
|
2020-08-28 07:53:12 +02:00
|
|
|
prefs := new_vdoc_preferences()
|
2020-10-21 10:26:33 +02:00
|
|
|
fentries := os.ls(base_dir) or {
|
2020-06-02 16:19:55 +02:00
|
|
|
[]string{}
|
|
|
|
}
|
2020-08-28 07:53:12 +02:00
|
|
|
files := fentries.filter(!os.is_dir(it))
|
|
|
|
if 'v.mod' in files {
|
|
|
|
// the top level is reached, no point in climbing up further
|
|
|
|
return ''
|
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
v_files := prefs.should_compile_filtered_files(base_dir, files)
|
2020-06-02 12:10:01 +02:00
|
|
|
if v_files.len == 0 {
|
2020-06-02 16:19:55 +02:00
|
|
|
parent_mod := get_parent_mod(base_dir) or {
|
2020-10-20 02:52:56 +02:00
|
|
|
return input_dir_name
|
2020-06-02 16:19:55 +02:00
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
if parent_mod.len > 0 {
|
2020-10-20 02:52:56 +02:00
|
|
|
return parent_mod + '.' + input_dir_name
|
2020-03-10 19:49:04 +01:00
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
return error('No V files found.')
|
2020-03-10 19:49:04 +01:00
|
|
|
}
|
2020-06-06 17:47:16 +02:00
|
|
|
tbl := table.new_table()
|
2020-07-17 19:13:22 +02:00
|
|
|
scope := &ast.Scope{
|
|
|
|
parent: 0
|
|
|
|
}
|
2020-06-06 17:47:16 +02:00
|
|
|
file_ast := parser.parse_file(v_files[0], tbl, .skip_comments, prefs, scope)
|
2020-06-02 12:10:01 +02:00
|
|
|
if file_ast.mod.name == 'main' {
|
|
|
|
return ''
|
|
|
|
}
|
2020-06-02 16:19:55 +02:00
|
|
|
parent_mod := get_parent_mod(base_dir) or {
|
2020-10-21 10:26:33 +02:00
|
|
|
return input_dir_name
|
2020-06-02 16:19:55 +02:00
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
if parent_mod.len > 0 {
|
2020-10-21 10:26:33 +02:00
|
|
|
return '${parent_mod}.$file_ast.mod.name'
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
return file_ast.mod.name
|
2020-03-10 19:49:04 +01:00
|
|
|
}
|
|
|
|
|
2020-10-21 10:26:33 +02:00
|
|
|
pub fn (mut d Doc) generate_from_ast(file_ast ast.File) map[string]DocNode {
|
|
|
|
mut contents := map[string]DocNode{}
|
|
|
|
orig_mod_name := file_ast.mod.name
|
2020-09-21 16:40:39 +02:00
|
|
|
stmts := file_ast.stmts
|
|
|
|
d.fmt.file = file_ast
|
|
|
|
d.fmt.set_current_module_name(orig_mod_name)
|
|
|
|
d.fmt.process_file_imports(file_ast)
|
|
|
|
mut last_import_stmt_idx := 0
|
|
|
|
for sidx, stmt in stmts {
|
|
|
|
if stmt is ast.Import {
|
|
|
|
last_import_stmt_idx = sidx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mut prev_comments := []ast.Comment{}
|
|
|
|
mut imports_section := true
|
|
|
|
for sidx, stmt in stmts {
|
|
|
|
// eprintln('stmt typeof: ' + typeof(stmt))
|
|
|
|
if stmt is ast.ExprStmt {
|
|
|
|
if stmt.expr is ast.Comment as cmt {
|
|
|
|
prev_comments << cmt
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Fetch head comment once
|
|
|
|
if stmt is ast.Module {
|
2020-10-05 07:59:17 +02:00
|
|
|
if !d.with_head {
|
|
|
|
continue
|
|
|
|
}
|
2020-09-21 16:40:39 +02:00
|
|
|
// the previous comments were probably a copyright/license one
|
|
|
|
module_comment := get_comment_block_right_before(prev_comments)
|
|
|
|
prev_comments = []
|
|
|
|
if !d.is_vlib && !module_comment.starts_with('Copyright (c)') {
|
|
|
|
if module_comment in ['', d.head.comment] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if d.head.comment != '' {
|
|
|
|
d.head.comment += '\n'
|
|
|
|
}
|
|
|
|
d.head.comment += module_comment
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if last_import_stmt_idx > 0 && sidx == last_import_stmt_idx {
|
|
|
|
// the accumulated comments were interspersed before/between the imports;
|
|
|
|
// just add them all to the module comment:
|
2020-10-05 07:59:17 +02:00
|
|
|
if d.with_head {
|
|
|
|
import_comments := merge_comments(prev_comments)
|
|
|
|
if d.head.comment != '' {
|
|
|
|
d.head.comment += '\n'
|
|
|
|
}
|
|
|
|
d.head.comment += import_comments
|
2020-09-21 16:40:39 +02:00
|
|
|
}
|
|
|
|
prev_comments = []
|
|
|
|
imports_section = false
|
|
|
|
}
|
|
|
|
if stmt is ast.Import {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mut node := DocNode{
|
2020-10-05 07:59:17 +02:00
|
|
|
name: d.get_name(stmt)
|
|
|
|
content: d.get_signature(stmt, file_ast)
|
2020-09-21 16:40:39 +02:00
|
|
|
comment: ''
|
2020-10-05 07:59:17 +02:00
|
|
|
pos: convert_pos(file_ast.path, d.get_pos(stmt))
|
2020-09-21 16:40:39 +02:00
|
|
|
file_path: file_ast.path
|
|
|
|
}
|
2020-10-05 07:59:17 +02:00
|
|
|
if (!node.content.starts_with('pub') && d.pub_only) || stmt is ast.GlobalDecl {
|
|
|
|
prev_comments = []
|
2020-09-21 16:40:39 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-10-05 07:59:17 +02:00
|
|
|
if node.name.starts_with(orig_mod_name + '.') {
|
|
|
|
node.name = node.name.all_after(orig_mod_name + '.')
|
2020-09-21 16:40:39 +02:00
|
|
|
}
|
2020-10-05 07:59:17 +02:00
|
|
|
if node.name.len == 0 && node.comment.len == 0 && node.content.len == 0 {
|
|
|
|
continue
|
2020-09-21 16:40:39 +02:00
|
|
|
}
|
|
|
|
match stmt {
|
2020-10-05 07:59:17 +02:00
|
|
|
ast.ConstDecl {
|
2020-10-21 10:26:33 +02:00
|
|
|
node.kind = .const_group
|
|
|
|
node.parent_name = 'Constants'
|
|
|
|
if node.parent_name !in contents {
|
|
|
|
contents[node.parent_name] = DocNode{
|
|
|
|
name: 'Constants'
|
|
|
|
kind: .const_group
|
|
|
|
}
|
2020-10-05 07:59:17 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-06 06:24:50 +02:00
|
|
|
ast.EnumDecl {
|
2020-10-21 10:26:33 +02:00
|
|
|
node.kind = .enum_
|
2020-10-06 06:24:50 +02:00
|
|
|
}
|
|
|
|
ast.InterfaceDecl {
|
2020-10-21 10:26:33 +02:00
|
|
|
node.kind = .interface_
|
2020-10-06 06:24:50 +02:00
|
|
|
}
|
|
|
|
ast.StructDecl {
|
2020-10-21 10:26:33 +02:00
|
|
|
node.kind = .struct_
|
2020-10-06 06:24:50 +02:00
|
|
|
}
|
|
|
|
ast.TypeDecl {
|
2020-10-21 10:26:33 +02:00
|
|
|
node.kind = .typedef
|
2020-10-06 06:24:50 +02:00
|
|
|
}
|
2020-10-05 07:59:17 +02:00
|
|
|
ast.FnDecl {
|
2020-10-21 10:26:33 +02:00
|
|
|
node.deprecated = stmt.is_deprecated
|
|
|
|
node.kind = .function
|
|
|
|
if stmt.receiver.typ !in [0, 1] {
|
|
|
|
method_parent := d.fmt.table.type_to_str(stmt.receiver.typ).trim_left('&')
|
|
|
|
if method_parent != 'void' && method_parent !in contents {
|
|
|
|
contents[method_parent] = DocNode{
|
|
|
|
name: method_parent
|
|
|
|
kind: .none_
|
2020-10-05 07:59:17 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
node.kind = .method
|
|
|
|
node.parent_name = method_parent
|
2020-10-05 07:59:17 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-21 16:40:39 +02:00
|
|
|
else {}
|
|
|
|
}
|
|
|
|
if d.with_comments && (prev_comments.len > 0) {
|
2020-10-21 10:26:33 +02:00
|
|
|
// last_comment := contents[contents.len - 1].comment
|
|
|
|
// cmt := last_comment + '\n' + get_comment_block_right_before(prev_comments)
|
|
|
|
cmt := get_comment_block_right_before(prev_comments)
|
|
|
|
node.comment = cmt
|
2020-09-21 16:40:39 +02:00
|
|
|
}
|
|
|
|
prev_comments = []
|
2020-10-21 10:26:33 +02:00
|
|
|
if node.parent_name.len > 0 {
|
|
|
|
parent_name := node.parent_name
|
|
|
|
if node.parent_name == 'Constants' {
|
|
|
|
node.parent_name = ''
|
|
|
|
}
|
|
|
|
contents[parent_name].children << node
|
|
|
|
} else {
|
|
|
|
contents[node.name] = node
|
|
|
|
}
|
2020-09-21 16:40:39 +02:00
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
d.fmt.mod2alias = map[string]string{}
|
2020-10-05 07:59:17 +02:00
|
|
|
return contents
|
|
|
|
}
|
|
|
|
|
2020-10-21 10:26:33 +02:00
|
|
|
pub fn (mut d Doc) generate_from_ast_with_pos(file_ast ast.File, pos int) map[string]DocNode {
|
2020-10-05 07:59:17 +02:00
|
|
|
lscope := file_ast.scope.innermost(pos)
|
2020-10-21 10:26:33 +02:00
|
|
|
mut contents := map[string]DocNode{}
|
2020-10-05 07:59:17 +02:00
|
|
|
for name, val in lscope.objects {
|
|
|
|
if val !is ast.Var {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
vr_data := val as ast.Var
|
|
|
|
l_node := DocNode{
|
|
|
|
name: name
|
|
|
|
pos: convert_pos(file_ast.path, vr_data.pos)
|
|
|
|
file_path: file_ast.path
|
2020-10-21 10:26:33 +02:00
|
|
|
from_scope: true
|
|
|
|
kind: .variable
|
|
|
|
parent_name: d.expr_typ_to_string(vr_data.expr)
|
2020-10-05 07:59:17 +02:00
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
contents[l_node.name] = l_node
|
2020-10-05 07:59:17 +02:00
|
|
|
}
|
|
|
|
return contents
|
2020-09-21 16:40:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut d Doc) expr_typ_to_string(ex ast.Expr) string {
|
|
|
|
expr_typ := d.checker.expr(ex)
|
2020-10-13 16:46:56 +02:00
|
|
|
return d.fmt.table.type_to_str(expr_typ)
|
2020-09-21 16:40:39 +02:00
|
|
|
}
|
|
|
|
|
2020-06-19 10:36:45 +02:00
|
|
|
fn (mut d Doc) generate() ?Doc {
|
2020-06-02 12:10:01 +02:00
|
|
|
// get all files
|
2020-10-01 01:30:22 +02:00
|
|
|
base_path := if os.is_dir(d.input_path) { d.input_path } else { os.real_path(os.dir(d.input_path)) }
|
2020-09-21 16:40:39 +02:00
|
|
|
d.is_vlib = 'vlib' !in base_path
|
2020-06-02 16:19:55 +02:00
|
|
|
project_files := os.ls(base_path) or {
|
2020-06-10 11:13:42 +02:00
|
|
|
return error_with_code(err, 0)
|
2020-06-02 16:19:55 +02:00
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
v_files := d.prefs.should_compile_filtered_files(base_path, project_files)
|
|
|
|
if v_files.len == 0 {
|
2020-06-10 11:13:42 +02:00
|
|
|
return error_with_code('vdoc: No valid V files were found.', 1)
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
// parse files
|
2020-10-21 10:26:33 +02:00
|
|
|
mut comments_mode := scanner.CommentsMode.skip_comments
|
|
|
|
if d.with_comments {
|
|
|
|
comments_mode = .toplevel_comments
|
|
|
|
}
|
|
|
|
global_scope := &ast.Scope{
|
|
|
|
parent: 0
|
|
|
|
}
|
|
|
|
mut parent_mod_name := ''
|
2020-09-21 16:40:39 +02:00
|
|
|
mut fname_has_set := false
|
2020-10-21 10:26:33 +02:00
|
|
|
mut orig_mod_name := ''
|
|
|
|
for i, file in v_files {
|
|
|
|
file_ast := parser.parse_file(file, d.table, comments_mode, d.prefs, global_scope)
|
2020-09-21 16:40:39 +02:00
|
|
|
if d.filename.len > 0 && d.filename in file && !fname_has_set {
|
|
|
|
d.filename = file
|
|
|
|
fname_has_set = true
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
if i == 0 {
|
2020-06-02 16:19:55 +02:00
|
|
|
parent_mod_name = get_parent_mod(base_path) or {
|
|
|
|
''
|
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
mut module_name := file_ast.mod.name
|
2020-06-02 12:10:01 +02:00
|
|
|
orig_mod_name = module_name
|
|
|
|
if module_name != 'main' && parent_mod_name.len > 0 {
|
|
|
|
module_name = parent_mod_name + '.' + module_name
|
|
|
|
}
|
2020-10-05 07:59:17 +02:00
|
|
|
if d.with_head {
|
|
|
|
d.head = DocNode{
|
|
|
|
name: module_name
|
|
|
|
content: 'module $module_name'
|
2020-10-21 10:26:33 +02:00
|
|
|
kind: .none_
|
2020-10-05 07:59:17 +02:00
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
} else if file_ast.mod.name != orig_mod_name {
|
2020-03-10 19:49:04 +01:00
|
|
|
continue
|
|
|
|
}
|
2020-09-21 16:40:39 +02:00
|
|
|
if file_ast.path == d.filename {
|
2020-10-21 10:26:33 +02:00
|
|
|
d.checker.check(file_ast)
|
|
|
|
d.scoped_contents = d.generate_from_ast_with_pos(file_ast, d.pos)
|
|
|
|
}
|
|
|
|
contents := d.generate_from_ast(file_ast)
|
|
|
|
for name, node in contents {
|
|
|
|
if name in d.contents && (d.contents[name].kind != .none_ || node.kind == .none_) {
|
|
|
|
d.contents[name].children << node.children
|
|
|
|
d.contents[name].children.sort_by_name()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
d.contents[name] = node
|
2020-03-10 19:49:04 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
d.time_generated = time.now()
|
2020-07-29 21:40:43 +02:00
|
|
|
return *d
|
2020-03-10 19:49:04 +01:00
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
|
2020-10-15 15:17:52 +02:00
|
|
|
pub fn generate_from_pos(input_path string, filename string, pos int) ?Doc {
|
2020-09-21 16:40:39 +02:00
|
|
|
mut doc := new(input_path)
|
|
|
|
doc.pub_only = false
|
|
|
|
doc.with_comments = true
|
|
|
|
doc.with_pos = true
|
|
|
|
doc.filename = filename
|
|
|
|
doc.pos = pos
|
|
|
|
return doc.generate()
|
|
|
|
}
|
|
|
|
|
2020-10-15 15:17:52 +02:00
|
|
|
pub fn generate(input_path string, pub_only bool, with_comments bool) ?Doc {
|
2020-06-02 16:19:55 +02:00
|
|
|
mut doc := new(input_path)
|
2020-06-02 12:10:01 +02:00
|
|
|
doc.pub_only = pub_only
|
|
|
|
doc.with_comments = with_comments
|
2020-06-19 10:36:45 +02:00
|
|
|
return doc.generate()
|
2020-06-02 16:18:12 +02:00
|
|
|
}
|
2020-10-21 10:26:33 +02:00
|
|
|
|
|
|
|
pub fn lookup_module(mod string) ?string {
|
|
|
|
mod_path := mod.replace('.', os.path_separator)
|
|
|
|
compile_dir := os.real_path(os.dir('.'))
|
|
|
|
modules_dir := os.join_path(compile_dir, 'modules', mod_path)
|
|
|
|
vlib_path := os.join_path(os.dir(@VEXE), 'vlib', mod_path)
|
|
|
|
vmodules_path := os.join_path(os.home_dir(), '.vmodules', mod_path)
|
|
|
|
paths := [modules_dir, vlib_path, vmodules_path]
|
|
|
|
for path in paths {
|
|
|
|
if !os.exists(path) || os.is_dir_empty(path) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
return error('module "$mod" not found.')
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate_from_mod(module_name string, pub_only bool, with_comments bool) ?Doc {
|
|
|
|
mod_path := lookup_module(module_name) ?
|
|
|
|
return generate(mod_path, pub_only, with_comments)
|
|
|
|
}
|