vdoc: fmt doc/doc.v

pull/5179/head
Alexander Medvednikov 2020-06-02 16:19:55 +02:00
parent fb3e52ca63
commit 640688d8cf
1 changed files with 77 additions and 88 deletions

View File

@ -14,59 +14,64 @@ import v.util
pub struct Doc { pub struct Doc {
pub mut: pub mut:
input_path string = '' input_path string = ''
prefs &pref.Preferences = &pref.Preferences{} prefs &pref.Preferences = &pref.Preferences{}
table &table.Table = &table.Table{} table &table.Table = &table.Table{}
pub_only bool = true pub_only bool = true
head DocNode head DocNode
with_comments bool = true with_comments bool = true
contents []DocNode contents []DocNode
time_generated time.Time time_generated time.Time
} }
pub struct DocPos { pub struct DocPos {
pub: pub:
line int line int
col int col int
} }
pub struct DocNode { pub struct DocNode {
pub mut: pub mut:
name string name string
content string = '' content string = ''
comment string comment string
pos DocPos = DocPos{-1,-1} pos DocPos = DocPos{-1, -1}
file_path string = '' file_path string = ''
parent_type string = '' parent_type string = ''
} }
pub fn write_comment_bw(stmts []ast.Stmt, start_idx int) string { pub fn write_comment_bw(stmts []ast.Stmt, start_idx int) string {
mut comment := '' mut comment := ''
for i := start_idx; i >= 0; i-- { for i := start_idx; i >= 0; i-- {
stmt := stmts[i] stmt := stmts[i]
if stmt is ast.Comment { if stmt is ast.Comment {
cmt := stmt as ast.Comment cmt := stmt as ast.Comment
cmt_content := cmt.text.trim_left('|') cmt_content := cmt.text.trim_left('|')
comment = cmt_content + if cmt_content.starts_with('```') { '\n' } else { ' ' } + comment comment = cmt_content + if cmt_content.starts_with('```') {
'\n'
} else {
' '
} + comment
} else { } else {
panic('Not a comment') panic('Not a comment')
} }
if i - 1 >= 0 && !(stmts[i - 1] is ast.Comment) {
if i-1 >= 0 && !(stmts[i-1] is ast.Comment) {
break break
} }
} }
return comment return comment
} }
fn convert_pos(file_path string, pos token.Position) DocPos { fn convert_pos(file_path string, pos token.Position) DocPos {
source := util.read_file(file_path) or {''} 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) } 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)
}
} }
pub fn (d Doc) get_signature(stmt ast.Stmt) string { pub fn (d Doc) get_signature(stmt ast.Stmt) string {
@ -77,7 +82,6 @@ pub fn (d Doc) get_signature(stmt ast.Stmt) string {
indent: 0 indent: 0
is_debug: false is_debug: false
} }
match stmt { match stmt {
ast.Module { ast.Module {
return 'module $it.name' return 'module $it.name'
@ -116,23 +120,26 @@ pub fn (d Doc) get_name(stmt ast.Stmt) string {
pub fn new(input_path string) Doc { pub fn new(input_path string) Doc {
return Doc{ return Doc{
input_path: os.real_path(input_path), input_path: os.real_path(input_path)
prefs: &pref.Preferences{}, prefs: &pref.Preferences{}
table: table.new_table(), table: table.new_table()
head: DocNode{}, head: DocNode{}
contents: []DocNode{}, contents: []DocNode{}
time_generated: time.now() time_generated: time.now()
} }
} }
pub fn (nodes []DocNode) find_children_of(parent_type string) []DocNode { pub fn (nodes []DocNode) find_children_of(parent_type string) []DocNode {
if parent_type.len == 0 { return []DocNode{} } if parent_type.len == 0 {
return []DocNode{}
}
mut children := []DocNode{} mut children := []DocNode{}
for node in nodes { for node in nodes {
if node.parent_type != parent_type { continue } if node.parent_type != parent_type {
continue
}
children << node children << node
} }
return children return children
} }
@ -142,26 +149,28 @@ fn get_parent_mod(dir string) ?string {
return os.file_name(base_dir) return os.file_name(base_dir)
} }
prefs := &pref.Preferences{} prefs := &pref.Preferences{}
files := os.ls(base_dir) or { []string{} } files := os.ls(base_dir) or {
[]string{}
}
v_files := prefs.should_compile_filtered_files(base_dir, files) v_files := prefs.should_compile_filtered_files(base_dir, files)
if v_files.len == 0 { if v_files.len == 0 {
parent_mod := get_parent_mod(base_dir) or { '' } parent_mod := get_parent_mod(base_dir) or {
''
}
if parent_mod.len > 0 { if parent_mod.len > 0 {
return parent_mod + '.' + os.file_name(base_dir) return parent_mod + '.' + os.file_name(base_dir)
} }
return error('No V files found.') return error('No V files found.')
} }
file_ast := parser.parse_file( file_ast := parser.parse_file(v_files[0], table.new_table(), .skip_comments, prefs, &ast.Scope{
v_files[0], parent: 0
table.new_table(), })
.skip_comments,
prefs,
&ast.Scope{parent: 0}
)
if file_ast.mod.name == 'main' { if file_ast.mod.name == 'main' {
return '' return ''
} }
parent_mod := get_parent_mod(base_dir) or { '' } parent_mod := get_parent_mod(base_dir) or {
''
}
if parent_mod.len > 0 { if parent_mod.len > 0 {
return parent_mod + '.' + file_ast.mod.name return parent_mod + '.' + file_ast.mod.name
} }
@ -171,120 +180,100 @@ fn get_parent_mod(dir string) ?string {
pub fn (mut d Doc) generate() ?bool { pub fn (mut d Doc) generate() ?bool {
// get all files // get all files
base_path := if os.is_dir(d.input_path) { d.input_path } else { os.real_path(os.base_dir(d.input_path)) } base_path := if os.is_dir(d.input_path) { d.input_path } else { os.real_path(os.base_dir(d.input_path)) }
project_files := os.ls(base_path) or { panic(err) } project_files := os.ls(base_path) or {
panic(err)
}
v_files := d.prefs.should_compile_filtered_files(base_path, project_files) v_files := d.prefs.should_compile_filtered_files(base_path, project_files)
if v_files.len == 0 { if v_files.len == 0 {
return error('vdoc: No valid V files were found.') return error('vdoc: No valid V files were found.')
} }
// parse files // parse files
mut file_asts := []ast.File{} mut file_asts := []ast.File{}
// TODO: remove later for vlib // TODO: remove later for vlib
comments_mode := if d.with_comments { comments_mode := if d.with_comments { scanner.CommentsMode.parse_comments } else { scanner.CommentsMode.skip_comments }
scanner.CommentsMode.parse_comments
} else {
scanner.CommentsMode.skip_comments
}
for file in v_files { for file in v_files {
file_ast := parser.parse_file( file_ast := parser.parse_file(file, d.table, comments_mode, d.prefs, &ast.Scope{
file, parent: 0
d.table, })
comments_mode,
d.prefs,
&ast.Scope{parent: 0}
)
file_asts << file_ast file_asts << file_ast
} }
mut module_name := '' mut module_name := ''
mut parent_mod_name := '' mut parent_mod_name := ''
mut orig_mod_name := '' mut orig_mod_name := ''
for i, file_ast in file_asts { for i, file_ast in file_asts {
if i == 0 { if i == 0 {
parent_mod_name = get_parent_mod(base_path) or { '' } parent_mod_name = get_parent_mod(base_path) or {
''
}
module_name = file_ast.mod.name module_name = file_ast.mod.name
orig_mod_name = module_name orig_mod_name = module_name
if module_name != 'main' && parent_mod_name.len > 0 { if module_name != 'main' && parent_mod_name.len > 0 {
module_name = parent_mod_name + '.' + module_name module_name = parent_mod_name + '.' + module_name
} }
d.head = DocNode{ d.head = DocNode{
name: module_name, name: module_name
content: 'module $module_name', content: 'module $module_name'
comment: '' comment: ''
} }
} else if file_ast.mod.name != module_name { } else if file_ast.mod.name != module_name {
continue continue
} }
stmts := file_ast.stmts stmts := file_ast.stmts
for si, stmt in stmts { for si, stmt in stmts {
if stmt is ast.Comment { continue } if stmt is ast.Comment {
continue
//if !(stmt is ast.Module) { }
if stmt !is ast.Module { if stmt !is ast.Module {
// todo: accumulate consts // todo: accumulate consts
mut name := d.get_name(stmt) mut name := d.get_name(stmt)
signature := d.get_signature(stmt) signature := d.get_signature(stmt)
pos := d.get_pos(stmt) pos := d.get_pos(stmt)
if !signature.starts_with('pub') && d.pub_only { if !signature.starts_with('pub') && d.pub_only {
continue continue
} }
if name.starts_with(orig_mod_name + '.') { if name.starts_with(orig_mod_name + '.') {
name = name.all_after(orig_mod_name + '.') name = name.all_after(orig_mod_name + '.')
} }
mut node := DocNode{ mut node := DocNode{
name: name, name: name
content: signature, content: signature
comment: '', comment: ''
pos: convert_pos(v_files[i], pos), pos: convert_pos(v_files[i], pos)
file_path: v_files[i] file_path: v_files[i]
} }
if stmt is ast.FnDecl { if stmt is ast.FnDecl {
fnd := stmt as ast.FnDecl fnd := stmt as ast.FnDecl
if fnd.receiver.typ != 0 { if fnd.receiver.typ != 0 {
mut parent_type := d.table.get_type_name(fnd.receiver.typ) mut parent_type := d.table.get_type_name(fnd.receiver.typ)
if parent_type.starts_with(module_name + '.') { if parent_type.starts_with(module_name + '.') {
parent_type = parent_type.all_after(module_name + '.') parent_type = parent_type.all_after(module_name + '.')
} }
node.parent_type = parent_type node.parent_type = parent_type
} }
} }
d.contents << node d.contents << node
} }
if d.with_comments && (si - 1 >= 0 && stmts[si - 1] is ast.Comment) {
if d.with_comments && (si-1 >= 0 && stmts[si-1] is ast.Comment) {
if stmt is ast.Module { if stmt is ast.Module {
d.head.comment = write_comment_bw(stmts, si-1) d.head.comment = write_comment_bw(stmts, si - 1)
} else { } else {
last_comment := d.contents[d.contents.len-1].comment last_comment := d.contents[d.contents.len - 1].comment
d.contents[d.contents.len-1].comment = last_comment + '\n' + write_comment_bw(stmts, si-1) d.contents[d.contents.len - 1].comment = last_comment + '\n' + write_comment_bw(stmts,
si - 1)
} }
} }
} }
} }
d.time_generated = time.now() d.time_generated = time.now()
return true return true
} }
pub fn generate(input_path string, pub_only bool, with_comments bool) ?Doc { pub fn generate(input_path string, pub_only, with_comments bool) ?Doc {
mut doc := doc.new(input_path) mut doc := new(input_path)
doc.pub_only = pub_only doc.pub_only = pub_only
doc.with_comments = with_comments doc.with_comments = with_comments
_ = doc.generate() or { _ = doc.generate() or {
return error(err) return error(err)
} }
return doc return doc
} }