mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vdoc: fix sorting + other minor improvements
This commit is contained in:
@@ -123,7 +123,7 @@ pub fn (ftp FTP) login(user, passwd string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
mut code, mut data := ftp.read()
|
||||
mut code, _ := ftp.read()
|
||||
if code == logged_in {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -19,11 +19,11 @@ import picohttpparser
|
||||
#include "src/picoev.h"
|
||||
|
||||
const (
|
||||
MAX_FDS = 1024
|
||||
TIMEOUT_SECS = 8
|
||||
MAX_TIMEOUT = 10
|
||||
MAX_READ = 4096
|
||||
MAX_WRITE = 8192
|
||||
max_fds = 1024
|
||||
timeout_secs = 8
|
||||
max_timeout = 10
|
||||
max_read = 4096
|
||||
max_write = 8192
|
||||
)
|
||||
|
||||
struct C.in_addr {
|
||||
@@ -122,10 +122,10 @@ fn rw_callback(loop &C.picoev_loop, fd, events int, cb_arg voidptr) {
|
||||
return
|
||||
}
|
||||
else if (events & C.PICOEV_READ) != 0 {
|
||||
C.picoev_set_timeout(loop, fd, TIMEOUT_SECS)
|
||||
buf := (p.buf + fd * MAX_READ)
|
||||
C.picoev_set_timeout(loop, fd, timeout_secs)
|
||||
buf := (p.buf + fd * max_read)
|
||||
idx := p.idx[fd]
|
||||
mut r := myread(fd, buf, MAX_READ, idx)
|
||||
mut r := myread(fd, buf, max_read, idx)
|
||||
if r == 0 {
|
||||
close_conn(loop, fd)
|
||||
p.idx[fd] = 0
|
||||
@@ -141,7 +141,7 @@ fn rw_callback(loop &C.picoev_loop, fd, events int, cb_arg voidptr) {
|
||||
} else {
|
||||
r += idx
|
||||
mut s := tos(buf, r)
|
||||
out := (p.out + fd * MAX_WRITE)
|
||||
out := (p.out + fd * max_write)
|
||||
mut res := picohttpparser.Response{
|
||||
fd: fd
|
||||
date: p.date
|
||||
@@ -191,7 +191,7 @@ fn accept_callback(loop &C.picoev_loop, fd, events int, cb_arg voidptr) {
|
||||
newfd := C.accept(fd, 0, 0)
|
||||
if newfd != -1 {
|
||||
setup_sock(newfd)
|
||||
C.picoev_add(loop, newfd, C.PICOEV_READ, TIMEOUT_SECS, rw_callback, cb_arg)
|
||||
C.picoev_add(loop, newfd, C.PICOEV_READ, timeout_secs, rw_callback, cb_arg)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,14 +223,14 @@ pub fn new(port int, cb voidptr) &Picoev {
|
||||
|
||||
setup_sock(fd)
|
||||
|
||||
C.picoev_init(MAX_FDS)
|
||||
loop := C.picoev_create_loop(MAX_TIMEOUT)
|
||||
C.picoev_init(max_fds)
|
||||
loop := C.picoev_create_loop(max_timeout)
|
||||
pv := &Picoev{
|
||||
loop: loop
|
||||
cb: cb
|
||||
date: C.get_date()
|
||||
buf: malloc(MAX_FDS * MAX_READ + 1)
|
||||
out: malloc(MAX_FDS * MAX_WRITE + 1)
|
||||
buf: malloc(max_fds * max_read + 1)
|
||||
out: malloc(max_fds * max_write + 1)
|
||||
}
|
||||
C.picoev_add(loop, fd, C.PICOEV_READ, 0, accept_callback, pv)
|
||||
|
||||
|
||||
158
vlib/v/doc/doc.v
158
vlib/v/doc/doc.v
@@ -21,6 +21,7 @@ pub mut:
|
||||
head DocNode
|
||||
with_comments bool = true
|
||||
contents []DocNode
|
||||
fmt fmt.Fmt
|
||||
time_generated time.Time
|
||||
}
|
||||
|
||||
@@ -37,7 +38,7 @@ pub mut:
|
||||
comment string
|
||||
pos DocPos = DocPos{-1, -1}
|
||||
file_path string = ''
|
||||
parent_type string = ''
|
||||
attrs map[string]string
|
||||
}
|
||||
|
||||
pub fn merge_comments(stmts []ast.Stmt) string {
|
||||
@@ -112,65 +113,55 @@ fn convert_pos(file_path string, pos token.Position) DocPos {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (d Doc) get_signature(stmt ast.Stmt, file &ast.File) string {
|
||||
mut f := fmt.Fmt{
|
||||
out: strings.new_builder(1000)
|
||||
out_imports: strings.new_builder(200)
|
||||
table: d.table
|
||||
file: file
|
||||
cur_mod: d.head.name.split('.').last()
|
||||
indent: 0
|
||||
is_debug: false
|
||||
}
|
||||
f.process_file_imports(file)
|
||||
pub fn (mut d Doc) get_signature(stmt ast.Stmt, file &ast.File) string {
|
||||
match stmt {
|
||||
ast.Module {
|
||||
return 'module $it.name'
|
||||
return 'module $stmt.name'
|
||||
}
|
||||
ast.FnDecl {
|
||||
return it.str(d.table).replace(f.cur_mod + '.', '')
|
||||
return stmt.str(d.table).replace(d.fmt.cur_mod + '.', '')
|
||||
}
|
||||
else {
|
||||
f.stmt(stmt)
|
||||
return f.out.str().trim_space()
|
||||
d.fmt.out = strings.new_builder(1000)
|
||||
d.fmt.stmt(stmt)
|
||||
return d.fmt.out.str().trim_space()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (d Doc) get_pos(stmt ast.Stmt) token.Position {
|
||||
match stmt {
|
||||
ast.FnDecl { return it.pos }
|
||||
ast.StructDecl { return it.pos }
|
||||
ast.EnumDecl { return it.pos }
|
||||
ast.InterfaceDecl { return it.pos }
|
||||
ast.ConstDecl { return it.pos }
|
||||
ast.FnDecl { return stmt.pos }
|
||||
ast.StructDecl { return stmt.pos }
|
||||
ast.EnumDecl { return stmt.pos }
|
||||
ast.InterfaceDecl { return stmt.pos }
|
||||
ast.ConstDecl { return stmt.pos }
|
||||
else { return token.Position{} }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (d Doc) get_type_name(decl ast.TypeDecl) string {
|
||||
match decl {
|
||||
ast.SumTypeDecl { return it.name }
|
||||
ast.FnTypeDecl { return it.name }
|
||||
ast.AliasTypeDecl { return it.name }
|
||||
ast.SumTypeDecl { return decl.name }
|
||||
ast.FnTypeDecl { return decl.name }
|
||||
ast.AliasTypeDecl { return decl.name }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (d Doc) get_name(stmt ast.Stmt) string {
|
||||
cur_mod := d.head.name.split('.').last()
|
||||
match stmt {
|
||||
ast.FnDecl { return it.name }
|
||||
ast.StructDecl { return it.name }
|
||||
ast.EnumDecl { return it.name }
|
||||
ast.InterfaceDecl { return it.name }
|
||||
ast.TypeDecl { return d.get_type_name(it).replace('&' + cur_mod + '.', '').replace(cur_mod + '.', '') }
|
||||
ast.FnDecl { return stmt.name }
|
||||
ast.StructDecl { return stmt.name }
|
||||
ast.EnumDecl { return stmt.name }
|
||||
ast.InterfaceDecl { return stmt.name }
|
||||
ast.TypeDecl { return d.get_type_name(stmt) }
|
||||
ast.ConstDecl { return 'Constants' }
|
||||
else { return '' }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(input_path string) Doc {
|
||||
return Doc{
|
||||
mut d := Doc{
|
||||
input_path: os.real_path(input_path)
|
||||
prefs: &pref.Preferences{}
|
||||
table: table.new_table()
|
||||
@@ -178,28 +169,59 @@ pub fn new(input_path string) Doc {
|
||||
contents: []DocNode{}
|
||||
time_generated: time.now()
|
||||
}
|
||||
d.fmt = fmt.Fmt{
|
||||
indent: 0
|
||||
is_debug: false
|
||||
table: d.table
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
pub fn (nodes []DocNode) index_by_name(node_name string) ?int {
|
||||
pub fn (mut nodes []DocNode) sort_by_name() {
|
||||
nodes.sort_with_compare(compare_nodes_by_name)
|
||||
}
|
||||
|
||||
pub fn (mut nodes []DocNode) sort_by_category() {
|
||||
nodes.sort_with_compare(compare_nodes_by_category)
|
||||
}
|
||||
|
||||
fn compare_nodes_by_name(a, b &DocNode) int {
|
||||
al := a.name.to_lower()
|
||||
bl := b.name.to_lower()
|
||||
return compare_strings(al, bl)
|
||||
}
|
||||
|
||||
fn compare_nodes_by_category(a, b &DocNode) int {
|
||||
al := a.attrs['category']
|
||||
bl := b.attrs['category']
|
||||
return compare_strings(al, bl)
|
||||
}
|
||||
|
||||
pub fn (nodes []DocNode) index_by_name(node_name string) int {
|
||||
for i, node in nodes {
|
||||
if node.name != node_name { continue }
|
||||
return i
|
||||
}
|
||||
return error('Node with the name "$node_name" was not found.')
|
||||
return -1
|
||||
}
|
||||
|
||||
pub fn (nodes []DocNode) find_children_of(parent_type string) []DocNode {
|
||||
if parent_type.len == 0 {
|
||||
return []DocNode{}
|
||||
pub fn (nodes []DocNode) find_children_of(parent string) []DocNode {
|
||||
return nodes.find_nodes_with_attr('parent', parent)
|
||||
}
|
||||
|
||||
pub fn (nodes []DocNode) find_nodes_with_attr(attr_name string, value string) []DocNode {
|
||||
mut subgroup := []DocNode{}
|
||||
if attr_name.len == 0 {
|
||||
return subgroup
|
||||
}
|
||||
mut children := []DocNode{}
|
||||
for node in nodes {
|
||||
if node.parent_type != parent_type {
|
||||
if !node.attrs.exists(attr_name) || node.attrs[attr_name] != value {
|
||||
continue
|
||||
}
|
||||
children << node
|
||||
subgroup << node
|
||||
}
|
||||
return children
|
||||
subgroup.sort_by_name()
|
||||
return subgroup
|
||||
}
|
||||
|
||||
fn get_parent_mod(dir string) ?string {
|
||||
@@ -242,7 +264,7 @@ fn get_parent_mod(dir string) ?string {
|
||||
return file_ast.mod.name
|
||||
}
|
||||
|
||||
pub fn (mut d Doc) generate() ?bool {
|
||||
fn (mut d Doc) generate() ?Doc {
|
||||
// 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)) }
|
||||
project_files := os.ls(base_path) or {
|
||||
@@ -285,7 +307,9 @@ pub fn (mut d Doc) generate() ?bool {
|
||||
continue
|
||||
}
|
||||
stmts := file_ast.stmts
|
||||
//
|
||||
d.fmt.file = file_ast
|
||||
d.fmt.cur_mod = 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 {
|
||||
@@ -306,10 +330,7 @@ pub fn (mut d Doc) generate() ?bool {
|
||||
module_comment := get_comment_block_right_before(prev_comments)
|
||||
prev_comments = []
|
||||
if 'vlib' !in base_path && !module_comment.starts_with('Copyright (c)') {
|
||||
if module_comment == '' {
|
||||
continue
|
||||
}
|
||||
if module_comment == d.head.comment {
|
||||
if module_comment in ['', d.head.comment] {
|
||||
continue
|
||||
}
|
||||
if d.head.comment != '' {
|
||||
@@ -350,25 +371,45 @@ pub fn (mut d Doc) generate() ?bool {
|
||||
pos: convert_pos(v_files[i], pos)
|
||||
file_path: v_files[i]
|
||||
}
|
||||
if node.name.len == 0 && node.comment.len == 0 && node.content.len == 0 {
|
||||
continue
|
||||
}
|
||||
if stmt is ast.FnDecl {
|
||||
fnd := stmt as ast.FnDecl
|
||||
if fnd.receiver.typ != 0 {
|
||||
mut parent_type := d.table.get_type_name(fnd.receiver.typ)
|
||||
if parent_type.starts_with(module_name + '.') {
|
||||
parent_type = parent_type.all_after(module_name + '.')
|
||||
node.attrs['parent'] = d.fmt.type_to_str(fnd.receiver.typ).trim_left('&')
|
||||
p_idx := d.contents.index_by_name(node.attrs['parent'])
|
||||
if p_idx == -1 && node.attrs['parent'] != 'void' {
|
||||
d.contents << DocNode{
|
||||
name: node.attrs['parent']
|
||||
content: ''
|
||||
comment: ''
|
||||
attrs: {'category': 'Structs'}
|
||||
}
|
||||
}
|
||||
node.parent_type = parent_type
|
||||
}
|
||||
}
|
||||
if stmt is ast.ConstDecl {
|
||||
if const_idx == -1 {
|
||||
const_idx = sidx
|
||||
} else {
|
||||
node.parent_type = 'Constants'
|
||||
node.attrs['parent'] = 'Constants'
|
||||
}
|
||||
}
|
||||
if node.name.len == 0 && node.comment.len == 0 && node.content.len == 0 {
|
||||
continue
|
||||
match stmt {
|
||||
ast.ConstDecl { node.attrs['category'] = 'Constants' }
|
||||
ast.EnumDecl { node.attrs['category'] = 'Enums' }
|
||||
ast.InterfaceDecl { node.attrs['category'] = 'Interfaces' }
|
||||
ast.StructDecl { node.attrs['category'] = 'Structs' }
|
||||
ast.TypeDecl { node.attrs['category'] = 'Typedefs' }
|
||||
ast.FnDecl {
|
||||
node.attrs['category'] = if node.attrs['parent'] in ['void', ''] || !node.attrs.exists('parent') {
|
||||
'Functions'
|
||||
} else {
|
||||
'Methods'
|
||||
}
|
||||
}
|
||||
else {}
|
||||
}
|
||||
d.contents << node
|
||||
if d.with_comments && (prev_comments.len > 0) {
|
||||
@@ -378,17 +419,18 @@ pub fn (mut d Doc) generate() ?bool {
|
||||
}
|
||||
prev_comments = []
|
||||
}
|
||||
|
||||
d.fmt.mod2alias = map[string]string{}
|
||||
}
|
||||
d.time_generated = time.now()
|
||||
return true
|
||||
d.contents.sort_by_name()
|
||||
d.contents.sort_by_category()
|
||||
return d
|
||||
}
|
||||
|
||||
pub fn generate(input_path string, pub_only, with_comments bool) ?Doc {
|
||||
mut doc := new(input_path)
|
||||
doc.pub_only = pub_only
|
||||
doc.with_comments = with_comments
|
||||
doc.generate() or {
|
||||
return error_with_code(err, errcode)
|
||||
}
|
||||
return doc
|
||||
return doc.generate()
|
||||
}
|
||||
|
||||
@@ -514,7 +514,7 @@ pub fn (mut f Fmt) struct_field_expr(fexpr ast.Expr) {
|
||||
}
|
||||
}
|
||||
|
||||
fn (f &Fmt) type_to_str(t table.Type) string {
|
||||
pub fn (f &Fmt) type_to_str(t table.Type) string {
|
||||
mut res := f.table.type_to_str(t)
|
||||
for res.ends_with('_ptr') {
|
||||
// type_ptr => &type
|
||||
|
||||
@@ -282,6 +282,7 @@ fn os_from_string(os string) pref.OS {
|
||||
|
||||
// Helper function to convert string names to CC enum
|
||||
pub fn cc_from_string(cc_str string) pref.CompilerType {
|
||||
if cc_str.len == 0 { return .gcc }
|
||||
cc := cc_str.replace('\\', '/').split('/').last().all_before('.')
|
||||
if 'tcc' in cc { return .tinyc }
|
||||
if 'tinyc' in cc { return .tinyc }
|
||||
|
||||
Reference in New Issue
Block a user